Hi I tried to introduce some inheritance to my game engine from the book SDL Game Development but I am getting a lot of errors. I am a real noob at c++ so I would appreciate a bit of detail about each error and what I can do to fix it, as far as I can tell the code is exactly the same as in the book on chapter 3 “Working with game objects” Please tell me what I am doing wrong to produce these errors and how I can fix them. Thanks.
g++ main.cpp -fpermissive -lSDL2 -lSDL2_image
In file included from main.cpp:7:0:
GameObject.h: In member function ‘void Player::draw()’:
GameObject.h:30:18: error: no matching function for call to ‘Player::draw()’
GameObject::draw()
^
GameObject.h:8:6: note: candidate: void GameObject::draw(SDL_Renderer*)
void draw(SDL_Renderer* pRenderer);
^~~~
GameObject.h:8:6: note: candidate expects 1 argument, 0 provided
In file included from main.cpp:8:0:
Player.h: At global scope:
Player.h:4:7: error: redefinition of ‘class Player’
class Player : public GameObject
^~~~~~
In file included from main.cpp:7:0:
GameObject.h:25:7: note: previous definition of ‘class Player’
class Player : public GameObject
^~~~~~
In file included from main.cpp:11:0:
Game.cpp: In member function ‘void Game::render()’:
Game.cpp:84:11: error: no matching function for call to ‘GameObject::draw()’
m_go.draw();
^
In file included from main.cpp:7:0:
GameObject.h:8:6: note: candidate: void GameObject::draw(SDL_Renderer*)
void draw(SDL_Renderer* pRenderer);
^~~~
GameObject.h:8:6: note: candidate expects 1 argument, 0 provided
In file included from main.cpp:11:0:
Game.cpp:86:26: error: no matching function for call to ‘Player::draw(SDL_Renderer*&)’
m_player.draw(m_pRenderer);
^
In file included from main.cpp:7:0:
GameObject.h:28:6: note: candidate: void Player::draw()
void draw()
^~~~
GameObject.h:28:6: note: candidate expects 0 arguments, 1 provided
In file included from main.cpp:13:0:
Player.cpp: At global scope:
Player.cpp:1:77: error: no ‘void Player::load(int, int, int, int, std::__cxx11::string)’ member function declared in class ‘Player’
void Player::load(int x, int y, int width, int height, std::string textureID)
^
Player.cpp:6:6: error: prototype for ‘void Player::draw(SDL_Renderer*)’ does not match any in class ‘Player’
void Player::draw(SDL_Renderer* pRenderer)
^~~~~~
In file included from main.cpp:7:0:
GameObject.h:28:6: error: candidate is: void Player::draw()
void draw()
^~~~
In file included from main.cpp:13:0:
Player.cpp:11:6: error: redefinition of ‘void Player::update()’
void Player::update()
^~~~~~
In file included from main.cpp:7:0:
GameObject.h:35:6: note: ‘void Player::update()’ previously defined here
void update()
main.cpp
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include <string>
#include <map>
#include "TextureManager.h"
#include "GameObject.h"
#include "Player.h"
#include "Game.h"
#include "TextureManager.cpp"
#include "Game.cpp"
#include "GameObject.cpp"
#include "Player.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();
SDL_Delay(10);
}
g_game->clean();
return 0;
}
TextureManager.h
#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
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);
}
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 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;
};
GameObject m_go;
Player m_player;
#endif /* defined(__Game_h__) */
Game.cpp
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;
}
m_go.load(100,100,128,82, "animate");
m_player.load(300,300,128,82, "animate");
std::cout<<"Init success\n";
m_bRunning = true; // everything inited successfully
// start the main loop
return true;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer);
//to draw
//TheTextureManager::Instance()->draw("animate",0,0, 128, 82, m_pRenderer);
//to animate
TheTextureManager::Instance()->drawFrame("animate",100,100,128,82,1,m_currentFrame, m_pRenderer);
m_go.draw();
m_player.draw(m_pRenderer);
SDL_RenderPresent(m_pRenderer);
}
void Game::update()
{
m_currentFrame = int(((SDL_GetTicks() / 100) % 6));
m_go.update();
m_player.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;
}
}
}
GameObject.h
#ifndef __GameObject__
#define __GameObject__
class GameObject
{
public:
void load(int x, int y, int width, int height, std::string textureID);
void draw(SDL_Renderer* pRenderer);
void update();
void clean();
protected:
std::string m_textureID;
int m_currentFrame;
int m_currentRow;
int m_x;
int m_y;
int width;
int height;
int m_width;
int m_height;
};
class Player : public GameObject
{
public:
void draw()
{
GameObject::draw()
std::cout<<"draw player\n";
}
void update()
{
std::cout<<"update player\n";
m_x=10;
m_y=20;
}
void clean()
{
GameObject::clean();
std::cout<<"clean object\n";
}
};
#endif /* defined(__GameObject_h__) */
GameObject.cpp
void GameObject::load(int x, int y, int width, int height, std::string textureID)
{
m_x = x;
m_y = y;
m_width = width;
m_height = height;
m_textureID = textureID;
m_currentRow = 1;
m_currentFrame = 1;
}
void GameObject::draw(SDL_Renderer* pRenderer)
{
TextureManager::Instance()->drawFrame(m_textureID, m_x, m_y, m_width, m_height, m_currentRow,
m_currentFrame, pRenderer);
}
void GameObject::update()
{
m_x += 1;
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
class Player : public GameObject
{
public:
void load(int x, int y, int width, int height, std::string name, std::string textureID);
void draw(SDL_Renderer* pRenderer);
void update();
void clean();
};
#endif // PLAYER_H
Player.cpp
void Player::load(int x, int y, int width, int height, std::string textureID)
{
GameObject::load(x,y, width, height, textureID);
}
void Player::draw(SDL_Renderer* pRenderer)
{
GameObject::draw(pRenderer);
}
void Player::update()
{
m_x -=1;
}