Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » bitmap structure

This thread is locked; no one can reply to it. rss feed Print
 1   2 
bitmap structure
lourentz hek
Member #5,044
September 2004

I'm trying to make a bitmap structure so that I can load multiple bitmaps at once in a function. The problem is that I get an error when I want to load the bitmap. So far I have come up with this code:

struct characters_praten
{
  BITMAP *voor1;
  BITMAP *voor2;
  BITMAP *links1;
  BITMAP *links2;
  BITMAP *rechts1;
  BITMAP *rechts2;
  BITMAP *achter;
} broer;

broer.voor1 = load_bitmap("broer1.bmp",NULL);

Do you know how I can load these bitmaps?

Gideon Weems
Member #3,925
October 2003

That's a bit hard-coded, you know, but I guess it's fine if you've just started learning the library. When you say that you're getting an error, what exactly happens? Are you checking the value of broer.voor1 against NULL?

broer.voor1 = load_bitmap("broer1.bmp",NULL);
if(broer.voor1 == NULL)
  exit(42);

In order for your code to work, broer1.bmp must exist in the current directory and Allegro must be set up properly already.

miran
Member #2,407
June 2002

Uhm, don't you need to make an instance of your structure in order to be able to use it? Or does that C declaration of the structure already do that?

--
sig used to be here

CGamesPlay
Member #2,559
July 2002
avatar

miran: That isn't a C definition, It's a C++ one :) He name the struct characters_praten, and created an instance named broer.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

miran
Member #2,407
June 2002

Really? Well, you learn something new every day.

Then maybe he's trying to load the bitmap outside of a function? :-X

--
sig used to be here

lourentz hek
Member #5,044
September 2004

Well. The compiler shows 3 errors:

error C2143: syntax error : missing ';' before '.'
(The '.' is the one between broer and voor1)

error C2501: 'broer' : missing storage-class or type specifiers

error C2371: 'broer' : redefinition; different basic types

dudaskank
Member #561
July 2000
avatar

in C you can do this too... I think :p

Toque a balada do amor inabalável, eterna love song de nós dois
Eduardo "Dudaskank"
[ Home Page (ptbr) | Blog (ptbr) | Tetris 1.1 (ptbr) | Resta Um (ptbr) | MJpgAlleg 2.3 ]

ReyBrujo
Moderator
January 2001
avatar

I would use it this way:

typedef struct characters_praten
{
    BITMAP *voor1;
    BITMAP *voor2;
    BITMAP *links1;
    BITMAP *links2;
    BITMAP *rechts1;
    BITMAP *rechts2;
    BITMAP *achter;
} characters_praten;

characters_praten broer;

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

Matt Smith
Member #783
November 2000

Me too. I always typedef a struct with the same name as its tag. It makes casting and defining instances (especially for pointers) obvious and simple. BITMAP is itself declared this way, as are all the common Allegro structs.

CGamesPlay
Member #2,559
July 2002
avatar

lourentz hek: You must be trying to set broer.voor1 on the global scope. You have to do it inside of a function.

He doesn't need to typedef the structs if he's using C++.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Evert
Member #794
November 2000
avatar

Quote:

I would use it this way:

So would I, but

struct something {
/* some stuff */
} somevar;

is perfectly valid C code where somevar is of type struct something.

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

error C2143: syntax error : missing ';' before '.'
(The '.' is the one between broer and voor1)

error C2501: 'broer' : missing storage-class or type specifiers

error C2371: 'broer' : redefinition; different basic types

Have you included <allegro.h>? Any other headers? Looks like the compiler doesn't understand you struct definition, although it is, in itself, valid C code; maybe BITMAP* is not defined. Oh yes: do you compile this as a C or a C++ program?
And you're not trying to compile te above as-is, are you? Because it should be something like this:

1#include <allegro.h>
2 
3struct characters_praten
4{
5 BITMAP *voor1;
6 BITMAP *voor2;
7 BITMAP *links1;
8 BITMAP *links2;
9 BITMAP *rechts1;
10 BITMAP *rechts2;
11 BITMAP *achter;
12} broer;
13 
14int main() {
15 allegro_init();
16 set_color_depth(8);
17 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
18 broer.voor1 = load_bitmap("broer1.bmp",NULL);
19 if (!broer.voor1)
20 return -1;
21 destroy_bitmap(broer.voor1);
22 return 0;
23}
24END_OF_MAIN()

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Evert
Member #794
November 2000
avatar

I'd also recommend not mixing two languages in your sourcecode - this will only be confusing and ugly in the long run. I won't say you should use English throughout (although I've myself adopted English as my primary language when doing anything programming related, or even anything computer related), but I'd avoid things like characters_praten.

Tobias Dammers
Member #2,604
August 2002
avatar

Hehe, two languages... I first thought you mean C and C++... anyway, since all C and C++ keywords are actually English words, I use English names for everything else, too. Sort of easier to read. Especially for people on a forum who are trying to help you. Knowing what a variable name means usually gives a good hint on what the programmer wants it to do. (ik spreek dan toevallig zelf wél Nederlands, but most people here don't).

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Avenger
Member #4,550
April 2004

For me, not using english when using my computer just seems.. Weird

Thomas Fjellstrom
Member #476
June 2000
avatar

ditto.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Evert
Member #794
November 2000
avatar

Quote:

(ik spreek dan toevallig zelf wél Nederlands, but most people here don't).

Ik moet altijd even omschakelen als ik een zin twee talen zie staan. Meestal begrijp ik de eerste keer dat ik het lees niet wat er staat... ;)

So yeah, probably best to be consistent and just use English... but I can understand people not doing that.

Tobias Dammers
Member #2,604
August 2002
avatar

Yeah, I was making a point here. Though I get quite thorough practice when I rehearse with my bands, with members from the Netherlands, Germany, Belgium, Spain, Cuba and Suriname. Pretty cool actually, I'm learning Spanish while I work...

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Matt Smith
Member #783
November 2000

How about a code editor which always shows all variables in the local language? If Korean hurts your eyes, press a button!

lourentz hek
Member #5,044
September 2004

It's working fine now. Thanks for you help. I only have one last question. Is it possible to declare the structure outside a function let's say in a header?

miran
Member #2,407
June 2002

That's where you're supposed to declare it ::) If you declare instances of the structure though, you should declare them as extern in the header and then later define them in the .cpp file:

1// declaration of a struct in code.h
2struct my_struct {
3 int a;
4 int b;
5};
6 
7// declaration of a global instance of the struct in code.h
8extern my_struct instance;
9 
10// definition of the struct instance in code.cpp
11my_struct instance;
12 
13// you can fill in the global instance and use it in a function in code.cpp
14void some_function() {
15 instance.a = 5;
16 instance.b = 32;
17}

I'm not sure about the exact rules in C but I don't think it's much different...

--
sig used to be here

A J
Member #3,025
December 2002
avatar

slightly off topic, but its a BITMAP structure question...

why did someone re-define the windows bitmap structure memebers in allegro to use unsigned long, when the M$ document lists it as DWORD.
this makes it rather difficult to port allegro to 64bits.

___________________________
The more you talk, the more AJ is right. - ML

Thomas Fjellstrom
Member #476
June 2000
avatar

DWORD == unsigned 32 bits. current unsigned long? unsigned 32bits.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Oscar Giner
Member #2,207
April 2002
avatar

A J said:

why did someone re-define the windows bitmap structure memebers in allegro to use unsigned long, when the M$ document lists it as DWORD.

WinDef.h said:

typedef unsigned long DWORD;

so what's the problem?

A J
Member #3,025
December 2002
avatar

unsigned long on 64bit system is AFAIK 64bit.
DWORD is 32bit.

___________________________
The more you talk, the more AJ is right. - ML

 1   2 


Go to: