hardware rendering again, to persistent texture. Incredibly fast

This commit is contained in:
Jakob Hördt 2024-08-04 19:21:50 +02:00
parent fbc667b321
commit cc58fa1f50

View file

@ -95,25 +95,32 @@ int main() try {
window_t window{SDL_CreateWindow( window_t window{SDL_CreateWindow(
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, "random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
height, 0//SDL_WINDOW_FULLSCREEN height, 0 // SDL_WINDOW_FULLSCREEN
)}; )};
renderer_t renderer{SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)
// owned by window };
auto* window_surface = sdl_check(SDL_GetWindowSurface(window)); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
if (window_surface->format->palette) { sdl_check(SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE));
std::println("unsupported window format, palette"); sdl_check(SDL_RenderClear(renderer));
return EXIT_FAILURE; sdl_check(SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE));
{
// 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(
"got surface with {} bits per pixel", // no clue who owns this
window_surface->format->BitsPerPixel auto* texture = SDL_CreateTexture(
renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, width,
height
); );
if (!(window_surface->format->format & SDL_PIXELFORMAT_RGBA32)) {
std::println("unsupported window format, rgb");
return EXIT_FAILURE;
}
SDL_Point pos{.x = width / 2, .y = height / 2}; SDL_Point pos{.x = width / 2, .y = height / 2};
constexpr SDL_Point directions[] = { constexpr SDL_Point directions[] = {
{.x = -1, .y = 0}, {.x = 1, .y = 0}, {.x = 0, .y = -1}, {.x = 0, .y = 1} {.x = -1, .y = 0}, {.x = 1, .y = 0}, {.x = 0, .y = -1}, {.x = 0, .y = 1}
}; };
@ -123,13 +130,7 @@ int main() try {
// aarrggbb?? // aarrggbb??
constexpr Uint32 bg_color = 0xffdbd7c0; constexpr Uint32 bg_color = 0xffdbd7c0;
constexpr Uint32 walk_color = 0xff0d0f14; constexpr Uint32 walk_color = 0xff0d0f14;
constexpr auto batchsize = 1000; constexpr auto batchsize = 1;
sdl_check(SDL_LockSurface(window_surface));
for (int i = 0; i < window_surface->w * window_surface->h; ++i) {
static_cast<Uint32*>(window_surface->pixels)[i] = bg_color;
}
SDL_UnlockSurface(window_surface);
const auto start_time = clock::now(); const auto start_time = clock::now();
auto next_poll_events_time = start_time; auto next_poll_events_time = start_time;
@ -149,7 +150,7 @@ int main() try {
); );
} }
sdl_check(SDL_LockSurface(window_surface)); sdl_check(SDL_SetRenderTarget(renderer, texture));
for (auto step = 0z; step < batchsize; ++step) { for (auto step = 0z; step < batchsize; ++step) {
SDL_Point newpoint; SDL_Point newpoint;
do { do {
@ -157,15 +158,17 @@ int main() try {
} while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or } while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or
newpoint.y >= height); newpoint.y >= height);
pos = newpoint; pos = newpoint;
static_cast<Uint32*>(window_surface->pixels sdl_check(SDL_RenderDrawPoint(renderer, pos.x, pos.y));
)[pos.x + pos.y * window_surface->w] = walk_color;
} }
SDL_UnlockSurface(window_surface);
sdl_check(SDL_UpdateWindowSurface(window)); sdl_check(SDL_SetRenderTarget(renderer, nullptr));
sdl_check(SDL_RenderCopy(renderer, texture, nullptr, nullptr));
SDL_RenderPresent(renderer);
} }
// leaking memory, nothing I can do // leaking memory, nothing I can do
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::puts(e.what()); std::puts(e.what());
return EXIT_FAILURE; return EXIT_FAILURE;
} }