For 2D (sprite) games, do I use OpenGL or something else?

Moderator
Posts: 3,570
Joined: 2003.06
Post: #16
kendric Wrote:Curious


For me opengl and game logic is like 98% of my code. How about you guys?

For me, OpenGL is maybe only 5-10% and my game logic is the big dog at maybe 80% -- mostly C. I have an awful lot of code repetition in the game logic as a side-effect of using brute-force C. My usual style is to write first and generalize later, which is one of the reasons I tend not to prefer C++ up front. Not saying I wouldn't use C++ though; it just doesn't fit with my coding style most of the time. Finally, I have maybe another 10% in general branching/state management and setup, much of which will often be Objective-C. I have nearly all of my OpenGL tucked away in a little renderer module. I hardly ever call the GL directly, and I don't use the GL's modelView transform routines anymore. Another aspect to my coding is that my drawing routines are completely separate from my update routines. That is, you'll never see me do something like objectX += velocity * dt; sitting next to glDrawArrays(); in the same method/function.

Going back to the description of how you set up your game hierarchy as a network of objects hints a lot about why Objective-C wasn't working for you performance-wise. On the Mac I seriously doubt that would be much of a performance impediment, but on iPhone, yes, a large network of objects like that in Objective-C is going to involve a lot of messaging. Yes, in that case, you might prefer using C++. Not to say that you can't do it in Obj-C on iPhone, but indeed, there will be performance issues to contend with once a non-trivial level of complexity starts forming.

Something to keep in mind about Objective-C performance on iPhone: Much of TouchFighter was written in Objective-C (even more than I would use myself).
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2009.05
Post: #17
Wow, it feels like the C-vs-Pascal days all over again Wink

However, since nobody but the first reply actually mentioned it, I'm assuming that OpenGL really is the only practical graphics technology for the iPhone.

So if I might ask another question;

To draw a 2D sprite on the screen I have to create a rectangular shape using two triangles, and then apply a texture to it, some of which can be transparent. Is that right?

If somebody has a block of code or a tutorial page that does this: initialise OpenGL, load a texture, apply it to a rectangle and blit it on the screen as a 2d sprite, (and perhaps make it fly around) ... I'd love to see it. Every single OpenGL tutorial I can find seems to go straight into 3D :-/

I did have another look at Cocos-2d last night, but I can't even get the simple examples to compile, let alone do anything else :-/

Thanks,
Lindsay
Quote this message in a reply
Member
Posts: 51
Joined: 2009.02
Post: #18
There really should be more 2D OpenGL ES examples for the iPhone. Many people start with the Texture2D class. There used to be a game called Crashlander that did everything you want, but it was pulled.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #19
Nosredna Wrote:There really should be more 2D OpenGL ES examples for the iPhone. Many people start with the Texture2D class. There used to be a game called Crashlander that did everything you want, but it was pulled.
Yes, I have no idea why Apple pulled CrashLanding. Weird, and unfortunate. There definitely should be more sample code for games programming. I get the feeling they're working hard on the next major release, so we'll see how things pan out in the future.

lindsay Wrote:Wow, it feels like the C-vs-Pascal days all over again Wink

However, since nobody but the first reply actually mentioned it, I'm assuming that OpenGL really is the only practical graphics technology for the iPhone.
For game graphics, yes, OpenGL is the way to go for iPhone (and Mac too).

lindsay Wrote:So if I might ask another question;

To draw a 2D sprite on the screen I have to create a rectangular shape using two triangles, and then apply a texture to it, some of which can be transparent. Is that right?
Yes, that's right.

lindsay Wrote:If somebody has a block of code or a tutorial page that does this: initialise OpenGL, load a texture, apply it to a rectangle and blit it on the screen as a 2d sprite, (and perhaps make it fly around) ... I'd love to see it. Every single OpenGL tutorial I can find seems to go straight into 3D :-/
Check out Apple's GLSprite sample for iPhone.
Quote this message in a reply
⌘-R in Chief
Posts: 1,237
Joined: 2002.05
Post: #20
Talk about a hijacked thread. Rasp

kendric Wrote:Infact I even saw performance bloat from using a synthesized function vs doing my own implementation.

Probably because you didn't specify nonatomic in the property definition. If you did, I believe there shouldn't be any difference.
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2009.05
Post: #21
AnotherJake Wrote:Check out Apple's GLSprite sample for iPhone.
Great, thanks! Smile
Quote this message in a reply
Member
Posts: 24
Joined: 2009.02
Post: #22
lindsay Wrote:Wow, it feels like the C-vs-Pascal days all over again Wink

Seeing as you brought it up. Pascal is better! LOL
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.05
Post: #23
Hi!

I think the CrashLanding example was pretty nice; it encapsulated the OpenGL stuff in the Texture2D class. I recommend you get it somehow. Why did Apple pull it anyway?

On the same subject, can someone shed a quick light on how to apply transparency to a Texture2D from that example? I've never touched OpenGL, although I'm starting to get into it. If you know a good resource to read about OpenGL ES, that would be nice too.

Thanks!
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #24
All my properties are nonatomic for what its worth. What i saw may be due to sharks oddities when looking at objective c code. You can read more about that in my shark post.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.05
Post: #25
lindsay Wrote:If somebody has a block of code or a tutorial page that does this: initialise OpenGL, load a texture, apply it to a rectangle and blit it on the screen as a 2d sprite, (and perhaps make it fly around) ... I'd love to see it. Every single OpenGL tutorial I can find seems to go straight into 3D :-/

You might want to pay a visit to:
http://www.71squared.co.uk

I'm not sure that this is what you're looking for but they have a couple video tutorials about showing a 2d sprite on the screen (using OpenGL es Wink).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Modfied OpenGL Sprite Rendering tonyb 3 2,873 Jul 26, 2009 07:30 AM
Last Post: tonyb
  OpenGL Animated Sprite Question Aboqa 6 5,729 May 6, 2009 11:17 AM
Last Post: AnotherJake
  Do you activate depth buffer when doing 2D games and opengl es? riruilo 13 8,585 Apr 3, 2009 11:02 AM
Last Post: miq01
  OpenGL ES sprite alpha Holmes 27 14,368 Mar 16, 2009 01:06 PM
Last Post: Holmes
  OpenGl Es Sprite render jjslay 43 25,053 Nov 1, 2008 04:24 AM
Last Post: wonza