Skip to main content
GameDev.net gamedev.net
🔒 Locked

allegro class problem

Started by moon_rabbits Mar 27, 2007 at 2:52 PM 1 replies 750+ views
Original Post
moon_rabbits
moon_rabbits
I am making a game similar to missile defence so that I can become used to Allegro and whatnot. I wrote something up that generates a background and saves it to a bitmap, everything was going good until I added a class in a separate .cpp file.
[source lang = "cpp"]
/********************
 * Tank Class Header*
 ********************/
 
#ifndef _TANK_H 
#define _TANK_H

#define MAX_BULLETS 10
#define MOVE_LEFT 0
#define MOVE_RIGHT 1

#define TANK_W  100
#define TANK_H  35

#define CANNON_W 25
#define CANNON_H 50

class CTank
{
public:
   CTank();
   ~CTank();
   
   int x, y;  // x and y coordinate of the player
   int health; // health of the player
   int reload; // reload time of the player
     
   int score;
     
   BITMAP *tank, *cannon; //bitmaps to hold the tank and cannon
   
   void MoveTank(int dir);
   void FireBullet();
};

#endif

[source lang = "cpp"]
/********************
 * Tank Source File *
 ********************/
 
#include <allegro.h>
#include "tank.h"

CTank::CTank()
/* Sets up tank object */
{
    x = (SCREEN_W/2);  // init player
    y = (SCREEN_H/4);
    health = 100;
    reload = 0;
    score = 0;
    tank = create_bitmap(TANK_W, TANK_H);
    cannon = create_bitmap(CANNON_W, CANNON_H);
     
    rectfill(tank, 10, 0, (TANK_W-10), TANK_H, makecol(45, 200, 60));
    rect(tank, 10, 0, (TANK_W-10), TANK_H, makecol(60, 230, 70));
}
//--------------------------------------------    

CTank::~CTank()
/* Deletes tank bmps */
{
    destroy_bitmap(tank);
    destroy_bitmap(cannon);
}
//--------------------------------------------    

void CTank::MoveTank(int dir)
/* Moves tank */
{
    if((dir == MOVE_LEFT) && (x < 0))
      x -= 1;
    else if((dir == MOVE_RIGHT) && ((x+TANK_W) > (SCREEN_W))
      x += 1;
      
    return;
}
//--------------------------------------------    

void CTank::FireBullet()
{}
//--------------------------------------------    

That's the tank class header and .cpp file. The program compiles fine, but this error is given when it runs: The instruction at "0x10020efc" referenced memory at "0x00000038". The memory could not be "read". Click on OK to terminate the program Click on CANCEL to debug the problem. Help, please?
rherm23
rherm23
im just curious to see if you have another file? like a main.cpp so i can run it
"choices always were a problem for you......" Maynard James Keenan
morx
morx
We would really need to see your main.cpp file in order to fully know what's going on. However, just from the look of it, I would guess that in your main() function that you're doing something like this:

CTank tank;allegro_init();install_keyboard();...tank.MoveTank();...


If you are, the problem is that the tank's constructor is being called before allegro is initialized and so the create_bitmap() functions are returning NULL.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.