From b4ff44788a4a53db114ce74de11073387501770a Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 24 Apr 2024 06:51:37 -0400 Subject: [PATCH] Bring in a simple SDL2 demo for completeness. It's running in a main.cpp to test SDL2 in C++. --- README.md | 2 +- sdl2demo/main.cpp | 36 +++++++++++++++++++++++++++++ sdl2demo/meson.build | 11 +++++++++ sdl2demo/setup.ps1 | 5 ++++ sfmldemo/{sfmlprog.cpp => main.cpp} | 0 sfmldemo/meson.build | 6 +++-- 6 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 sdl2demo/main.cpp create mode 100644 sdl2demo/meson.build create mode 100644 sdl2demo/setup.ps1 rename sfmldemo/{sfmlprog.cpp => main.cpp} (100%) diff --git a/README.md b/README.md index 02af62a..439bd8d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ To get this working the rough (ROUGH) steps are: 2. Run setup.ps1 3. `meson compile -C builddir` 4. `meson install -C builddir` -- Careful with this, it might install stuff in unwanted areas. On my computer it put them in `C:\` but I have to read the instructions on how to set the install location. -5. `./builddir/sfmlprog` -- That should run it and you see a window with ImGUI's demo panel. +5. `./builddir/sfmldemo` -- That should run it and you see a window with ImGUI's demo panel. I'll have more extensive instructions in a later blog post, but if you have time try this out and let me know how it went at help@learncodethehardway.com. Please let me know if you tried a different compiler, Windows version, etc. If you're on OSX or Linux it should work the same but Linux people might want to use their package manager instead. diff --git a/sdl2demo/main.cpp b/sdl2demo/main.cpp new file mode 100644 index 0000000..39eb0f8 --- /dev/null +++ b/sdl2demo/main.cpp @@ -0,0 +1,36 @@ +#include "SDL.h" + +int main(int argc, char *argv[]) +{ + SDL_Window *window; + SDL_Renderer *renderer; + SDL_Surface *surface; + SDL_Event event; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); + return 3; + } + + if (SDL_CreateWindowAndRenderer(1920, 1080, SDL_WINDOW_RESIZABLE, &window, &renderer)) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError()); + return 3; + } + + while (1) { + SDL_PollEvent(&event); + if (event.type == SDL_QUIT) { + break; + } + SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); + SDL_RenderClear(renderer); + SDL_RenderPresent(renderer); + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + + SDL_Quit(); + + return 0; +} diff --git a/sdl2demo/meson.build b/sdl2demo/meson.build new file mode 100644 index 0000000..abd24b0 --- /dev/null +++ b/sdl2demo/meson.build @@ -0,0 +1,11 @@ +project('sdl2demo', 'cpp', + default_options: [ + 'cpp_std=c++17', + ]) + +sdl2_dep = dependency('sdl2') +imgui_dep = dependency('imgui') + +executable('sdl2demo', 'main.cpp', + win_subsystem: 'windows', + dependencies: [sdl2_dep, imgui_dep]) diff --git a/sdl2demo/setup.ps1 b/sdl2demo/setup.ps1 new file mode 100644 index 0000000..aa48b9a --- /dev/null +++ b/sdl2demo/setup.ps1 @@ -0,0 +1,5 @@ +mkdir builddir +mkdir subprojects +meson wrap install imgui +meson wrap install sdl2 +meson setup builddir diff --git a/sfmldemo/sfmlprog.cpp b/sfmldemo/main.cpp similarity index 100% rename from sfmldemo/sfmlprog.cpp rename to sfmldemo/main.cpp diff --git a/sfmldemo/meson.build b/sfmldemo/meson.build index b03b0f7..4efb0ae 100644 --- a/sfmldemo/meson.build +++ b/sfmldemo/meson.build @@ -1,9 +1,11 @@ project('sfmldemo', 'cpp', - default_options: ['cpp_std=c++17']) + default_options: [ + 'cpp_std=c++17', + ]) sfml_dep = dependency('sfml') imgui_dep = dependency('imgui-sfml') -executable('sfmlprog', 'sfmlprog.cpp', +executable('sfmldemo', 'main.cpp', win_subsystem: 'windows', dependencies: [sfml_dep, imgui_dep])