首页 > 解决方案 > SDL best way to draw gradient background

问题描述

I'm trying to draw gradient (Both horizontal & vertical) background, problem is this causes high cpu usage. Size of a gradient paint is 32767x32767 but of course i'm only drawing based on screen width/height and on offset (starting point of painting). As far as i know i cant put such big painting in memory so paint should be created dynamically. I am using this code right now (on 1600x900 it uses 10-20% of cpu, without gradient paint (looping SDL_RenderDrawLine) it's just 0%):

    SDL_SetRenderDrawColor(Renderer, 59, 156, 156, 255);
    SDL_RenderClear(Renderer);

    for (int X = 0; X < Width; X++)
    {
        SDL_SetRenderDrawColor(Renderer, 75, 0, 130, X * 255 / 0xfff);
        SDL_RenderDrawLine(Renderer, X, 0, X, Height);
    }

    for (int Y = 0; Y < Height; Y++)
    {
        SDL_SetRenderDrawColor(Renderer, 75, 0, 130, Y * 255 / 0xfff);
        SDL_RenderDrawLine(Renderer, 0, Y, Width, Y);
    }

Is there better way to solve this problem ?

标签: c++sdl

解决方案


推荐阅读