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