Here's a tiny little example Xcode project which might be useful for anyone starting out making games for iPhones and such: Download.
It's mega simple, just shows how I load .png spritesheets and draw sprites with 'em, using OpenGL ES 1.1, in C++.. (except for the bits which have to be Objective-C). Other useful things about it are, it builds universal apps and handles multitasking.
It's probably mostly self explanatory if you've looked at some iOS OGL stuff before. There are lots of tutorials around, and big complicated libs like Cocos2D, but when I was starting I just wanted to know how to draw one sprite. So that's what this shows ya.
The main game loop is in game.mm. The GL setup and draw commands are in GLInterface.mm.. the actual rendering is done in RenderBuffer.mm. The big 1024x1024 sprite sheet (mostly empty) is because that is the maximum size texture you can load on 2nd gen devices. On the new ones you can go up to 2048x2048.
How it works is basically DrawImage() adds textured quads to a list, and RenderBuffer sends everything on that list to the gfx hardware all at once, every frame. It's the "all at once" bit that makes it fast.
Probably some of it is silly and could be made way better but it's fast enough to throw around masses of sprites, with colour changing, scaling and rotating, at 60 FPS, even on 2G iPod Touch! So it's not too bad.
Feel free to use this code for whatever, I hope it's useful to someone. I'll try and update it soon with multitouch capability and sound playing stuff. Any questions, ask away!
This is pretty cool! Been playing with it for couple of days and was quite surprised how fast and well it runs on my 3G! I hope to see future updates :)
ReplyDeleteThanks for sharing!
I stumbled upon your Forget-Me-Not game which led me here which enticed me to download and take a look at your little sprite engine. Very well done. Simple and direct. Yes, you could probably squeeze a little more performance out of it if you used matrices in place of all the trig functions in GLInterface::DrawImage but it seems to me that it isn't really needed for what you've been doing and I'm a big fan of optimizing where it's needed and just leaving it alone where it isn't. Great stuff. Thanks for putting it out there.
ReplyDeleteThanks! This was a very useful starting point for me. Did you write most of Forget.Me.Not in C++ as this snippet suggests?
ReplyDeletesvale - yep, apart from the few Obj-C bits needed to interface with iOS it's all C++. (though I'm increasingly heading back towards plain C style for a lot of things)
ReplyDeleteNice code, simple and clean=) I've done something similar except a bit more complex focusing on state changes etc. You could optimize the rotation by calling the cos/sin functions for a given angle just once..
ReplyDeletehey Erlend..oooh good idea, thanks! Makes perfect sense, dunno why I didn't think of it :) I'll have to get around to updating this code some time..
ReplyDelete