This post is a portion of Part 7 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.
- 7.0 – Sprites
- 7.1 – Bitmap Transparency in Allegro 5
- 7.2 – Basic Animation in Allegro 5
- 7.3 – Using Allegro 5 to Create a Sprite Sheet
- 7.4 – Sprite Sheet Animation in Allegro 5
Images
Up until this point, we have been working predominately with static images. Whether they were graphical primitives or loaded bitmaps, the items appearing on screen have been solid, unmoving (mostly), and unchanging. In this part, we change that by introducing something called a Sprite.
Sprites, not the drink
Sprites refer to any graphical screen element that moves or changes (for the most part). Therefore, our Space Ship from part 5 could be considered a sprite, while the background (if there was one) would not be. In reality, what being a “sprite” means is having the complex framework that allows our game image to do something meaningful. A little bit we started this framework. In our Space Ship struct we had the ships position, bounding box, speed, and a few other attributes. That is already a good start for sprite support. A couple things that our ship didn’t have that we are going to look at adding in this section is a self contained image and animation.
Classy Structs
Like I mentioned above, the key to utilizing the power of sprites is to have well thought out code supporting your game actions. It is important for a sprite object to know where is it, what animation frame it is on, if it is collide-able, and what image represents itself on the screen. Normally that means that I would program a Sprite class and build all of the foundational functionality into it. As you will recall, however, I have vowed to do this whole series with as little “advanced” coding as possible. What this means is that we will be doing all of our work with structs. What structs lose in power, they gain in simplicity. Remember that if you would to tackle larger scale projects, it is a good idea to learn classes and OOP (object oriented programming), but for now, using structs will suffice.
Hi! How can I draw scaled bitmap regions? Are there any possibilities to do that?
Check here:
http://alleg.sourceforge.net/a5docs/refman/graphics.html#al_draw_scaled_bitmap
This allows from drawing regions built in
Hey man, Im just starting coding with allegro and i still find it somewhat hard to remember all the code i should write, should i watch all the videos again or should i continue??
Thanks in adnvance
Definitely watch the videos again. You are not SUPPOSED to memorize the techniques and things in the first programming lessons. A couple helpful ways to do this:
1. Add tons of comments on each new line of code, then go over your code once or twice when your done writing it.
2. Think long about the ways you could make games with the code you have learned. Remember that while programming tuts will give you the tools, you need to write the game code on your own.
3. Gradually watch videos. Maybe go back and rewatch the videos before, watch 3 new ones, and then watch chapter X again. You should be able to write code without going back and forth between videos, and you should be able to work on different projects than the ones displayed, more importantly.
Hope this helped!
Great advice. Thanks for posting.
Its not about memory. Its about understanding. I give you modular tools. You can build whatever you want, but a hammer is only useful once you understand how it works.
Long story short. Watch them again if you feel it would help. Make your own stuff based on my code if you feel that would help more.
It’s probably best to try not so much to remember the code, but how it’s placed. Instead of focusing on the details, try to focus on the different elements. That’s what Object Oriented Programming is all about. He did refer to the allegro reference manual in this video, if you had that, you wouldn’t have to remember each function. Just knowing how and when to use the functions is the hard part. So, learn everything from C++ you can especially the parts about OOP (arrays, vectors, classes, structs, pointers, methods, mutators, accessors), then open up the Allegro world.
This.