Trying to create an SDL texture, just get blank screen

Hello I have got the book SDL game development from amazon and I am trying to learn SDL2 to write games. I have my first game engine up and running but when adding code to try and get a bmp on the screen on lines 8-15 in Game.cpp I just get a blank screen when I run. Have I got the texture creation code in the right place in the init function. Please can someone help me so I can progress further through the book. Thanks.

main.cpp

#include <SDL2/SDL.h>
#include “Game.h”
#include “Game.cpp”

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

g_game->init(“Chapter 1”, 100,100,640,480,false);

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

return 0;

}

Game.cpp

#include “Game.h”
#include

bool Game::init(const char* title, int xpos, int ypos, int width,
int height, bool fullscreen)
{

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;	

int flags = 0;

if (fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}

// attempt to initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout<<"SDL Init Success\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) // render init success
	{
	std::cout<<"Renderer Creation Success\n";
	SDL_SetRenderDrawColor(m_pRenderer, 0,0,0,0);
	}	
	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;

}

void Game::renderer()
{
SDL_RenderClear(m_pRenderer); // clears the renderer
// to the draw colour

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

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

void Game::update()
{
}

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

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

Game.h

#ifndef Game
#define Game

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

bool init(const char* title, int xpos, int ypos, int width,
int height, bool fullscreen);
void renderer();
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_h) */

Set out breakpoints in your code to check if everything is created successfully. For example, after the creation of the SDL_Surface, after the creation of the SDL_Texture etc. The image file might not be found, which causes the surface to be invalid, which causes the texture to be invalid and so on.
If the SDL_Surface and SDL_Texture are successfully created, check that the source rectangle and the destination rectangle are containing correct data.

Thanks I will try this.

Btw, I now realize that you’re attempting to execute SDL-specific functions before SDL is initialized. You need to initialize SDL before doing any SDL-specific operations and before executing any SDL-specific functions.

So my recommendation is that, at the startup of the program, create everything in the following order:

  1. Initialize SDL by exeuting SDL_Init();
  2. Create the SDL window by executing SDL_CreateWindow();
  3. Create the SDL renderer by executing SDL_CreateRenderer();
  4. Create the texture(s) and other data.

Don’t forget to do error checking when creating stuff. SDL_GetError(); will help you a lot.

Note: when pasting code into a post here on the forum, you need to highlight all your code and then embed it into a code block by pressing the </> button. By doing this, the whole code is embedded into a code block and will be much easier to read.

Hi, thanks for your help managed to get it to work by putting the image creation code after SetRenderDrawColor.

Thanks for your help bro.