From d5bdd3c101d0184408c85e8878bdbc9cdcd15fa2 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Mon, 9 May 2022 12:38:22 +0800 Subject: [PATCH] Update CMake config, add events demo Make CMake load dependencies relative to project directory. --- .gitignore | 62 ++++++++++++++++++++ CMakeLists.txt | 27 +++++++++ README.md | 17 ++++++ src/packages/game/main.c | 119 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 225 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 src/packages/game/main.c 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..90d8c05 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,27 @@ +cmake_minimum_required(VERSION 3.20) + +# Set your project name here +project(izanagi C) + +set(CMAKE_C_STANDARD 11) + +include_directories("${PROJECT_SOURCE_DIR}/dependencies/SDL2/include") + +if (WIN32) + if (CMAKE_SIZEOF_VOID_P EQUAL 8) + set(PROJECT_ARCH x64) + else () + set(PROJECT_ARCH x86) + endif () +endif () + +link_directories("${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}") +add_executable(izanagi src/packages/game/main.c) +target_link_libraries(izanagi SDL2main SDL2) + +if (WIN32) + add_custom_command(TARGET izanagi 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 + $) # <--this is out-file path +endif () diff --git a/README.md b/README.md new file mode 100644 index 0000000..a08e21d --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# 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). + - For Windows + 1. Unpack the **MSVC** archive under `dependencies` folder in this project's root. + 2. Rename the directory as `SDL2`. + - For macOS + + `TODO` + + _Alternatively, on Windows, you can link the external libraries via symlink._ +3. Build via CMake. diff --git a/src/packages/game/main.c b/src/packages/game/main.c new file mode 100644 index 0000000..a6c98aa --- /dev/null +++ b/src/packages/game/main.c @@ -0,0 +1,119 @@ +#include +#include +#include + +const char* APP_NAME = "SDL2"; +const unsigned int SCREEN_WIDTH = 640; +const unsigned int SCREEN_HEIGHT = 480; + +const unsigned char CONTROLS = 16; + +const SDL_KeyCode KEYBOARD_CONTROLS[CONTROLS] = { + SDLK_RIGHT, + SDLK_DOWN, + SDLK_LEFT, + SDLK_UP, + SDLK_RETURN, // yes + SDLK_BACKSPACE, // no + SDLK_a, // action0 + SDLK_s, // action1 + SDLK_d, // action2 + SDLK_f, // action3 + SDLK_z, // action4 + SDLK_x, // action5 + SDLK_c, // action6 + SDLK_v, // action7 + SDLK_w, // action8 + SDLK_e, // action9 +}; + +int main(int argc, char* args[]) { + SDL_Window* window = NULL; + SDL_Surface* screenSurface = NULL; + + if (SDL_Init( + SDL_INIT_VIDEO + | SDL_INIT_GAMECONTROLLER + | SDL_INIT_EVENTS + ) < 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); + + unsigned short action = 0; + while (!quit) { + SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x00)); + uint64_t ticks = SDL_GetTicks64(); + for (unsigned char i = 0; i < 64; i += 1) { + const unsigned char column = (64 - i) % 32; + const unsigned char row = i / 32; + const uint64_t bitflag = (0x1lu << i); + const unsigned char size = 4; + if (ticks & bitflag) { + SDL_FillRect(screenSurface, &(SDL_Rect) { + column * size, + SCREEN_HEIGHT - ((row + 1) * size), + size, + size + }, SDL_MapRGB(screenSurface->format, 0x00, 0xff, 0xff)); + } + } + + while (SDL_PollEvent(&e) != 0) { + if (e.type == SDL_QUIT) { + quit = true; + } + + for (unsigned char i = 0; i < CONTROLS; i += 1) { + // TODO do same for gamepad + if (e.key.keysym.sym == KEYBOARD_CONTROLS[i]) { + const unsigned short bitflag = (0x1 << i); + if (e.type == SDL_KEYDOWN) { + action |= bitflag; + } else if (e.type == SDL_KEYUP) { + action &= ~bitflag; + } + } + } + + for (unsigned char i = 0; i < CONTROLS; i += 1) { + const unsigned char column = i % 4; + const unsigned char row = i / 4; + const unsigned short bitflag = (0x1 << i); + const unsigned char size = 4; + if (action & bitflag) { + SDL_FillRect(screenSurface, &(SDL_Rect) { + column * size, + row * size, + size, + size + }, SDL_MapRGB(screenSurface->format, 0xff, 0xff, 0x00)); + } + } + + SDL_UpdateWindowSurface(window); + } + } + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +}