This post is a portion of Part 8 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.
- 8.0 – Structured Sprites
- 8.1 – Our Sprite Struct
- 8.2 – Sprite Struct Demo
- 8.3 – Sprite Struct Gravity Demo
Having created our Sprite Struct in the previous video, this video pushes the code even further. By the end, you will see how we can utilize a simple array and the code we have already looked at to animate 15,000 independent sprites on the screen at the same time. Enjoy!
Full source can be found here.
Mike,
I’m looking for the sprite sheet ‘space-core.png’. I’m having hard time looking for free sheets like the ones you have. Is there any chance you could direct me to the source?
Just do what I did. Type the URL to the page he got it from as he shows it — yes, it takes awhile to type it out, but you get the image.
The space-core image used in the video:
Link
When I put 15000 sprites up my FPS jumped to 75 – 80. ;0
Hi, you’ve made a slight mistake which makes the orbs travel the same speed on both directions:
sprite.x += sprite.velX * sprite.dirX;
sprite.y += sprite.velX * sprite.dirY;
Should be
sprite.x += sprite.velX * sprite.dirX;
sprite.y += sprite.velY * sprite.dirY;
Also another way to see how much it taxes the system is using Windows Task Manager. For me rendering 150 sprites takes %7 or so, but 10000 takes %50 which is the maximum since my PC is dual core.
http://blog.makeyourflashgame.com/232/to-make-a-game-in-flash-part-iii-first-blender-models.html
link to the image he used
Awesome. Thanks
Question (for anyone really), in the UpdateSprites function,
Quick question (and if anyone has any ideas please chime in), in the UpdateSprites function why do we check which direction the sprite is going in? If the x position of the sprite is less than zero, wouldn’t the direction automatically be -1? Similarly, wouldn’t the direction HAVE to be 1 when checking to see if it’s moving off the right hand of the screen? Maybe I wasn’t looking close enough but I removed those extra constraints to see what purpose they might server but I didn’t notice any changes.
Anyways, thank you for the wonderful tutorials! You have a very clear voice and get to the point quickly. If I had to make a criticism I would say that I would have preferred to see classes but OOP courses you have sound like they’re going to shut me up.
It depends on the velocity, if it’s low there’s a chance that once you detect an x position which is less than zero, you change the directions, but due to slow velocity it won’t move enough to make it to the next pixel in the next cycle. this will cause you to detect a less than zero x position again and flip the direction again. This will cause your sprite to get stuck on the screen’s borders.
But if the velocity is high enough there won’t be any difference as you mentioned.
When you’re programming, it’s always a good idea to think about different possibilities and adding arguments for the purpose of being on the safe side will never hurt!
One important question: Why we incrementate a frame variable in place where orbs are updating (the timer event, always 1/60 sec)? When i set an array to 15000 (i didn’t got stack overflow) i have around 70fps! But if I move incrementation to if statement where draw functions are, i got correct fps – 15. So question: it’s my thoughts right?
I use the term frame a little differently. I should have used a different term. I talk more about it in the comments above. It has been a while, but I believe I am measuring update frames and not render frames
First of all, thank you Mike for your videos! I have been looking for a good material like that for a long time!
For those who are having problems checking the acutal FPS.
remove your ‘frames++;’ from the timer event handler and place it
right before ‘al_flip_display();’
because you actually want to measure how many frames your game can draw per second.
Have fun, guys!
Hey ! First of all, thank you for all these great videos. I have one question that I just can’t understand. I don’t understand why ball is going the “correct/right” way when it hits the edge. I mean, how come that it knows to go the correct way (basic physics) i.e
When it hits the left bottom of the screen, how does the ball know to go down-right and then up (which is the right way) and not just some random way (perhaps up and then right).
I hope you understand what I want to say, just some basic physics. I can’t see that we have implemented anything like that in our code.
I just figure out which side the hit was on and then go in the opposite direction. Its in the code. I challenge you to find it!
If you want the physics explanation, it has to do with Newton’s third law of motion. Read more here:
https://en.wikipedia.org/wiki/Third_Law_of_Newton
Got it ! I think this is it
//
if((sprite.x = WIDTH – sprite.frameWidth && sprite.dirX == 1))
//
If it goes left and hits the left side of he screen, it will change its direction to the right (same for the other side).
I think this is what I was looking for. Thank you guys !! Also, thanks for that link ! It helped a lot 🙂
//
if((sprite.x = WIDTH – sprite.frameWidth && sprite.dirX == 1))
{
sprite.dirX *= -1;
sprite.animationDirection *= -1;
}
//
Don’t know why it didn’t copy the whole code, but you know what I’m pointing to.
Why don’t you just pass the array of the orbs to the functions and have a for loop in the functions?
The original site for the space-core.png is now dead, you can obtain the file here: http://i305.photobucket.com/albums/nn211/trioss/space-core_zpsceb9fc9b.png
Thank you for posting an archived copy of the sprite sheet.
Hi Mike, I have copied this code but for the dragon sprite used in the previous videos for sprites. However, my dragon has no animation even with all the code copied exactly how you have. Any idea why it isn’t animated?
I’m thinking it could be because of their only being 8 frames and it is cycling through them too quick?
When I get above around 50 sprites my FPS drops a lot. Is this just my hardware or have i done something wrong?
Thanks