From 5a95808266bdf52692892bf14c47e422214fd65c Mon Sep 17 00:00:00 2001 From: Linus_Probert Date: Tue, 23 Jan 2018 12:14:44 +0100 Subject: [PATCH] Prepared the gui frames. --- src/defines.h | 14 ++++++-- src/gui.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/gui.h | 4 ++- src/main.c | 39 +++++++++++++++++---- 4 files changed, 141 insertions(+), 12 deletions(-) diff --git a/src/defines.h b/src/defines.h index b5f00f2..33d8ac8 100644 --- a/src/defines.h +++ b/src/defines.h @@ -11,10 +11,20 @@ #define MAP_H_ROOM_COUNT 10 #define TILE_DIMENSION 32 +#define SPRITE_DIMENSION 16 /* Display stuff */ -#define SCREEN_WIDTH MAP_ROOM_WIDTH * TILE_DIMENSION -#define SCREEN_HEIGHT MAP_ROOM_HEIGHT * TILE_DIMENSION +#define GAME_VIEW_WIDTH MAP_ROOM_WIDTH * TILE_DIMENSION +#define GAME_VIEW_HEIGHT MAP_ROOM_HEIGHT * TILE_DIMENSION + +#define RIGHT_GUI_WIDTH 10 * SPRITE_DIMENSION +#define RIGHT_GUI_HEIGHT GAME_VIEW_HEIGHT + +#define BOTTOM_GUI_HEIGHT 10 * SPRITE_DIMENSION +#define BOTTOM_GUI_WIDTH GAME_VIEW_WIDTH + RIGHT_GUI_WIDTH + +#define SCREEN_WIDTH GAME_VIEW_WIDTH + RIGHT_GUI_WIDTH +#define SCREEN_HEIGHT GAME_VIEW_HEIGHT + BOTTOM_GUI_HEIGHT /* Windows and compile crap */ #ifdef _WIN32 diff --git a/src/gui.c b/src/gui.c index bae6466..b6c16d0 100644 --- a/src/gui.c +++ b/src/gui.c @@ -4,6 +4,35 @@ #include "gui.h" #include "util.h" +static SDL_Rect frame_top_left = { 16, 160, 16, 16 }; +static SDL_Rect frame_top_right = { 48, 160, 16, 16 }; +static SDL_Rect frame_bottom_left = { 16, 192, 16, 16 }; +static SDL_Rect frame_bottom_right = { 48, 192, 16, 16 }; +static SDL_Rect frame_top = { 32, 160, 16, 16 }; +static SDL_Rect frame_bottom = { 32, 192, 16, 16 }; +static SDL_Rect frame_center = { 32, 176, 16, 16 }; +static SDL_Rect frame_left = { 16, 176, 16, 16 }; +static SDL_Rect frame_right = { 48, 176, 16, 16 }; + +static char **gui_log; +static unsigned int log_length = 50; + +static void +gui_malloc_log(void) +{ + static bool log_allocated = false; + if (log_allocated) + return; + + unsigned int i; + + gui_log = ec_malloc(log_length * sizeof(char*)); + for (i = 0; i < log_length; ++i) + gui_log[i] = ec_malloc(200 * sizeof(char)); + + log_allocated = true; +} + Gui* gui_create() { @@ -11,6 +40,9 @@ gui_create() gui->sprites = linkedlist_create(); gui->health = linkedlist_create(); gui->textures = ht_create(5); + + gui_malloc_log(); + return gui; } @@ -86,9 +118,46 @@ gui_add_texture(Gui *gui, const char *path, SDL_Renderer *renderer) return t; } -void -gui_render(Gui *gui, Camera *cam) +static void +gui_render_frame(Gui *gui, unsigned int width, unsigned int height, Camera *cam) { + Texture *texture = ht_get(gui->textures, "assets/GUI/GUI0.png"); + Position pos = { 0, 0 }; + unsigned int i, j; + + for (i = 0; i < width; ++i) { + for (j = 0; j < height; ++j) { + pos.x = i * 16; + pos.y = j * 16; + + if (i == 0 && j == 0) { + texture_render_clip(texture, &pos, &frame_top_left, cam); + } else if (i == (width - 1) && j == 0) { + texture_render_clip(texture, &pos, &frame_top_right, cam); + } else if (i == 0 && j == (height - 1)) { + texture_render_clip(texture, &pos, &frame_bottom_left, cam); + } else if (i == (width - 1) && j == (height - 1)) { + texture_render_clip(texture, &pos, &frame_bottom_right, cam); + } else if (i == 0) { + texture_render_clip(texture, &pos, &frame_left, cam); + } else if (i == (width - 1)) { + texture_render_clip(texture, &pos, &frame_right, cam); + } else if (j == 0) { + texture_render_clip(texture, &pos, &frame_top, cam); + } else if (j == (height - 1)) { + texture_render_clip(texture, &pos, &frame_bottom, cam); + } else { + texture_render_clip(texture, &pos, &frame_center, cam); + } + } + } +} + +void +gui_render_panel(Gui *gui, unsigned int width, unsigned int height, Camera *cam) +{ + gui_render_frame(gui, width/16, height/16, cam); + LinkedList *item = gui->health; while (item != NULL) { Sprite *s = item->data; @@ -104,13 +173,36 @@ gui_render(Gui *gui, Camera *cam) } +void +gui_render_log(Gui *gui, unsigned int width, unsigned int height, Camera *cam) +{ + gui_render_frame(gui, width/16, height/16, cam); +} + +static void +destroy_log(void) +{ + if (gui_log == NULL) + return; + + unsigned int i; + for (i = 0; i < log_length; ++i) + free(gui_log[i]); + + free(gui_log); + gui_log = NULL; +} + void gui_destroy(Gui *gui) { + destroy_log(); + while (gui->sprites != NULL) sprite_destroy(linkedlist_pop(&gui->sprites)); while (gui->health != NULL) sprite_destroy(linkedlist_pop(&gui->health)); + ht_destroy_custom(gui->textures, (void (*)(void*)) &texture_destroy); free(gui); } diff --git a/src/gui.h b/src/gui.h index c2bc868..3e853b6 100644 --- a/src/gui.h +++ b/src/gui.h @@ -20,7 +20,9 @@ void gui_set_current_health(Gui*, int current); Texture* gui_add_texture(Gui*, const char *path, SDL_Renderer*); -void gui_render(Gui*, Camera*); +void gui_render_panel(Gui*, unsigned int width, unsigned int height, Camera*); + +void gui_render_log(Gui*, unsigned int width, unsigned int height, Camera*); void gui_destroy(Gui*); diff --git a/src/main.c b/src/main.c index 1dad567..49842ff 100644 --- a/src/main.c +++ b/src/main.c @@ -22,9 +22,13 @@ static LinkedList *gSpriteList = NULL; static Map *gMap = NULL; static RoomMatrix *gRoomMatrix = NULL; static Gui *gGui = NULL; +static unsigned int cLevel = 1; +static double renderScale = 1.0; static GameState gGameState; static Camera gCamera; -static unsigned int cLevel = 1; +static SDL_Rect gameViewport; +static SDL_Rect bottomGuiViewport; +static SDL_Rect rightGuiViewport; static bool initSDL(void) @@ -32,12 +36,11 @@ bool initSDL(void) int imgFlags = IMG_INIT_PNG; Dimension dim = getScreenDimensions(); //Dimension dim = (Dimension) { 1920, 1080 }; - double scale = 1.0; if (dim.height > 1080) { printf("[**] Hi resolution screen detected (%u x %u)\n", dim.width, dim.height); - scale = ((double) dim.height)/1080; - printf("[**] Scaling by %f\n", scale); + renderScale = ((double) dim.height)/1080; + printf("[**] Scaling by %f\n", renderScale); } if (SDL_Init(SDL_INIT_VIDEO) < 0) @@ -61,8 +64,8 @@ bool initSDL(void) gWindow = SDL_CreateWindow("Breakhack", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - (int)(SCREEN_WIDTH * scale), - (int)(SCREEN_HEIGHT * scale), + (int)(SCREEN_WIDTH * renderScale), + (int)((SCREEN_HEIGHT * renderScale)), SDL_WINDOW_SHOWN); if (gWindow == NULL) { @@ -90,10 +93,24 @@ bool initSDL(void) return true; } +static void +initViewports(void) +{ + gameViewport = (SDL_Rect) { 0, 0, + GAME_VIEW_WIDTH, GAME_VIEW_HEIGHT }; + + bottomGuiViewport = (SDL_Rect) { 0, GAME_VIEW_HEIGHT, + BOTTOM_GUI_WIDTH, BOTTOM_GUI_WIDTH }; + + rightGuiViewport = (SDL_Rect) { GAME_VIEW_WIDTH, 0, + RIGHT_GUI_WIDTH, RIGHT_GUI_HEIGHT }; +} + static bool initGame(void) { gSpriteList = linkedlist_create(); + initViewports(); gMap = map_lua_generator_run(cLevel, gRenderer); return gSpriteList == NULL; } @@ -181,10 +198,18 @@ run_game(void) SDL_RenderClear(gRenderer); + SDL_RenderSetViewport(gRenderer, &gameViewport); map_render(gMap, &gCamera); player_render(gPlayer, &gCamera); roommatrix_render_lightmap(gRoomMatrix, &gCamera); - gui_render(gGui, &gCamera); + + SDL_RenderSetViewport(gRenderer, &rightGuiViewport); + gui_render_panel(gGui, RIGHT_GUI_WIDTH, + RIGHT_GUI_HEIGHT, &gCamera); + + SDL_RenderSetViewport(gRenderer, &bottomGuiViewport); + gui_render_log(gGui, BOTTOM_GUI_WIDTH, + BOTTOM_GUI_HEIGHT, &gCamera); SDL_RenderPresent(gRenderer);