Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Crashes because of class

Credits go to Elverion and Jonatan Hedborg for helping out!
This thread is locked; no one can reply to it. rss feed Print
Crashes because of class
moon_rabbits
Member #8,469
March 2007
avatar

I have a class called TerrainClass, and ever since I added it to my program it has caused errors at runtime.

Here's the terrain.h and terrain.cpp files:

terrain.h

1/* Kelly Crawford 21 May 2007
2 Terrain header file */
3
4#include <allegro.h>
5 
6class TerrainClass{
7public:
8
9 TerrainClass();
10 ~TerrainClass();
11
12 void PaintTerrain(BITMAP *buffer);
13
14private:
15
16 BITMAP *bmp;
17};
18//------------------------------------------------------

Terrain.cpp

1/* Kelly Crawford 21 May 2007
2 Terrain source file */
3
4#include "terrain.h"
5 
6TerrainClass::TerrainClass()
7/* Creates terrain from min to max with amount of hills */
8{
9 bmp = create_bitmap(200, 200);
10
11}
12//------------------------------------------------------
13 
14TerrainClass::~TerrainClass()
15{
16 destroy_bitmap(bmp);
17}
18//------------------------------------------------------
19 
20void TerrainClass::PaintTerrain(BITMAP *buffer)
21/* Paints the terrain to buffer */
22{
23 blit(bmp, buffer, 0, 0, 0, 0, bmp->w, bmp->h);
24 return;
25}
26//------------------------------------------------------
27//------------------------------------------------------

And the game class code:

game.cpp

1/** Kelly Crawford, 15 May 2007
2 GameClass definitions */
3 
4#include "game.h"
5 
6 
7volatile int tick_count = 0; // for the timer
8void add_tick(){tick_count++;}END_OF_FUNCTION(add_tick) // function to increment ticks
9 
10GameClass::GameClass(int gfx_mode, int w, int h, int depth)
11/* Initializes the game and allegro. The screen
12 is set to widthxheight, with a graphics mode defined
13 by gfx_mode */
14{
15 // store screen parameters
16 width = w;
17 height = h;
18
19 // initialize allegro
20 allegro_init();
21 install_keyboard();
22 install_mouse();
23
24 // install timer
25 install_timer();
26 LOCK_VARIABLE(tick_count);
27 LOCK_FUNCTION(add_tick);
28 install_int_ex(add_tick, BPS_TO_TIMER(60));
29
30 // set up graphics
31 set_color_depth(depth);
32 set_gfx_mode(gfx_mode, width, height, 0, 0);
33
34 game_state = 0;
35
36 buffer = create_bitmap(SCREEN_W, SCREEN_H);
37
38 srand(time(NULL));
39
40 mouse_down = false;
41 cur_firework = 0;
42}
43//------------------------------------------------------
44 
45GameClass::~GameClass()
46/* Destroy any bitmaps and release memory here */
47{
48 destroy_bitmap(buffer);
49}
50//------------------------------------------------------
51 
52void GameClass::UpdateState()
53/* Updates the game logic, making use of the timer */
54{
55 for(;tick_count>0;tick_count--){
56
57 if(mouse_b & 1) mouse_down = true;
58 if(!mouse_b & 1) mouse_down = false;
59
60 if(mouse_down){
61 explo[cur_firework].SetParticle(mouse_x, mouse_y);
62 ++cur_firework;
63 if(cur_firework >= MAX_FIREWORKS) cur_firework = 0;
64 }
65
66 for(int i = 0; i < MAX_FIREWORKS; i++)
67 if(explo<i>.exist) explo<i>.MoveParticle();
68
69 }
70}
71//------------------------------------------------------
72 
73void GameClass::Paint()
74/* Called after all logic is updated - updates screen */
75{
76 terrain.PaintTerrain(buffer);
77 for(int i = 0; i < MAX_FIREWORKS; i++)
78 explo<i>.PaintParticle(buffer);
79 blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
80 clear(buffer);
81}
82//------------------------------------------------------
83//------------------------------------------------------

Basically, the game.cpp file's functions UpdateState() and Paint() are called every loop, UpdateState() is within a timer to keep the game running at a steady flow. A TerrainClass called terrain is declared in game.h. I think it is something in the constructor for TerrainClass that the error is occuring.

When the game is compiled and run it says (in Vista):

tank.exe has stopped working.
--- Searching for a solution to problem

And it has an exit button.

What is happening?

Elverion
Member #6,239
September 2005
avatar

bmp = create_bitmap(200, 200);

I bet this is the problem line. You should be checking it's return value.

bmp = create_bitmap(200, 200);
if( bmp == NULL )
{
  allegro_message("bmp is NULL!");
  exit(1);
}

--
SolarStrike Software - MicroMacro home - Automation software.

Jonatan Hedborg
Member #4,886
July 2004
avatar

Judging from what you have posted, you are putting your instance of TerrainClass on the stack (Ie "TerrainClass terrain;" instead of "TerrainClass *terrain;" followed by a "terrain = new TerratinClass();" in the init code), which means you will try to create the bitmap before you initiate allegro.

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

moon_rabbits
Member #8,469
March 2007
avatar

Yeah, it was crashing because Allegro wasn't initialized before the TerrainClass was.

I used Jonatan's solution, but now I get these compiler errors and am unsure why:

Quote:

- In member function `void GameClass::Paint()':
- `PaintTerrain' has not been declared
- request for member of non-aggregate type before '(' token

But PaintTerrain has been declared and defined, etc.

EDIT: Oops, I'm an idiot. ->!

Go to: