From 8632d36b6936555681cc923488760427712cbeac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophyl=20Hoguet?= Date: Fri, 11 Mar 2022 17:59:14 +0100 Subject: [PATCH] cmake + main.c --- CMakeLists.txt | 26 ++++++++++++++++++-- main.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 main.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 08ce0ba..12d9569 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,23 @@ enable_testing() set(MEMORYCHECK_COMMAND_OPTIONS "--error-exitcode=1 --leak-check=full") include(CTest) +## use pkgconfig to solve SDL2 dependencies (Linux only) +include(FindPkgConfig) # pkg-config --list-all | grep -i sdl2 +pkg_search_module(SDL2 REQUIRED sdl2) +pkg_search_module(SDL2_IMG REQUIRED SDL2_image) +pkg_search_module(SDL2_TTF REQUIRED SDL2_ttf) +set(SDL2_ALL_LIBS ${SDL2_LIBRARIES} ${SDL2_IMG_LIBRARIES} ${SDL2_TTF_LIBRARIES}) +set(SDL2_ALL_INC ${SDL2_INCLUDE_DIRS} ${SDL2_IMG_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}) + +## use a CMake3 package to solve SDL2 dependencies +# find_package(SDL2 REQUIRED) +# set(SDL2_ALL_INC ${SDL2_INCLUDE_DIRS}) +# set(SDL2_ALL_LIBS ${SDL2_LIBRARIES} -lSDL2_ttf -lSDL2_image) # add extra SDL2 libraries + + +message(STATUS "SDL2 include dir: ${SDL2_ALL_INC}") +message(STATUS "SDL2 all libraries: ${SDL2_ALL_LIBS}") + #positionne la variable set(CMAKE_C_FLAGS "-std=c99 -Wall") set(CMAKE_C_FLAGS_DEBUG "-g --coverage -pthread -Wextra -Wunused-parameter -Wpedantic -Werror") # use CMake option: -DCMAKE_BUILD_TYPE=DEBUG @@ -20,14 +37,19 @@ set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG") # use CMake option: -DCMAKE_BUILD_TY add_executable(game_text game_text.c) target_link_libraries(game_text game) -# game tests +# game_tests add_executable(game_test game_test.c) target_link_libraries(game_test game) -# game solve +# game_solve add_executable(game_solve game_solve.c) target_link_libraries(game_solve game) +# game_sdl +include_directories(${SDL2_ALL_INC}) +add_executable(game_sdl main.c model.c) +target_link_libraries(game_sdl game ${SDL2_ALL_LIBS} m) + # libgame add_library (game game.c game_aux.c game_ext.c game_private.c history.c game_tools.c game_examples.c) diff --git a/main.c b/main.c new file mode 100644 index 0000000..fa104d1 --- /dev/null +++ b/main.c @@ -0,0 +1,67 @@ +// SDL2 Demo by aurelien.esnard@u-bordeaux.fr + +#include +#include // required to load transparent texture from PNG +#include // required to use TTF fonts +#include +#include +#include "model.h" + +/* **************************************************************** */ + +int main(int argc, char* argv[]) { + /* initialize SDL2 and some extensions */ + if (SDL_Init(SDL_INIT_VIDEO) != 0) + ERROR("Error: SDL_Init VIDEO (%s)", SDL_GetError()); + if (IMG_Init(IMG_INIT_PNG & IMG_INIT_PNG) != IMG_INIT_PNG) + ERROR("Error: IMG_Init PNG (%s)", SDL_GetError()); + if (TTF_Init() != 0) + ERROR("Error: TTF_Init (%s)", SDL_GetError()); + + /* create window and renderer */ + SDL_Window* win = + SDL_CreateWindow(APP_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); + if (!win) + ERROR("Error: SDL_CreateWindow (%s)", SDL_GetError()); + SDL_Renderer* ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (!ren) + ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE); + if (!ren) + ERROR("Error: SDL_CreateRenderer (%s)", SDL_GetError()); + + /* initialize your environment */ + Env* env = init(win, ren, argc, argv); + + /* main render loop */ + SDL_Event e; + bool quit = false; + while (!quit) { + /* manage events */ + while (SDL_PollEvent(&e)) { + /* process your events */ + quit = process(win, ren, env, &e); + if (quit) + break; + } + + /* background in gray */ + SDL_SetRenderDrawColor(ren, 0xA0, 0xA0, 0xA0, 0xFF); + SDL_RenderClear(ren); + + /* render all what you want */ + render(win, ren, env); + SDL_RenderPresent(ren); + SDL_Delay(DELAY); + } + + /* clean your environment */ + clean(win, ren, env); + + SDL_DestroyRenderer(ren); + SDL_DestroyWindow(win); + IMG_Quit(); + TTF_Quit(); + SDL_Quit(); + + return EXIT_SUCCESS; +}