将纹理转换为Uint32数组

时间:2018-11-27 13:51:45

标签: c++ graphics sdl-2 sdl-ttf

是否可以将包含文本的纹理转换为Uint32数组,而无需将文本拉伸到窗口的尺寸?我需要检查相邻像素,并且我只知道如何使用Uint32数组。该代码大部分是从教程中复制的,我在游戏循环中添加了注释,这些注释显示了我通常如何使用Uint32数组渲染纹理。

#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
using namespace std;

int main(int argc, char ** argv)
{
    bool quit = false;
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();

    SDL_Window * window = SDL_CreateWindow("Merry 
    Christmas", SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED, 1000, 1000, 0);

    SDL_Renderer * renderer = SDL_CreateRenderer(window,-1, SDL_RENDERER_PRESENTVSYNC);

    TTF_Font * font = TTF_OpenFont("/home/Michael/.fonts/NewAmsterdam.ttf", 100);
    const char * error = TTF_GetError();
    SDL_Color color = { 255, 255, 255 };
    SDL_Surface * surface = TTF_RenderText_Solid(font,
    "Merry Christmas", color);
    SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface);
    int texW = 0;
    int texH = 0;
    SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
    SDL_Rect dstrect = { 200, 400, texW, texH };

    while (!quit)
    {
        SDL_WaitEvent(&event);

        switch (event.type)
        {
            case SDL_QUIT:
            quit = true;
            break;
        }

        SDL_RenderCopy(renderer, texture, NULL, &dstrect);
        SDL_RenderPresent(renderer);

        // SDL_UpdateTexture(texture, NULL, buffer, SCREEN_WIDTH * sizeof(Uint32));
        // SDL_RenderClear(renderer);
        // SDL_RenderCopy(renderer, texture, NULL, NULL);
        // SDL_RenderPresent(renderer);
    }

    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);
    TTF_CloseFont(font);

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    TTF_Quit();
    SDL_Quit();

    return 0;
}

0 个答案:

没有答案