From 2c24a938c5250f838b3d0b7632a867a7ac3f9516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20H=C3=B6rdt?= Date: Sun, 6 Jul 2025 18:02:10 +0200 Subject: [PATCH] port to SDL3 closes #1 --- build-web.sh | 2 +- build.sh | 2 +- main.cpp | 63 +++++++++++++++++++++++----------------------------- 3 files changed, 30 insertions(+), 37 deletions(-) diff --git a/build-web.sh b/build-web.sh index c20dcd4..b5c4cfb 100755 --- a/build-web.sh +++ b/build-web.sh @@ -1,2 +1,2 @@ #!/bin/bash -/usr/lib/emscripten/em++ -std=c++26 --use-port=sdl2 -O2 --shell-file emscripten_shell.html -sASSERTIONS -o index.html main.cpp \ No newline at end of file +/usr/lib/emscripten/em++ -std=c++26 --use-port=sdl3 -O2 --shell-file emscripten_shell.html -sASSERTIONS -o index.html main.cpp diff --git a/build.sh b/build.sh index 841b665..87aa258 100755 --- a/build.sh +++ b/build.sh @@ -1,2 +1,2 @@ #!/bin/bash -clang++ -Wall -std=c++26 -O2 -g -o main main.cpp -fuse-ld=mold `pkg-config --libs --cflags sdl2` \ No newline at end of file +clang++ -Wall -std=c++26 -O2 -g -o main main.cpp -fuse-ld=mold `pkg-config --libs --cflags sdl3` diff --git a/main.cpp b/main.cpp index 2a99190..8f7beb9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ -#include +#include +#include #include #include @@ -27,10 +28,10 @@ auto sdl_throw_current_error(const std::source_location location) { } auto sdl_check( - int errc, + bool success, const std::source_location location = std::source_location::current() ) { - if (errc != 0) { sdl_throw_current_error(location); } + if (!success) { sdl_throw_current_error(location); } } // make sure someone already owns passed sdl_obj, or memory will be leaked @@ -43,17 +44,11 @@ auto sdl_check( } template -struct sdl_wrapper_t : std::unique_ptr { - explicit sdl_wrapper_t(SDLT* p) - : std::unique_ptr{p, deletef} { - sdl_check(p); - } - - operator SDLT*() { return std::unique_ptr::get(); } -}; +using sdl_wrapper_t = + std::unique_ptr; using window_t = sdl_wrapper_t; -using surface_t = sdl_wrapper_t; +using surface_t = sdl_wrapper_t; using renderer_t = sdl_wrapper_t; auto operator+(SDL_Point a, SDL_Point b) { @@ -67,8 +62,8 @@ constexpr int height = 512; constexpr auto steps_per_second = 10'000; template -struct [[nodiscard("give this a name so SDL_Quit is called at the end" -)]] Defer { +struct [[nodiscard("give this a name so SDL_Quit is called at the end")]] +Defer { Defer() = default; Defer(const Defer&) = delete; Defer(Defer&&) = delete; @@ -80,7 +75,7 @@ struct [[nodiscard("give this a name so SDL_Quit is called at the end" auto poll_events(bool& continu) { for (SDL_Event e; SDL_PollEvent(&e);) { - if (e.type == SDL_QUIT) { continu = false; } + if (e.type == SDL_EVENT_QUIT) { continu = false; } } } @@ -133,7 +128,7 @@ void mainloop(void* userData) { sdl_check(SDL_SetRenderDrawColor( renderer, walker.col.r, walker.col.g, walker.col.b, walker.col.a )); - std::array point_batch; + std::array point_batch; for (auto step = 0z; step < batchsize; ++step) { SDL_Point newpoint; do { @@ -141,9 +136,12 @@ void mainloop(void* userData) { } while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or newpoint.y >= height); walker.pos = newpoint; - point_batch[step] = newpoint; + point_batch[step] = SDL_FPoint{ + static_cast(newpoint.x), + static_cast(newpoint.y) + }; } - sdl_check(SDL_RenderDrawPoints( + sdl_check(SDL_RenderPoints( renderer, point_batch.data(), point_batch.size() )); } @@ -156,7 +154,7 @@ void mainloop(void* userData) { ); SDL_RenderClear(renderer); sdl_check(SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE)); - sdl_check(SDL_RenderCopy(renderer, texture, nullptr, nullptr)); + sdl_check(SDL_RenderTexture(renderer, texture, nullptr, nullptr)); SDL_RenderPresent(renderer); } @@ -164,27 +162,22 @@ int main() try { sdl_check(SDL_Init(SDL_INIT_VIDEO)); Defer defer_SDL_Quit; - window_t window{SDL_CreateWindow( - "random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, - height, 0 // SDL_WINDOW_FULLSCREEN - )}; - renderer_t renderer{SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED) - }; - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); + window_t window_; + renderer_t renderer_; + SDL_CreateWindowAndRenderer( + "test", width, height, 0, std::out_ptr(window_), std::out_ptr(renderer_) + ); + sdl_check(window_.get()); + auto* renderer = sdl_check(renderer_.get()); + { // dump renderer info - SDL_RendererInfo renderer_info; - SDL_GetRendererInfo(renderer, &renderer_info); - std::println("Renderer name: {}", renderer_info.name); - std::println("Texture formats: "); - for (Uint32 i = 0; i < renderer_info.num_texture_formats; i++) { - std::puts(SDL_GetPixelFormatName(renderer_info.texture_formats[i])); - } + std::println("Renderer name: {}", SDL_GetRendererName(renderer)); } // no clue who owns this auto* texture = SDL_CreateTexture( - renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, width, + renderer, SDL_PIXELFORMAT_XRGB8888, SDL_TEXTUREACCESS_TARGET, width, height ); sdl_check(SDL_SetRenderTarget(renderer, texture)); @@ -242,4 +235,4 @@ int main() try { std::puts(e.what()); return EXIT_FAILURE; -} \ No newline at end of file +}