SDL_LoadBMP not working

Hi I,m going through the book SDL Game Development by Shaun Mitchell, I got through the 1st chapter fine beginning building the game engine, now in chapter 2 it introduces SDL_LoadBMP, I have included it in my code which compiles but I cant get it to actually do anything, why? is there any code I need to move around in the init function in Game.cpp?

Please help.
Thanks.

main.cpp

#include "Game.cpp"

//our game object
Game* g_game = 0;

int main(int argc, char* argv[])
{
g_game = new Game();

g_game->init("Chapter 1",100,100,640,480,SDL_WINDOW_SHOWN);

while(g_game->running())
{
g_game->handleEvents();
//g_game->update();
g_game->render();
}
g_game->clean();

return 0;
}

Game.h

#ifndef __Game__
#define __Game__

#include<SDL2/SDL.h>

class Game
{
public:
Game() {}
~Game() {}

// simply set the running variable to true
bool init(const char* title, int xpos, int ypos, int width,
    int height, int flags);

void render();
void update();
void handleEvents();
void clean();

// a function to access the private running variable
bool running() { return m_bRunning; }

private:

SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;

SDL_Texture* m_pTexture; // the new SDL_Texture variable
SDL_Rect m_sourceRectangle; // the first rectangle
SDL_Rect m_destinationRectangle; // another rectangle
bool m_bRunning;
};

#endif /* defined(__Game__) */

Game.cpp

#include "Game.h"
#include<iostream>

    bool Game::init(const char* title, int xpos, int ypos, int width,
    int height, int flags)
    {
		m_bRunning = true; 
		//attempt to initialize SDL
			if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
			{
			std::cout<<"SDL init sucess\n";
            //init the window
            m_pWindow = SDL_CreateWindow(title, xpos, ypos,
            width, height, flags);
				if(m_pWindow !=0) // window init success
				{
				std::cout << "window creation success\n";
				m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
    
					if(m_pRenderer !=0) // renderer init success
					{
					std::cout << "renderer creation success\n";
					SDL_SetRenderDrawColor(m_pRenderer,
					0,0,0,255);
					}
					else
					{
					std::cout<<"renderer init fail\n";
					return false; // renderer init fail
					}
				}
				else
				{
				std::cout <<"window init fail\n";
				return false; // window init fail
				}
			}
			else
			{
			std::cout<<"SDL init fail\n";
			return false; // SDL init fail
			}

		std::cout <<"init success\n";
		m_bRunning= true; // everything inited successfully,
		//start the main loop

		return true;
		
		SDL_Surface* pTempSurface = SDL_LoadBMP("assets/rider.bmp");
		
		m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurface);
		
		SDL_FreeSurface(pTempSurface);
		
		SDL_QueryTexture(m_pTexture, NULL, NULL,
		&m_sourceRectangle.w, &m_sourceRectangle.h);
		
		m_destinationRectangle.x = m_sourceRectangle.x = 0;
		m_destinationRectangle.y = m_sourceRectangle.y = 0;
		m_destinationRectangle.w = m_sourceRectangle.w;
		m_destinationRectangle.h = m_sourceRectangle.h;
		}

void Game::render()
{
SDL_RenderClear(m_pRenderer); // clear the renderer to the
// draw color

SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle,
&m_destinationRectangle);

SDL_RenderPresent(m_pRenderer); // draw to the screen
}

void Game::handleEvents()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
    switch (event.type)
    {
    case SDL_QUIT:
        m_bRunning = false;
    break;

    default:
    break;
}
}
}


void Game::clean()
{
std::cout<<"cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}

In your Game::init() function, you’re returning too early from the function. Right after you set m_bRunning to true, you’re executing return true, which means that the code leaves the function, the m_pTexture object isn’t created, and then in the Game::render() function, the texture isn’t rendered at all, since it’s invalid. Your m_pTexture object isn’t initialized to nullptr in the startup of the program and not created properly either, which makes the SDL_RenderCopy() function return -1, which it does when an error occurs.

So just move the return true to the very end of the function, after the texture creation code.

1 Like