parent
95233f5fbe
commit
2c24a938c5
3 changed files with 30 additions and 37 deletions
|
@ -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
|
||||
/usr/lib/emscripten/em++ -std=c++26 --use-port=sdl3 -O2 --shell-file emscripten_shell.html -sASSERTIONS -o index.html main.cpp
|
||||
|
|
2
build.sh
2
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`
|
||||
clang++ -Wall -std=c++26 -O2 -g -o main main.cpp -fuse-ld=mold `pkg-config --libs --cflags sdl3`
|
||||
|
|
63
main.cpp
63
main.cpp
|
@ -1,6 +1,7 @@
|
|||
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_render.h>
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
|
@ -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 <typename SDLT, auto deletef>
|
||||
struct sdl_wrapper_t : std::unique_ptr<SDLT, decltype(deletef)> {
|
||||
explicit sdl_wrapper_t(SDLT* p)
|
||||
: std::unique_ptr<SDLT, decltype(deletef)>{p, deletef} {
|
||||
sdl_check(p);
|
||||
}
|
||||
|
||||
operator SDLT*() { return std::unique_ptr<SDLT, decltype(deletef)>::get(); }
|
||||
};
|
||||
using sdl_wrapper_t =
|
||||
std::unique_ptr<SDLT, decltype([](auto* p) { deletef(p); })>;
|
||||
|
||||
using window_t = sdl_wrapper_t<SDL_Window, SDL_DestroyWindow>;
|
||||
using surface_t = sdl_wrapper_t<SDL_Surface, SDL_FreeSurface>;
|
||||
using surface_t = sdl_wrapper_t<SDL_Surface, SDL_DestroySurface>;
|
||||
using renderer_t = sdl_wrapper_t<SDL_Renderer, SDL_DestroyRenderer>;
|
||||
|
||||
auto operator+(SDL_Point a, SDL_Point b) {
|
||||
|
@ -67,8 +62,8 @@ constexpr int height = 512;
|
|||
constexpr auto steps_per_second = 10'000;
|
||||
|
||||
template <auto EF>
|
||||
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<SDL_Point, batchsize> point_batch;
|
||||
std::array<SDL_FPoint, batchsize> 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<float>(newpoint.x),
|
||||
static_cast<float>(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<SDL_Quit> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue