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 <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "gui.h"
#include "util.h"
@ -186,16 +188,21 @@ gui_render_panel(Gui *gui, unsigned int width, unsigned int height, Camera *cam)
}
void
gui_log(char *message)
gui_log(const char *fmt, ...)
{
// TODO(Linus): This could take va_args, would be nicer
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, 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++;
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_log(char *message);
void gui_log(const char *fmt, ...);
void gui_destroy(Gui*);

View File

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

View File

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

View File

@ -220,14 +220,12 @@ Map* map_lua_generator_run(unsigned int level, SDL_Renderer *renderer)
int status, result;
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();
status = luaL_loadfile(L, file);
if (status) {
fprintf(stderr, "[!!] Couldn't load file: %s\n",
lua_tostring(L, -1));
exit(-1);
fatal("Couldn't load file: %s\n", lua_tostring(L, -1));
}
// 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);
if (result) {
fprintf(stderr, "[!!] Failed to run script: %s\n",
lua_tostring(L, -1));
exit(-1);
fatal("Failed to run script: %s\n", lua_tostring(L, -1));
}
lua_getglobal(L, "map");
@ -267,7 +263,7 @@ Map* map_lua_generator_run(unsigned int level, SDL_Renderer *renderer)
lua_close(L);
printf("[**] Done\n");
info("Done");
return map;
}

View File

@ -63,19 +63,15 @@ 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)
m_sprintf(msg, 200, "Monster '%s' hit you for %u damage",
monster->label, dmg);
gui_log("Monster '%s' hit you for %u damage", monster->label, dmg);
else
m_sprintf(msg, 200, "Monster '%s' missed you", monster->label);
gui_log(msg);
free(msg);
gui_log("Monster '%s' missed you", monster->label);
}
return space->occupied;

View File

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

View File

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

View File

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

View File

@ -52,13 +52,44 @@ m_sprintf(char * dest, size_t destsz, const char * format, ...)
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 ");
m_strncat(error_message, 100, message, 83);
perror(error_message);
void info(const char * fmt, ...)
{
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);
}

View File

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