commit 7c55b91d91d7b55696df646e74c23824d1d0ecdc Author: TheoryOfNekomata Date: Sun Nov 21 17:11:05 2021 +0800 Implement core Take inspiration from re3 on directory structure. diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ce31d3c --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = tab +insert_final_newline = true +max_line_length = 120 +tab_width = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..def08d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +*.d +*.o +*.ko +*.obj +*.elf +*.ilk +*.map +*.exp +*.gch +*.pch +*.lib +*.a +*.la +*.lo +*.dll +*.so +*.so.* +*.dylib +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex +*.dSYM/ +*.su +*.idb +*.pdb +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db +*.stackdump +[Dd]esktop.ini +$RECYCLE.BIN/ +*.cab +*.msi +*.msix +*.msm +*.msp +*.lnk +.idea/ +cmake-build-debug/ +dependencies/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..70e3c05 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.20) +project(izanami C) + +set(CMAKE_C_STANDARD 11) + +set(SDL2PATH "E:\\Projects\\Games\\izanami\\dependencies\\SDL2-2.0.16\\x86_64-w64-mingw32") +find_package(SDL2 REQUIRED) +include_directories(${SDL2_INCLUDE_DIR}) +add_executable(izanami src/packages/game/main.c src/packages/game/math/IZ_vector.c src/packages/game/math/IZ_vector.h src/packages/game/math/IZ_math.c src/packages/game/math/IZ_math.h src/packages/game/core/IZ_placeable.c src/packages/game/core/IZ_placeable.h src/packages/game/core/IZ_movable.c src/packages/game/core/IZ_movable.h src/packages/game/core/IZ_spatial.c src/packages/game/core/IZ_spatial.h) +target_link_libraries(izanami ${SDL2_LIBRARY}) + + +if (WIN32) +add_custom_command(TARGET izanami POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." + "${PROJECT_SOURCE_DIR}/dependencies/SDL2-2.0.16/x86_64-w64-mingw32/bin/SDL2.dll" # <--this is in-file + $) # <--this is out-file path +endif (WIN32) + +if (WIN64) + add_custom_command(TARGET izanami POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." + "${PROJECT_SOURCE_DIR}/dependencies/SDL2-2.0.16/x86_64-w64-mingw32/bin/SDL2.dll" # <--this is in-file + $) # <--this is out-file path +endif (WIN64) diff --git a/README.md b/README.md new file mode 100644 index 0000000..0fef913 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# sdl2-hello-world + +A beginner template for SDL2 powered by CMake. + +## Setup + +1. Clone this repo. +2. Download the **SDL2 Development Libraries** from the [official download page](https://www.libsdl.org/download-2.0.php). + 1. For Windows, unpack the archive under `dependencies` folder in this project's root. +3. Build via CMake. + +## Todo + +- [X] Windows setup +- [ ] macOS setup diff --git a/src/packages/game/core/IZ_movable.c b/src/packages/game/core/IZ_movable.c new file mode 100644 index 0000000..6fb84a9 --- /dev/null +++ b/src/packages/game/core/IZ_movable.c @@ -0,0 +1 @@ +#include "IZ_movable.h" diff --git a/src/packages/game/core/IZ_movable.h b/src/packages/game/core/IZ_movable.h new file mode 100644 index 0000000..5c384b4 --- /dev/null +++ b/src/packages/game/core/IZ_movable.h @@ -0,0 +1,10 @@ +#ifndef IZ_MOVABLE_H +#define IZ_MOVABLE_H + +#include "../math/IZ_vector.h" + +typedef struct { + IZ_Vector velocity; +} IZ_Movable; + +#endif //IZ_MOVABLE_H diff --git a/src/packages/game/core/IZ_placeable.c b/src/packages/game/core/IZ_placeable.c new file mode 100644 index 0000000..92feeeb --- /dev/null +++ b/src/packages/game/core/IZ_placeable.c @@ -0,0 +1 @@ +#include "IZ_placeable.h" diff --git a/src/packages/game/core/IZ_placeable.h b/src/packages/game/core/IZ_placeable.h new file mode 100644 index 0000000..6b06be6 --- /dev/null +++ b/src/packages/game/core/IZ_placeable.h @@ -0,0 +1,10 @@ +#ifndef IZ_PLACEABLE_H +#define IZ_PLACEABLE_H + +#include "../math/IZ_vector.h" + +typedef struct { + IZ_Vector location; +} IZ_Placeable; + +#endif //IZ_PLACEABLE_H diff --git a/src/packages/game/core/IZ_spatial.c b/src/packages/game/core/IZ_spatial.c new file mode 100644 index 0000000..bf4fc57 --- /dev/null +++ b/src/packages/game/core/IZ_spatial.c @@ -0,0 +1 @@ +#include "IZ_spatial.h" diff --git a/src/packages/game/core/IZ_spatial.h b/src/packages/game/core/IZ_spatial.h new file mode 100644 index 0000000..3b0adcf --- /dev/null +++ b/src/packages/game/core/IZ_spatial.h @@ -0,0 +1,11 @@ +#ifndef IZ_SPATIAL_H +#define IZ_SPATIAL_H + +#include "IZ_placeable.h" + +typedef struct { + IZ_Placeable placeable; + IZ_Vector size; +} IZ_Spatial; + +#endif //IZ_SPATIAL_H diff --git a/src/packages/game/main.c b/src/packages/game/main.c new file mode 100644 index 0000000..23dbcac --- /dev/null +++ b/src/packages/game/main.c @@ -0,0 +1,48 @@ +#include +#include +#include + +const char* APP_NAME = "SDL2"; +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +int main(int argc, char* args[]) { + SDL_Window* window = NULL; + SDL_Surface* screenSurface = NULL; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + return -1; + } + + window = SDL_CreateWindow( + APP_NAME, + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + SCREEN_WIDTH, + SCREEN_HEIGHT, + SDL_WINDOW_SHOWN + ); + + if (window == NULL) { + printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + return -2; + } + + bool quit = false; + SDL_Event e; + screenSurface = SDL_GetWindowSurface(window); + while (!quit) { + while (SDL_PollEvent(&e) != 0) { + if (e.type == SDL_QUIT) { + quit = true; + } + SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); + SDL_UpdateWindowSurface(window); + } + } + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +} diff --git a/src/packages/game/math/IZ_math.c b/src/packages/game/math/IZ_math.c new file mode 100644 index 0000000..26d9c52 --- /dev/null +++ b/src/packages/game/math/IZ_math.c @@ -0,0 +1 @@ +#include "IZ_math.h" diff --git a/src/packages/game/math/IZ_math.h b/src/packages/game/math/IZ_math.h new file mode 100644 index 0000000..e3c60d2 --- /dev/null +++ b/src/packages/game/math/IZ_math.h @@ -0,0 +1,10 @@ +#ifndef IZ_MATH_H +#define IZ_MATH_H + +#include + +typedef float IZ_Real; + +inline IZ_Real IZ_Sqrt(IZ_Real x) { return (IZ_Real) sqrtf(x); } + +#endif //IZ_MATH_H diff --git a/src/packages/game/math/IZ_vector.c b/src/packages/game/math/IZ_vector.c new file mode 100644 index 0000000..272b999 --- /dev/null +++ b/src/packages/game/math/IZ_vector.c @@ -0,0 +1 @@ +#include "IZ_vector.h" diff --git a/src/packages/game/math/IZ_vector.h b/src/packages/game/math/IZ_vector.h new file mode 100644 index 0000000..23cfc5e --- /dev/null +++ b/src/packages/game/math/IZ_vector.h @@ -0,0 +1,52 @@ +#ifndef IZ_VECTOR_H +#define IZ_VECTOR_H + +#include "IZ_math.h" + +typedef struct { + // positive X is rightwards + IZ_Real x; + + // positive Y is downwards + IZ_Real y; +} IZ_Vector; + +inline IZ_Vector IZ_VectorAdd(IZ_Vector a, IZ_Vector b) { + return (IZ_Vector) { + .x = a.x + b.x, + .y = a.y + b.y, + }; +} + +inline IZ_Vector IZ_VectorSubtract(IZ_Vector a, IZ_Vector b) { + return (IZ_Vector) { + .x = a.x - b.x, + .y = a.y - b.y, + }; +} + +inline IZ_Vector IZ_VectorMultiply(IZ_Vector a, IZ_Real b) { + return (IZ_Vector) { + .x = a.x * b, + .y = a.y * b, + }; +} + +inline IZ_Vector IZ_VectorDivide(IZ_Vector a, IZ_Real b) { + return (IZ_Vector) { + .x = a.x / b, + .y = a.y / b, + }; +} + +inline IZ_Real IZ_VectorDot(IZ_Vector a, IZ_Vector b) { + return a.x * b.x + a.y * b.y; +} + +inline IZ_Real IZ_VectorDistance(IZ_Vector a, IZ_Vector b) { + IZ_Real x = b.x - a.x; + IZ_Real y = b.y - a.y; + return IZ_Sqrt(x * x + y * y); +} + +#endif //IZ_VECTOR_H