elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Problema con Juego en Allegro
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problema con Juego en Allegro  (Leído 1,552 veces)
SNP

Desconectado Desconectado

Mensajes: 3


Ver Perfil
Problema con Juego en Allegro
« en: 17 Junio 2013, 04:48 am »

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
Código
  1. #include <allegro.h>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. #include "Actor.h"
  7. #include "ActorManager.h"
  8. #include "Game.h"
  9. #include "StageManager.h"
  10.  
  11.  
  12.  
  13. class AirCraft : public Actor
  14. {
  15. public:
  16.    AirCraft() : Actor(){ image=NULL; };
  17.    void draw(BITMAP *bmp);
  18.    void update();
  19.    void set_image(BITMAP *bmp);
  20. protected:
  21.    BITMAP *image;
  22. };
  23.  
  24.  
  25. /*AirCraft::AirCraft()
  26. {
  27.     image=NULL;
  28. }*/
  29.  
  30.  
  31. void AirCraft::draw(BITMAP *bmp)
  32. {
  33.    draw_sprite(bmp, image, x,y);
  34. }
  35.  
  36.  
  37. void AirCraft::update()
  38. {
  39.    if (key[KEY_UP])    y-=2;
  40.    if (key[KEY_DOWN])  y+=2;
  41.    if (key[KEY_LEFT])  x-=2;
  42.    if (key[KEY_RIGHT]) x+=2;
  43.    if (x<0)            x=0;
  44.    if (x>SCREEN_W-image->w) x=SCREEN_W-image->w;
  45.    if (y<0)            y=0;
  46.    if (y>SCREEN_H-image->h) y=SCREEN_H-image->h;
  47. }
  48.  
  49.  
  50. void AirCraft::set_image(BITMAP *bmp)
  51. {
  52.    image=bmp;
  53. }
  54.  
  55.  
  56.  
  57. //////////////////////////////////////////////
  58. class Star : public Actor
  59. {
  60. public:
  61.    Star() : Actor(){ reinit(); };
  62.    void update();
  63.    void draw(BITMAP *bmp);
  64.  
  65. protected:
  66.    void reinit();
  67.    int vy;
  68. };
  69.  
  70.  
  71. /*Star::Star()
  72. {
  73.     reinit();
  74. }*/
  75.  
  76.  
  77. void Star::reinit()
  78. {
  79.    x=rand()%SCREEN_W;
  80.    y=0;
  81.    vy=1+rand()%4;
  82. }
  83.  
  84.  
  85.  
  86. void Star::update()
  87. {
  88.    y+=vy;
  89.    if (y>SCREEN_H)
  90.    {
  91.        reinit();
  92.    }
  93. }
  94.  
  95.  
  96. void Star::draw(BITMAP *bmp)
  97. {
  98.    putpixel(bmp,x,y,makecol(255,255,255));
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105. /////////////////////////////////////////////////////////////
  106. class Test : public Game
  107. {
  108. public:
  109.    //Test() : Game() {};
  110.    void main();
  111. };
  112.  
  113.  
  114. void Test::main()
  115. {
  116.    BITMAP *bmp;
  117.    PALETTE tmp;
  118.    AirCraft *a=new AirCraft;
  119.    Star *star_tmp;
  120.  
  121.    for (int i=0; i<200;i++)
  122.    {
  123.        star_tmp=new Star;
  124.        star_tmp->set_y( rand()%SCREEN_H );
  125.        actor_manager->add(star_tmp);
  126.    }
  127.  
  128.    bmp = load_bitmap("nave.bmp", tmp);
  129.    a->set_image(bmp);
  130.    a->set_x(SCREEN_W/2);
  131.    a->set_y(SCREEN_H/2);
  132.    actor_manager->add(a);
  133.  
  134.    while (!key[KEY_ESC])
  135.           update();
  136.  
  137.    save_bitmap("screen.bmp",screen,tmp);
  138.    destroy_bitmap(bmp);
  139. }
  140.  
  141.  
  142. ////////////////////////////////////////////////
  143. int main()
  144. {
  145.    Test game;
  146.    srand(time(NULL));
  147.    game.set_name("Test del Marco de Trabajo");
  148.    game.init(GFX_AUTODETECT, 640,480,16);
  149.  
  150.    return 0;
  151. }
  152.  


Actor.h
Código
  1. #ifndef ACTOR_H_INCLUDED
  2. #define ACTOR_H_INCLUDED
  3.  
  4. #include <allegro.h>
  5.  
  6.  
  7. class Actor
  8. {
  9.    public:
  10.        Actor();
  11.        virtual ~Actor();
  12.  
  13.        virtual void draw(BITMAP *bmp);
  14.        virtual void update();
  15.        void set_x(int pos_x) { x=pos_x; }
  16.        void set_y(int pos_y) { y=pos_y; }
  17.        int get_x() { return x; }
  18.        int get_y() { return y; }
  19.  
  20.    protected:
  21.        int x, y;
  22. };
  23.  
  24.  
  25.  
  26. #endif // ACTOR_H_INCLUDED
  27.  
  28.  


Actor.cpp
Código
  1. #include "Actor.h"
  2.  
  3. Actor::Actor()
  4. {
  5. }
  6.  
  7.  
  8. Actor::~Actor()
  9. {
  10. }
  11.  
  12.  
  13. void Actor::draw(BITMAP *bmp)
  14. {
  15. }
  16.  
  17.  
  18. void Actor::update()
  19. {
  20. }
  21.  


ActorManager.h
Código
  1. #ifndef ACTORMANAGER_H_INCLUDED
  2. #define ACTORMANAGER_H_INCLUDED
  3.  
  4. #include <algorithm>
  5. #include <list>
  6. #include <iterator>
  7. using namespace std;
  8.  
  9. class Actor;
  10. class Game;
  11.  
  12. class ActorManager
  13. {
  14.    public:
  15.        ActorManager(Game *g);
  16.        ~ActorManager();
  17.  
  18.        void add(Actor *a);
  19.        void del(Actor *a);
  20.        void rewind();
  21.        Actor *next();
  22.        Actor *current();
  23.        void update();
  24.        int num_actors();
  25.  
  26.    protected:
  27.        Game *game;
  28.  
  29.        list<Actor*> actors;
  30.        list<Actor*>::iterator actors_iter;
  31. };
  32.  
  33. #endif // ACTORMANAGER_H_INCLUDED
  34.  
  35.  


ActorManager.cpp
Código
  1. #include "Actor.h"
  2. #include "ActorManager.h"
  3.  
  4.  
  5. ActorManager::ActorManager(Game *g)
  6. {
  7.    game=g;
  8. }
  9.  
  10.  
  11. ActorManager::~ActorManager()
  12. {
  13.    for (actors_iter=actors.begin(); actors_iter!=actors.end(); actors_iter++)
  14.            delete (*actors_iter);
  15. }
  16.  
  17.  
  18. void ActorManager::add(Actor *a)
  19. {
  20.    actors.push_back(a);
  21. }
  22.  
  23.  
  24. void ActorManager::del(Actor *a)
  25. {
  26.    list<Actor*>::iterator tmp_actors_iter;
  27.  
  28.    tmp_actors_iter=find(actors.begin(), actors.end(), a);
  29.    if (tmp_actors_iter!=actors.end())
  30.            actors.erase(tmp_actors_iter);
  31.  
  32. }
  33.  
  34.  
  35. void ActorManager::rewind()
  36. {
  37.    actors_iter = actors.begin();
  38. }
  39.  
  40.  
  41. Actor *ActorManager::next()
  42. {
  43.        Actor *tmp_a;
  44.  
  45.        tmp_a = *actors_iter;
  46.        if (actors_iter==actors.end()) return NULL;
  47.        actors_iter++;
  48.        return tmp_a;
  49. }
  50.  
  51.  
  52. Actor *ActorManager::current()
  53. {
  54.    if (actors_iter==actors.end()) return NULL;
  55.    else
  56.            return *actors_iter;
  57. }
  58.  
  59.  
  60. int ActorManager::num_actors()
  61. {
  62.    return actors.size();
  63. }
  64.  
  65.  
  66. void ActorManager::update()
  67. {
  68.    list<Actor*>::iterator tmp_iter;
  69.    for (tmp_iter=actors.begin(); tmp_iter!=actors.end(); tmp_iter++)
  70.           (*tmp_iter)->update();
  71. }
  72.  
  73.  


StageManager.h
Código
  1. #ifndef STAGEMANAGER_H_INCLUDED
  2. #define STAGEMANAGER_H_INCLUDED
  3.  
  4. #include <allegro.h>
  5.  
  6. class Game;
  7.  
  8. class StageManager
  9. {
  10.    public:
  11.        StageManager(Game *g, int w, int h);
  12.        ~StageManager();
  13.        int w();
  14.        int h();
  15.        void update();
  16.        void draw();
  17.  
  18.    protected:
  19.        Game *game;
  20.        BITMAP *buffer;
  21.        int width, height;
  22. };
  23.  
  24. #endif // STAGEMANAGER_H_INCLUDED
  25.  
  26.  


StageManager.cpp
Código
  1. #include "Game.h"
  2. #include "Actor.h"
  3. #include "ActorManager.h"
  4. #include "StageManager.h"
  5.  
  6. StageManager::StageManager(Game *g, int w, int h)
  7. {
  8.    game=g;
  9.    width=w;
  10.    height=h;
  11.    buffer = create_bitmap(SCREEN_W, SCREEN_H);
  12. }
  13.  
  14.  
  15. StageManager::~StageManager()
  16. {
  17.    destroy_bitmap(buffer);
  18. }
  19.  
  20.  
  21. int StageManager::w()
  22. {
  23.    return width;
  24. }
  25.  
  26.  
  27. int StageManager::h()
  28. {
  29.    return height;
  30. }
  31.  
  32.  
  33. void StageManager::update()
  34. {
  35.    draw();
  36. }
  37.  
  38.  
  39. void StageManager::draw()
  40. {
  41.    Actor *tmp;
  42.    game->actor_manager->rewind();
  43.    clear(buffer);
  44.    while ( (tmp=game->actor_manager->next()) != NULL )
  45.    {
  46.        tmp->draw(buffer);
  47.    }
  48.    blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
  49. }
  50.  
  51.  
  52.  


Game.h
Código
  1. #ifndef GAME_H_INCLUDED
  2. #define GAME_H_INCLUDED
  3.  
  4. #include <string>
  5. using namespace std;
  6.  
  7. class ActorManager;
  8. class StageManager;
  9.  
  10. class Game
  11. {
  12.    public:
  13.        Game();
  14.        virtual ~Game();
  15.        ActorManager *actor_manager;
  16.        StageManager *stage_manager;
  17.        virtual void init(int gfx_mode, int w, int h, int col);
  18.        virtual void main();
  19.        void set_name(string name);
  20.        string get_name();
  21.  
  22.    protected:
  23.        string name;
  24.        void update();
  25.        int gfx_w,gfx_h;
  26.        int colors;
  27.  
  28.    private:
  29.        void start();
  30.        void shutdown(string message="Gracias por jugar");
  31.        void create_actormanager();
  32.        void create_stagemanager();
  33. };
  34.  
  35. #endif // GAME_H_INCLUDED
  36.  
  37.  


Game.cpp
Código
  1. #include <allegro.h>
  2. #include <string>
  3. using namespace std;
  4.  
  5. #include "ActorManager.h"
  6. #include "StageManager.h"
  7.  
  8. #include "Game.h"
  9.  
  10. Game::Game()
  11. {
  12.    actor_manager=NULL;
  13.    stage_manager=NULL;
  14. }
  15.  
  16.  
  17. Game::~Game()
  18. {
  19. }
  20.  
  21.  
  22. void Game::init(int gfx_mode, int w, int h, int col)
  23. {
  24.    allegro_init();
  25.    install_keyboard();
  26.    set_color_depth(col);
  27.    if (set_gfx_mode(gfx_mode,w, h, 0,0)<0)
  28.    {
  29.        shutdown("No se pudo inicializar modo grafico");
  30.        return;
  31.    }
  32.    create_actormanager();
  33.    create_stagemanager();
  34.    start();
  35. }
  36.  
  37.  
  38. void Game::shutdown(string message="Gracias por jugar")
  39. {
  40.    delete actor_manager;
  41.    delete stage_manager;
  42.    set_gfx_mode(GFX_TEXT,0,0,0,0);
  43.    cout << name << endl;
  44.    cout << message << endl;
  45.    allegro_exit();
  46. }
  47.  
  48.  
  49. void Game::create_actormanager()
  50. {
  51.    actor_manager = new ActorManager(this);
  52. }
  53.  
  54.  
  55. void Game::create_stagemanager()
  56. {
  57.    stage_manager = new StageManager(this, gfx_w, gfx_h);
  58. }
  59.  
  60.  
  61. void Game::start()
  62. {
  63.    main();
  64.    shutdown();
  65. }
  66.  
  67.  
  68. void Game::main()
  69. {
  70.    while (!key[KEY_ESC]);
  71. }
  72.  
  73.  
  74. void Game::set_name(string n)
  75. {
  76.    name=n;
  77. }
  78.  
  79.  
  80. string Game::get_name()
  81. {
  82.    return name;
  83. }
  84.  
  85.  
  86. void Game::update()
  87. {
  88.    stage_manager->update();
  89.    actor_manager->update();
  90. }
  91.  


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Juego Mario bros Allegro
Programación C/C++
mancilla_almona 0 5,079 Último mensaje 28 Junio 2010, 02:17 am
por mancilla_almona
Problema con un code de Allegro « 1 2 »
Programación C/C++
vojok 10 6,118 Último mensaje 25 Julio 2010, 18:55 pm
por vojok
Problema con allegro (marcianitos c++)
Programación C/C++
lluk 5 4,069 Último mensaje 15 Mayo 2011, 23:23 pm
por ssaammuu
problema vectores, allegro. « 1 2 3 »
Programación C/C++
MasterPunk 20 10,523 Último mensaje 14 Enero 2012, 02:32 am
por SirLanceCC
Problema al instalar librería Allegro 5
GNU/Linux
eleon 0 2,663 Último mensaje 14 Junio 2012, 16:37 pm
por eleon
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines