仅使用纹理和渲染器更新屏幕的一部分?

时间:2017-06-29 17:03:09

标签: c sdl-2

我试图以表格的形式在纹理中显示char数组。它运行良好,但现在我想动态修改数组中的一些char(表中的值)而不重新加载整个渲染器。

我再次尝试使用SDL_RenderClearSDL_RenderPresent,但它清除了整个窗口。

如何只选择表格的一部分并更改它而不修改其余部分?

void draw_battlefield(t_env *e, int origin_x, int origin_y, int width, int height)
{
int j;
float x;
float y;
SDL_Color color = {255, 0, 0, 0};
SDL_Surface *surface;
SDL_Texture *texture;
int texW = 0;
int texH = 0;
SDL_Rect dstrect;

j = -1;
x = (float)origin_x;
y = origin_y;
while (++j < 4096)
{
    surface = TTF_RenderText_Solid(e->font_nb, base((unsigned char)e->battlefield[j]), color);
    texture = SDL_CreateTextureFromSurface(e->renderer, surface);
    texW = 0;
    texH = 0;
    SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
    dstrect = fill_rect((int)x, (int)y, &texW, &texH);
    x += ((float)width / 64);
    SDL_RenderCopy(e->renderer, texture, NULL, &dstrect);
    if ((j & 0x3f) == 0x3f)
    {
        x = (float)origin_x;
        y += ((float)height / 64);
    }
    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);
}
}

1 个答案:

答案 0 :(得分:0)

如果您只想刷新屏幕的一部分,可以使用视口
来自lazyfoo.net教程的引用只是说了所有内容:

  

有些时候,您只想渲染部分屏幕,例如小地图。使用视口,您可以控制在屏幕上呈现的位置。

有关如何设置和获取与给定渲染相关联的视口的详细信息,请参阅SDL_RenderGetViewportSDL_RenderSetViewport

基本思想是用户可以设置要渲染的矩形,并且坐标将相对于矩形。如果您只想更新纹理而不刷新整个窗口,请将其放在视口中,这就是全部。

相关问题