Skip to content

Commit

Permalink
platform fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
EmperorPenguin18 committed May 15, 2024
1 parent 4c8c1da commit f67a0fa
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 117 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/gba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ jobs:
- name: download devkitpro
run: wget https://apt.devkitpro.org/install-devkitpro-pacman
- name: devkitpro install
run: chmod +x ./install-devkitpro-pacman && ./install-devkitpro-pacman
run: chmod +x ./install-devkitpro-pacman && sudo ./install-devkitpro-pacman
- name: install pkg
run: pacman -S gba-dev
run: sudo pacman -S gba-dev
- name: add to path
run: source /etc/profile.d/devkit-env.sh
- name: configure
run: arm-none-eabi-cmake . -DCMAKE_BUILD_TYPE=Release
run: cmake .. -DHOST_SYSTEM_NAME=GBA -DCMAKE_BUILD_TYPE=Release
working-directory: ./build
- name: build
run: cmake --build .
working-directory: ./build

4 changes: 3 additions & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: configure
run: cmake . -DCMAKE_BUILD_TYPE=Release
run: cmake .. -DCMAKE_BUILD_TYPE=Release
working-directory: ./build
- name: build
run: cmake --build .
working-directory: ./build

4 changes: 3 additions & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: configure
run: cmake . -DCMAKE_BUILD_TYPE=Release
run: cmake .. -DCMAKE_BUILD_TYPE=Release
working-directory: ./build
- name: build
run: cmake --build .
working-directory: ./build

4 changes: 3 additions & 1 deletion .github/workflows/web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ jobs:
- name: add to path
run: echo /home/runner/work/tidal2d/tidal2d/emsdk/upstream/emscripten >> $GITHUB_PATH
- name: configure
run: emcmake cmake . -DCMAKE_BUILD_TYPE=Release
run: cmake .. -DHOST_SYSTEM_NAME=Emscripten -DCMAKE_BUILD_TYPE=Release
working-directory: ./build
- name: build
run: cmake --build .
working-directory: ./build

4 changes: 3 additions & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: configure
run: cmake . -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
run: cmake .. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
working-directory: ./build
- name: build
run: mingw32-make
working-directory: ./build

136 changes: 74 additions & 62 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ cmake_minimum_required(VERSION 3.28)
project(tidal2d)
include(FetchContent)

file(GLOB_RECURSE decoder_sources decoder/*.c)

add_executable(decoder ${decoder_sources})

set(CMAKE_C_FLAGS_DEBUG_INIT "-Wall -pedantic")
# More warnings
add_compile_options(-Wall -pedantic)
# For multi-configs like MSVC
set(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE})

# Helper function for header-only libs
function(fetch_header target name example path url tag repo_path)
find_path(
${name}_INCLUDE_DIR
NAMES "${example}"
PATHS "${path}"
try_compile(
result
SOURCE_FROM_CONTENT test.c "#include <${example}>"
CMAKE_FLAGS -DINCLUDE_DIRECTORIES=${path}
)
if (${name}_INCLUDE_DIR)
target_include_directories(${target} PRIVATE ${${name}_INCLUDE_DIR})
if (result)
target_include_directories(${target} PRIVATE ${path})
else()
FetchContent_Declare(
${name}
Expand All @@ -31,18 +30,56 @@ function(fetch_header target name example path url tag repo_path)
endif()
endfunction()

fetch_header(decoder stb stb_image.h /usr/include/stb https://github.com/nothings/stb ae721c50eaf761660b4f90cc590453cdb0c2acd0 "") # Commit Feb 13, 2024
fetch_header(decoder nanosvg nanosvgrast.h /usr/include/nanosvg https://github.com/memononen/nanosvg 93ce879dc4c04a3ef1758428ec80083c38610b1f "src") # Commit Dec 29, 2023

# Platform linking
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(decoder PRIVATE m)
# Cross compilation settings
set(HOST_SYSTEM_NAME ${CMAKE_SYSTEM_NAME} CACHE STRING "")
set(CURRENT_COMPILER "NATIVE")
set(NATIVE_C_COMPILER CMAKE_C_COMPILER)
set(HOST_C_COMPILER "")
set(CROSSCOMPILING ${CMAKE_CROSSCOMPILING})
if (NOT HOST_SYSTEM_NAME MATCHES ${CMAKE_SYSTEM_NAME})
set(CROSSCOMPILING true)
if (HOST_SYSTEM_NAME MATCHES Linux)
# Nothing for now
elseif (HOST_SYSTEM_NAME MATCHES Emscripten)
set(HOST_C_COMPILER "emcc")
elseif (HOST_SYSTEM_NAME MATCHES Windows)
set(HOST_C_COMPILER "x86_64-w64-mingw32-gcc")
endif()
endif()

file(GLOB_RECURSE engine_sources engine/*.c)
# Cross compilation macros
macro(use_host_compiler)
if (CROSSCOMPILING AND ${CURRENT_COMPILER} STREQUAL "NATIVE")
# Save current native flags
set(NATIVE_C_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "GCC flags for the native compiler." FORCE)

set(resource_path "" CACHE STRING "")
# Change compiler
set(CMAKE_SYSTEM_NAME ${HOST_SYSTEM_NAME})
#set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
set(CMAKE_C_COMPILER ${HOST_C_COMPILER})
set(CMAKE_C_FLAGS ${HOST_C_FLAGS})
set(CURRENT_COMPILER "HOST" CACHE STRING "Which compiler we are using." FORCE)
endif()
endmacro()
macro(use_native_compiler)
if (${CURRENT_COMPILER} STREQUAL "HOST")
# Save current host flags
set(HOST_C_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "GCC flags for the host compiler." FORCE)

# Change compiler
set(CMAKE_SYSTEM_NAME ${CMAKE_HOST_SYSTEM_NAME})
#set(CMAKE_SYSTEM_PROCESSOR ${NATIVE_SYSTEM_PROCESSOR})
set(CMAKE_C_COMPILER ${NATIVE_C_COMPILER})
set(CMAKE_C_FLAGS ${NATIVE_C_FLAGS})
set(CURRENT_COMPILER "NATIVE" CACHE STRING "Which compiler we are using." FORCE)
endif()
endmacro()

# Build decoder
add_subdirectory(decoder)

# List resource folder
set(resource_path "" CACHE STRING "")
if (NOT "${resource_path}" STREQUAL "")
file(REAL_PATH ${resource_path} real_path)
if (NOT IS_DIRECTORY ${real_path})
Expand All @@ -51,49 +88,24 @@ if (NOT "${resource_path}" STREQUAL "")
file(GLOB_RECURSE resources "${real_path}/*")
endif()

if (NOT CMAKE_CROSSCOMPILING)
add_custom_command(
OUTPUT data.c
COMMAND decoder ${resources}
DEPENDS decoder ${resources}
)
endif()
# Generate data array
add_custom_command(
OUTPUT data.c
COMMAND decoder
ARGS ${resources}
DEPENDS decoder ${resources}
)
add_custom_target(
data
DEPENDS data.c
)
set_source_files_properties(
data.c
PROPERTIES GENERATED TRUE
)

# Allow for custom game titles
set(title "game" CACHE STRING "")

add_executable(${title} ${engine_sources} data.c)

fetch_header(${title} sokol sokol_gfx.h /usr/include https://github.com/floooh/sokol 55bc9cf3fa4051d485d10412c75c893c3135e885 "") # Commit May 6, 2024
fetch_header(${title} sokol sokol_gl.h /usr/include https://github.com/floooh/sokol 55bc9cf3fa4051d485d10412c75c893c3135e885 "util") # Commit May 6, 2024
fetch_header(${title} sokol_gp sokol_gp.h /usr/include https://github.com/edubart/sokol_gp a6ce39f93fb2da2c47b70cdd4d1c0a35c0e756ef "") # Commit Mar 22, 2024
fetch_header(${title} physac physac.h /usr/include https://github.com/victorfisac/physac 29d9fc06860b54571a02402fff6fa8572d19bd12 "src") # Commit Dec 10, 2023
fetch_header(${title} minilua minilua.h /usr/include https://github.com/edubart/minilua 79a00fab1639517de38a372db9e60fd6cc730d69 "") # v5.4.6
fetch_header(${title} fontstash fontstash.h /usr/include https://github.com/memononen/fontstash b5ddc9741061343740d85d636d782ed3e07cf7be "src") # Commit Apr 22, 2019

# Platform linking
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(${title} PRIVATE X11)
target_link_libraries(${title} PRIVATE Xi)
target_link_libraries(${title} PRIVATE Xcursor)
target_link_libraries(${title} PRIVATE GL)
target_link_libraries(${title} PRIVATE m)
target_link_libraries(${title} PRIVATE asound)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set_target_properties(${title} PROPERTIES OUTPUT_NAME "index")
set_target_properties(${title} PROPERTIES SUFFIX ".html")
#target_link_options(${title} PRIVATE "--shell-file ${CMAKE_SOURCE_DIR}/index.html")
target_link_options(${title} PRIVATE "-sFULL_ES3")
target_link_options(${title} PRIVATE "-sTOTAL_MEMORY=67108864")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_compile_options(${title} PRIVATE "-mwin32")
target_link_libraries(${title} PRIVATE kernel32)
target_link_libraries(${title} PRIVATE user32)
target_link_libraries(${title} PRIVATE shell32)
target_link_libraries(${title} PRIVATE gdi32)
#target_link_libraries(${title} PRIVATE ssp)
endif()

#file(ARCHIVE_CREATE
#OUTPUT "${title}_${CMAKE_SYSTEM_NAME}.zip"
#PATHS ${title}
#FORMAT "zip")
# Build game
add_subdirectory(engine)
13 changes: 13 additions & 0 deletions decoder/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use_native_compiler()

file(GLOB_RECURSE decoder_sources *.c)

add_executable(decoder ${decoder_sources})

fetch_header(decoder stb stb_image.h /usr/include/stb https://github.com/nothings/stb ae721c50eaf761660b4f90cc590453cdb0c2acd0 "") # Commit Feb 13, 2024
fetch_header(decoder nanosvg nanosvgrast.h /usr/include/nanosvg https://github.com/memononen/nanosvg 93ce879dc4c04a3ef1758428ec80083c38610b1f "src") # Commit Dec 29, 2023

# Platform linking
if (CMAKE_SYSTEM_NAME MATCHES Linux)
target_link_libraries(decoder PRIVATE m)
endif()
2 changes: 1 addition & 1 deletion decoder/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string.h>

#define CHECK_ERROR(var, msg) \
if (!var) { \
if (!(var)) { \
fprintf(stderr, "Failure: " #msg "\n"); \
return EXIT_FAILURE; \
}
Expand Down
9 changes: 5 additions & 4 deletions decoder/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

typedef struct file_table {
const char* name;
Expand All @@ -17,7 +18,7 @@ static bool validate_ext(const char* ext) {
const char* s = valid_extensions[0];
do
if (strcmp(s, ext) == 0) return true;
while (s = strchr(s, '\0')+1);
while ((s = strchr(s, '\0')+1));
return false;
}

Expand All @@ -37,7 +38,7 @@ static int ext_cmp(const char* ext1, const char* ext2) {
{"lua", 3},
{NULL, -1}
};
int val1, val2;
int val1 = 0, val2 = 0;
for (int i = 0; map[i].ext; i++) {
if (strcmp(ext1, map[i].ext) == 0) val1 = map[i].val;
if (strcmp(ext2, map[i].ext) == 0) val2 = map[i].val;
Expand Down Expand Up @@ -103,7 +104,7 @@ int main(int argc, char** argv) {
}
free(buf);
}
err = fputs("\n};\n\nextern const unsigned char data_info[];\n\nconst unsigned char data_info[] = {\n ", out);
err = fputs("0x0\n};\n\nextern const char data_info[];\n\nconst char data_info[] = {\n ", out);
CHECK_ERROR(err > 0, "couldn't write to file");
lc = 0;
for (int i = 0; ft[i].name; i++) {
Expand All @@ -112,7 +113,7 @@ int main(int argc, char** argv) {
LINE_BREAK(lc, out);
}
for (int j = 0; j < B_IN_SZ; j++) {
PRINT_BYTE(out, (unsigned char)((ft[i].size >> (j*8)) & 0xff) );
PRINT_BYTE(out, (uint8_t)((ft[i].size >> (j*8)) & 0xff) );
LINE_BREAK(lc, out);
}
}
Expand Down
52 changes: 52 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use_host_compiler()

file(GLOB_RECURSE engine_sources *.c)

add_executable(${title} ${engine_sources} ${CMAKE_BINARY_DIR}/data.c)
add_dependencies(${title} data)

fetch_header(${title} sokol sokol_gfx.h /usr/include https://github.com/floooh/sokol 55bc9cf3fa4051d485d10412c75c893c3135e885 "") # Commit May 6, 2024
fetch_header(${title} sokol sokol_gl.h /usr/include https://github.com/floooh/sokol 55bc9cf3fa4051d485d10412c75c893c3135e885 "util") # Commit May 6, 2024
fetch_header(${title} sokol_gp sokol_gp.h /usr/include https://github.com/edubart/sokol_gp a6ce39f93fb2da2c47b70cdd4d1c0a35c0e756ef "") # Commit Mar 22, 2024
fetch_header(${title} physac physac.h /usr/include https://github.com/victorfisac/physac 29d9fc06860b54571a02402fff6fa8572d19bd12 "src") # Commit Dec 10, 2023
fetch_header(${title} minilua minilua.h /usr/include https://github.com/edubart/minilua 79a00fab1639517de38a372db9e60fd6cc730d69 "") # v5.4.6
fetch_header(${title} fontstash fontstash.h /usr/include https://github.com/memononen/fontstash b5ddc9741061343740d85d636d782ed3e07cf7be "src") # Commit Apr 22, 2019
fetch_header(${title} glibc force_link_glibc_2.23.h /usr/include https://github.com/wheybags/glibc_version_header 60d54829f34f21dc440126ad5630e6a9789a48b2 "version_headers/x64") # Commit May 3, 2019

# Platform linking
if (CMAKE_SYSTEM_NAME MATCHES Linux)
target_compile_options(${title} PRIVATE "-includeforce_link_glibc_2.23.h")
target_link_libraries(${title} PRIVATE X11)
target_link_libraries(${title} PRIVATE Xi)
target_link_libraries(${title} PRIVATE Xcursor)
target_link_libraries(${title} PRIVATE GL)
target_link_libraries(${title} PRIVATE m)
target_link_libraries(${title} PRIVATE asound)
elseif(CMAKE_SYSTEM_NAME MATCHES Emscripten)
set_target_properties(${title} PROPERTIES OUTPUT_NAME "index")
set_target_properties(${title} PROPERTIES SUFFIX ".html")
#target_link_options(${title} PRIVATE "--shell-file ${CMAKE_SOURCE_DIR}/index.html")
target_link_options(${title} PRIVATE "-sFULL_ES3")
target_link_options(${title} PRIVATE "-sTOTAL_MEMORY=67108864")
#target_link_options(${title} PRIVATE "-sALLOW_MEMORY_GROWTH")
elseif(CMAKE_SYSTEM_NAME MATCHES Windows)
set_target_properties(${title} PROPERTIES SUFFIX ".exe")
file(READ "${CMAKE_BINARY_DIR}/_deps/physac-src/src/physac.h" TEXT)
string(REPLACE "int __stdcall QueryPerformanceCounter(unsigned long long int* lpPerformanceCount);" "" TEXT "${TEXT}")
string(REPLACE "int __stdcall QueryPerformanceFrequency(unsigned long long int* lpFrequency);" "" TEXT "${TEXT}")
string(REPLACE "GetCurrentTime" "GetCurrTime" TEXT "${TEXT}")
file(WRITE "${CMAKE_BINARY_DIR}/_deps/physac-src/src/physac.h" "${TEXT}")
target_link_options(${title} PRIVATE "-static")
target_compile_options(${title} PRIVATE "-mwin32")
target_link_options(${title} PRIVATE "-mwindows")
target_link_libraries(${title} PRIVATE ole32)
endif()

# Output a zipped archive
set(ZIP_FILE ${CMAKE_BINARY_DIR}/${title}_${HOST_SYSTEM_NAME}.tar.gz)
add_custom_command(
TARGET ${title}
POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E tar "cfz" "${ZIP_FILE}" "$<TARGET_FILE:${title}>"
)
12 changes: 6 additions & 6 deletions engine/actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int tidal_set_sprite(lua_State* L) {
const char* name = luaL_checkstring(L, 2);
sgp_rect* dst = &e.ins[index].rect.dst;

size_t offset;
uint64_t offset;
DATA_LOOP(offset,
if (strcmp(name, basename(data_info+i)) == 0) break;
);
Expand Down Expand Up @@ -56,8 +56,8 @@ int tidal_set_sprite(lua_State* L) {
int tidal_set_shape(lua_State* L) {
int index = luaL_checkinteger(L, 1);
const char* shape = luaL_checkstring(L, 2);
/*PhysicsBody body;
if (body = GetPhysicsBody(index)) DestroyPhysicsBody(body);
PhysicsBody body;
if ((body = GetPhysicsBody(index))) DestroyPhysicsBody(body);
if (strcmp(shape, "box") == 0) {
sgp_rect dst = e.ins[index].rect.dst;
Vector2 pos = {dst.x, dst.y};
Expand All @@ -70,14 +70,14 @@ int tidal_set_shape(lua_State* L) {
// add more shapes
} else {
return luaL_error(L, "incorrect shape name");
}*/
}
return 0;
}

int tidal_set_gravity(lua_State* L) {
float x = luaL_checknumber(L, 1);
float y = luaL_checknumber(L, 2);
//SetPhysicsGravity(x, y);
SetPhysicsGravity(x, y);
return 0;
}

Expand Down Expand Up @@ -107,7 +107,7 @@ int tidal_set_text(lua_State* L) {

int tidal_set_music(lua_State* L) {
const char* name = luaL_checkstring(L, 1);
size_t offset;
uint64_t offset;
DATA_LOOP(offset,
if (strcmp(name, basename(data_info+i)) == 0) {
e.music_offset = offset;
Expand Down
Loading

0 comments on commit f67a0fa

Please sign in to comment.