SDL/C++ 渲染多个纹理



于是,我拿起了 SDL,开始编写一个简单的游戏。当我尝试使用SDL_RenderCopy将两个纹理绘制到屏幕上时,我感到非常困惑。重要的部分是在while(!quit)中,其中有两个SDL_RenderCopy调用。出于某种原因,只有第二个在屏幕上画了一些东西。这是我的代码:

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 720;
enum TEXTURES{
    T_GRASS,
    T_ALL
};
bool init();
bool loadMedia(const int texture, char* path);
void close();
SDL_Texture* loadTexture(char* path);
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* textures[] = {NULL};

int main(int argc, char* args[]){
    if(!init())
        printf("Failed to initialize!n");
    else
        if(!loadMedia(T_GRASS, "images/tGrass.png"))
            printf("Failed to load media!n");
        else{
            bool quit = false;
            SDL_Event e;
            while(!quit){
                while(SDL_PollEvent(&e) != 0)
                    if(e.type == SDL_QUIT)
                        quit = true;
                SDL_RenderClear(renderer);
                SDL_Rect pos;
                pos.h = 16;
                pos.w = 16;
                pos.x = 0;
                pos.y = 0;
                SDL_Rect pos1;
                pos1.h = 16;
                pos1.w = 16;
                pos.x = 16;
                pos.y = 16;
                SDL_RenderCopy(renderer,textures[T_GRASS],NULL,&pos);
                SDL_RenderCopy(renderer,textures[T_GRASS],NULL,&pos1);
                SDL_RenderPresent(renderer);
            }
        }
    close();
    return 0;
}
bool init(){
    bool success = true;
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        printf("SDL could not initialize! Error: %sn", SDL_GetError());
        success = false;
    }else{
        if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
            printf("WARNING: Linear texture filtering not enabled!n");
        window = SDL_CreateWindow("openZ", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if(window == NULL){
            printf("Window could not be created! Error: %sn", SDL_GetError());
            success = false;
        }else{
            renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
            if(renderer == NULL){
                printf("Render could not be created! Error: %sn", SDL_GetError());
                success = false;
            }else{
                SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
                int imgFlags = IMG_INIT_PNG;
                if(!(IMG_Init(imgFlags) & imgFlags)){
                    printf("SDL Image could not initialize! Error: %sn", SDL_GetError());
                    success = false;
                }
            }
        }
    }
    return success;
}
bool loadMedia(const int texture, char* path){
    bool success = true;
    textures[texture] = loadTexture(path);
    if(textures[texture] == NULL){
        printf("Failed to load texture image %s!n", path);
        success = false;
    }
    return success;
}
void close(){
    for(int i = 0; i < T_ALL; i++){
        SDL_DestroyTexture(textures[i]);
        textures[i] = NULL;
    }
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    window = NULL;
    renderer = NULL;
    IMG_Quit();
    SDL_Quit();
}
SDL_Texture* loadTexture(char* path){
    SDL_Texture* newTexture = NULL;
    SDL_Surface* loadedSurface = IMG_Load(path);
    if(loadedSurface == NULL){
        printf("Unable to load image %s! Error: %sn", path, IMG_GetError());
    }else{
        newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
        if(newTexture == NULL)
            printf("Unable to create texture from %s! Error: %sn", path, SDL_GetError());
        SDL_FreeSurface(loadedSurface);
    }
    return newTexture;
}

对不起!我的坏..原来我有 2 个错别字,使两个纹理相互渲染。我写道:

SDL_Rect pos;
pos.h = 16;
pos.w = 16;
pos.x = 0;
pos.y = 0;
SDL_Rect pos1;
pos1.h = 16;
pos1.w = 16;
pos.x = 16;
pos.y = 16;

它应该在哪里:

SDL_Rect pos;
pos.h = 16;
pos.w = 16;
pos.x = 0;
pos.y = 0;
SDL_Rect pos1;
pos1.h = 16;
pos1.w = 16;
pos1.x = 16;
pos1.y = 16;

^^最后几行引用了错误的矩形。

最新更新