How to use SDL_CreateTexture

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 18:34:30

问题


I would like to know ho to use SDL_CreateTexture function.
I just want to create texture, give it a color and draw it somewhere on the screen. I dont wanna load any picture into it.
I thought I can use SDL_CreateTexture, SDL_SetTextureColorMod, SDL_RenderCopy, SDL_RenderPresent in that order, but I always get just a black rectangle instead of red one.

#include <SDL.h>

int main(int argc, char* argv[]) {

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window *MainWindow = SDL_CreateWindow("My Game Window",
                                  SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
                                  1024, 768,
                                  SDL_WINDOW_SHOWN
                                  );

    SDL_Renderer *renderer = SDL_CreateRenderer(MainWindow, -1, 0);

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    SDL_RenderClear(renderer);


    SDL_Texture *Tile = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_TARGET,8,8);

    //I want my texture to be red
    SDL_SetTextureColorMod(Tile,255,0,0);

    //I just try this i dont know if I have to do that
    SDL_SetTextureAlphaMod(Tile,255);

    SDL_Rect destination = {320,240,8,8};
    SDL_RenderCopy(renderer,Tile,NULL,&destination);
    SDL_RenderPresent(renderer);
    SDL_Delay(3000);

    //Clean up
    SDL_DestroyTexture(Tile);
    SDL_DestroyWindow(MainWindow);
    SDL_Quit();

    return 0;
}


I also dont know if I am using right format (SDL_PixelFormatEnum) value in SDL_CreateTexture. There is a lot of them and I dont understand what they mean. Which one can I use form this purpose?


回答1:


SDL_SetTextureColorMod will make sure subsequent render copy operations will have the specified multiplier taken into account. It doesn't change color to the texture texels.

You should rather either load a bitmap or initialize your texture with a red color as in the following example

int main(int argc, char* argv[]) {

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window *MainWindow = SDL_CreateWindow("My Game Window",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        1024, 768,
        SDL_WINDOW_SHOWN
        );

    SDL_Renderer *renderer = SDL_CreateRenderer(MainWindow, -1, 0);

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    SDL_RenderClear(renderer);

    SDL_Texture *Tile = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
                        SDL_TEXTUREACCESS_STREAMING, 8, 8);

    // Initialize texture pixels to a red opaque RGBA value
    unsigned char* bytes = nullptr;
    int pitch = 0;
    SDL_LockTexture(Tile, nullptr, reinterpret_cast<void**>(&bytes), &pitch);
    unsigned char rgba[4] = { 255, 0, 0, 255 };
    for(int y = 0; y < 8; ++y) {
        for (int x = 0; x < 8; ++x) {
            memcpy(&bytes[(y * 8 + x)*sizeof(rgba)], rgba, sizeof(rgba));
        }
    }
    SDL_UnlockTexture(Tile);

    SDL_Rect destination = { 320, 240, 8, 8 };
    SDL_RenderCopy(renderer, Tile, NULL, &destination);
    SDL_RenderPresent(renderer);
    SDL_Delay(3000);

    //Clean up
    SDL_DestroyTexture(Tile);
    SDL_DestroyWindow(MainWindow);
    SDL_Quit();

    return 0;
}


来源:https://stackoverflow.com/questions/26698378/how-to-use-sdl-createtexture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!