Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How would you do it?

This thread is locked; no one can reply to it. rss feed Print
 1   2 
How would you do it?
Albin Engström
Member #8,110
December 2006
avatar

I've written many different types of builds for my games, i would like you to describe how you would have done it, as i have no name for the subject i will describe my "builds", and you will probably realize what im talking about:

1. To put everything in main... no functions at all and drawing directly to the screen.
2. To put everything in main. no functions at all and using double buffering.
3. using functions without callbacks(or whatever it's called) with global variables, using either double of triplebuffering.
4. using functions without callbacks with global variables, using a logic loop of 1000/sec with double buffering.
5. using functions without callbacks with global variables, putting the graphic routines in a timer... using triplebuffering with a logic rate of 1000.
6. using functions without callbacks with variables put in global structures, using a timer for stacking a variable used to loop the logic, using triplebuffering.
7. global structures with variables and large functions without callbacks(like a continuation of the code, inline?) and small with callbacks. with a timer recording the delay between two frames in ms and uses the value for the logic loop, and an un looped logic ruotine. using double buffering with vsync or triple buffering.

I've done a lot more but the difference between them and these are so small so i didn't post them. ALL of these was failures, at first i thought number 7 was a little good and it ran so well, but the frames lagged more the more the cpu was used, and it did not look good at all when using draw_trans_sprite for fading(the cpu went 100%). also, i realized that if the cpu could not execute a logic loop in less than a millisecond on normal speed the program would screw up.

Before i struggle with the next one i'd like to listen some advices, because i really need some as you might have realized, the reason i like to have a logic rate of a 1000 is because it's convenient, the collision detection gets much better looking and i don't have to waste as much time as i would have with a logic rate synced with the frame rat. if you have any smart ideas and tricks on how to make an efficient yet good build, please tell me.

Onewing
Member #6,152
August 2005
avatar

Quote:

How would you do it?

With classes and style. 8-)

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

Kikaru
Member #7,616
August 2006
avatar

I don't get what you want to attempt. Here is what I do, though most people don't like it.

I put a mix of things in main. Mostly, if it seems too long, or is redundant, or needs arguments, or needs a return value, then it goes in a global function. I always use double buffering, and put all drawing in main. I use timed game loops, with the help of an interrupt handler from Allegro.
Oh, the part most people don't like is that I use all global variables. 8-)

Onewing
Member #6,152
August 2005
avatar

Quote:

Oh, the part most people don't like is that I use all global variables.

Using global variables is not a sin, but using all global variables could be a questionable religion in itself.

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

Arthur Kalliokoski
Second in Command
February 2005
avatar

If a given module (such as main) grows beyond a screenful or two, it's easy to get confused. The little example programs in allegro/examples are pushing it, but they're "self contained" as examples. If your program gets really big, main() should be more like a table of contents, letting you see the big picture, and you'd look at the individual modules for more detail.

Same with globals, since every module can see them, it gets harder to think of unique names for them all in a big program, also they can slow down a program if the access patterns are always causing cache misses. Having static variables in your modules can do the same, but you eliminate the first problem. Locals in functions are on the stack, which is accessed pretty often (especially since a called function pushed the return value) but if you need to access some variable in several modules or they're huge arrays or something then that's out too.

They all watch too much MSNBC... they get ideas.

Trezker
Member #1,739
December 2001
avatar

I'm very much satisfied with 100 logic updates per second. If collisions require more then I'll improve the handling in those situations but I wont do more frequent handling.

I rather not put anything in main except call the run function of the game.

I also don't want to go into more of my programming style. There's way too much of it.

ReyBrujo
Moderator
January 2001
avatar

Classes, inheritance and interfaces. Every class is small, doing very little things. I use composite to create bigger classes with smaller ones. And things like that :)

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

Audric
Member #907
January 2001

If you have performance problems, compile your game with profiling options, and analyze what takes time.

Doing more than 100 updates per second is probably wasteful, but there's probably something else as well.

Evert
Member #794
November 2000
avatar

Lots of functions with as few global variables as possible (I still have a few more than I should though). I do use a fair number of static variables in each source file, which is probably ok given that each of these source files are fairly specialised to a specific task.
Main is fairly minimal and mostly interprets commandline options and calls initialisation functions. When the game is done, it calls shutdown functions.
I use a re-usable game loop based on the one in Allegro's FAQ, with a fixed logic rate of 60Hz (I think, been a long while since I set it up). The screen update code is optionally double buffered, triple buffered or any other method. The game loop rests when no graphics need to be updated and the next logic cycle hasn't started yet (reducing CPU usage to a few percent).

Quote:

it did not look good at all when using draw_trans_sprite for fading(the cpu went 100%).

Make sure you don't use draw_trans_sprite() with video bitmaps, since reading from video bitmaps is slow.

Quote:

also, i realized that if the cpu could not execute a logic loop in less than a millisecond on normal speed the program would screw up.

Limit the number of frames that may be skipped.

Quote:

the reason i like to have a logic rate of a 1000 is because it's convenient,

Do not run your logic at 1000Hz; Allegro's timers can't run at that frequency reliably. Stick to 100Hz if you need an easy number.

Quote:

the collision detection gets much better looking

Collision detection should not depend on your logic rate (I see how it could, but it shouldn't).

Quote:

and i don't have to waste as much time as i would have with a logic rate synced with the frame rat.

What do you mean here?

Albin Engström
Member #8,110
December 2006
avatar

"Audric: Doing more than 100 updates per second is probably wasteful / Evert: Stick to 100Hz if you need an easy number." got it, but would you say no to multiply it with 10 and use it with a logic loop? :P.

"Evert: The game loop rests when no graphics need to be updated and the next logic cycle hasn't started yet (reducing CPU usage to a few percent)." Great tip!! gave me a lot of good ideas :).

"Evert: Make sure you don't use draw_trans_sprite() with video bitmaps, since reading from video bitmaps is slow." drawing a video bitmap or to a video bitmap? i'm (was) drawing to a videobitmap.

"Evert: Collision detection should not depend on your logic rate (I see how it could, but it shouldn't)." what should it depend on? i can't see any easy solutions that would satisfy me :(.

"Evert: What do you mean here?" I meant that i don't have to waste as much time on figuring out how to convert everything from 1000ll to 100ll with the same effect.

gnolam
Member #2,030
March 2002
avatar

Gah. Use [quotes} like normal people. :P

Quote:

got it, but would you say no to multiply it with 10 and use it with a logic loop? :P

I would. You don't need a 1000 Hz update frequency.

Quote:

drawing a video bitmap or to a video bitmap? i'm (was) drawing to a videobitmap.

Think about it: to simulate transparency, you take the pixels from both the bitmap you're drawing from and the one you're drawing to and blend them together. So if reading from video bitmaps is slow, that means..?

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

Evert
Member #794
November 2000
avatar

Quote:

drawing a video bitmap or to a video bitmap?

Check the manual on vram->vram blits; you should only do it if what you're trying tio do can be accelerated in hardware (Allegro's translucent blits can not be).

Quote:

what should it depend on?

Old and new positions of the colliders, maybe the two or three previous positions as well if you need estimates for the velocity and the acceleration.

Albin Engström
Member #8,110
December 2006
avatar

don't know how to use quotes ^^, looked in the help file, but i don't want to test it on a "serious" conversation.

"gnolam: Think about it: to simulate transparency, you take the pixels from both the bitmap you're drawing from and the one you're drawing to and blend them together. So if reading from video bitmaps is slow, that means..?"

haha, i guess the answer was that simple, but i'd have to ask, you never know what may happen in this strange world of allegro and c++ if you don't know the truth..

"Old and new positions of the colliders, maybe the two or three previous positions as well if you need estimates for the velocity and the acceleration." sounds good, but i can't help to think that it would be much harder to inplent.

my other questions still remains to answer... and "I would. You don't need a 1000 Hz update frequency." doesn't qualify as an answer :P

thanks!

Evert
Member #794
November 2000
avatar

Quote:

looked in the help file, but i don't want to test it on a "serious" conversation.

You can edit your posts, you know?

Quote:

i can't help to think that it would be much harder to inplent.

Depends on your implementation, I suppose. Without seeing an implmentation I can't comment on that (you don't have to post code; I don't have time to look at it in detail anyway).

LennyLen
Member #5,313
December 2004
avatar

Quote:

You can edit your posts, you know?

Or use the preview feature.

Albin Engström
Member #8,110
December 2006
avatar

"You can edit your posts, you know?"
yes i know, i do that all the time :).

"you don't have to post code"
I don't have any code to post anyway :).

gnolam
Member #2,030
March 2002
avatar

And so you choose not to better yourself because..?

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

Michael Jensen
Member #2,870
October 2002
avatar

Quote:

I don't have any code to post anyway .

ummm, what?

Have you been programming on paper again?

Albin Engström
Member #8,110
December 2006
avatar

"gnolam: And so you choose not to better yourself because..?"
Because i don't know if it's the better thing to do..

"Michael Jensen: Have you been programming on paper again? [thedailywtf.com]"
^^' no, not really,(nice link by the way), what i meant was that i don't have anything to show that would be worth posting, it would be a waste of time. :).

LennyLen
Member #5,313
December 2004
avatar

Quote:

Gah. Use [quotes} like normal people.

Just a reminder.

BAF
Member #2,981
December 2002
avatar

Use quotes FFS!

Albin Engström
Member #8,110
December 2006
avatar

:'(

BAF said:

Use quotes FFS!

HA! in your face..

ImLeftFooted
Member #3,935
October 2003
avatar

Go to the mockup page and read the section entitled 'Quotes'. Very nice feature :).

edit: Wow that is the definition of beaten :P

Goalie Ca
Member #2,579
July 2002
avatar

If a tree falls down an no one is around does it make a sound?

In other news does foot->kick(ass) or does ass->getKicked(foot)?

I haven't quite figured that one out.

-------------
Bah weep granah weep nini bong!

Richard Phipps
Member #1,632
November 2001
avatar

Everyone has a different opinion on the correct way to program, and the style you should use. While some methods are going to be better than other, I think personally the most important thing is finishing a project rather than how it is written.

 1   2 


Go to: