Gui log is working

This commit is contained in:
Linus_Probert 2018-01-23 14:11:03 +01:00
parent 5a95808266
commit dc7927c6b0
4 changed files with 100 additions and 19 deletions

View File

@ -4,8 +4,10 @@
#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 };
#define DEFAULT_LOG { NULL, 50, 0, 200 }
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 };
@ -14,33 +16,43 @@ 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 struct LogData_t {
char **log;
unsigned int len;
unsigned int count;
unsigned int strlen;
} log_data = DEFAULT_LOG;
static void
gui_malloc_log(void)
{
static bool log_allocated = false;
if (log_allocated)
if (log_data.log != NULL)
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;
log_data.log = ec_malloc(log_data.len * sizeof(char*));
for (i = 0; i < log_data.len; ++i)
log_data.log[i] = NULL;
}
Gui*
gui_create()
{
Texture *t;
unsigned int i;
Gui *gui = ec_malloc(sizeof(Gui));
gui->sprites = linkedlist_create();
gui->health = linkedlist_create();
gui->textures = ht_create(5);
for (i = 0; i < LOG_LINES_COUNT; ++i) {
t = texture_create();
texture_load_font(t, "assets/GUI/SDS_8x8.ttf", LOG_FONT_SIZE);
gui->log_lines[i] = t;
}
gui_malloc_log();
return gui;
@ -173,24 +185,64 @@ gui_render_panel(Gui *gui, unsigned int width, unsigned int height, Camera *cam)
}
void
gui_log(char *message)
{
char *new_message;
unsigned int i;
assert(strlen(message) <= log_data.strlen);
new_message = ec_malloc(log_data.strlen * sizeof(char));
m_strcpy(new_message, 200, message);
log_data.count++;
if (log_data.count > log_data.len) {
log_data.count = log_data.len;
free(log_data.log[log_data.count-1]);
log_data.log[log_data.count-1] = NULL;
}
for (i = log_data.count - 1; i > 0; --i) {
log_data.log[i] = log_data.log[i-1];
}
log_data.log[0] = new_message;
}
void
gui_render_log(Gui *gui, unsigned int width, unsigned int height, Camera *cam)
{
static SDL_Color color = { 255, 255, 255, 255 };
Texture *t;
unsigned int i;
unsigned int render_count;
Position p;
render_count = LOG_LINES_COUNT > log_data.count ? log_data.count : LOG_LINES_COUNT;
p = (Position) { 16, 0 };
gui_render_frame(gui, width/16, height/16, cam);
for (i = 0; i < render_count; ++i) {
p.y = 16 + ((LOG_FONT_SIZE+1) * i);
t = gui->log_lines[i];
texture_load_from_text(t, log_data.log[i], color, cam->renderer);
texture_render(t, &p, cam);
}
}
static void
destroy_log(void)
{
if (gui_log == NULL)
if (log_data.log == NULL)
return;
unsigned int i;
for (i = 0; i < log_length; ++i)
free(gui_log[i]);
for (i = 0; i < log_data.count; ++i)
free(log_data.log[i]);
free(gui_log);
gui_log = NULL;
free(log_data.log);
log_data.log = NULL;
}
void

View File

@ -1,6 +1,9 @@
#ifndef GUI_H_
#define GUI_H_
#define LOG_LINES_COUNT 15
#define LOG_FONT_SIZE 8
#include "linkedlist.h"
#include "hashtable.h"
#include "sprite.h"
@ -10,6 +13,7 @@ typedef struct {
LinkedList *sprites;
LinkedList *health;
Hashtable *textures;
Texture *log_lines[LOG_LINES_COUNT];
} Gui;
Gui* gui_create(void);
@ -24,6 +28,8 @@ 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_log(char *message);
void gui_destroy(Gui*);
#endif // GUI_H_

View File

@ -5,6 +5,7 @@
#include "player.h"
#include "monster.h"
#include "random.h"
#include "gui.h"
static void
monster_load_texts(Monster *m, SDL_Renderer *renderer)
@ -62,9 +63,19 @@ has_collided(Monster *monster, RoomMatrix *matrix)
RoomSpace *space = &matrix->spaces[roomPos.x][roomPos.y];
if (space->player) {
char *msg = ec_malloc(200 * sizeof(char));
unsigned int dmg = stats_fight(&monster->stats,
&space->player->stats);
player_hit(space->player, dmg);
if (dmg > 0)
sprintf(msg, "Monster '%s' hit you for %d damage",
monster->label, dmg);
else
sprintf(msg, "Monster '%s' missed you", monster->label);
gui_log(msg);
free(msg);
}
return space->occupied;
@ -211,9 +222,6 @@ monster_move(Monster *m, RoomMatrix *rm)
monsterRoomPos = position_to_matrix_coords(&m->sprite->pos);
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = true;
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = m;
if (m->label)
printf("Monster '%s' moved.\n", m->label);
}
void

View File

@ -5,6 +5,7 @@
#include "player.h"
#include "monster.h"
#include "util.h"
#include "gui.h"
#define ENGINEER_STATS { 12, 12, 5, 7, 2, 1, 1 }
#define MAGE_STATS { 12, 12, 5, 7, 2, 1, 1 }
@ -36,10 +37,24 @@ has_collided(Player *player, RoomMatrix *matrix)
else
player->misses += 1;
if (hit > 0) {
char *msg = ec_malloc(200 * sizeof(char));
sprintf(msg, "You hit '%s' for %d damage",
space->monster->label, hit);
gui_log(msg);
free(msg);
}
if (space->monster->stats.hp <= 0) {
// TODO(Linus): This needs some love later on.
player->kills += 1;
player->xp += 10;
char *msg = ec_malloc(200 * sizeof(char));
sprintf(msg, "You killed '%s' and gained %d xp",
space->monster->label, 10);
gui_log(msg);
free(msg);
}
}