Can anybody tell me why the animation is not appering on the screen


#1

Hi I have my own game engine up and running from the book SDL Game Programming, However when completing the end of chapter 2 and turning the texture manager class into a singleton (TextureManager.h-see private constructor line 33) and adding a load and draw function (Game.cpp line 57 and 90) the animation does not appear on screen, can anyone tell me what I have to change in the code to get the animation on screen, Thanks.

main.cpp

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include <string>
#include <map>
#include "TextureManager.h"
#include "Game.h"
#include "TextureManager.cpp"
#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->render();
}
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 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_Surface * pTempSurface;

int m_currentFrame;

bool m_bRunning;
};


#endif /* defined(__Game_h__) */

Game.cpp


#include "Game.h"

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);			       
		}
		else
		{
		std::cout<<"Window Init Fail\n";
		return false; //window init fail
		}

		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, 255,0,0,255);
			}	
			else
			{
			std::cout<<"Renderer Init Fail\n";
			return false; // renderer init fail
			}



	}

	else
	{
	std::cout<<"SDL Init Fail\n";
	return false; // SDL init fail
	}

	
	
		//to load					
		if(!TheTextureManager::Instance()->load("assets/animate-alpha.png","animate", m_pRenderer))
		{
		return false;
		}
	


	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_RenderPresent(m_pRenderer); // draw to the screen
}

void Game::render()
{

SDL_RenderClear(m_pRenderer);

SDL_RenderPresent(m_pRenderer);

//to draw
TheTextureManager::Instance()->draw("animate",0,0, 128, 82, m_pRenderer);

}

void Game::update()
{
m_currentFrame = 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;
		}
	}
}


TextureManager.h

```c++

#ifndef __TextureManager__
#define __TextureManager__

class TextureManager
{
public:
TextureManager(){}

bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer);

//draw
void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);

// drawframe
void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);

std::map<std::string, SDL_Texture*> m_textureMap;

static TextureManager *s_pInstance;

static TextureManager* Instance()
{
	if(s_pInstance == 0)
	{
	s_pInstance = new TextureManager();
	return s_pInstance;
	}

return s_pInstance;
}

private:
~TextureManager(){}

};

typedef TextureManager TheTextureManager;

#endif /* defined(__TextureManager_h__) */

TextureManager.cpp


#include "TextureManager.h"

TextureManager* TextureManager::s_pInstance = 0;

bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer)
{
SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());

if(pTempSurface == 0)
{
return false;
printf("IMG_Load Error %s\n", SDL_GetError());
}
else
{
std::cout <<"IMG_Load Success!\n";
}

SDL_Texture* pTexture =
SDL_CreateTextureFromSurface(pRenderer, pTempSurface);

SDL_FreeSurface(pTempSurface);

// everything went ok, add texture to our list
if(pTexture !=0)
{
m_textureMap[id] = pTexture;
return true;
}

// reaching here means something went wrong
return false;
}

void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;

srcRect.x =0;
srcRect.y =0;
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x =x;
destRect.y =y;

SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip);
}

void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow,
int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = width * currentFrame;
srcRect.y = height * (currentRow -1);
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x = x;
destRect.y = y;

SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0,0, flip);
}


#2

SDL_RenderClear(m_pRenderer);
SDL_RenderPresent(m_pRenderer);
TheTextureManager::Instance()->draw(“animate”,0,0, 128, 82, m_pRenderer);

You need to move TheTextureManager in between clear and present.


#3

Hi tried that and it shows a still picture of the dog, any idea why it is not moving?

Thanks.


#4

draw(“animate”,0,0, 128, 82, m_pRenderer);

You pass the same values for the x y w and h.
I think you need to re read the book as I reckon you missed something.

Or maybe you should be calling the draw frame function.


#5

Hi I added the drawframe function and iv’e got it working, thanks for your help.


#6

Can you run it?
I have make same mistake like you and then I found the solution. Here you are:
if (!TheTextureManager::Instance()->load(“assets/animate-alpha.png”, “animate”, m_pRenderer))
{
return false;
}
This part is in the end of Game::init before

std::cout << “Init success\n”;
m_bRunning = true;

Then this part

SDL_RenderClear(m_pRenderer); //clear the renderer to the draw color
TheTextureManager::Instance()->draw(“animate”, 250, 100, 128, 82, m_pRenderer);
// This code just print out the first frame in the picture.
TheTextureManager::Instance()->drawFrame(“animate”, 250, 200, 128, 82, 1, m_currentFrame, m_pRenderer); // This code to run our animate
SDL_RenderPresent(m_pRenderer); // draw to the screen


#7

Moreover in TextureManager.h
comment this line of your code
~TextureManager(){}