2024-08-03 02:42:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
2024-08-03 23:52:26 +02:00
|
|
|
#include <chrono>
|
2024-08-04 15:38:07 +02:00
|
|
|
#include <format>
|
2024-08-03 02:42:49 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <print>
|
2024-08-03 03:23:16 +02:00
|
|
|
#include <random>
|
2024-08-04 15:38:07 +02:00
|
|
|
#include <source_location>
|
|
|
|
#include <stdexcept>
|
2024-08-03 02:42:49 +02:00
|
|
|
|
2024-08-03 23:52:26 +02:00
|
|
|
namespace {
|
|
|
|
|
2024-08-04 15:38:07 +02:00
|
|
|
auto sdl_throw_current_error(const std::source_location location) {
|
|
|
|
throw std::runtime_error{std::format(
|
|
|
|
"error at {}:{}:{} {}", location.file_name(), location.line(),
|
|
|
|
location.column(), SDL_GetError()
|
|
|
|
)};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sdl_check(
|
|
|
|
int errc,
|
|
|
|
const std::source_location location = std::source_location::current()
|
|
|
|
) {
|
|
|
|
if (errc != 0) { sdl_throw_current_error(location); }
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure someone already owns passed sdl_obj, or memory will be leaked
|
|
|
|
auto sdl_check(
|
|
|
|
auto* sdl_obj,
|
|
|
|
const std::source_location location = std::source_location::current()
|
|
|
|
) {
|
|
|
|
if (sdl_obj == nullptr) { sdl_throw_current_error(location); }
|
|
|
|
return sdl_obj;
|
|
|
|
}
|
|
|
|
|
2024-08-03 18:55:58 +02:00
|
|
|
template <typename SDLT, auto deletef>
|
|
|
|
struct sdl_wrapper_t : std::unique_ptr<SDLT, decltype(deletef)> {
|
|
|
|
explicit sdl_wrapper_t(SDLT* p)
|
2024-08-04 15:38:07 +02:00
|
|
|
: std::unique_ptr<SDLT, decltype(deletef)>{p, deletef} {
|
|
|
|
sdl_check(p);
|
|
|
|
}
|
2024-08-03 02:42:49 +02:00
|
|
|
|
2024-08-03 18:55:58 +02:00
|
|
|
operator SDLT*() { return std::unique_ptr<SDLT, decltype(deletef)>::get(); }
|
|
|
|
};
|
2024-08-03 03:23:16 +02:00
|
|
|
|
|
|
|
using window_t = sdl_wrapper_t<SDL_Window, SDL_DestroyWindow>;
|
|
|
|
using surface_t = sdl_wrapper_t<SDL_Surface, SDL_FreeSurface>;
|
|
|
|
using renderer_t = sdl_wrapper_t<SDL_Renderer, SDL_DestroyRenderer>;
|
|
|
|
|
2024-08-03 22:06:57 +02:00
|
|
|
auto operator+(SDL_Point a, SDL_Point b) {
|
|
|
|
return SDL_Point{.x = a.x + b.x, .y = a.y + b.y};
|
|
|
|
}
|
|
|
|
|
2024-08-04 02:07:41 +02:00
|
|
|
// constexpr int width = 1000;
|
|
|
|
// constexpr int height = 1000;
|
|
|
|
constexpr int width = 3440;
|
2024-08-04 15:38:07 +02:00
|
|
|
constexpr int height = 1440;
|
2024-08-03 02:42:49 +02:00
|
|
|
|
2024-08-03 22:40:14 +02:00
|
|
|
template <auto EF>
|
|
|
|
struct [[nodiscard("give this a name so SDL_Quit is called at the end"
|
|
|
|
)]] Defer {
|
|
|
|
Defer() = default;
|
|
|
|
Defer(const Defer&) = delete;
|
|
|
|
Defer(Defer&&) = delete;
|
|
|
|
auto operator=(Defer&&) = delete;
|
|
|
|
auto operator=(const Defer&) = delete;
|
|
|
|
|
|
|
|
~Defer() { EF(); }
|
|
|
|
};
|
|
|
|
|
2024-08-03 23:52:26 +02:00
|
|
|
using clock = std::chrono::steady_clock;
|
|
|
|
|
|
|
|
auto poll_events(bool& continu) {
|
|
|
|
for (SDL_Event e; SDL_PollEvent(&e);) {
|
|
|
|
if (e.type == SDL_QUIT) { continu = false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2024-08-04 15:38:07 +02:00
|
|
|
int main() try {
|
2024-08-04 15:58:14 +02:00
|
|
|
sdl_check(SDL_Init(SDL_INIT_VIDEO));
|
2024-08-03 22:40:14 +02:00
|
|
|
Defer<SDL_Quit> defer_SDL_Quit;
|
|
|
|
|
|
|
|
window_t window{SDL_CreateWindow(
|
|
|
|
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
|
2024-08-04 19:40:28 +02:00
|
|
|
height, SDL_WINDOW_FULLSCREEN
|
2024-08-03 22:40:14 +02:00
|
|
|
)};
|
2024-08-04 19:21:50 +02:00
|
|
|
renderer_t renderer{SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)
|
|
|
|
};
|
|
|
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
|
|
|
|
{
|
|
|
|
// 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]));
|
|
|
|
}
|
2024-08-04 15:38:07 +02:00
|
|
|
}
|
2024-08-04 19:21:50 +02:00
|
|
|
|
|
|
|
// no clue who owns this
|
|
|
|
auto* texture = SDL_CreateTexture(
|
2024-08-05 00:27:10 +02:00
|
|
|
renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, width,
|
2024-08-04 19:21:50 +02:00
|
|
|
height
|
2024-08-03 23:38:19 +02:00
|
|
|
);
|
2024-08-04 19:40:28 +02:00
|
|
|
sdl_check(SDL_SetRenderTarget(renderer, texture));
|
2024-08-05 00:27:10 +02:00
|
|
|
sdl_check(
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, SDL_ALPHA_OPAQUE)
|
|
|
|
);
|
2024-08-04 19:40:28 +02:00
|
|
|
sdl_check(SDL_RenderClear(renderer));
|
2024-08-03 22:40:14 +02:00
|
|
|
|
2024-08-05 00:27:10 +02:00
|
|
|
SDL_Point pos{.x = width / 2, .y = height / 2};
|
2024-08-05 10:55:36 +02:00
|
|
|
SDL_Point pos1{.x = width / 2, .y = height / 2};
|
2024-08-04 15:38:07 +02:00
|
|
|
constexpr SDL_Point directions[] = {
|
2024-08-03 22:40:14 +02:00
|
|
|
{.x = -1, .y = 0}, {.x = 1, .y = 0}, {.x = 0, .y = -1}, {.x = 0, .y = 1}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::mt19937_64 rne(std::random_device{}());
|
|
|
|
std::uniform_int_distribution dist(0, 3);
|
2024-08-05 00:27:10 +02:00
|
|
|
constexpr auto batchsize = 10000;
|
2024-08-04 02:07:41 +02:00
|
|
|
|
2024-08-03 23:52:26 +02:00
|
|
|
const auto start_time = clock::now();
|
|
|
|
auto next_poll_events_time = start_time;
|
2024-08-04 14:57:29 +02:00
|
|
|
auto frame_start = start_time;
|
|
|
|
clock::duration frame_time;
|
2024-08-03 23:06:35 +02:00
|
|
|
for (bool continu = true; continu;) {
|
2024-08-04 15:00:16 +02:00
|
|
|
// measure frame_time
|
|
|
|
const auto now = clock::now();
|
|
|
|
frame_time = now - frame_start;
|
|
|
|
frame_start = now;
|
|
|
|
|
|
|
|
if (now >= next_poll_events_time) {
|
|
|
|
next_poll_events_time = now + std::chrono::milliseconds{200};
|
|
|
|
poll_events(continu);
|
|
|
|
std::println(
|
|
|
|
"last frame_time={} batchsize={}", frame_time, batchsize
|
|
|
|
);
|
2024-08-04 02:07:41 +02:00
|
|
|
}
|
|
|
|
|
2024-08-04 19:40:28 +02:00
|
|
|
std::array<SDL_Point, batchsize> point_batch;
|
2024-08-04 14:57:29 +02:00
|
|
|
for (auto step = 0z; step < batchsize; ++step) {
|
2024-08-03 23:52:26 +02:00
|
|
|
SDL_Point newpoint;
|
|
|
|
do {
|
|
|
|
newpoint = pos + directions[dist(rne)];
|
|
|
|
} while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or
|
|
|
|
newpoint.y >= height);
|
|
|
|
pos = newpoint;
|
2024-08-04 19:40:28 +02:00
|
|
|
point_batch[step] = newpoint;
|
2024-08-03 23:52:26 +02:00
|
|
|
}
|
2024-08-04 19:21:50 +02:00
|
|
|
|
2024-08-05 00:27:10 +02:00
|
|
|
///// Render image
|
|
|
|
sdl_check(SDL_SetRenderTarget(renderer, texture));
|
|
|
|
sdl_check(SDL_SetRenderDrawBlendMode(
|
|
|
|
renderer,
|
|
|
|
SDL_ComposeCustomBlendMode(
|
|
|
|
SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
|
|
|
|
SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ZERO,
|
|
|
|
SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD
|
|
|
|
)
|
|
|
|
));
|
2024-08-05 10:55:36 +02:00
|
|
|
sdl_check(SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 32));
|
2024-08-05 00:27:10 +02:00
|
|
|
sdl_check(SDL_RenderDrawPoints(
|
|
|
|
renderer, point_batch.data(), point_batch.size()
|
|
|
|
));
|
2024-08-05 10:55:36 +02:00
|
|
|
for (auto step = 0z; step < batchsize; ++step) {
|
|
|
|
SDL_Point newpoint;
|
|
|
|
do {
|
|
|
|
newpoint = pos1 + directions[dist(rne)];
|
|
|
|
} while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or
|
|
|
|
newpoint.y >= height);
|
|
|
|
pos1 = newpoint;
|
|
|
|
point_batch[step] = newpoint;
|
|
|
|
}
|
2024-08-05 00:27:10 +02:00
|
|
|
sdl_check(SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 32));
|
2024-08-05 10:55:36 +02:00
|
|
|
sdl_check(SDL_RenderDrawPoints(
|
|
|
|
renderer, point_batch.data(), point_batch.size()
|
|
|
|
));
|
|
|
|
// sdl_check(SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 32));
|
|
|
|
// sdl_check(SDL_RenderFillRect(renderer, nullptr));
|
2024-08-05 00:27:10 +02:00
|
|
|
// SDL_RenderCopy(renderer, fade_texture, nullptr, nullptr);
|
|
|
|
|
|
|
|
///// Render screen
|
2024-08-04 19:21:50 +02:00
|
|
|
sdl_check(SDL_SetRenderTarget(renderer, nullptr));
|
2024-08-05 00:27:10 +02:00
|
|
|
sdl_check(
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE)
|
|
|
|
);
|
|
|
|
SDL_RenderClear(renderer);
|
2024-08-04 20:08:01 +02:00
|
|
|
sdl_check(SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE));
|
2024-08-05 00:27:10 +02:00
|
|
|
// sdl_check(SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE));
|
2024-08-04 19:21:50 +02:00
|
|
|
sdl_check(SDL_RenderCopy(renderer, texture, nullptr, nullptr));
|
|
|
|
SDL_RenderPresent(renderer);
|
2024-08-03 02:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// leaking memory, nothing I can do
|
2024-08-04 15:38:07 +02:00
|
|
|
} catch (const std::exception& e) {
|
2024-08-04 19:21:50 +02:00
|
|
|
|
2024-08-04 15:38:07 +02:00
|
|
|
std::puts(e.what());
|
|
|
|
return EXIT_FAILURE;
|
2024-08-03 02:42:49 +02:00
|
|
|
}
|