hardware rendering again, to persistent texture. Incredibly fast
This commit is contained in:
parent
fbc667b321
commit
cc58fa1f50
1 changed files with 30 additions and 27 deletions
55
main.cpp
55
main.cpp
|
@ -95,23 +95,30 @@ 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)
|
||||||
|
};
|
||||||
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
|
||||||
|
sdl_check(SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE));
|
||||||
|
sdl_check(SDL_RenderClear(renderer));
|
||||||
|
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]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// owned by window
|
// no clue who owns this
|
||||||
auto* window_surface = sdl_check(SDL_GetWindowSurface(window));
|
auto* texture = SDL_CreateTexture(
|
||||||
if (window_surface->format->palette) {
|
renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, width,
|
||||||
std::println("unsupported window format, palette");
|
height
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
std::println(
|
|
||||||
"got surface with {} bits per pixel",
|
|
||||||
window_surface->format->BitsPerPixel
|
|
||||||
);
|
);
|
||||||
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[] = {
|
||||||
|
@ -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;
|
||||||
}
|
}
|
Loading…
Reference in a new issue