Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How does one declare global class objects?

Credits go to ReyBrujo for helping out!
This thread is locked; no one can reply to it. rss feed Print
How does one declare global class objects?
cd wheat
Member #6,578
November 2005

Some of my classes need to be accessed by other classes in other files for stuff such as saving and editing without having to pass it through functions to save time. My other globals work. CMap Map declared in the main() function worked but the program crashes when I turn it into a global varaible.

Globals.h

1#ifndef GLOBALS_H
2#define GLOBALS_H
3 
4#include <allegro.h>
5#include "Map.h"
6 
7#define SCREEN_WIDTH 900
8#define SCREEN_HIEGHT 700
9 
10#define COLOR_DEPTH 16
11 
12#define BPS 20
13 
14#define MAX_MAP_WIDTH 256
15#define MAX_MAP_HIEGHT 256
16 
17#define HORIZONTAL_TILES 20
18#define VERTICLE_TILES 15
19#define TILE_SIZE 32
20 
21#define MATH_PI 3.1415926
22 
23#define COLOR_WHITE makecol(255,255,255)
24#define COLOR_SILVER makecol(192,192,192)
25#define COLOR_GREY makecol(128,128,128)
26#define COLOR_BLACK makecol(0,0,0)
27#define COLOR_RED makecol(255,0,0)
28#define COLOR_ORANGE makecol(255,160,0)
29#define COLOR_YELLOW makecol(255,255,0)
30#define COLOR_GREEN makecol(0,164,0)
31#define COLOR_LIME makecol(204,255,0)
32#define COLOR_BLUE makecol(0,0,255)
33#define COLOR_NAVY makecol(0,0,128)
34#define COLOR_CYAN makecol(0,255,255)
35#define COLOR_VIOLET makecol(102,0,153)
36#define COLOR_MAGNETA makecol(255,0,255)
37 
38/* Globals Variables */
39extern bool QUIT;
40extern bool ShowHelpMenu;
41 
42extern BITMAP* buffer;
43 
44extern CMap Map;
45#endif

Globals.cpp

#include "Globals.h"

BITMAP *buffer = NULL;
bool QUIT = false;
bool ShowHelpMenu = false;
CMap Map;

1#include "Globals.h"
2#include "Map.h"
3#include "Layout.h"
4 
5volatile long ticker = 0;
6void increment_speed_counter(){ticker++;}
7END_OF_FUNCTION(increment_speed_counter);
8void AllegInit();
9 
10int main()
11{
12 AllegInit();
13
14 buffer = create_bitmap(SCREEN_WIDTH, SCREEN_HIEGHT);
15
16 while(!QUIT)
17 {
18 while(ticker > 0)
19 {
20 //logic()
21 if (key[KEY_UP])
22 {
23 //Move Map Up 1 tile
24 Map.MoveMap(0, -1);
25 }
26 if (key[KEY_RIGHT])
27 {
28 //Move Map Right 1 tile
29 Map.MoveMap(1, 0);
30 }
31 if (key[KEY_DOWN])
32 {
33 //Move Map Down 1 tile
34 Map.MoveMap(0, 1);
35 }
36 if (key[KEY_LEFT])
37 {
38 //Move Map Left 1 tile
39 Map.MoveMap(-1, 0);
40 }
41 else if (key[KEY_PGDN])
42 {
43 }
44 else if (key[KEY_PGUP])
45 {
46 }
47 else if (key[KEY_HOME])
48 {
49 }
50 else if (key[KEY_END])
51 {
52 }
53 else if (key[KEY_F1])
54 {
55 ShowHelpMenu = true;
56 }
57 else if (key[KEY_F2])
58 {
59 }
60 else if (key[KEY_F3])
61 {
62 }
63 else if (key[KEY_F4])
64 {
65
66 }
67 else if (key[KEY_ESC])
68 {
69 QUIT = true;
70 }
71
72 ticker--;
73 }
74 clear_to_color(buffer, COLOR_WHITE);
75 //draw()
76 DrawBorders();
77 Map.DrawMap(16,16);
78 ShowHelp(ShowHelpMenu);
79 textprintf_ex(buffer, font, 0, 0, COLOR_WHITE, -1, "MouseX:%d MouseY:%d", mouse_x, mouse_y);
80 textprintf_ex(buffer, font, 196, 0, COLOR_WHITE, -1, "TileX:%d TileY:%d", Map.x, Map.y);
81 blit(buffer, screen, 0, 0, 0, 0, SCREEN_WIDTH, SCREEN_HIEGHT);
82 }
83destroy_bitmap(buffer);
84return 0;
85}END_OF_MAIN();
86 
87void AllegInit()
88{
89 allegro_init();
90 set_window_title("Map Editor");
91 install_keyboard();
92 install_timer();
93 install_mouse();
94 show_os_cursor(2);
95 LOCK_VARIABLE(speed_counter);
96 LOCK_FUNCTION(increment_speed_counter);
97 install_int_ex(increment_speed_counter, BPS_TO_TIMER(BPS));
98 set_color_depth(COLOR_DEPTH);
99 set_gfx_mode(GFX_AUTODETECT_WINDOWED, SCREEN_WIDTH, SCREEN_HIEGHT,0,0);
100}

ReyBrujo
Moderator
January 2001
avatar

Remember that the constructor will be called before Allegro is initialized. If you are creating a bitmap inside the constructor of your CMap class, it will crash.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

cd wheat
Member #6,578
November 2005

Thank you. That was the problem. I fixed it by adding load and destroy bitmap functions to the class.

Carrus85
Member #2,633
August 2002
avatar

cd wheat: Another solution is to use auto_ptr to wrap a pointer to the global class, which you initialize once immediately after allegro_init( );
Example:

extern auto_ptr<otherClassName> otherClass; // Another source file will actually
// define the storage for this (more than likely the implementation file for otherClass)

int main( int argc, char **argv )
{
    allegro_init( );
    otherClass = auto_ptr<otherClassName>( new otherClassName() );

    otherClass->function1( );
    otherClass->function2( );
    otherClass->function3( );
    return 0;
}

Go to: