【问题标题】:SDL/C++ Rendering multiple texturesSDL/C++ 渲染多个纹理
【发布时间】:2016-06-05 17:24:30
【问题描述】:

所以,我选择了 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: %s\n", 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: %s\n", SDL_GetError());
            success = false;
        }else{
            renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
            if(renderer == NULL){
                printf("Render could not be created! Error: %s\n", 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: %s\n", 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: %s\n", path, IMG_GetError());
    }else{
        newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
        if(newTexture == NULL)
            printf("Unable to create texture from %s! Error: %s\n", path, SDL_GetError());
        SDL_FreeSurface(loadedSurface);
    }
    return newTexture;
}

【问题讨论】:

    标签: c++ opengl graphics sdl


    【解决方案1】:

    对不起!我的坏..原来我有 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;
    

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

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多