2016-06-05 2 views
0

Итак, я взял SDL и начал писать простую игру. Есть некоторая проблема, с которой я очень смущен, когда пытаюсь нарисовать две текстуры на экране с помощью SDL_RenderCopy. Важная часть находится внутри while (! Quit), где есть два вызова SDL_RenderCopy. По какой-то причине только второй рисует что-то на экране. Вот мой код:SDL/C++ Рендеринг нескольких текстур

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

ответ

0

Извините! Мой плохой ... получается, у меня было 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; 

^^ последний тоже строки были ссылки на неправильный прямоугольник.

Смежные вопросы