From faad866c8ba129c5477d0cd554a601eef7e3d052 Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Tue, 19 Dec 2017 21:00:02 +0100 Subject: [PATCH] Fixed a lot of stuff to help cross platform compilation --- .vimrc | 3 ++- CMakeLists.txt | 26 +++++++++++++++++++++----- src/actiontext.c | 2 +- src/actiontext.h | 2 +- src/camera.c | 2 +- src/config.h.in | 6 ++++++ src/defines.h | 9 ++++++++- src/hashtable.c | 5 +++-- src/linkedlist.c | 2 +- src/linkedlist.h | 2 +- src/main.c | 16 ++++++++-------- src/map.c | 2 +- src/map.h | 2 +- src/map_lua.c | 2 +- src/map_lua.h | 2 +- src/monster.c | 4 ++-- src/player.c | 33 ++++++++++++++++++--------------- src/position.c | 2 +- src/random.c | 1 + src/roommatrix.h | 2 +- src/screenresolution.c | 2 +- src/screenresolution.h | 2 +- src/sprite.c | 2 +- src/sprite.h | 2 +- src/stats.h | 4 ++-- src/texture.c | 2 +- src/texture.h | 2 +- src/timer.h | 2 +- src/util.c | 34 ++++++++++++++++++++++++++++++---- src/util.h | 8 ++++++++ test/check_hashtable.c | 2 +- test/check_linkedlist.c | 3 ++- test/check_util.c | 2 +- 33 files changed, 131 insertions(+), 61 deletions(-) create mode 100644 src/config.h.in diff --git a/.vimrc b/.vimrc index 1c60932..e73b90c 100644 --- a/.vimrc +++ b/.vimrc @@ -1,6 +1,7 @@ nnoremap :Make +nnoremap :Make clean au FileType c 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' ] diff --git a/CMakeLists.txt b/CMakeLists.txt index 57eb9b3..93b43a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,27 +5,38 @@ SET(CMAKE_COLOR_MAKEFILE ON) project(breakhack C) include(FindLua) -include(FindX11) include(cmake/FindSDL2.cmake) include(cmake/FindSDL2_image.cmake) include(cmake/FindSDL2_mixer.cmake) include(cmake/FindSDL2_ttf.cmake) +configure_file( + "${PROJECT_SOURCE_DIR}/src/config.h.in" + "${PROJECT_BINARY_DIR}/config.h" + ) + if (NOT WIN32) + include(FindX11) include(cmake/FindCheck.cmake) endif (NOT WIN32) include_directories( + ${PROJECT_BINARY_DIR} ${LUA_INCLUDE_DIR} ${SDL2_INCLUDE_DIR} ${SDL2_IMAGE_INCLUDE_DIR} ${SDL2_MIXER_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: add_executable(breakhack @@ -55,9 +66,14 @@ target_link_libraries(breakhack ${SDL2_IMAGE_LIBRARY} ${SDL2_MIXER_LIBRARY} ${SDL2_TTF_LIBRARY} - ${X11_LIBRARIES} ) +if (NOT WIN32) + target_link_libraries(breakhack + ${X11_LIBRARIES} + ) +endif (NOT WIN32) + # TESTS: IF (CHECK_FOUND) find_package(Threads REQUIRED) diff --git a/src/actiontext.c b/src/actiontext.c index e5210f4..0377ef3 100644 --- a/src/actiontext.c +++ b/src/actiontext.c @@ -9,7 +9,7 @@ actiontext_create() t->texture = texture_create(); t->timer = timer_create(); t->active = false; - t->color = (SDL_Color) { 255, 255, 255 }; + t->color = (SDL_Color) { 255, 255, 255, 255 }; return t; } diff --git a/src/actiontext.h b/src/actiontext.h index 7030642..2bfc9f6 100644 --- a/src/actiontext.h +++ b/src/actiontext.h @@ -15,7 +15,7 @@ typedef struct { SDL_Color color; } ActionText; -ActionText* actiontext_create(); +ActionText* actiontext_create(void); void actiontext_load_font(ActionText*, const char *path, unsigned int size); diff --git a/src/camera.c b/src/camera.c index bf5a432..93a7c21 100644 --- a/src/camera.c +++ b/src/camera.c @@ -11,7 +11,7 @@ Position camera_to_camera_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_height = MAP_ROOM_HEIGHT * TILE_DIMENSION; diff --git a/src/config.h.in b/src/config.h.in new file mode 100644 index 0000000..fd84242 --- /dev/null +++ b/src/config.h.in @@ -0,0 +1,6 @@ +#ifndef CONFIG_H_ +#define CONFIG_H_ + +#cmakedefine _WIN32 ${WIN32} + +#endif // CONFIG_H_ diff --git a/src/defines.h b/src/defines.h index a9c760a..b5f00f2 100644 --- a/src/defines.h +++ b/src/defines.h @@ -1,7 +1,7 @@ #ifndef DEFINES_H_ #define DEFINES_H_ -#define WINDOWS 0 +#include "config.h" /* Room/Map dimensions */ #define MAP_ROOM_WIDTH 16 @@ -16,4 +16,11 @@ #define SCREEN_WIDTH MAP_ROOM_WIDTH * 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_ diff --git a/src/hashtable.c b/src/hashtable.c index 1ca7524..9171dd0 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -3,6 +3,7 @@ #include #include #include "hashtable.h" +#include "defines.h" static void* ec_malloc(unsigned int size) @@ -41,7 +42,7 @@ static unsigned int hash(Hashtable *table, const char *key) { unsigned long int hashval = 0; - int i = 0; + unsigned int i = 0; while (hashval < ULONG_MAX && i < strlen(key)) { hashval = hashval << 8; @@ -57,7 +58,7 @@ entry_create(const char *key, void *value) Entry *entry; entry = ec_malloc(sizeof(Entry)); - entry->key = _strdup(key); + entry->key = strdup(key); entry->value = value; entry->next = NULL; diff --git a/src/linkedlist.c b/src/linkedlist.c index 9e07039..4084ba2 100644 --- a/src/linkedlist.c +++ b/src/linkedlist.c @@ -15,7 +15,7 @@ void *linkedlist_malloc(unsigned int size) } static -LinkedList* linkedlist_node_create() +LinkedList* linkedlist_node_create(void) { LinkedList *newList = linkedlist_malloc(sizeof(LinkedList)); newList->next = NULL; diff --git a/src/linkedlist.h b/src/linkedlist.h index 70bedfe..418cf1c 100644 --- a/src/linkedlist.h +++ b/src/linkedlist.h @@ -7,7 +7,7 @@ struct Node { }; typedef struct Node LinkedList; -LinkedList* linkedlist_create(); +LinkedList* linkedlist_create(void); void linkedlist_push(LinkedList **head, void *value); diff --git a/src/main.c b/src/main.c index 98d8f3d..98ff8af 100644 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,7 @@ static RoomMatrix *gRoomMatrix = NULL; static Camera gCamera; static -bool initSDL() +bool initSDL(void) { int imgFlags = IMG_INIT_PNG; Dimension dim = getScreenDimensions(); @@ -86,7 +86,7 @@ bool initSDL() } static -bool initGame() +bool initGame(void) { gSpriteList = linkedlist_create(); gMap = map_lua_generator_run(gRenderer); @@ -94,7 +94,7 @@ bool initGame() } static -bool init() +bool init(void) { bool result = true; result = result && initSDL(); @@ -109,13 +109,13 @@ bool init() } static -void loadMedia() +void loadMedia(void) { gPlayer = player_create(ROGUE, gRenderer); } static -bool handle_events() +bool handle_events(void) { static SDL_Event event; bool quit = false; @@ -135,7 +135,7 @@ bool handle_events() } static -void run() +void run(void) { bool quit = false; Timer* fpsTimer = timer_create(); @@ -175,7 +175,7 @@ void run() } static -void close() +void close(void) { player_destroy(gPlayer); map_destroy(gMap); @@ -186,7 +186,7 @@ void close() SDL_Quit(); } -int main(int argc, char** argv) +int main(void) { if (!init()) return 1; diff --git a/src/map.c b/src/map.c index f677544..4c8ba4b 100644 --- a/src/map.c +++ b/src/map.c @@ -3,7 +3,7 @@ #include "util.h" static -Room* create_room() +Room* create_room(void) { int i, j; Room *room; diff --git a/src/map.h b/src/map.h index c8fd582..3763297 100644 --- a/src/map.h +++ b/src/map.h @@ -37,7 +37,7 @@ typedef struct Map_t { int level; } Map; -Map* map_create(); +Map* map_create(void); int map_add_texture(Map*, const char *path, SDL_Renderer*); diff --git a/src/map_lua.c b/src/map_lua.c index 38df7cb..d24d6e0 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -9,7 +9,7 @@ #include "util.h" static -lua_State* load_lua_state() +lua_State* load_lua_state(void) { lua_State *L = luaL_newstate(); luaL_openlibs(L); diff --git a/src/map_lua.h b/src/map_lua.h index 534a4b4..a8e7096 100644 --- a/src/map_lua.h +++ b/src/map_lua.h @@ -3,6 +3,6 @@ #include "map.h" -Map* map_lua_generator_run(); +Map* map_lua_generator_run(SDL_Renderer *renderer); #endif // MAP_LUA_H_ diff --git a/src/monster.c b/src/monster.c index 27547f0..b8e6873 100644 --- a/src/monster.c +++ b/src/monster.c @@ -10,14 +10,14 @@ monster_load_texts(Monster *m, SDL_Renderer *renderer) { ActionText *t = actiontext_create(); 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); t->pos = m->sprite->pos; m->hitText = t; t = actiontext_create(); 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); t->pos = m->sprite->pos; m->missText = t; diff --git a/src/player.c b/src/player.c index 8a337bb..4426596 100644 --- a/src/player.c +++ b/src/player.c @@ -1,12 +1,15 @@ -#include #include +#include +#include + #include "player.h" #include "monster.h" +#include "util.h" #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 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 } static bool @@ -38,7 +41,7 @@ has_collided(Player *player, RoomMatrix *matrix) } static void -player_step(Player *p, RoomMatrix* m) +player_step(Player *p) { p->total_steps++; p->steps++; @@ -54,7 +57,7 @@ move_left(Player *player, RoomMatrix *matrix) if (has_collided(player, matrix)) { player->sprite->pos.x += TILE_DIMENSION; } - player_step(player, matrix); + player_step(player); } static @@ -65,7 +68,7 @@ void move_right(Player *player, RoomMatrix *matrix) if (has_collided(player, matrix)) { player->sprite->pos.x -= TILE_DIMENSION; } - player_step(player, matrix); + player_step(player); } static @@ -76,7 +79,7 @@ void move_up(Player *player, RoomMatrix *matrix) if (has_collided(player, matrix)) { player->sprite->pos.y += TILE_DIMENSION; } - player_step(player, matrix); + player_step(player); } static @@ -87,7 +90,7 @@ void move_down(Player *player, RoomMatrix *matrix) if (has_collided(player, matrix)) { player->sprite->pos.y -= TILE_DIMENSION; } - player_step(player, matrix); + player_step(player); } static @@ -127,14 +130,14 @@ player_load_texts(Player *p, SDL_Renderer *renderer) { ActionText *t = actiontext_create(); 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); t->pos = p->sprite->pos; p->hitText = t; t = actiontext_create(); 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); t->pos = p->sprite->pos; p->missText = t; @@ -152,23 +155,23 @@ player_create(class_t class, SDL_Renderer *renderer) char asset[100]; switch (class) { case ENGINEER: - strcpy_s(asset, 100, "assets/Commissions/Engineer.png"); + _strcpy(asset, 100, "assets/Commissions/Engineer.png"); player->stats = (Stats) ENGINEER_STATS; break; case MAGE: - strcpy_s(asset, 100, "assets/Commissions/Mage.png"); + _strcpy(asset, 100, "assets/Commissions/Mage.png"); player->stats = (Stats) MAGE_STATS; break; case PALADIN: - strcpy_s(asset, 100, "assets/Commissions/Paladin.png"); + _strcpy(asset, 100, "assets/Commissions/Paladin.png"); player->stats = (Stats) PALADIN_STATS; break; case ROGUE: - strcpy_s(asset, 100, "assets/Commissions/Rogue.png"); + _strcpy(asset, 100, "assets/Commissions/Rogue.png"); player->stats = (Stats) ROGUE_STATS; break; case WARRIOR: - strcpy_s(asset, 100, "assets/Commissions/Warrior.png"); + _strcpy(asset, 100, "assets/Commissions/Warrior.png"); player->stats = (Stats) WARRIOR_STATS; break; } diff --git a/src/position.c b/src/position.c index 554ba8c..a3a3822 100644 --- a/src/position.c +++ b/src/position.c @@ -37,7 +37,7 @@ position_equals(const Position *p1, const Position *p2) 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_height = TILE_DIMENSION * MAP_ROOM_HEIGHT; diff --git a/src/random.c b/src/random.c index e1b9544..af0bff9 100644 --- a/src/random.c +++ b/src/random.c @@ -1,6 +1,7 @@ #include #include #include +#include "random.h" unsigned int get_random(unsigned int max) diff --git a/src/roommatrix.h b/src/roommatrix.h index 03db0bc..62d5458 100644 --- a/src/roommatrix.h +++ b/src/roommatrix.h @@ -25,7 +25,7 @@ typedef struct { Position playerRoomPos; } RoomMatrix; -RoomMatrix* roommatrix_create(); +RoomMatrix* roommatrix_create(void); void roommatrix_populate_from_map(RoomMatrix*, Map*); diff --git a/src/screenresolution.c b/src/screenresolution.c index 1f222b3..84af914 100644 --- a/src/screenresolution.c +++ b/src/screenresolution.c @@ -7,7 +7,7 @@ #include "screenresolution.h" -Dimension getScreenDimensions() +Dimension getScreenDimensions(void) { #ifndef WINDOWS Display *d = XOpenDisplay(NULL); diff --git a/src/screenresolution.h b/src/screenresolution.h index 151c9a4..fe7a2b0 100644 --- a/src/screenresolution.h +++ b/src/screenresolution.h @@ -3,6 +3,6 @@ #include "dimension.h" -Dimension getScreenDimensions(); +Dimension getScreenDimensions(void); #endif // SCREENRESOLUTION_H_ diff --git a/src/sprite.c b/src/sprite.c index 34b57d7..be6bf93 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -2,7 +2,7 @@ #include "util.h" static -Sprite* sprite_create_default() +Sprite* sprite_create_default(void) { Position pos = { 0, 0 }; diff --git a/src/sprite.h b/src/sprite.h index aa2d598..0ad9ee3 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -18,7 +18,7 @@ typedef struct Sprite_t { unsigned int texture_index; } Sprite; -Sprite* sprite_create(); +Sprite* sprite_create(void); void sprite_load_texture(Sprite *, char *path, int index, SDL_Renderer *); diff --git a/src/stats.h b/src/stats.h index 35d89ba..143920d 100644 --- a/src/stats.h +++ b/src/stats.h @@ -6,8 +6,8 @@ typedef struct Stats_t { int dmg; /* Damage modifier */ int atk; /* Attack rating */ int def; /* Defence rating */ - int speed; /* Speed */ - int lvl; /* Level */ + unsigned int speed; /* Speed */ + unsigned int lvl; /* Level */ } Stats; unsigned int diff --git a/src/texture.c b/src/texture.c index 51ca2fa..e2db244 100644 --- a/src/texture.c +++ b/src/texture.c @@ -5,7 +5,7 @@ #include "util.h" Texture* -texture_create() +texture_create(void) { Texture *t = ec_malloc(sizeof(Texture)); t->dim.height = 0; diff --git a/src/texture.h b/src/texture.h index a6983e6..8a59405 100644 --- a/src/texture.h +++ b/src/texture.h @@ -13,7 +13,7 @@ typedef struct { Dimension dim; } Texture; -Texture* texture_create(); +Texture* texture_create(void); void texture_load_from_file(Texture*, const char *path, SDL_Renderer*); diff --git a/src/timer.h b/src/timer.h index 0007611..c37f4b4 100644 --- a/src/timer.h +++ b/src/timer.h @@ -7,7 +7,7 @@ typedef struct { unsigned int startTime; } Timer; -Timer* timer_create(); +Timer* timer_create(void); void timer_start(Timer*); void timer_stop(Timer*); bool timer_started(Timer*); diff --git a/src/util.c b/src/util.c index 4d186b2..ac77f77 100644 --- a/src/util.c +++ b/src/util.c @@ -1,19 +1,45 @@ #include "defines.h" #include #include -#ifndef WINDOWS +#ifndef _WIN32 #include -#endif +#endif // _WIN32 #include #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) { char error_message[100]; - strcpy_s(error_message, 100, "[!!] Fatal Error "); - strncat_s(error_message, 100, message, 83); + _strcpy(error_message, 100, "[!!] Fatal Error "); + _strncat(error_message, 100, message, 83); perror(error_message); exit(-1); } diff --git a/src/util.h b/src/util.h index e21fcff..1c7ba30 100644 --- a/src/util.h +++ b/src/util.h @@ -5,4 +5,12 @@ void fatal(char *message); 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_ diff --git a/test/check_hashtable.c b/test/check_hashtable.c index df01e71..af4c87c 100644 --- a/test/check_hashtable.c +++ b/test/check_hashtable.c @@ -58,7 +58,7 @@ START_TEST(test_hashtable_set_get) } END_TEST -Suite* t_suite_create() +static Suite* t_suite_create(void) { Suite *s; TCase *tc_core; diff --git a/test/check_linkedlist.c b/test/check_linkedlist.c index 7419efb..a914126 100644 --- a/test/check_linkedlist.c +++ b/test/check_linkedlist.c @@ -207,7 +207,8 @@ START_TEST (test_linkedlist_each) } END_TEST -Suite* t_suite_create() +static Suite* +t_suite_create(void) { Suite *s; TCase *tc_core; diff --git a/test/check_util.c b/test/check_util.c index 0cebcac..543532b 100644 --- a/test/check_util.c +++ b/test/check_util.c @@ -10,7 +10,7 @@ START_TEST(test_util_ec_malloc) } END_TEST -Suite* t_suite_create() +static Suite* t_suite_create(void) { Suite *s; TCase *tc_core;