Better info, debug, error and fatal printouts.

This commit is contained in:
Linus Probert 2018-01-23 22:54:02 +01:00
parent b96ba5a2e0
commit 4088357584
11 changed files with 108 additions and 80 deletions

View File

@ -1,6 +1,8 @@
#include <stdio.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h>
#include "gui.h" #include "gui.h"
#include "util.h" #include "util.h"
@ -186,16 +188,21 @@ gui_render_panel(Gui *gui, unsigned int width, unsigned int height, Camera *cam)
} }
void void
gui_log(char *message) gui_log(const char *fmt, ...)
{ {
// TODO(Linus): This could take va_args, would be nicer
char *new_message; char *new_message;
unsigned int i; unsigned int i;
assert(strlen(message) <= log_data.strlen);
new_message = ec_malloc(log_data.strlen * sizeof(char)); new_message = ec_malloc(log_data.strlen * sizeof(char));
m_strcpy(new_message, log_data.strlen, message);
va_list args;
va_start(args, fmt);
#ifndef _MSC_VER
vsprintf(new_message, fmt, args);
#else // _MSC_VER
vsprintf_s(new_message, log_data.strlen, fmt, args);
#endif // _MSC_VER
va_end(args);
log_data.count++; log_data.count++;
if (log_data.count > log_data.len) { if (log_data.count > log_data.len) {

View File

@ -28,7 +28,7 @@ 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_render_log(Gui*, unsigned int width, unsigned int height, Camera*);
void gui_log(char *message); void gui_log(const char *fmt, ...);
void gui_destroy(Gui*); void gui_destroy(Gui*);

View File

@ -4,14 +4,14 @@
#include <limits.h> #include <limits.h>
#include "hashtable.h" #include "hashtable.h"
#include "defines.h" #include "defines.h"
#include "util.h"
static void* static void*
ec_malloc(unsigned int size) ec_malloc(unsigned int size)
{ {
void *ptr = malloc(size); void *ptr = malloc(size);
if (ptr == NULL) { if (ptr == NULL) {
fprintf(stderr, "[!!] Failed to allocate hashtable\n"); fatal("Failed to allocate hashtable");
exit(-1);
} }
return ptr; return ptr;

View File

@ -14,6 +14,7 @@
#include "roommatrix.h" #include "roommatrix.h"
#include "gamestate.h" #include "gamestate.h"
#include "gui.h" #include "gui.h"
#include "util.h"
static SDL_Window *gWindow = NULL; static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL; static SDL_Renderer *gRenderer = NULL;
@ -38,25 +39,25 @@ bool initSDL(void)
//Dimension dim = (Dimension) { 1920, 1080 }; //Dimension dim = (Dimension) { 1920, 1080 };
if (dim.height > 1080) { if (dim.height > 1080) {
printf("[**] Hi resolution screen detected (%u x %u)\n", dim.width, dim.height); info("Hi resolution screen detected (%u x %u)\n", dim.width, dim.height);
renderScale = ((double) dim.height)/1080; renderScale = ((double) dim.height)/1080;
printf("[**] Scaling by %f\n", renderScale); info("Scaling by %f\n", renderScale);
} }
if (SDL_Init(SDL_INIT_VIDEO) < 0) if (SDL_Init(SDL_INIT_VIDEO) < 0)
{ {
printf("[!!] Could not initiate SDL2: %s\n", SDL_GetError()); error("Could not initiate SDL2: %s\n", SDL_GetError());
return false; return false;
} }
if ( (IMG_Init(imgFlags) & imgFlags) == 0 ) { if ( (IMG_Init(imgFlags) & imgFlags) == 0 ) {
printf("[!!] Unable to initiate img loading: %s\n", error("Unable to initiate img loading: %s\n",
IMG_GetError()); IMG_GetError());
return false; return false;
} }
if ( TTF_Init() == -1 ) { if ( TTF_Init() == -1 ) {
printf("[!!] Unable to initiate ttf library: %s\n", error("Unable to initiate ttf library: %s\n",
TTF_GetError()); TTF_GetError());
return false; return false;
} }
@ -69,23 +70,23 @@ bool initSDL(void)
SDL_WINDOW_SHOWN); SDL_WINDOW_SHOWN);
if (gWindow == NULL) if (gWindow == NULL)
{ {
printf("[!!] Unable to create window: %s\n", SDL_GetError()); error("Unable to create window: %s\n", SDL_GetError());
return false; return false;
} }
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer == NULL) if (gRenderer == NULL)
{ {
printf("[!!] Unable to create renderer: %s\n", SDL_GetError()); error("Unable to create renderer: %s\n", SDL_GetError());
return false; return false;
} }
if (SDL_SetRenderDrawBlendMode(gRenderer, SDL_BLENDMODE_BLEND) < 0) { if (SDL_SetRenderDrawBlendMode(gRenderer, SDL_BLENDMODE_BLEND) < 0) {
printf("[!!] Unable to set blend mode: %s\n", SDL_GetError()); error("Unable to set blend mode: %s\n", SDL_GetError());
return false; return false;
} }
if (SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0) if (SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0)
{ {
printf("[!!] Unable to initiate scaling: %s\n", error("Unable to initiate scaling: %s\n",
SDL_GetError()); SDL_GetError());
return false; return false;
} }
@ -167,11 +168,11 @@ check_next_level(void)
MapTile *tile = room->tiles[pos.x][pos.y]; MapTile *tile = room->tiles[pos.x][pos.y];
if (!tile) { if (!tile) {
printf("[!!] Looks like we are out of place\n"); error("Looks like we are out of place\n");
return; return;
} }
if (tile->levelExit) { if (tile->levelExit) {
printf("[**] Building new map\n"); info("Building new map\n");
map_destroy(gMap); map_destroy(gMap);
gMap = map_lua_generator_run(++cLevel, gRenderer); gMap = map_lua_generator_run(++cLevel, gRenderer);
gPlayer->sprite->pos = (Position) { gPlayer->sprite->pos = (Position) {
@ -233,14 +234,13 @@ void run(void)
run_game(); run_game();
break; break;
case MENU: case MENU:
fprintf(stderr, "[!!] MENU not implemented\n"); error("MENU not implemented\n");
break; break;
case IN_GAME_MENU: case IN_GAME_MENU:
fprintf(stderr, error("IN_GAME_MENU not implemented\n");
"[!!] IN_GAME_MENU not implemented\n");
break; break;
case GAME_OVER: case GAME_OVER:
fprintf(stderr, "[!!] GAME_OVER not implemented\n"); error("GAME_OVER not implemented\n");
break; break;
default: default:
break; break;

View File

@ -220,14 +220,12 @@ Map* map_lua_generator_run(unsigned int level, SDL_Renderer *renderer)
int status, result; int status, result;
char file[] = "data/mapgen.lua"; char file[] = "data/mapgen.lua";
printf("[**] Running lua map script: %s\n", file); info("Running lua map script: %s", file);
lua_State *L = load_lua_state(); lua_State *L = load_lua_state();
status = luaL_loadfile(L, file); status = luaL_loadfile(L, file);
if (status) { if (status) {
fprintf(stderr, "[!!] Couldn't load file: %s\n", fatal("Couldn't load file: %s\n", lua_tostring(L, -1));
lua_tostring(L, -1));
exit(-1);
} }
// Present stuff to lua // Present stuff to lua
@ -257,9 +255,7 @@ Map* map_lua_generator_run(unsigned int level, SDL_Renderer *renderer)
result = lua_pcall(L, 0, LUA_MULTRET, 0); result = lua_pcall(L, 0, LUA_MULTRET, 0);
if (result) { if (result) {
fprintf(stderr, "[!!] Failed to run script: %s\n", fatal("Failed to run script: %s\n", lua_tostring(L, -1));
lua_tostring(L, -1));
exit(-1);
} }
lua_getglobal(L, "map"); lua_getglobal(L, "map");
@ -267,7 +263,7 @@ Map* map_lua_generator_run(unsigned int level, SDL_Renderer *renderer)
lua_close(L); lua_close(L);
printf("[**] Done\n"); info("Done");
return map; return map;
} }

View File

@ -63,19 +63,15 @@ has_collided(Monster *monster, RoomMatrix *matrix)
RoomSpace *space = &matrix->spaces[roomPos.x][roomPos.y]; RoomSpace *space = &matrix->spaces[roomPos.x][roomPos.y];
if (space->player) { if (space->player) {
char *msg = ec_malloc(200 * sizeof(char));
unsigned int dmg = stats_fight(&monster->stats, unsigned int dmg = stats_fight(&monster->stats,
&space->player->stats); &space->player->stats);
player_hit(space->player, dmg); player_hit(space->player, dmg);
if (dmg > 0) if (dmg > 0)
m_sprintf(msg, 200, "Monster '%s' hit you for %u damage", gui_log("Monster '%s' hit you for %u damage", monster->label, dmg);
monster->label, dmg);
else else
m_sprintf(msg, 200, "Monster '%s' missed you", monster->label); gui_log("Monster '%s' missed you", monster->label);
gui_log(msg);
free(msg);
} }
return space->occupied; return space->occupied;

View File

@ -37,29 +37,17 @@ has_collided(Player *player, RoomMatrix *matrix)
else else
player->misses += 1; player->misses += 1;
char *msg = ec_malloc(200 * sizeof(char)); if (hit > 0)
if (hit > 0) { gui_log("You hit '%s' for %u damage", space->monster->label, hit);
printf("Dmg: %u, Label: %s\n", hit, space->monster->label); else
m_sprintf(msg, 200, "You hit '%s' for %u damage", gui_log("You missed '%s'", space->monster->label);
space->monster->label, hit);
gui_log(msg);
} else {
m_sprintf(msg, 200, "You missed '%s'",
space->monster->label);
gui_log(msg);
}
free(msg);
if (space->monster->stats.hp <= 0) { if (space->monster->stats.hp <= 0) {
// TODO(Linus): This needs some love later on. // TODO(Linus): This needs some love later on.
player->kills += 1; player->kills += 1;
player->xp += 10; player->xp += 10;
char *msg = ec_malloc(200 * sizeof(char)); gui_log("You killed '%s' and gained %d xp", space->monster->label, 10);
m_sprintf(msg, 200, "You killed '%s' and gained %d xp",
space->monster->label, 10);
gui_log(msg);
free(msg);
} }
} }
@ -243,16 +231,16 @@ player_print(Player *p)
Position roomPos = position_to_matrix_coords(&p->sprite->pos); Position roomPos = position_to_matrix_coords(&p->sprite->pos);
Position pos = p->sprite->pos; Position pos = p->sprite->pos;
printf("\n"); debug("\n");
printf("--------=== <[ Player Stats ]> ===--------\n"); debug("--------=== <[ Player Stats ]> ===--------");
printf("HP: %d\n", p->stats.hp); debug("HP: %d", p->stats.hp);
printf("Level: %u\tXP:\t%u\n", p->stats.lvl, p->xp); debug("Level: %u\tXP:\t%u", p->stats.lvl, p->xp);
printf("Hits: %u\tMisses:\t%u\n", p->hits, p->misses); debug("Hits: %u\tMisses:\t%u", p->hits, p->misses);
printf("Kills: %u\n", p->kills); debug("Kills: %u", p->kills);
printf("Steps: %u\n", p->total_steps); debug("Steps: %u", p->total_steps);
printf("Pos: %dx%d\tRoomPos: %dx%d\n", pos.x, pos.y, debug("Pos: %dx%d\tRoomPos: %dx%d", pos.x, pos.y,
roomPos.x, roomPos.y); roomPos.x, roomPos.y);
printf("------------------------------------------\n"); debug("------------------------------------------");
} }
void void

View File

@ -5,6 +5,7 @@
#include "stats.h" #include "stats.h"
#include "random.h" #include "random.h"
#include "util.h"
unsigned int unsigned int
stats_fight(Stats *attacker, Stats *defender) stats_fight(Stats *attacker, Stats *defender)
@ -19,9 +20,9 @@ stats_fight(Stats *attacker, Stats *defender)
defRoll = (get_random(19) + 1) + defender->def; defRoll = (get_random(19) + 1) + defender->def;
dmgRoll = 0; dmgRoll = 0;
printf("\n"); debug("");
printf("-----------[ FIGHT ]---------\n"); debug("-----------[ FIGHT ]---------");
printf("Attacking: %d Defending: %d\n", atkRoll, defRoll); debug("Attacking: %d Defending: %d", atkRoll, defRoll);
if (atkRoll > defRoll) { if (atkRoll > defRoll) {
dmgRoll = (rand() % attacker->dmg) + 1; dmgRoll = (rand() % attacker->dmg) + 1;
@ -30,8 +31,8 @@ stats_fight(Stats *attacker, Stats *defender)
defender->hp -= dmgRoll; defender->hp -= dmgRoll;
} }
printf("Attacker hp: %d Defender hp: %d\n", attacker->hp, defender->hp); debug("Attacker hp: %d Defender hp: %d", attacker->hp, defender->hp);
printf("-----------------------------\n"); debug("-----------------------------");
return dmgRoll; return dmgRoll;
} }

View File

@ -24,7 +24,7 @@ texture_load_from_file(Texture *texture,
if (surface == NULL) if (surface == NULL)
{ {
printf("[!!] Failed to load texture (%s): %s\n", error("Failed to load texture (%s): %s",
path, IMG_GetError()); path, IMG_GetError());
return; return;
} }
@ -40,7 +40,7 @@ texture_load_from_file(Texture *texture,
surface); surface);
if (texture->texture == NULL) if (texture->texture == NULL)
{ {
printf("[!!] Failed to create texture (%s): %s\n", error("Failed to create texture (%s): %s",
path, path,
SDL_GetError()); SDL_GetError());
} }
@ -55,7 +55,7 @@ texture_load_font(Texture *t, const char *path, unsigned int size)
TTF_CloseFont(t->font); TTF_CloseFont(t->font);
t->font = TTF_OpenFont(path, size); t->font = TTF_OpenFont(path, size);
if (t->font == NULL) { if (t->font == NULL) {
fprintf(stderr, "[!!] Failed to load font %s: %s\n", error("Failed to load font %s: %s",
path, path,
TTF_GetError()); TTF_GetError());
return; return;
@ -71,7 +71,7 @@ texture_load_from_text(Texture *t,
SDL_Surface *surface = TTF_RenderText_Solid( t->font, text, c ); SDL_Surface *surface = TTF_RenderText_Solid( t->font, text, c );
if (surface == NULL) if (surface == NULL)
{ {
printf("[!!] Unable to create texture from rendered text: %s\n", error("Unable to create texture from rendered text: %s",
IMG_GetError()); IMG_GetError());
return; return;
} }
@ -83,7 +83,7 @@ texture_load_from_text(Texture *t,
t->texture = SDL_CreateTextureFromSurface( renderer, surface ); t->texture = SDL_CreateTextureFromSurface( renderer, surface );
if (t->texture == NULL) { if (t->texture == NULL) {
printf("[!!] Failed to create texture from text: %s\n", error("Failed to create texture from text: %s",
SDL_GetError()); SDL_GetError());
return; return;
} }

View File

@ -52,13 +52,44 @@ m_sprintf(char * dest, size_t destsz, const char * format, ...)
va_end(args); va_end(args);
} }
void fatal(char *message) void debug(const char *fmt, ...)
{ {
char error_message[100]; va_list args;
printf("[--] ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
printf("\n");
}
m_strcpy(error_message, 100, "[!!] Fatal Error "); void info(const char * fmt, ...)
m_strncat(error_message, 100, message, 83); {
perror(error_message); va_list args;
printf("[**] ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
printf("\n");
}
void error(const char *fmt, ...)
{
va_list args;
fprintf(stderr, "[!*] Error ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}
void fatal(const char *fmt, ...)
{
va_list args;
fprintf(stderr, "[!!] Fatal Error ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
exit(-1); exit(-1);
} }

View File

@ -2,7 +2,16 @@
#define UTIL_H_ #define UTIL_H_
void void
fatal(char *message); fatal(const char *fmt, ...);
void
error(const char *fmt, ...);
void
debug(const char *fmt, ...);
void
info(const char *fmt, ...);
void * void *
ec_malloc(unsigned int size); ec_malloc(unsigned int size);