4 Replies - 891 Views - Last Post: 24 August 2011 - 08:57 AM Rate Topic: -----

#1 litedrive   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Allegro Not Working Properly

Posted 22 August 2011 - 08:58 AM

I have the following code:

GameSetup::GameSetup()
{
    this->x = 10;
    this->y = 10;
}


GameSetup::~GameSetup()
{
    delete &x;
    delete &y;
    
    std::cout << "this object is being destroyed..." << std::endl;
}

void GameSetup::initialize()
{
    allegro_init();
    install_keyboard();
    set_gfx_mode(GFX_AUTODETECT, 
                 SCREEN_WIDTH, 
                 SCREEN_HEIGHT, 
                 0, 0);
    
    while(!key[KEY_ESC])
    {
        
        clear_keybuf();
        
        acquire_screen();
        
        textout_ex(screen, font, " ", x, y, makecol(0, 0, 0), makecol(0, 0, 0));
        
        if (key[KEY_UP]) --y;
        else if (key[KEY_DOWN]) ++y;
        else if (key[KEY_LEFT]) --x;
        else if (key[KEY_RIGHT]) ++x;
        
        textout_ex(screen, font, "hello world!", x, y, makecol(0, 255, 0), makecol(0, 0, 0));
        
        release_screen();
        rest(50);
        
    }
}
 
int GameSetup::get_x() const
{
    return this->x;
}

int GameSetup::get_y() const
{
    return this->y;
}


And here's what I have in main:

int main()
{
    {
        GameSetup * game;

        
        game->initialize();
    }
    
    
    return 0;
} 


It's nothing complicated; just the 'Hello World' version of Allegro which allows the user to move text around the screen with the arrow keys. The problem is that if I try to do this the object oriented way, nothing really works. The program compiles, and the screen loads, but that's it. No text. And when I move the arrow keys, the program exits.

Any idea why this is happening?

Is This A Good Question/Topic? 0
  • +

Replies To: Allegro Not Working Properly

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Allegro Not Working Properly

Posted 22 August 2011 - 09:13 AM

My opinion? You're not ready to start developing a game. Sorry to be so blunt, but you need to have at least intermediate C++ down before you start down this path.

GameSetup * game;


That's a raw pointer to some memory you do not own. As soon as you access it in the next line, you've engaged in undefined behavior. You need to allocate memory and construct an actual instance before you use it.

GameSetup * game = new GameSetup();
...
// do whatever
delete game;
return 0;


GameSetup::~GameSetup()
{
    delete &x;
    delete &y;
    
    std::cout << "this object is being destroyed..." << std::endl;
}


NO. Absolutely not.

Please, read and practice some more before you start mucking about with games. You're just not ready.
Was This Post Helpful? 0
  • +
  • -

#3 litedrive   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Allegro Not Working Properly

Posted 22 August 2011 - 09:19 AM

Ok. What's the proper way to destruct such an object, and would a text-based RPG be a better route to go you think?

Edit:

I'll rephrase that question: what are some resources you know of off hand which could help me in this regard?

This post has been edited by litedrive: 22 August 2011 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Allegro Not Working Properly

Posted 22 August 2011 - 05:11 PM

Quote

I'll rephrase that question: what are some resources you know of off hand which could help me in this regard?
Accelerated C++ by Koenig and C++ Primer by Lippman.

You seriously need to go through the basics. C++ is not a language suitable for guessing your way through code. Don't take it personally, you just need to understand the fundamental constructs properly.
Was This Post Helpful? 0
  • +
  • -

#5 hulla   User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 733
  • Joined: 05-March 11

Re: Allegro Not Working Properly

Posted 24 August 2011 - 08:57 AM

For free information, try learncpp.com, cplusplus.com tutorial and the tutorials on this website.

This post has been edited by hulla: 24 August 2011 - 08:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1