I am starting to use SLD and SDL_image I have searched for error information and in most cases it is due to a bad configuration with the linker in visual studio 2012, however I have used the solutions shown, but they do not affect it. I present the code the errors that come out at the time of compiling are:
Error 1 error LNK2019: símbolo externo "public: __thiscall Game::Game(void)" (??0Game@@QAE@XZ) sin resolver al que se hace referencia en la función _SDL_main C:\Users\Miguel G. Pelagio\documents\visual studio 2012\Projects\SDL_game\SDL_game\main.obj SDL_game
Error 2 error LNK1120: 1 externos sin resolver C:\Users\Miguel G. Pelagio\documents\visual studio 2012\Projects\SDL_game\Debug\SDL_game.lib SDL_game
If someone went through a similar situation or understands why the problem can help me to move forward with my study at SDL.
main.ccp
#include<SDL.h>
#include "Game.h"
#include <Windows.h>
//our Game object
Game* g_game=0;
int main(int argc,char* argv[])
{
AllocConsole ();
g_game= new Game();
g_game->init ("Chapter 2",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_
#include "SDL.h"
#include "TextureManager.h"
class Game
{
public:
Game();
~Game();
//simply the Running variable to true
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 acces the private running variable
bool running(){return m_bRunning;}
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
int m_currentFrame;
TextureManager m_textureManager;
bool m_bRunning;
};
#endif/*define(_Game_)*/
Game.ccp
#include "Game.h"
#include "TextureManager.h"
#include <iostream>
#include <SDL_image.h>
using namespace std;
bool Game::init(const char* title,int xpos,int ypos,int width ,int height,bool fullscreen)
{
//attempt to initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING )==0)
{
int flags=0;
if(fullscreen )
{
flags=SDL_WINDOW_FULLSCREEN ;
}
std::cout<<"SDL init success\n"<<endl;
//init the window
m_pWindow=SDL_CreateWindow (title,xpos,ypos,width,height,flags);
//Window init success
if(m_pWindow !=0)
{
std::cout <<"window creation success\n"<<std::endl;
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);
m_textureManager.load("C:/Users/Miguel G. Pelagio/Pictures/pruebas SDL/animate-alpha.png","animate",m_pRenderer);
}
else
{
std::cout<<"renderer init fail\n";
}
}
else
{
std::cout<<"Window init fail\n";
}
}
else
{
std::cout<<"SDL init fail\n";
}
std::cout<<"init success\n";
m_bRunning =true;//everything initied successfully, start the main loop
return true;
}
void Game::render()
{
SDL_RenderClear (m_pRenderer );//clear the renderer to draw color
m_textureManager.draw("animate",0,0,128,82,m_pRenderer,SDL_FLIP_NONE);
m_textureManager.drawFrame("animate",100,100,128,82,1,m_currentFrame,m_pRenderer,SDL_FLIP_NONE);
SDL_RenderPresent (m_pRenderer );
}
void Game::handleEvents ()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT :
m_bRunning =false;
break;
default:
break;
}
}
}
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();
}
TextureManager.h
#ifndef _Game1_
#define _Game1_
#include <SDL.h>
#include <iostream>
#include <map>
#include <string>
using namespace std;
class TextureManager
{
public:
bool load(std::string fileName,std::string id,SDL_Renderer* pRenderer);
void draw(std::string id,int x,int y,int width,int height,SDL_Renderer* pRenderer,SDL_RendererFlip flip);
void drawFrame(std::string id,int x,int y,int width,int heigh,int concurrentRow,int concurrentFrame,SDL_Renderer* pRenderer,SDL_RendererFlip flip);
private:
std::map<std::string,SDL_Texture*> m_textureMap;
};
#endif/*define(_Game_)*/
TextureManager.ccp
#include "TextureManager.h"
#include <iostream>
#include <SDL_image.h>
using namespace std;
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;
}
SDL_Texture* pTexture=SDL_CreateTextureFromSurface(pRenderer,pTempSurface);
SDL_FreeSurface(pTempSurface);
//everything went ok, add the texture to our map
if(pTexture!=0)
{
m_textureMap[id]=pTexture;
return true;
}
//reaching here means somthing 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_FLIP_NONE)
{
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 heigh,int concurrentRow,int concurrentFrame,SDL_Renderer* pRenderer,SDL_RendererFlip flip=SDL_FLIP_NONE)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x=width*concurrentFrame;
srcRect.y=heigh*(concurrentRow-1);
srcRect.w=destRect.w=width;
srcRect.h=destRect.h=heigh;
destRect.x=x;
destRect.y=y;
SDL_RenderCopyEx(pRenderer,m_textureMap[id],&srcRect,&destRect,0,0,flip);
}