From 30058ea4e0d5568972b0ad80dfa073d0c4a48eeb Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Sun, 19 Aug 2018 20:25:31 +0200 Subject: [PATCH] Creates gui_util and moves some code out from gui.c --- CMakeLists.txt | 1 + src/gui.c | 107 +++------------------------------------------- src/gui_util.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ src/gui_util.h | 27 ++++++++++++ 4 files changed, 148 insertions(+), 100 deletions(-) create mode 100644 src/gui_util.c create mode 100644 src/gui_util.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 750b7cc..25b436f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,6 +177,7 @@ add_executable(breakhack src/screen src/hiscore src/object + src/gui_util ) # Sqlite has some warnings that I we don't need to see diff --git a/src/gui.c b/src/gui.c index 63b15cf..0aaa162 100644 --- a/src/gui.c +++ b/src/gui.c @@ -26,6 +26,7 @@ #include "util.h" #include "map.h" #include "texturecache.h" +#include "gui_util.h" #define DEFAULT_LOG { NULL, LOG_LINES_COUNT, 0, 200 } #define DEFAULT_EVENT_MESSAGES { NULL, 5, 0, 200 } @@ -33,16 +34,6 @@ #define POS_Y_COLLECTABLES 64 #define POS_Y_XPBAR 128 -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 struct LogData_t { char **log; unsigned int len; @@ -57,9 +48,6 @@ static struct GuiEventMsgData_t { unsigned int strlen; } event_messages = DEFAULT_EVENT_MESSAGES; -static Sprite* -gui_create_frame(unsigned int width, unsigned int height, Camera *cam); - static void gui_malloc_log(void) { @@ -174,12 +162,12 @@ init_sprites(Gui *gui, Camera *cam) s->pos = (Position) { 16, POS_Y_COLLECTABLES + 32 }; linkedlist_append(&gui->sprites, s); - gui->rightFrame = gui_create_frame(RIGHT_GUI_WIDTH/16, - RIGHT_GUI_HEIGHT/16, - cam); - gui->bottomFrame = gui_create_frame(BOTTOM_GUI_WIDTH/16, - BOTTOM_GUI_HEIGHT/16, - cam); + gui->rightFrame = gui_util_create_frame_sprite(RIGHT_GUI_WIDTH/16, + RIGHT_GUI_HEIGHT/16, + cam); + gui->bottomFrame = gui_util_create_frame_sprite(BOTTOM_GUI_WIDTH/16, + BOTTOM_GUI_HEIGHT/16, + cam); } Gui* @@ -400,87 +388,6 @@ gui_update_player_stats(Gui *gui, Player *player, Map *map, SDL_Renderer *render } } -static Sprite* -gui_create_frame(unsigned int width, unsigned int height, Camera *cam) -{ - Sprite *frame = sprite_create(); - Texture *texture = texture_create(); - texture->dim = (Dimension) { - width * 16, - height * 16 - }; - frame->textures[0] = texture; - frame->destroyTextures = true; - frame->pos = (Position) { 0, 0 }; - frame->dim = (Dimension) { width*16, height*16 }; - frame->fixed = true; - texture_create_blank(texture, - SDL_TEXTUREACCESS_TARGET, - cam->renderer); - Texture *source = texturecache_get("GUI/GUI0.png"); - - SDL_SetRenderTarget(cam->renderer, texture->texture); - SDL_RenderClear(cam->renderer); - - SDL_Rect box = { 0, 0, 16, 16 }; - unsigned int i, j; - for (i = 0; i < width; ++i) { - for (j = 0; j < height; ++j) { - box.x = i * 16; - box.y = j * 16; - - if (i == 0 && j == 0) { - texture_render_clip(source, - &box, - &frame_top_left, - cam); - } else if (i == (width - 1) && j == 0) { - texture_render_clip(source, - &box, - &frame_top_right, - cam); - } else if (i == 0 && j == (height - 1)) { - texture_render_clip(source, - &box, - &frame_bottom_left, - cam); - } else if (i == (width - 1) && j == (height - 1)) { - texture_render_clip(source, - &box, - &frame_bottom_right, - cam); - } else if (i == 0) { - texture_render_clip(source, - &box, - &frame_left, - cam); - } else if (i == (width - 1)) { - texture_render_clip(source, - &box, - &frame_right, - cam); - } else if (j == 0) { - texture_render_clip(source, - &box, - &frame_top, - cam); - } else if (j == (height - 1)) { - texture_render_clip(source, - &box, - &frame_bottom, - cam); - } else { - texture_render_clip(source, - &box, - &frame_center, - cam); - } - } - } - SDL_SetRenderTarget(cam->renderer, NULL); - return frame; -} - void gui_render_panel(Gui *gui, Camera *cam) { diff --git a/src/gui_util.c b/src/gui_util.c new file mode 100644 index 0000000..2e7c9cb --- /dev/null +++ b/src/gui_util.c @@ -0,0 +1,113 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 Linus Probert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "texturecache.h" +#include "gui_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 }; + +Sprite * +gui_util_create_frame_sprite(Uint32 width, + Uint32 height, + Camera *cam) +{ + Sprite *frame = sprite_create(); + Texture *texture = texture_create(); + texture->dim = (Dimension) { + width * 16, + height * 16 + }; + frame->textures[0] = texture; + frame->destroyTextures = true; + frame->pos = (Position) { 0, 0 }; + frame->dim = (Dimension) { width*16, height*16 }; + frame->fixed = true; + texture_create_blank(texture, + SDL_TEXTUREACCESS_TARGET, + cam->renderer); + Texture *source = texturecache_get("GUI/GUI0.png"); + + SDL_SetRenderTarget(cam->renderer, texture->texture); + SDL_RenderClear(cam->renderer); + + SDL_Rect box = { 0, 0, 16, 16 }; + unsigned int i, j; + for (i = 0; i < width; ++i) { + for (j = 0; j < height; ++j) { + box.x = i * 16; + box.y = j * 16; + + if (i == 0 && j == 0) { + texture_render_clip(source, + &box, + &frame_top_left, + cam); + } else if (i == (width - 1) && j == 0) { + texture_render_clip(source, + &box, + &frame_top_right, + cam); + } else if (i == 0 && j == (height - 1)) { + texture_render_clip(source, + &box, + &frame_bottom_left, + cam); + } else if (i == (width - 1) && j == (height - 1)) { + texture_render_clip(source, + &box, + &frame_bottom_right, + cam); + } else if (i == 0) { + texture_render_clip(source, + &box, + &frame_left, + cam); + } else if (i == (width - 1)) { + texture_render_clip(source, + &box, + &frame_right, + cam); + } else if (j == 0) { + texture_render_clip(source, + &box, + &frame_top, + cam); + } else if (j == (height - 1)) { + texture_render_clip(source, + &box, + &frame_bottom, + cam); + } else { + texture_render_clip(source, + &box, + &frame_center, + cam); + } + } + } + SDL_SetRenderTarget(cam->renderer, NULL); + return frame; +} diff --git a/src/gui_util.h b/src/gui_util.h new file mode 100644 index 0000000..52e4d7f --- /dev/null +++ b/src/gui_util.h @@ -0,0 +1,27 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 Linus Probert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "sprite.h" +#include "camera.h" + +Sprite * +gui_util_create_frame_sprite(Uint32 width, + Uint32 height, + Camera*);