Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » 4 assorted questions

This thread is locked; no one can reply to it. rss feed Print
 1   2 
4 assorted questions
William Labbett
Member #4,486
March 2004
avatar

hi,
being unable to use the net (which I'm finding does wonders for my game development) I've accumulated a few questions which I'd appreciate answers to.

1. I was told a while ago something about not declaring functions that I define.
Declaring and defining is wrong. But I find if I don't give a prototype before the definition I always get :- type mismatch with previous declaration. foobar() was previously implicitly declared to return int when the definition is

void foobar()
{
}

So I always give a prototype and the errors go away but I'd like to know if I'm missing something.

2. Flickering problem :-

I've got a function draw_to_screen() which calls vsync() and blits the buffer to the screen. This worked well initially but since I started drawing more complicated stuff on the buffer I get flickering, even though only one function draws to the screen and vsync()'s always called before it.
If I do this :-

1 
2update_buffer()
3{
4 blit(layer1, buffer, 0, 0, 0, 0, 640, 480);
5 
6 draw_screen();
7}
8 
9 
10draw_screen()
11{
12 vsync();
13 blit(buffer, screen, 0, 0, 0, 0, 640, 480);
14}

no flickering.

But if I do :-

update_buffer()
{
   blit(layer1, buffer, 0, 0, 0, 0, 640, y);
   blit(layer1, buffer, 0, 0, 0, 0, 640, 480 - y);
   draw_screen();
}

draw_screen()
{
   vsync();
   blit(buffer, screen, 0, 0, 0, 0, 640, 480);
}

I get the flickers.

Please help.

3. (Numbskull C question)

I've got a function defined as

BITMAP *
make_pattern(BITMAP *b)
{
BITMAP *c;

...
do stuff
...

return c;
}

Then I call it with

bitmap_pointer = make_pattern(another_bitmap_pointer);

...and I get a compile error

[Warning] assignment makes pointer from integer without a cast.

When I run it all works like I wanted but I don't get what's wrong with the
code.
I checked some allegro source files which have functions which have the same
form and they seem to do the same thing. I'm not much experienced with pointers
but I'm keen to get better at understanding them.

4. Lastly.

Just wondering :- I noticed that with allegro-4.0.0 if you call install_keyboard() after set_gfx_mode() it doesn't work properly. Might just be me but it had me confused for ages. Don't know if this is a known issue or not.

Thanks. Will

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Declaring and defining is wrong.

Whoever said this doesn't know C. You're doing it right.

For the second, something else is factoring in.

Quote:

...and I get a compile error

Forward declare the function and it will work. Also, warnings are not errors.

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

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

William Labbett
Member #4,486
March 2004
avatar

Thanks ever-present CGamesPlay :)

When you say forward declare the function - I'm sure what you mean.

Richard Phipps
Member #1,632
November 2001
avatar

2. Flickering: Unless you are using C++ then y is undefined and could be any random memory location each time the function is called. This could result in random numbers and flickering..

gnolam
Member #2,030
March 2002
avatar

Quote:

allegro-4.0.0

Is bloody ancient. Use 4.2.1.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

William Labbett
Member #4,486
March 2004
avatar

Sorry - I wrote the code unclearly.

y is an automatic in that function. The bitmaps get drawn properly just with flickering. I don't grok why a simple change like that (that's really the only difference although the function does contain other stuff) [edit] ...would cause flickering.

BTW - well done for getting in EDGE magazine. Looking forward to your next release :)

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

When you say forward declare the function - I'm sure what you mean.

I mean do what you want to do as per your first question. Declare it but don't define it.

[append]

Quote:

ever-present CGamesPlay :)

Thanks, I'm hoping for 21 000 posts before the year is out.

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

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

William Labbett
Member #4,486
March 2004
avatar

You could get a few more if you can make me understand why the warning comes up -
i still don't understand what to do - if I don't define the function how will it work?

CGamesPlay
Member #2,559
July 2002
avatar

Okay, in C, when you do this:

void myFunc(void) {
    foo();
}

The compiler assumes, unless it is told otherwise, that the prototype for foo() is this:int foo();(Keep in mind that in C this means the function accepts any number of arguments.)
Obviously, you can't convert an int to a BITMAP:BITMAP* bmp = 7;So converting from the return value of that function also doesn't work. Here's a complete example of code that doesn't work:

void myFunc(void) {
    BITMAP* another_bitmap_pointer;
    BITMAP* bitmap_pointer;
    another_bitmap_pointer = load_bitmap("base.bmp", NULL);
    bitmap_pointer = make_pattern(another_bitmap_pointer);
    destroy_bitmap(another_bitmap_pointer);
    destroy_bitmap(bitmap);
}

BITMAP* make_pattern(BITMAP* input) {
    BITMAP* c;
    c = create_bitmap(input->w, input->h);
    blit(input, c, 0, 0, 0, 0, c->w, c->h);
    return c;
}

What's happening? The compiler gets to the make_pattern line and can't find it declared any place else before. This is called 'implicit declaration', and it assumes the prototype is int make_pattern(). How do you fix it? Forward-declare the function:

1BITMAP* make_pattern(BITMAP* input);
2 
3void myFunc(void) {
4 BITMAP* another_bitmap_pointer;
5 BITMAP* bitmap_pointer;
6 another_bitmap_pointer = load_bitmap("base.bmp", NULL);
7 bitmap_pointer = make_pattern(another_bitmap_pointer);
8 destroy_bitmap(another_bitmap_pointer);
9 destroy_bitmap(bitmap);
10}
11 
12BITMAP* make_pattern(BITMAP* input) {
13 BITMAP* c;
14 c = create_bitmap(input->w, input->h);
15 blit(input, c, 0, 0, 0, 0, c->w, c->h);
16 return c;
17}

[append]

Quote:

Unless you are using C++ then y is undefined and could be any random memory location each time the function is called. This could result in random numbers and flickering..

No, the only thing that the snippet told us about y is that is isn't a local variable :P Whether it's uninitialized is a different issue.

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

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

William Labbett
Member #4,486
March 2004
avatar

Thanks for that reply CGamesPlay. I get what you're saying now.

Trouble is I think I have declared the functions but I'll go home and check this.

I didn't know you have to destroy local BITMAP pointers. Is this because although the pointer only exists during the call - the memory the BITMAP used up is still classed as unavailable ?

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Trouble is I think I have declared the functions but I'll go home and check this.

Make sure you declare them before the compiler gets to the function you use them in.

Quote:

Is this because although the pointer only exists during the call - the memory the BITMAP used up is still classed as unavailable ?

Yes.

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

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

William Labbett
Member #4,486
March 2004
avatar

Thanks for all the help. I'm off home now to get on with my project and put your help to use. See you later.

CGamesPlay
Member #2,559
July 2002
avatar

:)

(a.k.a. bump)

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

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

Richard Phipps
Member #1,632
November 2001
avatar

True. I forgot about global variables.. :-X

LennyLen
Member #5,313
December 2004
avatar

Quote:

I forgot about global variables..

Not necessarily a bad thing.

ImLeftFooted
Member #3,935
October 2003
avatar

1) Already answered.
2) That really shouldn't be flickering... I would reccomend upgrading allegro to the latest version and see if the problem still occurs. How bad is this flickering?
3) Only problem I can see from your pasted code is the possibility of another_bitmap_pointer being defined as an int (instead of a BITMAP*).
4) Yes this is a problem on certain operating systems under certain settings. There is not much allegro can do to solve the issue, its mentioned in the docs that you should call them in that order.

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Only problem I can see from your pasted code is the possibility of another_bitmap_pointer being defined as an int (instead of a BITMAP*).

Not another one, because this is C so you can't overload, and even in C++ you can't overload by return type. See what I said about it above ;)

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

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

ImLeftFooted
Member #3,935
October 2003
avatar

BITMAP *
make_pattern(BITMAP *b)
{
BITMAP *c;

...
do stuff
...

return c;
}

Who said anything about overloading? I see a function 'make_pattern' which returns BITMAP*. I also see a variable 'c' of type BITMAP* that is returned. No type error here... :-X

CGamesPlay
Member #2,559
July 2002
avatar

Oh, I misunderstood. That isn't plausible because it said "assignment" and not "parameter".

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

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

ImLeftFooted
Member #3,935
October 2003
avatar

Hm, on second thought, I believe you are right CGames.

William: Try adding this right before you call the function and see if the error goes away:

BITMAP *make_pattern(BITMAP *b);

CGamesPlay
Member #2,559
July 2002
avatar

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

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

ImLeftFooted
Member #3,935
October 2003
avatar

Ok I read it all the way through this time. (its just so wordy :P)

Quote:

Then I call it with

bitmap_pointer = make_pattern(another_bitmap_pointer);

...and I get a compile error

implies that

bitmap_pointer = make_pattern(another_bitmap_pointer);

causes the error.

William: where is the error occuring?

William Labbett
Member #4,486
March 2004
avatar

hi, first time back on the web for a while.

Forward declaring the function did stop the warning coming up. I didn't know you could declare the same function in more than one source file.
As for the flickering problem, I've solved that now. I was a bit more complex than what the code said.
Thanks for the help.

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

I didn't know you could declare the same function in more than one source file.

Remember, this is what header files do. A #include is just the compiler copying the named file in place of that line.

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

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

William Labbett
Member #4,486
March 2004
avatar

thanks,

but if you define some variable in a header included by more than one file you
get errors ?

 1   2 


Go to: