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

标签 c sdl-2

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

我尝试再次使用 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);
}
}

最佳答案

如果你只想刷新屏幕的一部分,你可以使用viewports
来自 lazyfoo.net 的引文说明一切的教程:

Some times you only want to render part of the screen for things like minimaps. Using the viewport you can control where you render on the screen.

参见 SDL_RenderGetViewportSDL_RenderSetViewport有关如何设置和获取与给定渲染关联的视口(viewport)的更多详细信息。

基本思想是用户可以设置要渲染的矩形,坐标将相对于矩形。如果您只想更新纹理而不刷新整个窗口,只需将其放入视口(viewport)即可。

关于c - 使用纹理和渲染器仅更新屏幕的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44831129/

相关文章:

c - 目标文件和可执行文件的反汇编差异

c - 尝试编译 valkyrie(valgrind 的 GUI) 并抛出错误 :

c++ - glewInit() 失败,出现 "Missing GL version",SDL2 OpenGL 上下文,cygwin 编译器

c - 在 C 中获取屏幕分辨率,w/o "windows.h"

c - 访问单个结构成员是否会将整个结构拉入缓存?

c - 无法写入引用的内存

c - 卡在 C 指针结构中

sdl-2 - 在 SDL2 中混合纹理

c++ - 是否可以将 SDL2 与智能指针一起使用?

c++ - 是否可以将 SDL2 嵌入到应用程序中而不将其构建为库?