Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Interdependant Headers

This thread is locked; no one can reply to it. rss feed Print
Interdependant Headers
SkaxCo
Member #8,323
February 2007

I am trying to make my first game with more than one header. I have: main.cpp, mmm_aux, and mmm_drawing. Aux is globals, consts, and other trashy one-use functions. Drawing, is, well, drawing. I need to use mmm.dat, declared in Aux (globally declared) in Drawing. My includes in main are:

/*INCLUDES*/
#include <cmath>
#include <ctime>
#include <string.h>
#include "mmm_aux.h"
#include "mmm_drawing.h"
#include "MMM.h"
#include <allegro.h>

In Aux, mmm is declared as:

/*INCLUDES*/
#include <allegro.h>
/*THINGS*/
BITMAP *buffy;
DATAFILE *mmm;

then set in a function in Aux, game_setup(), as

void game_setup(){
  srand(time(NULL));
  buffy = create_bitmap(SCREEN_W, SCREEN_H);
  mmm = load_datafile("MMM.dat");
}

But, when I use this function in Drawing

void draw_cursor(float w_range, int d_x, int d_y){
  float c2 = (d_x*d_x)+(d_y*d_y);
  float distance = sqrt(c2);
  if(distance < w_range)
    masked_blit((BITMAP*)mmm[crosshair].dat, buffy, 0, 0, mouse_x-16, mouse_y-16, 32, 32);
  else
    masked_blit((BITMAP*)mmm[crosshair].dat, buffy, 32, 0, mouse_x-16, mouse_y-16, 32, 32);
}

It says:
error C2065: 'crosshair' undeclared identifier
error C2228: left of '.dat' must have class/struct/union

What am I doing wrong?

HoHo
Member #4,534
April 2004
avatar

Where do you define the "crosshair" variable? I can't see it anywhere and so does the compiler
Use ->dat instead of .dat, mmm is a pointer.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

SkaxCo
Member #8,323
February 2007

crosshair is a bitmap in mmm.dat. This code worked if I put it in the main body of code in main.cpp.

HoHo
Member #4,534
April 2004
avatar

Assuming that this isa preprocessor define created by dat utility you need to get the definitions in the .h that came with the .dat into the .cpp file that does the drawing. I think it is called MMM.h in your case but I might be wrong.

If it is just some random global you created yourself you need to learn extern variables. Long story short: don't define variables in headers, define them in sources and have extern versions of them in other sources/headers

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

SkaxCo
Member #8,323
February 2007

Well, I was forgetting to include MMM.h.

I'm going to try to decipher what you said and use it.

HoHo
Member #4,534
April 2004
avatar

Extern variables are actually relatively simple, just google for them or search the forum, there was some discussion about it some time ago. If anything is unclear feel free to come back and ask guidance

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

SkaxCo
Member #8,323
February 2007

Wait...so, in Drawing, in game_setup(), I would do something like

extern DATAFILE *mmm;
mmm = load_datafile("MMM.dat");

?

Because that didn't work.

Kibiz0r
Member #6,203
September 2005
avatar

In your header, you want
extern DATAFILE *mmm; //declaration
and in your source file, you want
DATAFILE *mmm = load_datafile("MMM.dat"); //definition

You have to have one formal definition of it like that, because extern just says "Yes, mmm exists somewhere, you just have to go find it" and the definition says "Here I am!".

Make sense?

SkaxCo
Member #8,323
February 2007

So I redefine it in each file? Ok, I got it, and it works. But...unrelated...my computer sucks so much, my parents are too cheap to improve it, and I can't get a job. They only got a new cooling system when it was overheating so much it wouldn't boot, and a new video card when some games I bought wouldn't run. I have like a 1.2 GHz processor and 512 of crappy RAM.

Kibiz0r
Member #6,203
September 2005
avatar

Quote:

So I redefine it in each file?

No, only define it once, in the .c/.cpp file you want to define the value in.

Any file you want to use it in, it has to be declared, using extern. You will probably accomplish this by including a header, though, to save time declaring it all over the place.

Onewing
Member #6,152
August 2005
avatar

You are talking to a computer. You must speaketh their language, but if you want a rough, un-technical translation, the word "extern" is bascially saying "I promise to declare this in my cpp file, honestly, so if anyone wants to know, it's in the cpp...honestly."

Don't break promises to the computer, they get angry.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Go to: