2016-06-05 8 views
0

Also, ich nahm SDL und begann ein einfaches Spiel zu schreiben. Es gibt ein Problem, über das ich sehr verwirrt bin, wenn ich versuche, mit SDL_RenderCopy zwei Texturen auf den Bildschirm zu zeichnen. Der wichtige Teil ist innerhalb der while (! Quit), wo zwei SDL_RenderCopy-Aufrufe sind. Aus irgendeinem Grund zeichnet nur der zweite etwas auf dem Bildschirm. Hier ist mein Code:SDL/C++ Rendern mehrerer Texturen

#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; 
} 

Antwort

0

Entschuldigung! Meine schlechte .. stellt sich heraus, dass ich zwei Tippfehler hatte, die die zwei Texturen übereinander rendern ließen. Ich schrieb:

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; 

, wo es sein sollte:

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; 

^^ letzten zu Linien das falsche Rechteck wurden verweisen.