Reputation: 565
I am trying to load multiple texture images in my SDL app but I cannot load or render the second image on the screen. All I see is my grass.bmp being displayed. Cannot load the Bob.bmp. I have no idea where the error could be as I have implemented everything properly
My header file
#include <SDL.h>
#include <SDL_image.h>
#ifndef GAMEWINDOW_H
#define GAMEWINDOW_H
class GameWindow{
private:
bool _running;
SDL_Window* _screen;
SDL_Renderer* _renderer;
SDL_Texture* _grassTexture;
SDL_Texture* _bobTexture;
SDL_Rect _grassRect;
SDL_Rect _bobRect;
......
my .cpp file
void GameWindow::LoadSprites(){
_grassTexture = IMG_LoadTexture(_renderer,"grass.bmp");
_grassRect.x = 0;
_grassRect.y = 0;
_grassRect.w = 600;
_grassRect.h = 500;
_bobTexture = IMG_LoadTexture(_renderer,"bob.bmp");
_bobRect.x = 150;
_bobRect.y = 150;
_bobRect.w = 80;
_bobRect.y = 50;
}
void GameWindow::Initialize(){
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
_running = false;
}
void GameWindow::SetupScreen(){
_screen = SDL_CreateWindow("My Game Window",
100,
100,
640, 480,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if(_screen == NULL){
_running = false;
}
_renderer = SDL_CreateRenderer(_screen,-1,SDL_RENDERER_ACCELERATED);
GameWindow::LoadSprites();
}
void GameWindow::Update(){
}
void GameWindow::Render(){
SDL_RenderClear(_renderer);
SDL_RenderCopy(_renderer,_bobTexture,NULL,&(_bobRect));
SDL_RenderCopy(_renderer,_grassTexture,NULL,&(_grassRect));
SDL_RenderPresent(_renderer);
}
All i see is the grass.bmp. I cannot render the second image. The image is fine. I tried debugging with the same grass.bmp in place of bob.bmp but it still does not show. Only the first image gets rendered and the second does not!! Hope someone could spot the error. I had no problems in SDL 1.2 but after switching to 2.0 its creating a whole lot of errors and confusion!!
Upvotes: 2
Views: 1896
Reputation: 44921
Ok, you've got two issues.
First. Look really hard at the last line of the _bobRect
initialization. Guess what value the _bobRect.h
parameter will have? (hint, it's 0 :)
_bobRect.x = 150;
_bobRect.y = 150;
_bobRect.w = 80;
_bobRect.y = 50;
Second, since you copy _grassTexture
on top of _bobTexture
and it's larger it will hide _bobTexture
unless it's transparent. So change the order so the background gets copied first like this
SDL_RenderCopy(_renderer,_grassTexture,NULL,&(_grassRect));
SDL_RenderCopy(_renderer,_bobTexture,NULL,&(_bobRect));
Then you should be fine.
Upvotes: 3