Fixed build for windows

Created m_sprintf to make msvc happy.
This commit is contained in:
Linus Probert 2018-01-23 21:03:43 +01:00
parent 62b963e643
commit b96ba5a2e0
6 changed files with 42 additions and 11 deletions

View File

@ -1,5 +1,6 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "gui.h" #include "gui.h"
#include "util.h" #include "util.h"
@ -187,13 +188,14 @@ gui_render_panel(Gui *gui, unsigned int width, unsigned int height, Camera *cam)
void void
gui_log(char *message) gui_log(char *message)
{ {
// 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); 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, 200, message); m_strcpy(new_message, log_data.strlen, message);
log_data.count++; log_data.count++;
if (log_data.count > log_data.len) { if (log_data.count > log_data.len) {

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
#include <lua.h> #include <lua.h>
#include <lualib.h> #include <lualib.h>

View File

@ -69,10 +69,10 @@ has_collided(Monster *monster, RoomMatrix *matrix)
player_hit(space->player, dmg); player_hit(space->player, dmg);
if (dmg > 0) if (dmg > 0)
sprintf(msg, "Monster '%s' hit you for %u damage", m_sprintf(msg, 200, "Monster '%s' hit you for %u damage",
monster->label, dmg); monster->label, dmg);
else else
sprintf(msg, "Monster '%s' missed you", monster->label); m_sprintf(msg, 200, "Monster '%s' missed you", monster->label);
gui_log(msg); gui_log(msg);
free(msg); free(msg);

View File

@ -37,13 +37,18 @@ has_collided(Player *player, RoomMatrix *matrix)
else else
player->misses += 1; player->misses += 1;
if (hit > 0) {
char *msg = ec_malloc(200 * sizeof(char)); char *msg = ec_malloc(200 * sizeof(char));
sprintf(msg, "You hit '%s' for %u damage", 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); space->monster->label, hit);
gui_log(msg); gui_log(msg);
free(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.
@ -51,7 +56,7 @@ has_collided(Player *player, RoomMatrix *matrix)
player->xp += 10; player->xp += 10;
char *msg = ec_malloc(200 * sizeof(char)); char *msg = ec_malloc(200 * sizeof(char));
sprintf(msg, "You killed '%s' and gained %d xp", m_sprintf(msg, 200, "You killed '%s' and gained %d xp",
space->monster->label, 10); space->monster->label, 10);
gui_log(msg); gui_log(msg);
free(msg); free(msg);

View File

@ -1,7 +1,9 @@
#include "defines.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h>
#include "defines.h"
#ifndef _WIN32 #ifndef _WIN32
#include <unistd.h> #include <unistd.h>
@ -35,6 +37,21 @@ m_strncat(char *dest,
#endif // _MSC_VER #endif // _MSC_VER
} }
void
m_sprintf(char * dest, size_t destsz, const char * format, ...)
{
va_list args;
va_start(args, format);
#ifndef _MSC_VER
UNUSED(destsz);
vsprintf(dest, format, args);
#else // _MSC_VER
vsprintf_s(dest, destsz, format, args);
#endif // _MSC_VER
va_end(args);
}
void fatal(char *message) void fatal(char *message)
{ {
char error_message[100]; char error_message[100];

View File

@ -1,13 +1,19 @@
#ifndef UTIL_H_ #ifndef UTIL_H_
#define UTIL_H_ #define UTIL_H_
void fatal(char *message); void
fatal(char *message);
void *ec_malloc(unsigned int size); void *
ec_malloc(unsigned int size);
void m_strcpy(char *dest, size_t destsz, char *src); void
m_strcpy(char *dest, size_t destsz, char *src);
void void
m_strncat(char *dest, size_t destsz, char *src, size_t srcsz); m_strncat(char *dest, size_t destsz, char *src, size_t srcsz);
void
m_sprintf(char *dest, size_t destsz, const char *format, ...);
#endif // UTIL_H_ #endif // UTIL_H_