Fixed a lot of stuff to help cross platform compilation

This commit is contained in:
Linus Probert 2017-12-19 21:00:02 +01:00
parent 7f6507d30c
commit faad866c8b
33 changed files with 131 additions and 61 deletions

3
.vimrc
View File

@ -1,6 +1,7 @@
nnoremap <F1> :Make<cr> nnoremap <F1> :Make<cr>
nnoremap <F2> :Make clean<cr>
au FileType c setl makeprg=ninja\ -C\ build au FileType c setl makeprg=ninja\ -C\ build
au FileType h setl makeprg=ninja\ -C\ build au FileType h setl makeprg=ninja\ -C\ build
let g:syntastic_c_include_dirs = [ 'linkedlist', 'hashtable' ] let g:syntastic_c_include_dirs = [ 'build' ]

View File

@ -5,27 +5,38 @@ SET(CMAKE_COLOR_MAKEFILE ON)
project(breakhack C) project(breakhack C)
include(FindLua) include(FindLua)
include(FindX11)
include(cmake/FindSDL2.cmake) include(cmake/FindSDL2.cmake)
include(cmake/FindSDL2_image.cmake) include(cmake/FindSDL2_image.cmake)
include(cmake/FindSDL2_mixer.cmake) include(cmake/FindSDL2_mixer.cmake)
include(cmake/FindSDL2_ttf.cmake) include(cmake/FindSDL2_ttf.cmake)
configure_file(
"${PROJECT_SOURCE_DIR}/src/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
if (NOT WIN32) if (NOT WIN32)
include(FindX11)
include(cmake/FindCheck.cmake) include(cmake/FindCheck.cmake)
endif (NOT WIN32) endif (NOT WIN32)
include_directories( include_directories(
${PROJECT_BINARY_DIR}
${LUA_INCLUDE_DIR} ${LUA_INCLUDE_DIR}
${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR} ${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_MIXER_INCLUDE_DIR} ${SDL2_MIXER_INCLUDE_DIR}
${SDL2_TTF_INCLUDE_DIR} ${SDL2_TTF_INCLUDE_DIR}
${X11_INCLUDE_DIR}
${CHECK_INCLUDE_DIR}
) )
add_definitions("-Wall") if (NOT WIN32)
include_directories(
${X11_INCLUDE_DIR}
${CHECK_INCLUDE_DIR}
)
endif (NOT WIN32)
add_definitions("-std=gnu11 -pedantic -Wall -Wextra -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes")
# PROGRAMS: # PROGRAMS:
add_executable(breakhack add_executable(breakhack
@ -55,9 +66,14 @@ target_link_libraries(breakhack
${SDL2_IMAGE_LIBRARY} ${SDL2_IMAGE_LIBRARY}
${SDL2_MIXER_LIBRARY} ${SDL2_MIXER_LIBRARY}
${SDL2_TTF_LIBRARY} ${SDL2_TTF_LIBRARY}
${X11_LIBRARIES}
) )
if (NOT WIN32)
target_link_libraries(breakhack
${X11_LIBRARIES}
)
endif (NOT WIN32)
# TESTS: # TESTS:
IF (CHECK_FOUND) IF (CHECK_FOUND)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)

View File

@ -9,7 +9,7 @@ actiontext_create()
t->texture = texture_create(); t->texture = texture_create();
t->timer = timer_create(); t->timer = timer_create();
t->active = false; t->active = false;
t->color = (SDL_Color) { 255, 255, 255 }; t->color = (SDL_Color) { 255, 255, 255, 255 };
return t; return t;
} }

View File

@ -15,7 +15,7 @@ typedef struct {
SDL_Color color; SDL_Color color;
} ActionText; } ActionText;
ActionText* actiontext_create(); ActionText* actiontext_create(void);
void actiontext_load_font(ActionText*, const char *path, unsigned int size); void actiontext_load_font(ActionText*, const char *path, unsigned int size);

View File

@ -11,7 +11,7 @@ Position camera_to_camera_position(Camera *cam, Position *pos)
void camera_follow_position(Camera *cam, Position *pos) void camera_follow_position(Camera *cam, Position *pos)
{ {
unsigned int room_width, room_height; int room_width, room_height;
room_width = MAP_ROOM_WIDTH * TILE_DIMENSION; room_width = MAP_ROOM_WIDTH * TILE_DIMENSION;
room_height = MAP_ROOM_HEIGHT * TILE_DIMENSION; room_height = MAP_ROOM_HEIGHT * TILE_DIMENSION;

6
src/config.h.in Normal file
View File

@ -0,0 +1,6 @@
#ifndef CONFIG_H_
#define CONFIG_H_
#cmakedefine _WIN32 ${WIN32}
#endif // CONFIG_H_

View File

@ -1,7 +1,7 @@
#ifndef DEFINES_H_ #ifndef DEFINES_H_
#define DEFINES_H_ #define DEFINES_H_
#define WINDOWS 0 #include "config.h"
/* Room/Map dimensions */ /* Room/Map dimensions */
#define MAP_ROOM_WIDTH 16 #define MAP_ROOM_WIDTH 16
@ -16,4 +16,11 @@
#define SCREEN_WIDTH MAP_ROOM_WIDTH * TILE_DIMENSION #define SCREEN_WIDTH MAP_ROOM_WIDTH * TILE_DIMENSION
#define SCREEN_HEIGHT MAP_ROOM_HEIGHT * TILE_DIMENSION #define SCREEN_HEIGHT MAP_ROOM_HEIGHT * TILE_DIMENSION
/* Windows and compile crap */
#ifdef _WIN32
#define strdup _strdup
#endif // _WIN32
#define UNUSED(x) (void)(x)
#endif // DEFINES_H_ #endif // DEFINES_H_

View File

@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include "hashtable.h" #include "hashtable.h"
#include "defines.h"
static void* static void*
ec_malloc(unsigned int size) ec_malloc(unsigned int size)
@ -41,7 +42,7 @@ static unsigned int
hash(Hashtable *table, const char *key) hash(Hashtable *table, const char *key)
{ {
unsigned long int hashval = 0; unsigned long int hashval = 0;
int i = 0; unsigned int i = 0;
while (hashval < ULONG_MAX && i < strlen(key)) { while (hashval < ULONG_MAX && i < strlen(key)) {
hashval = hashval << 8; hashval = hashval << 8;
@ -57,7 +58,7 @@ entry_create(const char *key, void *value)
Entry *entry; Entry *entry;
entry = ec_malloc(sizeof(Entry)); entry = ec_malloc(sizeof(Entry));
entry->key = _strdup(key); entry->key = strdup(key);
entry->value = value; entry->value = value;
entry->next = NULL; entry->next = NULL;

View File

@ -15,7 +15,7 @@ void *linkedlist_malloc(unsigned int size)
} }
static static
LinkedList* linkedlist_node_create() LinkedList* linkedlist_node_create(void)
{ {
LinkedList *newList = linkedlist_malloc(sizeof(LinkedList)); LinkedList *newList = linkedlist_malloc(sizeof(LinkedList));
newList->next = NULL; newList->next = NULL;

View File

@ -7,7 +7,7 @@ struct Node {
}; };
typedef struct Node LinkedList; typedef struct Node LinkedList;
LinkedList* linkedlist_create(); LinkedList* linkedlist_create(void);
void linkedlist_push(LinkedList **head, void *value); void linkedlist_push(LinkedList **head, void *value);

View File

@ -23,7 +23,7 @@ static RoomMatrix *gRoomMatrix = NULL;
static Camera gCamera; static Camera gCamera;
static static
bool initSDL() bool initSDL(void)
{ {
int imgFlags = IMG_INIT_PNG; int imgFlags = IMG_INIT_PNG;
Dimension dim = getScreenDimensions(); Dimension dim = getScreenDimensions();
@ -86,7 +86,7 @@ bool initSDL()
} }
static static
bool initGame() bool initGame(void)
{ {
gSpriteList = linkedlist_create(); gSpriteList = linkedlist_create();
gMap = map_lua_generator_run(gRenderer); gMap = map_lua_generator_run(gRenderer);
@ -94,7 +94,7 @@ bool initGame()
} }
static static
bool init() bool init(void)
{ {
bool result = true; bool result = true;
result = result && initSDL(); result = result && initSDL();
@ -109,13 +109,13 @@ bool init()
} }
static static
void loadMedia() void loadMedia(void)
{ {
gPlayer = player_create(ROGUE, gRenderer); gPlayer = player_create(ROGUE, gRenderer);
} }
static static
bool handle_events() bool handle_events(void)
{ {
static SDL_Event event; static SDL_Event event;
bool quit = false; bool quit = false;
@ -135,7 +135,7 @@ bool handle_events()
} }
static static
void run() void run(void)
{ {
bool quit = false; bool quit = false;
Timer* fpsTimer = timer_create(); Timer* fpsTimer = timer_create();
@ -175,7 +175,7 @@ void run()
} }
static static
void close() void close(void)
{ {
player_destroy(gPlayer); player_destroy(gPlayer);
map_destroy(gMap); map_destroy(gMap);
@ -186,7 +186,7 @@ void close()
SDL_Quit(); SDL_Quit();
} }
int main(int argc, char** argv) int main(void)
{ {
if (!init()) if (!init())
return 1; return 1;

View File

@ -3,7 +3,7 @@
#include "util.h" #include "util.h"
static static
Room* create_room() Room* create_room(void)
{ {
int i, j; int i, j;
Room *room; Room *room;

View File

@ -37,7 +37,7 @@ typedef struct Map_t {
int level; int level;
} Map; } Map;
Map* map_create(); Map* map_create(void);
int map_add_texture(Map*, const char *path, SDL_Renderer*); int map_add_texture(Map*, const char *path, SDL_Renderer*);

View File

@ -9,7 +9,7 @@
#include "util.h" #include "util.h"
static static
lua_State* load_lua_state() lua_State* load_lua_state(void)
{ {
lua_State *L = luaL_newstate(); lua_State *L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);

View File

@ -3,6 +3,6 @@
#include "map.h" #include "map.h"
Map* map_lua_generator_run(); Map* map_lua_generator_run(SDL_Renderer *renderer);
#endif // MAP_LUA_H_ #endif // MAP_LUA_H_

View File

@ -10,14 +10,14 @@ monster_load_texts(Monster *m, SDL_Renderer *renderer)
{ {
ActionText *t = actiontext_create(); ActionText *t = actiontext_create();
actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14); actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14);
t->color = (SDL_Color) { 255, 100, 0 }; t->color = (SDL_Color) { 255, 100, 0, 255 };
actiontext_set_text(t, "HIT", renderer); actiontext_set_text(t, "HIT", renderer);
t->pos = m->sprite->pos; t->pos = m->sprite->pos;
m->hitText = t; m->hitText = t;
t = actiontext_create(); t = actiontext_create();
actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14); actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14);
t->color = (SDL_Color) { 255, 255, 0 }; t->color = (SDL_Color) { 255, 255, 0, 255 };
actiontext_set_text(t, "MISS", renderer); actiontext_set_text(t, "MISS", renderer);
t->pos = m->sprite->pos; t->pos = m->sprite->pos;
m->missText = t; m->missText = t;

View File

@ -1,12 +1,15 @@
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "player.h" #include "player.h"
#include "monster.h" #include "monster.h"
#include "util.h"
#define ENGINEER_STATS { 11, 5, 7, 2, 1, 1 } #define ENGINEER_STATS { 11, 5, 7, 2, 1, 1 }
#define MAGE_STATS { 11, 5, 7, 2, 1, 1 } #define MAGE_STATS { 11, 5, 7, 2, 1, 1 }
#define PALADIN_STATS { 11, 5, 7, 2, 1, 1 } #define PALADIN_STATS { 11, 5, 7, 2, 1, 1 }
#define ROGUE_STATS { 11, 5, 7, 2, 2, 1 } #define ROGUE_STATS { 11, 5, 7, 2, 2, 1 }
#define WARRIOR_STATS { 11, 5, 7, 2, 1, 1 } #define WARRIOR_STATS { 11, 5, 7, 2, 1, 1 }
static bool static bool
@ -38,7 +41,7 @@ has_collided(Player *player, RoomMatrix *matrix)
} }
static void static void
player_step(Player *p, RoomMatrix* m) player_step(Player *p)
{ {
p->total_steps++; p->total_steps++;
p->steps++; p->steps++;
@ -54,7 +57,7 @@ move_left(Player *player, RoomMatrix *matrix)
if (has_collided(player, matrix)) { if (has_collided(player, matrix)) {
player->sprite->pos.x += TILE_DIMENSION; player->sprite->pos.x += TILE_DIMENSION;
} }
player_step(player, matrix); player_step(player);
} }
static static
@ -65,7 +68,7 @@ void move_right(Player *player, RoomMatrix *matrix)
if (has_collided(player, matrix)) { if (has_collided(player, matrix)) {
player->sprite->pos.x -= TILE_DIMENSION; player->sprite->pos.x -= TILE_DIMENSION;
} }
player_step(player, matrix); player_step(player);
} }
static static
@ -76,7 +79,7 @@ void move_up(Player *player, RoomMatrix *matrix)
if (has_collided(player, matrix)) { if (has_collided(player, matrix)) {
player->sprite->pos.y += TILE_DIMENSION; player->sprite->pos.y += TILE_DIMENSION;
} }
player_step(player, matrix); player_step(player);
} }
static static
@ -87,7 +90,7 @@ void move_down(Player *player, RoomMatrix *matrix)
if (has_collided(player, matrix)) { if (has_collided(player, matrix)) {
player->sprite->pos.y -= TILE_DIMENSION; player->sprite->pos.y -= TILE_DIMENSION;
} }
player_step(player, matrix); player_step(player);
} }
static static
@ -127,14 +130,14 @@ player_load_texts(Player *p, SDL_Renderer *renderer)
{ {
ActionText *t = actiontext_create(); ActionText *t = actiontext_create();
actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14); actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14);
t->color = (SDL_Color) { 255, 100, 0 }; t->color = (SDL_Color) { 255, 100, 0, 255 };
actiontext_set_text(t, "HIT", renderer); actiontext_set_text(t, "HIT", renderer);
t->pos = p->sprite->pos; t->pos = p->sprite->pos;
p->hitText = t; p->hitText = t;
t = actiontext_create(); t = actiontext_create();
actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14); actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14);
t->color = (SDL_Color) { 255, 255, 0 }; t->color = (SDL_Color) { 255, 255, 0, 255 };
actiontext_set_text(t, "MISS", renderer); actiontext_set_text(t, "MISS", renderer);
t->pos = p->sprite->pos; t->pos = p->sprite->pos;
p->missText = t; p->missText = t;
@ -152,23 +155,23 @@ player_create(class_t class, SDL_Renderer *renderer)
char asset[100]; char asset[100];
switch (class) { switch (class) {
case ENGINEER: case ENGINEER:
strcpy_s(asset, 100, "assets/Commissions/Engineer.png"); _strcpy(asset, 100, "assets/Commissions/Engineer.png");
player->stats = (Stats) ENGINEER_STATS; player->stats = (Stats) ENGINEER_STATS;
break; break;
case MAGE: case MAGE:
strcpy_s(asset, 100, "assets/Commissions/Mage.png"); _strcpy(asset, 100, "assets/Commissions/Mage.png");
player->stats = (Stats) MAGE_STATS; player->stats = (Stats) MAGE_STATS;
break; break;
case PALADIN: case PALADIN:
strcpy_s(asset, 100, "assets/Commissions/Paladin.png"); _strcpy(asset, 100, "assets/Commissions/Paladin.png");
player->stats = (Stats) PALADIN_STATS; player->stats = (Stats) PALADIN_STATS;
break; break;
case ROGUE: case ROGUE:
strcpy_s(asset, 100, "assets/Commissions/Rogue.png"); _strcpy(asset, 100, "assets/Commissions/Rogue.png");
player->stats = (Stats) ROGUE_STATS; player->stats = (Stats) ROGUE_STATS;
break; break;
case WARRIOR: case WARRIOR:
strcpy_s(asset, 100, "assets/Commissions/Warrior.png"); _strcpy(asset, 100, "assets/Commissions/Warrior.png");
player->stats = (Stats) WARRIOR_STATS; player->stats = (Stats) WARRIOR_STATS;
break; break;
} }

View File

@ -37,7 +37,7 @@ position_equals(const Position *p1, const Position *p2)
bool position_in_room(Position *pos, Position *roomPos) bool position_in_room(Position *pos, Position *roomPos)
{ {
unsigned int room_px_width, room_px_height, room_x_px, room_y_px; int room_px_width, room_px_height, room_x_px, room_y_px;
room_px_width = TILE_DIMENSION * MAP_ROOM_WIDTH; room_px_width = TILE_DIMENSION * MAP_ROOM_WIDTH;
room_px_height = TILE_DIMENSION * MAP_ROOM_HEIGHT; room_px_height = TILE_DIMENSION * MAP_ROOM_HEIGHT;

View File

@ -1,6 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <stdbool.h> #include <stdbool.h>
#include "random.h"
unsigned int unsigned int
get_random(unsigned int max) get_random(unsigned int max)

View File

@ -25,7 +25,7 @@ typedef struct {
Position playerRoomPos; Position playerRoomPos;
} RoomMatrix; } RoomMatrix;
RoomMatrix* roommatrix_create(); RoomMatrix* roommatrix_create(void);
void roommatrix_populate_from_map(RoomMatrix*, Map*); void roommatrix_populate_from_map(RoomMatrix*, Map*);

View File

@ -7,7 +7,7 @@
#include "screenresolution.h" #include "screenresolution.h"
Dimension getScreenDimensions() Dimension getScreenDimensions(void)
{ {
#ifndef WINDOWS #ifndef WINDOWS
Display *d = XOpenDisplay(NULL); Display *d = XOpenDisplay(NULL);

View File

@ -3,6 +3,6 @@
#include "dimension.h" #include "dimension.h"
Dimension getScreenDimensions(); Dimension getScreenDimensions(void);
#endif // SCREENRESOLUTION_H_ #endif // SCREENRESOLUTION_H_

View File

@ -2,7 +2,7 @@
#include "util.h" #include "util.h"
static static
Sprite* sprite_create_default() Sprite* sprite_create_default(void)
{ {
Position pos = { 0, 0 }; Position pos = { 0, 0 };

View File

@ -18,7 +18,7 @@ typedef struct Sprite_t {
unsigned int texture_index; unsigned int texture_index;
} Sprite; } Sprite;
Sprite* sprite_create(); Sprite* sprite_create(void);
void sprite_load_texture(Sprite *, char *path, int index, SDL_Renderer *); void sprite_load_texture(Sprite *, char *path, int index, SDL_Renderer *);

View File

@ -6,8 +6,8 @@ typedef struct Stats_t {
int dmg; /* Damage modifier */ int dmg; /* Damage modifier */
int atk; /* Attack rating */ int atk; /* Attack rating */
int def; /* Defence rating */ int def; /* Defence rating */
int speed; /* Speed */ unsigned int speed; /* Speed */
int lvl; /* Level */ unsigned int lvl; /* Level */
} Stats; } Stats;
unsigned int unsigned int

View File

@ -5,7 +5,7 @@
#include "util.h" #include "util.h"
Texture* Texture*
texture_create() texture_create(void)
{ {
Texture *t = ec_malloc(sizeof(Texture)); Texture *t = ec_malloc(sizeof(Texture));
t->dim.height = 0; t->dim.height = 0;

View File

@ -13,7 +13,7 @@ typedef struct {
Dimension dim; Dimension dim;
} Texture; } Texture;
Texture* texture_create(); Texture* texture_create(void);
void texture_load_from_file(Texture*, const char *path, SDL_Renderer*); void texture_load_from_file(Texture*, const char *path, SDL_Renderer*);

View File

@ -7,7 +7,7 @@ typedef struct {
unsigned int startTime; unsigned int startTime;
} Timer; } Timer;
Timer* timer_create(); Timer* timer_create(void);
void timer_start(Timer*); void timer_start(Timer*);
void timer_stop(Timer*); void timer_stop(Timer*);
bool timer_started(Timer*); bool timer_started(Timer*);

View File

@ -1,19 +1,45 @@
#include "defines.h" #include "defines.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifndef WINDOWS #ifndef _WIN32
#include <unistd.h> #include <unistd.h>
#endif #endif // _WIN32
#include <string.h> #include <string.h>
#include "util.h" #include "util.h"
void
_strcpy(char *restrict dest, size_t destsz, char *restrict src)
{
#ifndef _WIN32
UNUSED(destsz);
strcpy(dest, src);
#else
strcpy_s(dest, destsz, src);
#endif // _WIN32
}
void
_strncat(char *restrict dest,
size_t destsz,
char *restrict src,
size_t srcsz)
{
#ifndef _WIN32
UNUSED(destsz);
UNUSED(srcsz);
strncat(dest, src, srcsz);
#else
strncat_s(dest, destsz, src, srcsz);
#endif // _WIN32
}
void fatal(char *message) void fatal(char *message)
{ {
char error_message[100]; char error_message[100];
strcpy_s(error_message, 100, "[!!] Fatal Error "); _strcpy(error_message, 100, "[!!] Fatal Error ");
strncat_s(error_message, 100, message, 83); _strncat(error_message, 100, message, 83);
perror(error_message); perror(error_message);
exit(-1); exit(-1);
} }

View File

@ -5,4 +5,12 @@ void fatal(char *message);
void *ec_malloc(unsigned int size); void *ec_malloc(unsigned int size);
void _strcpy(char *restrict dest, size_t destsz, char *restrict src);
void
_strncat(char *restrict dest,
size_t destsz,
char *restrict src,
size_t srcsz);
#endif // UTIL_H_ #endif // UTIL_H_

View File

@ -58,7 +58,7 @@ START_TEST(test_hashtable_set_get)
} }
END_TEST END_TEST
Suite* t_suite_create() static Suite* t_suite_create(void)
{ {
Suite *s; Suite *s;
TCase *tc_core; TCase *tc_core;

View File

@ -207,7 +207,8 @@ START_TEST (test_linkedlist_each)
} }
END_TEST END_TEST
Suite* t_suite_create() static Suite*
t_suite_create(void)
{ {
Suite *s; Suite *s;
TCase *tc_core; TCase *tc_core;

View File

@ -10,7 +10,7 @@ START_TEST(test_util_ec_malloc)
} }
END_TEST END_TEST
Suite* t_suite_create() static Suite* t_suite_create(void)
{ {
Suite *s; Suite *s;
TCase *tc_core; TCase *tc_core;