Browse Source

Organize CMake, add safe fopen

Tidy up CMake.

Also provide abstraction to fopen_s only available to Windows.
master
TheoryOfNekomata 1 year ago
parent
commit
8d67fa3b08
7 changed files with 158 additions and 87 deletions
  1. +114
    -85
      CMakeLists.txt
  2. +4
    -1
      src/packages/game/output/video/IZ_video.c
  3. +1
    -0
      src/packages/game/output/video/IZ_video.h
  4. +26
    -0
      src/packages/io/IZ_io.c
  5. +11
    -0
      src/packages/io/IZ_io.h
  6. +1
    -1
      src/packages/net/svc/IZ_wsserver.c
  7. +1
    -0
      src/packages/net/svc/IZ_wsserver.h

+ 114
- 85
CMakeLists.txt View File

@@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.21)

# Set your project name here
project(izanagi C)

set(CMAKE_C_STANDARD 11)

#
# Set platform
#
if (WIN32)
# TODO set arch on target instead of generator
set(PLATFORM WINDOWS)
@@ -31,6 +31,9 @@ else()
)
endif ()

#
# Add configurations
#
add_definitions(
-DIZ_APP_NAME="Izanagi"
-DIZ_APP_DESCRIPTION="Run and gun game"
@@ -42,6 +45,9 @@ if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
add_definitions(-DIZ_DEBUG)
endif()

#
# Specify directories
#
include_directories(
"${PROJECT_SOURCE_DIR}/dependencies/SDL2/include"
"${PROJECT_SOURCE_DIR}/dependencies/SDL2_image"
@@ -78,15 +84,18 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
)
endif()

#
# Define executables
#
add_executable(
game
dependencies/minIni/dev/minIni.h
dependencies/minIni/dev/minIni.c
dependencies/getopt-for-windows/getopt.h
dependencies/getopt-for-windows/getopt.c
src/packages/game/output/video/IZ_video.h
src/packages/game/output/video/IZ_video.c
src/packages/common/IZ_common.h
src/packages/game/output/video/IZ_video.h
src/packages/game/output/video/IZ_video.c
src/packages/common/IZ_common.h
src/packages/game/input/IZ_action.h
src/packages/game/IZ_app.h
src/packages/game/IZ_app.c
@@ -95,9 +104,9 @@ add_executable(
src/packages/game/input/IZ_joystick.h
src/packages/game/input/IZ_keyboard.c
src/packages/game/input/IZ_keyboard.h
src/packages/config/IZ_config.c
src/packages/config/IZ_config.h
src/packages/game/geometry/IZ_vector2d.c
src/packages/config/IZ_config.c
src/packages/config/IZ_config.h
src/packages/game/geometry/IZ_vector2d.c
src/packages/game/geometry/IZ_vector2d.h
src/packages/game/geometry/IZ_rect.c
src/packages/game/geometry/IZ_rect.h
@@ -115,15 +124,32 @@ add_executable(
src/packages/game/input/IZ_midi.h
src/packages/game/data/IZ_list.c
src/packages/game/data/IZ_list.h
src/packages/net/svc/IZ_wsclient.c
src/packages/net/svc/IZ_wsclient.h
src/packages/log/IZ_log.c
src/packages/log/IZ_log.h
src/packages/midi/IZ_midi.c
src/packages/midi/IZ_midi.h
src/packages/net/core/IZ_websocket.h
src/packages/net/core/IZ_websocket.c
src/packages/net/IZ_net_client.c src/packages/net/IZ_net_client.h src/packages/game/IZ_app_net.c src/packages/game/IZ_app_net.h src/packages/game/IZ_app_video.c src/packages/game/IZ_app_video.h src/packages/game/IZ_subsystem.h src/packages/game/IZ_app_input.c src/packages/game/IZ_app_input.h src/packages/game/IZ_app_config.c src/packages/game/IZ_app_config.h src/packages/game/asset/IZ_asset.c src/packages/game/asset/IZ_asset.h src/packages/string/IZ_string.c src/packages/string/IZ_string.h)
src/packages/net/svc/IZ_wsclient.c
src/packages/net/svc/IZ_wsclient.h
src/packages/log/IZ_log.c
src/packages/log/IZ_log.h
src/packages/midi/IZ_midi.c
src/packages/midi/IZ_midi.h
src/packages/net/core/IZ_websocket.h
src/packages/net/core/IZ_websocket.c
src/packages/net/IZ_net_client.c
src/packages/net/IZ_net_client.h
src/packages/game/IZ_app_net.c
src/packages/game/IZ_app_net.h
src/packages/game/IZ_app_video.c
src/packages/game/IZ_app_video.h
src/packages/game/IZ_subsystem.h
src/packages/game/IZ_app_input.c
src/packages/game/IZ_app_input.h
src/packages/game/IZ_app_config.c
src/packages/game/IZ_app_config.h
src/packages/game/asset/IZ_asset.c
src/packages/game/asset/IZ_asset.h
src/packages/string/IZ_string.c
src/packages/string/IZ_string.h
src/packages/io/IZ_io.c
src/packages/io/IZ_io.h
)

if (WIN32)
target_link_libraries(
@@ -140,14 +166,15 @@ if (WIN32)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_link_libraries(
game
SDL2main
SDL2
SDL2_image
SDL2_ttf
portmidi
# libcrypto
# libssl
websockets
SDL2main
SDL2
SDL2_image
SDL2_ttf
portmidi
# FIXME
# libcrypto
# libssl
websockets
)
endif()

@@ -157,7 +184,7 @@ add_executable(
src/packages/test/IZ_mock.h
src/packages/test/IZ_test.h

src/packages/game/geometry/IZ_rect.h
src/packages/game/geometry/IZ_rect.h
src/packages/game/geometry/IZ_rect.c
src/packages/game/geometry/IZ_vector2d.h
src/packages/game/geometry/IZ_vector2d.c
@@ -177,7 +204,7 @@ add_executable(
__mocks__/SDL_stdinc.mock.h
__mocks__/portmidi.mock.h

src/packages/config/IZ_config.h
src/packages/config/IZ_config.h

src/packages/game/input/IZ_keyboard.h
src/packages/game/input/IZ_keyboard.c
@@ -188,8 +215,8 @@ add_executable(
src/packages/game/input/IZ_midi.h
src/packages/game/input/IZ_midi.c

src/packages/midi/IZ_midi.c
src/packages/midi/IZ_midi.h
src/packages/midi/IZ_midi.c
src/packages/midi/IZ_midi.h

src/packages/game/input/input.test.c
)
@@ -199,15 +226,12 @@ add_executable(
dependencies/bdd-for-c/bdd-for-c.h
src/packages/test/IZ_mock.h
src/packages/test/IZ_test.h

__mocks__/minIni.mock.h
__mocks__/SDL_stdinc.mock.h
__mocks__/SDL_render.mock.h

src/packages/config/IZ_config.h

src/packages/game/output/video/IZ_video.h
src/packages/game/output/video/IZ_video.c
src/packages/config/IZ_config.h
src/packages/game/output/video/IZ_video.h
src/packages/game/output/video/IZ_video.c
src/packages/game/output/output.test.c
)

@@ -216,7 +240,6 @@ add_executable(
dependencies/bdd-for-c/bdd-for-c.h
src/packages/test/IZ_mock.h
src/packages/test/IZ_test.h

src/packages/game/memory/IZ_pool.h
src/packages/game/memory/IZ_pool.c
src/packages/game/memory/memory.test.c
@@ -229,7 +252,6 @@ add_executable(
dependencies/bdd-for-c/bdd-for-c.h
src/packages/test/IZ_mock.h
src/packages/test/IZ_test.h

src/packages/game/data/IZ_list.h
src/packages/game/data/IZ_list.c
src/packages/game/data/data.test.c
@@ -259,14 +281,10 @@ add_executable(
src/packages/server/db/IZ_repo.c
src/packages/server/db/IZ_repo.h
src/packages/server/IZ_subsystem.h
src/packages/string/IZ_string.c src/packages/string/IZ_string.h)

add_executable(
asset-inv
dependencies/minIni/dev/minIni.h
dependencies/minIni/dev/minIni.c
src/packages/common/IZ_common.h
src/packages/asset-inv/main.c
src/packages/string/IZ_string.c
src/packages/string/IZ_string.h
src/packages/io/IZ_io.c
src/packages/io/IZ_io.h
)

target_link_libraries(
@@ -278,69 +296,80 @@ target_link_libraries(
websockets
)

add_executable(
asset-inv
dependencies/minIni/dev/minIni.h
dependencies/minIni/dev/minIni.c
src/packages/common/IZ_common.h
src/packages/asset-inv/main.c
)

#
# Add DLLs and required assets to the build directory
#
if (WIN32)
add_custom_command(TARGET game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll" # <--this is in-file
$<TARGET_FILE_DIR:game>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll"
$<TARGET_FILE_DIR:game>)

add_custom_command(TARGET game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/SDL2_image/lib/${PROJECT_ARCH}/SDL2_image.dll" # <--this is in-file
$<TARGET_FILE_DIR:game>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/SDL2_image/lib/${PROJECT_ARCH}/SDL2_image.dll"
$<TARGET_FILE_DIR:game>)

add_custom_command(TARGET game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/SDL2_ttf/lib/${PROJECT_ARCH}/SDL2_ttf.dll" # <--this is in-file
$<TARGET_FILE_DIR:game>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/SDL2_ttf/lib/${PROJECT_ARCH}/SDL2_ttf.dll"
$<TARGET_FILE_DIR:game>)

add_custom_command(TARGET game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/portmidi/Release/portmidi.dll" # <--this is in-file
$<TARGET_FILE_DIR:game>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/portmidi/Release/portmidi.dll"
$<TARGET_FILE_DIR:game>)

add_custom_command(TARGET game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll" # <--this is in-file
$<TARGET_FILE_DIR:game>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll"
$<TARGET_FILE_DIR:game>)

add_custom_command(TARGET game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll" # <--this is in-file
$<TARGET_FILE_DIR:game>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll"
$<TARGET_FILE_DIR:game>)

add_custom_command(TARGET game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll" # <--this is in-file
$<TARGET_FILE_DIR:game>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll"
$<TARGET_FILE_DIR:game>)

add_custom_command(TARGET server POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll" # <--this is in-file
$<TARGET_FILE_DIR:server>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll"
$<TARGET_FILE_DIR:server>)

add_custom_command(TARGET server POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll" # <--this is in-file
$<TARGET_FILE_DIR:server>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll"
$<TARGET_FILE_DIR:server>)

add_custom_command(TARGET server POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll" # <--this is in-file
$<TARGET_FILE_DIR:server>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll"
$<TARGET_FILE_DIR:server>)

add_custom_command(TARGET server POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll" # <--this is in-file
$<TARGET_FILE_DIR:server>) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll"
$<TARGET_FILE_DIR:server>)
endif ()

add_custom_command(TARGET game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/assets" # <--this is in-file
$<TARGET_FILE_DIR:game>/assets) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_SOURCE_DIR}/assets"
$<TARGET_FILE_DIR:game>/assets)

add_custom_command(TARGET asset-inv POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/assets" # <--this is in-file
$<TARGET_FILE_DIR:game>/assets) # <--this is out-file path
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_SOURCE_DIR}/assets"
$<TARGET_FILE_DIR:game>/assets)

+ 4
- 1
src/packages/game/output/video/IZ_video.c View File

@@ -122,7 +122,10 @@ u16 IZ_VideoGetNextFreeSpriteSlot(IZ_VideoState* state, IZ_VideoSpritePriority p
void IZ_VideoLoadSprite(IZ_VideoState* state, IZ_VideoLoadSpriteParams params, IZ_SpriteSlot* out) {
char full_path[2048];
sprintf(full_path, "%s/%s", params.dir, params.filename);
FILE* f = fopen(full_path, "r");
FILE* f;
if (IZ_fopen(&f, full_path, "r")) {
return;
}
u32 sprite_length_bytes = ini_getl(params.dir, params.filename, 0, "assets.ini");
u8* sprite = malloc(sprite_length_bytes + 1);
fread(sprite, 1, sprite_length_bytes, f);


+ 1
- 0
src/packages/game/output/video/IZ_video.h View File

@@ -9,6 +9,7 @@
#include "../../../net/IZ_net_client.h"
#include "../../../config/IZ_config.h"
#include "../../../common/IZ_common.h"
#include "../../../io/IZ_io.h"
#include "../../../string/IZ_string.h"
#include "../../geometry/IZ_vector2d.h"
#include "../../input/IZ_input.h"


+ 26
- 0
src/packages/io/IZ_io.c View File

@@ -0,0 +1,26 @@
#include "IZ_io.h"

int IZ_sprintf(char* buffer, size_t buffer_size, const char* format, ...) {
#if defined IZ_WIN64
va_list args;
va_start(args, format);
int result = sprintf_s(buffer, buffer_size, format, args);
va_end(args);
return result;
#else
va_list args;
va_start(args, format);
int result = sprintf(buffer, format, args);
va_end(args);
return result;
#endif
}

errno_t IZ_fopen(struct _iobuf** file, const char* filename, const char* mode) {
#if defined IZ_WIN64
return fopen_s(file, filename, mode);
#else
*file = fopen(filename, mode);
return *file == NULL ? 1 : 0;
#endif
}

+ 11
- 0
src/packages/io/IZ_io.h View File

@@ -0,0 +1,11 @@
#ifndef IZ_IO_H
#define IZ_IO_H

#include <stdio.h>
#include <stdarg.h>

int IZ_sprintf(char*, size_t, const char*, ...);

errno_t IZ_fopen(struct _iobuf**, const char*, const char*);

#endif

+ 1
- 1
src/packages/net/svc/IZ_wsserver.c View File

@@ -32,7 +32,7 @@ IZ_ProcedureResult IZ_WSServerCallback(

const char* IZ_WSServerTestPath(const char* base_dir, const char* file) {
static char test_path[32];
sprintf_s(test_path, 32, "%s/%s", base_dir, file);
IZ_sprintf(test_path, 32, "%s/%s", base_dir, file);
struct stat stats;
stat(test_path, &stats);
if (stats.st_mode & S_IREAD) {


+ 1
- 0
src/packages/net/svc/IZ_wsserver.h View File

@@ -4,6 +4,7 @@
#include <sys/stat.h>
#include <string.h>
#include "../../common/IZ_common.h"
#include "../../io/IZ_io.h"
#include "../core/IZ_websocket.h"

#ifndef S_ISDIR


Loading…
Cancel
Save