diff --git a/main.cpp b/main.cpp index 5c6d88b..2a37db2 100644 --- a/main.cpp +++ b/main.cpp @@ -95,25 +95,32 @@ int main() try { window_t window{SDL_CreateWindow( "random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, - height, 0//SDL_WINDOW_FULLSCREEN + height, 0 // SDL_WINDOW_FULLSCREEN )}; - - // owned by window - auto* window_surface = sdl_check(SDL_GetWindowSurface(window)); - if (window_surface->format->palette) { - std::println("unsupported window format, palette"); - return EXIT_FAILURE; + 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])); + } } - std::println( - "got surface with {} bits per pixel", - window_surface->format->BitsPerPixel + + // no clue who owns this + 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[] = { {.x = -1, .y = 0}, {.x = 1, .y = 0}, {.x = 0, .y = -1}, {.x = 0, .y = 1} }; @@ -123,13 +130,7 @@ int main() try { // aarrggbb?? constexpr Uint32 bg_color = 0xffdbd7c0; constexpr Uint32 walk_color = 0xff0d0f14; - constexpr auto batchsize = 1000; - - sdl_check(SDL_LockSurface(window_surface)); - for (int i = 0; i < window_surface->w * window_surface->h; ++i) { - static_cast(window_surface->pixels)[i] = bg_color; - } - SDL_UnlockSurface(window_surface); + constexpr auto batchsize = 1; const auto start_time = clock::now(); 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) { SDL_Point newpoint; do { @@ -157,15 +158,17 @@ int main() try { } while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or newpoint.y >= height); pos = newpoint; - static_cast(window_surface->pixels - )[pos.x + pos.y * window_surface->w] = walk_color; + sdl_check(SDL_RenderDrawPoint(renderer, pos.x, pos.y)); } - 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 } catch (const std::exception& e) { + std::puts(e.what()); return EXIT_FAILURE; } \ No newline at end of file