Skip to content

Optimize text rendering, particle draw state, and scene loop hot paths#47

Draft
alegemaate with Copilot wants to merge 4 commits into
mainfrom
copilot/implement-performance-fixes
Draft

Optimize text rendering, particle draw state, and scene loop hot paths#47
alegemaate with Copilot wants to merge 4 commits into
mainfrom
copilot/implement-performance-fixes

Conversation

Copilot AI commented Jul 3, 2026

Copy link
Copy Markdown

This change targets the highest-frequency rendering and scene-management paths without changing public behavior. It removes repeated work in text drawing, particle rendering, and scene update/draw loops, while keeping cache invalidation aligned with existing SDL resource ownership.

  • Text rendering

    • Cache rendered text textures by (renderer, font, text, color) instead of recreating a surface/texture on every draw.
    • Add explicit text-cache clearing so cached textures are released before renderer/font teardown.
    • Preserve the existing draw::text(...) API.
  • Text measurement / UI layout

    • Cache get_text_size(font, text) results to avoid repeating SDL_ttf layout work for stable UI strings.
    • Reuse this cache across existing callers such as buttons and other UI text paths.
    • Clear measurement cache when font caches are unloaded.
  • Particle rendering

    • Remove per-particle alpha reset churn for textured particles.
    • Only update texture alpha when the interpolated alpha actually changes, then restore default alpha once after the emitter draw pass.
  • Scene update/draw

    • Stop compacting dead objects every frame; only run cleanup after something is actually marked dead.
    • Stop sorting scene objects unconditionally every frame; sort only when insertion/order changes or current ordering is no longer valid.
    • Reserve capacity before batched object insertion to avoid avoidable vector growth during scene creation bursts.
  • Frame loop overhead

    • Reuse a single now() timestamp in managed scene loops instead of reading the clock multiple times per frame.

Example of the main rendering-path change:

TextCacheKey cache_key { r, font, text, pack_color(color) };
auto cached_text = text_cache.find(cache_key);
if (cached_text == text_cache.end()) {
    SDL_Surface* textSurface = TTF_RenderText_Blended(font.get(), text.c_str(), 0, sdlColor);
    SDL_Texture* textTexture = SDL_CreateTextureFromSurface(r, textSurface);
    cached_text = text_cache.emplace(cache_key, TextCacheEntry {
        make_cached_texture(textTexture),
        textSurface->w,
        textSurface->h,
    }).first;
}

SDL_RenderTexture(r, cached_text->second.texture.get(), nullptr, &dest);

Copilot AI changed the title [WIP] Implement performance-oriented fixes in AdsGames/asw Optimize text rendering, particle draw state, and scene loop hot paths Jul 3, 2026
Copilot AI requested a review from alegemaate July 3, 2026 14:11
@sonarqubecloud

sonarqubecloud Bot commented Jul 3, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants