【问题标题】:C++ SDL2 how to color individual tilesC ++ SDL2如何为单个图块着色
【发布时间】:2017-10-02 13:23:24
【问题描述】:

我正在通过编写经典的 roguelike 来学习 C++ 和 SDL2。现在我通过渲染像这样的tiles.png 图像的一部分来构建地图:

我关注了lazyfoo's tiling tutorial,它可以工作,但我希望能够更改每个磁贴的背景和前景色。我可以通过this other tutorial 之类的操作来更改完整的纹理颜色,但是如果我想要,比如说,某处的棕色门和其他地方的灰色门怎么办?

这里最好的方法是什么?显然,我不能将数百种颜色组合存储在 png 中。我应该为每个图块创建一个纹理,还是有更好的方法?

谢谢! :)

【问题讨论】:

    标签: c++ colors sdl-2 tile


    【解决方案1】:

    SDL-2 意味着您使用 opengl 来渲染图块。使用混合和颜色材料。使用SDL_SetRenderDrawColorSDL_SetRenderDrawBlendModeSDL_SetTextureBlendModeSDL_SetTextureColorModSDL_SetTextureAlphaMod。 例如画黄色字母:

    SDL_SetTextureColorMod(renderer, 255, 255, 0);
    

    要绘制不同的背景,您需要使用带有 alpha 通道的字母。首先,您需要在出现文本的位置绘制背景,然后绘制文本本身。 例如:

    //load surface with alpha-channel here
    SDL_SetRenderDrawColor(renderer, 0, 0, 255); //set blue background
    //draw rect for background here
    SDL_SetTextureColorMod(renderer, 255, 255, 0); //set yellow letters
    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
    //draw text here
    

    或 cheeting 版本(如果您不想使用 alpha 混合):

    SDL_SetRenderDrawColor(renderer, 0, 0, 255); //set blue background
    //draw rect for background here
    SDL_SetTextureColorMod(renderer, 255, 255, 0); //set yellow letters
    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
    //draw text here
    

    【讨论】:

    • 谢谢,我会试试的。但是SDL_SetTextureColorMod不会影响整个纹理吗?
    • 是的。整个质地变黄。实际上不是纹理,而是调制颜色。如果您想要标记文本(彩色),您需要编写自己的文本渲染器来定义您需要的文本颜色(可能通过分析标签)并使用 sdl_settexturecolormod 切换纹理颜色。文本只是具有不同纹理坐标的彩色矩形的序列。
    • 好的,成功了。首先我使用SDL_SetRenderDrawColor 设置平铺背景,然后我使用SDL_RenderFillRect。使用SDL_SetTextureColorMod 设置前景色,最后使用SDL_RenderCopySDL_RenderPresent 渲染所有内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多