Verán, tengo este proyecto con Allegro en C++ en el que me marca error al utilizar algunos elementos heredados de la clase Game en la clase Test, de la clase Abstracta Actor en AirCraft y Star. Según yo todo esta bien, pero me tira el mensaje de que son referencias indefinidas y no encuentro cual es el problema.
Puede alguien ayudarme a corregir este error? de antemano Gracias.
Les dejo el código completo.
Test.cpp
Actor.h
Actor.cpp
ActorManager.h
ActorManager.cpp
StageManager.h
StageManager.cpp
Game.h
Game.cpp
Puede alguien ayudarme a corregir este error? de antemano Gracias.
Les dejo el código completo.
Test.cpp
Código (cpp) [Seleccionar]
#include <allegro.h>
#include <ctime>
#include <cstdlib>
using namespace std;
#include "Actor.h"
#include "ActorManager.h"
#include "Game.h"
#include "StageManager.h"
class AirCraft : public Actor
{
public:
AirCraft() : Actor(){ image=NULL; };
void draw(BITMAP *bmp);
void update();
void set_image(BITMAP *bmp);
protected:
BITMAP *image;
};
/*AirCraft::AirCraft()
{
image=NULL;
}*/
void AirCraft::draw(BITMAP *bmp)
{
draw_sprite(bmp, image, x,y);
}
void AirCraft::update()
{
if (key[KEY_UP]) y-=2;
if (key[KEY_DOWN]) y+=2;
if (key[KEY_LEFT]) x-=2;
if (key[KEY_RIGHT]) x+=2;
if (x<0) x=0;
if (x>SCREEN_W-image->w) x=SCREEN_W-image->w;
if (y<0) y=0;
if (y>SCREEN_H-image->h) y=SCREEN_H-image->h;
}
void AirCraft::set_image(BITMAP *bmp)
{
image=bmp;
}
//////////////////////////////////////////////
class Star : public Actor
{
public:
Star() : Actor(){ reinit(); };
void update();
void draw(BITMAP *bmp);
protected:
void reinit();
int vy;
};
/*Star::Star()
{
reinit();
}*/
void Star::reinit()
{
x=rand()%SCREEN_W;
y=0;
vy=1+rand()%4;
}
void Star::update()
{
y+=vy;
if (y>SCREEN_H)
{
reinit();
}
}
void Star::draw(BITMAP *bmp)
{
putpixel(bmp,x,y,makecol(255,255,255));
}
/////////////////////////////////////////////////////////////
class Test : public Game
{
public:
//Test() : Game() {};
void main();
};
void Test::main()
{
BITMAP *bmp;
PALETTE tmp;
AirCraft *a=new AirCraft;
Star *star_tmp;
for (int i=0; i<200;i++)
{
star_tmp=new Star;
star_tmp->set_y( rand()%SCREEN_H );
actor_manager->add(star_tmp);
}
bmp = load_bitmap("nave.bmp", tmp);
a->set_image(bmp);
a->set_x(SCREEN_W/2);
a->set_y(SCREEN_H/2);
actor_manager->add(a);
while (!key[KEY_ESC])
update();
save_bitmap("screen.bmp",screen,tmp);
destroy_bitmap(bmp);
}
////////////////////////////////////////////////
int main()
{
Test game;
srand(time(NULL));
game.set_name("Test del Marco de Trabajo");
game.init(GFX_AUTODETECT, 640,480,16);
return 0;
}
Actor.h
Código (cpp) [Seleccionar]
#ifndef ACTOR_H_INCLUDED
#define ACTOR_H_INCLUDED
#include <allegro.h>
class Actor
{
public:
Actor();
virtual ~Actor();
virtual void draw(BITMAP *bmp);
virtual void update();
void set_x(int pos_x) { x=pos_x; }
void set_y(int pos_y) { y=pos_y; }
int get_x() { return x; }
int get_y() { return y; }
protected:
int x, y;
};
#endif // ACTOR_H_INCLUDED
Actor.cpp
Código (cpp) [Seleccionar]
#include "Actor.h"
Actor::Actor()
{
}
Actor::~Actor()
{
}
void Actor::draw(BITMAP *bmp)
{
}
void Actor::update()
{
}
ActorManager.h
Código (cpp) [Seleccionar]
#ifndef ACTORMANAGER_H_INCLUDED
#define ACTORMANAGER_H_INCLUDED
#include <algorithm>
#include <list>
#include <iterator>
using namespace std;
class Actor;
class Game;
class ActorManager
{
public:
ActorManager(Game *g);
~ActorManager();
void add(Actor *a);
void del(Actor *a);
void rewind();
Actor *next();
Actor *current();
void update();
int num_actors();
protected:
Game *game;
list<Actor*> actors;
list<Actor*>::iterator actors_iter;
};
#endif // ACTORMANAGER_H_INCLUDED
ActorManager.cpp
Código (cpp) [Seleccionar]
#include "Actor.h"
#include "ActorManager.h"
ActorManager::ActorManager(Game *g)
{
game=g;
}
ActorManager::~ActorManager()
{
for (actors_iter=actors.begin(); actors_iter!=actors.end(); actors_iter++)
delete (*actors_iter);
}
void ActorManager::add(Actor *a)
{
actors.push_back(a);
}
void ActorManager::del(Actor *a)
{
list<Actor*>::iterator tmp_actors_iter;
tmp_actors_iter=find(actors.begin(), actors.end(), a);
if (tmp_actors_iter!=actors.end())
actors.erase(tmp_actors_iter);
}
void ActorManager::rewind()
{
actors_iter = actors.begin();
}
Actor *ActorManager::next()
{
Actor *tmp_a;
tmp_a = *actors_iter;
if (actors_iter==actors.end()) return NULL;
actors_iter++;
return tmp_a;
}
Actor *ActorManager::current()
{
if (actors_iter==actors.end()) return NULL;
else
return *actors_iter;
}
int ActorManager::num_actors()
{
return actors.size();
}
void ActorManager::update()
{
list<Actor*>::iterator tmp_iter;
for (tmp_iter=actors.begin(); tmp_iter!=actors.end(); tmp_iter++)
(*tmp_iter)->update();
}
StageManager.h
Código (cpp) [Seleccionar]
#ifndef STAGEMANAGER_H_INCLUDED
#define STAGEMANAGER_H_INCLUDED
#include <allegro.h>
class Game;
class StageManager
{
public:
StageManager(Game *g, int w, int h);
~StageManager();
int w();
int h();
void update();
void draw();
protected:
Game *game;
BITMAP *buffer;
int width, height;
};
#endif // STAGEMANAGER_H_INCLUDED
StageManager.cpp
Código (cpp) [Seleccionar]
#include "Game.h"
#include "Actor.h"
#include "ActorManager.h"
#include "StageManager.h"
StageManager::StageManager(Game *g, int w, int h)
{
game=g;
width=w;
height=h;
buffer = create_bitmap(SCREEN_W, SCREEN_H);
}
StageManager::~StageManager()
{
destroy_bitmap(buffer);
}
int StageManager::w()
{
return width;
}
int StageManager::h()
{
return height;
}
void StageManager::update()
{
draw();
}
void StageManager::draw()
{
Actor *tmp;
game->actor_manager->rewind();
clear(buffer);
while ( (tmp=game->actor_manager->next()) != NULL )
{
tmp->draw(buffer);
}
blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
}
Game.h
Código (cpp) [Seleccionar]
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <string>
using namespace std;
class ActorManager;
class StageManager;
class Game
{
public:
Game();
virtual ~Game();
ActorManager *actor_manager;
StageManager *stage_manager;
virtual void init(int gfx_mode, int w, int h, int col);
virtual void main();
void set_name(string name);
string get_name();
protected:
string name;
void update();
int gfx_w,gfx_h;
int colors;
private:
void start();
void shutdown(string message="Gracias por jugar");
void create_actormanager();
void create_stagemanager();
};
#endif // GAME_H_INCLUDED
Game.cpp
Código (cpp) [Seleccionar]
#include <allegro.h>
#include <string>
using namespace std;
#include "ActorManager.h"
#include "StageManager.h"
#include "Game.h"
Game::Game()
{
actor_manager=NULL;
stage_manager=NULL;
}
Game::~Game()
{
}
void Game::init(int gfx_mode, int w, int h, int col)
{
allegro_init();
install_keyboard();
set_color_depth(col);
if (set_gfx_mode(gfx_mode,w, h, 0,0)<0)
{
shutdown("No se pudo inicializar modo grafico");
return;
}
create_actormanager();
create_stagemanager();
start();
}
void Game::shutdown(string message="Gracias por jugar")
{
delete actor_manager;
delete stage_manager;
set_gfx_mode(GFX_TEXT,0,0,0,0);
cout << name << endl;
cout << message << endl;
allegro_exit();
}
void Game::create_actormanager()
{
actor_manager = new ActorManager(this);
}
void Game::create_stagemanager()
{
stage_manager = new StageManager(this, gfx_w, gfx_h);
}
void Game::start()
{
main();
shutdown();
}
void Game::main()
{
while (!key[KEY_ESC]);
}
void Game::set_name(string n)
{
name=n;
}
string Game::get_name()
{
return name;
}
void Game::update()
{
stage_manager->update();
actor_manager->update();
}