First Attempt at sprite animation just producing a blank screen

Hi I bought the book SDL game development from amazon to learn SDL2 game programming. Iv’e got my first game engine skeleton up and running but I’m having a few problems with the sprite animation section of the book. It tells me to remove the QueryTexture function form the game loop code as I’m setting my own sizes for the animation BMP rectangle but all I get when I run it is a blank screen. Have I got the code in the right order? Am I missing a function?
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.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) */

Game.cpp

#include "Game.h" #include

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

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
		}

SDL_Surface * pTempSurface = SDL_LoadBMP("assets/animate.bmp");
	if(pTempSurface !=0)
{
std::cout<<"LoadBMP Success!\n";
}
else
{
printf("LoadBMP failed: %s\n", SDL_GetError());
}
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,pTempSurface);
	if(m_pTexture !=0)
{
std::cout<<"CreateTextureFromSurface Success!\n";
}
else
{
printf("CreateTextureFromSurface failed: %s\n", SDL_GetError());
}	
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;	
m_sourceRectangle.w=128;
m_sourceRectangle.h=82;	
}

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()
{
m_sourceRectangle.x = 128 * int (((SDL_GetTicks() / 100) % 6));
}

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

SDL_Rect m_destinationRectangle;// another rectangle

This is the most important rectangle when drawing.
Destination Rect is the location and size of your sprite / rect.

You have not set this rectangles values therefore you are not seeing it.
If something has a width and height of 0 you will not see it.

You need to set the width and height values to
m_destinationRectangle.w = 128
m_destinationRectangle.h = 82

You many also need to set the x and y to be inside the window dimensions.

Source Rect is used for getting a portion of the full texture.
You set the width and height for how much you want to show.

Please use wrap your code in code blocks in the future, its very difficult to see whats happening and people tend not to help if you are not going to help them view your code with ease.

Example

#include <iostream>
int main() {
    std::cout << "Hello World!\n";
    return 0;
}

You don’t set the source rectangle and the destination rectangle, which means that their fields (x, y, w and h) might contain garbage values. This means that your texture might be rendered off-screen.
So check the rectangles and their values.

Hi I gave it some more checking and I’ve got it working, thanks guys