This post is a portion of Part 5 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- 5.0 – Our First Game
- 5.1 – Our First Game: The Player
- 5.2 – Our First Game: Timing and Movement
- 5.3 – Our First Game: The Projectiles
- 5.4 – Our First Game: The Enemies
- 5.5 – Our First Game: Collision Detection
- 5.6 – Our First Game: Game Logic
- 5.7 – Our First Game: Conclusion
Building off of the last video, we now are adding timing and keyboard inputs to our game
Full source can be found here.
Notice that the source code is slightly different from the video? Here is why.
I don’t know if you’ll see this any time soon, but I keep trying to compile and VS 2010 keeps giving me a popup window that says “Unhandled exception at 0x0f7d4a36 in FBPgame.exe: 0xC0000005: Access violation reading location 0x00000000.”. In the error log it lists about 30 dll’s with the error message “Cannot find or open the PDB file”
Any idea what in god’s name is happening?
Can you email me your project? Probably a code or linker issue
I have the very same issue – got any ideas guys?
I’ve already gotten rid of my code. I just redid the whole thing following you step by step. I’ll try doing things differently sometime when I can find a way to use allegro with a different IDE. I don’t like VC++ too much. Anyway thanks for offering help, and thanks for the videos. You’re the only one that explains it this well
You can install it on Dev-C, its really easy
1.-Download “allegro-5.0.5-1cmw.DevPak”
2.-Execute the file and clic next,next…
3.-Done
4.-Create a new project “Allegro5 aplication”
I am using Dev-C and all runs perfect 🙂
Awesome, thanks!
Hey Mike, I was wondering if there was a way to save a Visual2010 “Allegro Game” template with the config properties already set for future coding. And I mean, without having to deal with XML :S. If you don’t know of a way, I suppose learning to make an XML sheet for Visual templates couldn’t kill me..
once i entered the if(ev.type == ALLEGRO_EVENT_TIMER)code my game loaded but no movement, i dont get an error or anything just no movement
can i send u my code and can you take a look please?
Sure, it may be a little while before I get to it though.
I’m kinda having a similar issue just like the person above, the window shows for a few seconds then it disappears with all the .dlls not executing which they never do but I never had a problem and I know for sure that I typed everything right. It could just be the laptop I’m on or the compiler I guess. Im using VC++ which I never had any problems with. here is the .cpp i believe, thanks for the tutorials on this, I really enjoy and study this every day for hours: http://www.mediafire.com/?qb55r5e84mn2vtd
I left my email just in case, thanks Mike.
Nevermind, I found out that it might have been a missing } or too many of them. I think thats what the problem was. I made FPS a const int since i forgot too and then I rewrote all the if statements in a different order of: ALLEGRO_EVENT_KEY_DOWN, ALLEGRO_EVENT_KEY_UP, ALLEGRO_EVENT_DISPLAY_CLOSE, ALLEGRO_EVENT_TIMER, and lastly the(redraw && al_is_event_queue_empty(event_queue)
hope this works for anyone having a problem with the window not showing up. I added the al_rest() function and noticed that the window stayed longer so it could have been something but idk, I just did the above and it worked.
stupid stupid me, i was wondering why my ship didn’t move… i was going over my text for two weeks and let my IT teacher have a few looks but we both could’t figure it out. today i opened the project and my eye suddenly caught the problem, i had typed “if(ev.type == ALLEGRO_KEY_DOWN)” istead of “if(ev.type == ALLEGRO_EVENT_KEY_DOWN)”. what a rookie mistake. luckely i type al my own code so i remember the comments better. and the funiest part was that from the beginning i knew something was wrong with the if statement inside the gameloop. but still needed 2 weeks to figure it out: it was waiting for the down key and not for a key to go down 😛
Can’t you just do
bool keys[5] = false; to define them all at once instead of going through them all?
The answer to that question is no. If you declare the array
bool keys[5] = false; wouldn’t build first of all. Secondly, it’s good programming practice to DEFAULT ALL VALUES. I believe if you did the code
bool keys[5] = {false}; which I can only assume you are asking, it will only assign the first value to false as default, IIRC. Anyway, it’s best just to follow the instructor’s examples if you aren’t sure.
I may want to add that bool keys[5] is an array, which is actually 5 different locations in memory, starting with 0 to 4. if you just set the first the others may have values in them like this
bool keys[5] = {false,NULL,NULL,NULL,NULL};
you could also do
bool keys[5] = {false, 0, 0, 0, 0}; and get a similar result.
when you allocate memory for an array, sometimes there are values there, that you may not know about. it’s best just to format it to something that you KNOW is there, rather than just taking a chance at it. i have built an array in the past and output it to the console and found some strange numbers just sitting around in there, like -80000000000 or something crazy. Which I had to spend hours trying to debug to get my program to run.
I put all the controls into a function called “ShipControls” and passed out &ev, &done, &redraw, and &ship. My game loop is so much more organized! It was a warm and fuzzy feeling brrrrr 8)
Please answer me as fast as you can, I’ve been trying to compile the game I did exactly what the video number 5.2 but still gives me the same error error C2628: ‘cannon’ followed by ‘int’ is not valid (has forgotten ‘;’?)
error C2440: ‘initializing’: can not convert from ‘int’ to ‘const canon’, C2664: ‘al_create_display’: can not convert parameter 1 from ‘const cannon’ to ‘int
and when I click to the problem sends me this part
const int width = 800;
const int height = 400;
You didn’t do things the exact same because there is no “cannon” on my videos. That being said, you probably forgot the semi colon at the end of your struct definition. Feel free to email me to code for further investigation, though I am pretty sure a semi colon is missing.
I just renamed my struct to cannon… 🙂 just for me to understand
enum IDS{player,bulet,enemy};
struct cannon {
int ID;
int X;
int Y;
int life;
int speed;
int choquex;
int choquey;
int score;
} ;//semi colon added here by me (Mike) to illustrate where it goes.
This comment has been edited by me (Mike) for length.
sorry for the change of variables in my program they are all the same… I’m trying to translate them from my language by the way thanks from Costa Rica 🙂
ship.colitoionx = 6; int choquex;
ship.colotiony = 7; int choquey;
player jugador
width ancho
height alto
I shortened your comment and added the semi colon I had mentioned before. Structs need semi colons after there definitions.
I’m picking up Allegro for the first time and I was fortunate enough to find these tutorials. It’s a great help! However, I do have one question. I have a scrolling background that moves at a fixed rate; However, I noticed that when I’m using my input at a rapid pace my background appears to start moving faster as well. I’m assuming this is related to the timer, so I was wondering if you had any ideas of how I could correct this issue.
I’m following these tutorials after our tutor asked us to view them, in order to learn C++ to create a game for our end of year project (ending my first year as a computer science undergrad student). They are great tutorials for beginners and a real help in introducing myself to the C++ language (I’ve done C#, but not C++). So, thank you for that!
However, in this tutorial I have 2 slight ‘problems’, nothing that prevents it from building, but things that are still not quite right.
When I run a debug on my program, the screen always start off as white, only when I initialise an event, does it turn black. Is this some sort of setting in Visual Studio?
Also, I have the window size set to 800×400 and the up/down/left/right voids set as the same values as the tutorial, but my spaceship goes too far off the screen on both axis. It appears that it is setting the value of ‘0’ to the centre of my object, allowing half of it to go off the screen.
Any advice/help would be greatly appreciated. Thank you!
Edit: Sorry, I have just noticed that your spaceship is going off the screen by the same amount (been staring at these monitors too long I think!)
Still the white screen is an issue.
Howdy, glad you got it figured out. Not sure about the white screen. Feel free to email me your code.
Thank you for your reply, much appreciated!
I have sent you an email.
sir i have i ques, i think a good one 😛
when FPS is not monitored, you said we have to monitor the FPS and should fix it at a constant speed for 2 reasons —1. for same game experience in different machines
2. if i dont, the loop will perform many times(about 1000) in a sec so if i press up/down/left/right the change in the axis will happen 1000 times in a sec so the object will move very fast and will move out of the screen. and theoretically i agree completely 🙂
BUT
when i(also in your vid) ran the this code in my pc i saw the spaceShip is moving much faster in 60 FPS than the un-monitored FPS(about 1000 i believe) … but shouldn’t it be the other way around????
sir, pls this ques is bugging me to death, SOS
Good Evening Sir Mike, I followed exactly as your tutorial above.. Everything works and not every single errors that appears while i try to compile it.. But, during i press key right, the ship went forward but, it wont stop until i press another key.. After that, the ship is stuck at the end of the screen (x or y) and wont move.. Please help, i dunno what’s wrong.. Newbie here in C++
For those of u who get access violation error ,
If u use al_draw before u create the display .mostly I got because of things like this failing to initialize something but using that value