How to use SDL_CreateTexture

扶醉桌前 提交于 2019-12-04 09:48:14

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