Creates gui_util and moves some code out from gui.c

This commit is contained in:
Linus Probert 2018-08-19 20:25:31 +02:00
parent 854f2c0918
commit 30058ea4e0
4 changed files with 148 additions and 100 deletions

View File

@ -177,6 +177,7 @@ add_executable(breakhack
src/screen src/screen
src/hiscore src/hiscore
src/object src/object
src/gui_util
) )
# Sqlite has some warnings that I we don't need to see # Sqlite has some warnings that I we don't need to see

107
src/gui.c
View File

@ -26,6 +26,7 @@
#include "util.h" #include "util.h"
#include "map.h" #include "map.h"
#include "texturecache.h" #include "texturecache.h"
#include "gui_util.h"
#define DEFAULT_LOG { NULL, LOG_LINES_COUNT, 0, 200 } #define DEFAULT_LOG { NULL, LOG_LINES_COUNT, 0, 200 }
#define DEFAULT_EVENT_MESSAGES { NULL, 5, 0, 200 } #define DEFAULT_EVENT_MESSAGES { NULL, 5, 0, 200 }
@ -33,16 +34,6 @@
#define POS_Y_COLLECTABLES 64 #define POS_Y_COLLECTABLES 64
#define POS_Y_XPBAR 128 #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 { static struct LogData_t {
char **log; char **log;
unsigned int len; unsigned int len;
@ -57,9 +48,6 @@ static struct GuiEventMsgData_t {
unsigned int strlen; unsigned int strlen;
} event_messages = DEFAULT_EVENT_MESSAGES; } event_messages = DEFAULT_EVENT_MESSAGES;
static Sprite*
gui_create_frame(unsigned int width, unsigned int height, Camera *cam);
static void static void
gui_malloc_log(void) gui_malloc_log(void)
{ {
@ -174,12 +162,12 @@ init_sprites(Gui *gui, Camera *cam)
s->pos = (Position) { 16, POS_Y_COLLECTABLES + 32 }; s->pos = (Position) { 16, POS_Y_COLLECTABLES + 32 };
linkedlist_append(&gui->sprites, s); linkedlist_append(&gui->sprites, s);
gui->rightFrame = gui_create_frame(RIGHT_GUI_WIDTH/16, gui->rightFrame = gui_util_create_frame_sprite(RIGHT_GUI_WIDTH/16,
RIGHT_GUI_HEIGHT/16, RIGHT_GUI_HEIGHT/16,
cam); cam);
gui->bottomFrame = gui_create_frame(BOTTOM_GUI_WIDTH/16, gui->bottomFrame = gui_util_create_frame_sprite(BOTTOM_GUI_WIDTH/16,
BOTTOM_GUI_HEIGHT/16, BOTTOM_GUI_HEIGHT/16,
cam); cam);
} }
Gui* 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 void
gui_render_panel(Gui *gui, Camera *cam) gui_render_panel(Gui *gui, Camera *cam)
{ {

113
src/gui_util.c Normal file
View File

@ -0,0 +1,113 @@
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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;
}

27
src/gui_util.h Normal file
View File

@ -0,0 +1,27 @@
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "sprite.h"
#include "camera.h"
Sprite *
gui_util_create_frame_sprite(Uint32 width,
Uint32 height,
Camera*);