hello everyone,
I have been learning to code in Allegro 5 over the last month or so. I am a pretty amateur programmer, so I do as much as I can to learn, and expect to have several things I can fix in everything I have programmed. So, all help is greatly appreciated in this topic.
I am unsure whether or not to post my entire code via an attachment, or just post small parts of the code. I currently have a game that is fairly basic following a tutorial made on Youtube by Mike Geig. It has been fantastic help
My problem is that now that I am progressing past his tutorial, I am running into a Choppy game. Unsure why, however, since I feel that it does very little. It is not consistently choppy, but it lags for a bit then smooths out again (the entire time it is running though).
So without continuing my rambling, should I post my entire code as an attachment or simply just go through it with you guys with posts?
I am running into a Choppy game.
You mean it goes too slow, or skips frames? How much cpu does it take in Resource Meter?
should I post my entire code as an attachment or simply just go through it with you guys with posts?
If it's more than maybe 300 lines it'd be better to just post relevant parts instead of expecting people to wade through the whole thing, but I get the impression you have no idea what part is at fault.
Just try one or the other, and if nobody answers very quickly it's probably that nobody who's seen your questions is sure of their answer.
thank you for your reply. I have no idea where the problem is coming from. The current game is around 900 lines long.
It is currently using 44k in task manager.
It does not seem to be skipping frames, however everything "stutters" for a few seconds, then just seems to smooth out perfectly, then goes back to doing it again.]
ill post everything at the moment from start of game to finish, minus the functions that are called to.
enemies are technically the "comets" that fly from right to left, but I plan to change that later on.
I don't really see anything that stands out as being wrong. Although 100 enemies might be a problem depending on how big the sprites are.
I tried compiling it myself to try it out (using some random images to substitute) but it lacks "objects.h" and I don't see the code to load the images.
Kudos on going the Mike Geig route, his video tutorials are good.
On the other hand, we're talking performance problems here, thus it's very hard find the reason if we can't tag a common situation in which the slowdown happens.
Does the slowdown (which I guess means diminishing FPS) seems random, or is it at timed intervals or maybe when more than (say) 10 comets are on the screen?
The enemies sprites are 64x64, that get resized to a random number between 40 and 60.
yeah I understand pkcrel, and I've had a horrible time going over it and I cannot understand what I did wrong, unless its some form of bad coding that I have not learned yet.
Objects.h is a seperate header file and contains the following:
Try seeing if there is anything running on your computer that likes to steal a lot of cpu time (virus scanner?).
It still won't compile because it's missing stuff like initBackground() etc.
There aren't any prototypes or definitions for what a Background is, nor a header file to do those things. Maybe your program is garbling things as a result, due to passing incorrect types or something.
[EDIT]
Try seeing if there is anything running on your computer that likes to steal a lot of cpu time (virus scanner?).
Yeah, when I asked how much cpu in Resource Meter I meant how much processor utilization, not how much memory it took.
the final part of my code is posted below. im sorry but i couldnt not figure out how to upload the game so you could just DL it(making things much easier). it kept saying failed at the end of the upload(possibly too large, even being compressed?)
in my resource monitor, it is using 10-14 cpu. my computer is more than able to run the latest games as well.
there is still a part i cannot link, since its too big for the post... you will have to respond for me to post the final bit.
im sorry but i couldnt not figure out how to upload the game so you could just DL it
You could put everything in a zipfile and use the Drop Attachments Here button
{"name":"608968","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/68ba347dffbacfa2bcb6f72dfbd5795a.png","w":492,"h":274,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/68ba347dffbacfa2bcb6f72dfbd5795a"}
and it'll show up next to your name as a paperclip, which you could then put in a link with Formatting Help if you wanted.
there we go, i had to remove some things that weren't necessary. (file was too big before to upload i suppose)
ah crap i should have mentioned that the current game only works with an attached controller (i use a connected xbox 360 controller). it will not change shooting directions if you have no controller.
however you can still see the problem without being able to play.
I get these warnings, maybe fixing them will fix the problem. Find out how to enable warnings for your compiler.
main.cpp:316:79: warning: suggest parentheses around '&&' within '||' [-Wparentheses] else if(joystate.stick[0].axis[0] >= -.3 && joystate.stick[0].axis[0] <= .3 && Cntrlr[LRIGHT] || Cntrlr[LLEFT]) ^ main.cpp:324:80: warning: suggest parentheses around '&&' within '||' [-Wparentheses] else if (joystate.stick[0].axis[1] >= -.3 && joystate.stick[0].axis[1] <= .3 && Cntrlr[LDOWN] || Cntrlr[LUP]) ^ main.cpp: In function 'void drawPulse(Pulse&)': main.cpp:599:16: warning: suggest parentheses around '&&' within '||' [-Wparentheses] if(i == 0 && pulseAttack.timer >= 5 || i == 1 && pulseAttack.timer >= 6 || i == 2 && pulseAttack.timer >= 7 || i == 3 && pulseAttack.timer >= 8) ^ main.cpp:599:88: warning: suggest parentheses around '&&' within '||' [-Wparentheses] if(i == 0 && pulseAttack.timer >= 5 || i == 1 && pulseAttack.timer >= 6 || i == 2 && pulseAttack.timer >= 7 || i == 3 && pulseAttack.timer >= 8) ^ main.cpp:599:124: warning: suggest parentheses around '&&' within '||' [-Wparentheses] if(i == 0 && pulseAttack.timer >= 5 || i == 1 && pulseAttack.timer >= 6 || i == 2 && pulseAttack.timer >= 7 || i == 3 && pulseAttack.timer >= 8) ^ main.cpp: In function 'void initEnemy(Enemies*, int)': main.cpp:688:13: warning: statement has no effect [-Wunused-value] enemy[i].x; ^ main.cpp:689:13: warning: statement has no effect [-Wunused-value] enemy[i].y; ^ main.cpp: In function 'int main()': main.cpp:338:15: warning: array subscript is above array bounds [-Warray-bounds] {keys[SELECT] = true;
I also see that player.cpp is 0 bytes? Is that a mistake?
Also, allegro.log is 2.8 megs, probably leave that out.
I see there's a Project1.exe in debug, but my virtual XP machine says it's not a valid Win32 application?
Right now I'm getting a crash in initPulse because the case of the image and the string in the program don't match (linux case sensitive).
I'll play with it a bit more, this is just an update.
[EDIT]
I finally got it to run, it hovers around 58-59 fps, and I don't see any choppiness, but it certainly does look... odd.
{"name":"608970","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/9\/d99b5c5b1dc6274e768323eaf57a8f1a.png","w":1440,"h":900,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/9\/d99b5c5b1dc6274e768323eaf57a8f1a"}
[EDIT2]
Compiling without debugging info makes it drop to around 45 fps. Uninitialized pointers?
AHA! I see one bugaboo now, you're loading bitmaps meteor1.png and meteor2.png in startEnemy() and it's in the loop, so you're loading bitmaps forever. You should load bitmaps to an ALLEGRO_BITMAP at initialization and only use the ALLEGRO_BITMAP * from then on.
thank you for your response, im going over each thing you have said to see what happens after the fixes. do you have any idea what i am supposed to do for these:
main.cpp:599:124: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
AHA! I see one bugaboo now, you're loading bitmaps meteor1.png and meteor2.png in startEnemy() and it's in the loop, so you're loading bitmaps forever. You should load bitmaps to an ALLEGRO_BITMAP at initialization and only use the ALLEGRO_BITMAP * from then on.
this is a great point, thank you. i didnt realize it was such a big deal. i will just pass the loaded bitmap after its initialized in the beginning when the function runs.
AHA! I see one bugaboo now, you're loading bitmaps meteor1.png and meteor2.png in startEnemy() and it's in the loop, so you're loading bitmaps forever. You should load bitmaps to an ALLEGRO_BITMAP at initialization and only use the ALLEGRO_BITMAP * from then on.
Right! you can also load on request only if the bitmap is nonexistant yet.
But, which this kind of thing shouldn't memory usage skyrocket? Correct me if I'm wrong but this should happen also in debug version.
It wants it like this: FIXED so the compiler doesn't complain.
if( ((i == 0) && (pulseAttack.timer >= 5)) || ((i == 1) && (pulseAttack.timer >= 6)) || ((i == 2) && (pulseAttack.timer >= 7)) || ((i == 3) && (pulseAttack.timer >= 8)) ) continue;
I think. That's a horribly complicated line. Maybe break it up more so it's easy to understand.
I also see that player.cpp is 0 bytes? Is that a mistake?
I was going to start working on a player .cpp and .h, but ran into troubles. I have put that aside for now until i understand how to call them easier in my main.cpp file.
It wants it like this: FIXED so the compiler doesn't complain.
if(
((i == 0) && (pulseAttack.timer >= 5))
||
((i == 1) && (pulseAttack.timer >= 6))
||
((i == 2) && (pulseAttack.timer >= 7))
||
((i == 3) && (pulseAttack.timer >= 8))
)
continue;
wow so picky! it is SO much cleaner though. thank you for putting your time into that, ill fix it all soon
I will do my best to clean the code up tomorrow when I get up. Thank you for your time tonight. Hopefully you'll be around tomorrow sleep well!
No, wait, the compiler is still complaining.
It's the ||s and &&s. You want to tell it which is higher precedence... Something like:
if((i == 0 && pulseAttack.timer >= 5) || (i == 1 && pulseAttack.timer >= 6) || (i == 2 && pulseAttack.timer >= 7) || (i == 3 && pulseAttack.timer >= 8)) continue;
At least I assume that's the logic you are going for.
Looks to be mostly the same as Arthur's but fewer typos[1]
I don't know if you noticed one of my edits above, the post wasn't old enough to send to top
AHA! I see one bugaboo now, you're loading bitmaps meteor1.png and meteor2.png in startEnemy() and it's in the loop, so you're loading bitmaps forever. You should load bitmaps to an ALLEGRO_BITMAP at initialization and only use the ALLEGRO_BITMAP * from then on.
I don't know if you noticed one of my edits above, the post wasn't old enough to send to top
I did. after I hit send
Hey guys, I have been working for some time now to fix things. Still seems to stutter here and there, but overall its not as bad. Do you know how I could turn on the warnings that you are getting? I am currently using Visual Studio 2012.
I have fixed the bugs that was posted in your comment earlier, and removed the infinite load of bitmaps in the loop. Still having issues, but ill continue to change some of my code up in the mean time to see if it helps.
Only had a quick look at the code. I'm pretty sure you're running into the issue on DirectX where every bitmap you draw into (via al_set_target_bitmap) has to get backed up because of the DirectX (mis)feature of losing graphics content sometimes. You have two options to fix it:
1) Use OpenGL (al_set_new_display_flags(ALLEGRO_OPENGL) before al_create_display)
2) Create your bitmaps with the ALLEGRO_NO_PRESERVE_TEXTURE flag (al_set_new_bitmap_flags(ALLEGRO_NO_PRESERVE_TEXTURE))