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
Terrain.cpp
| 1 | /* Kelly Crawford 21 May 2007 |
| 2 | Terrain source file */ |
| 3 | |
| 4 | #include "terrain.h" |
| 5 | |
| 6 | TerrainClass::TerrainClass() |
| 7 | /* Creates terrain from min to max with amount of hills */ |
| 8 | { |
| 9 | bmp = create_bitmap(200, 200); |
| 10 | |
| 11 | } |
| 12 | //------------------------------------------------------ |
| 13 | |
| 14 | TerrainClass::~TerrainClass() |
| 15 | { |
| 16 | destroy_bitmap(bmp); |
| 17 | } |
| 18 | //------------------------------------------------------ |
| 19 | |
| 20 | void 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 | |
| 7 | volatile int tick_count = 0; // for the timer |
| 8 | void add_tick(){tick_count++;}END_OF_FUNCTION(add_tick) // function to increment ticks |
| 9 | |
| 10 | GameClass::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 | |
| 45 | GameClass::~GameClass() |
| 46 | /* Destroy any bitmaps and release memory here */ |
| 47 | { |
| 48 | destroy_bitmap(buffer); |
| 49 | } |
| 50 | //------------------------------------------------------ |
| 51 | |
| 52 | void 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 | |
| 73 | void 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?
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); }
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.
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:
- 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. ->!