2

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!!

Htlcs
  • 545
  • 3
  • 13
  • 26
  • What does SDL_RenderCopy/Load_Texture return? If they dont succeed what does SDL_GetError() tell you? – Hjorthenify Mar 10 '14 at 09:09
  • I am sorry but how do I display errors? I have not debugged with a windows application before. Where and how do I see the log if there are any errors? – Htlcs Mar 10 '14 at 09:34
  • This is weird problem. I tried everything. Only the very first texture is loaded and displayed on the screen be it any image. The second image be it whatever image never gets loaded successfully ! – Htlcs Mar 10 '14 at 09:35
  • Avoid using underscores in front of member names when coding in C++, those are reserved for the implementation! see here: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – Profan Mar 10 '14 at 09:37
  • @JimZilla either write it to a file or allocate a console to do display the errors. Also SDL comes with a logging header https://wiki.libsdl.org/CategoryLog – Hjorthenify Mar 10 '14 at 10:08

1 Answers1

3

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.

jpw
  • 44,361
  • 6
  • 66
  • 86
  • @Hjorthenify Took me a while too, was about to start line by line debugging when I saw it :p – jpw Mar 10 '14 at 11:21
  • Holy mother of GOD. Thank you so much! I was like what in this world is wrong with my program? Everything seemed right and I could not spot that one stupid mistake. I was about to switch back to SDL 1.2, I almost spent an hour trying to locate the error hahahaha. Thanks again! – Htlcs Mar 10 '14 at 18:40