Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SDL_CreateTexture

Tags:

c++

sdl-2

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?

like image 622
Rokusjar Avatar asked Nov 02 '14 10:11

Rokusjar


People also ask

What does SDL_ querytexture do?

The function SDL_QueryTexture() is used to retrieve the basic settings of a texture, including the format, access, width, and height.

What is a texture in SDL?

As mentioned in the last lesson, textures are the GPU rendering equivalent of surfaces. Hence, textures are almost always created from surfaces, using the function SDL_CreateTextureFromSurface().

What is Sdl_rendercopy?

Copy a portion of the texture to the current rendering target.


1 Answers

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;
}
like image 99
Marco A. Avatar answered Sep 18 '22 22:09

Marco A.