[iPhone/OpenGL-ES] Loading/Drawing Pictures & Main Loop, where is it?

Posts: 13
Joined: 2008.06
Post: #1
Hello everyone!

After having done a lot of DirectX I'm finally done (once again) on windows for a week or so, so I wanted to spend this time wisely learning some iPhone related stuff.

I've had some free time in between projects to look through a lot of iPhone game/application sources and explanations. Now generally I'm not too good with documentations, I usually get distracted easily from one wall of text followed by another series of walls of texts while a basic example code or explanation would also do the job, but with less confusion (again, for some people, like myself.)

Sadly I used to have an old source code from Apple of a project called Crashlanding which had a few things covered of what I wanted to ask, but no matter as I can ask the great community on here instead. Smile

For one, how does drawing the textures work? I've seen how they get loaded (which also seemed a bit confusing, but I understood major part of the code,) but not how they were drawn.
I have a pretty long background in using SDL, so normally I'd say it could be easy enough to load a picture into a surface and draw it with SDL_BlitSurface(,,,);, but of course things aren't that easy on here..

Secondly..I've looked everywhere through the code and honestly I can't figure this out.. What is the main loop? You know, where we have the ingame logic, and can draw things on-screen, this has been bugging me for a while.

I've looked at the open source 2d engine called Cocos2d-iPhone, but I'd rather learn how to do these basic things as I can't have someone hold my hand and do it for me forever.
For every game I've made, I created a small built in wrapper that would store and draw images from a vector, same with audio and such. And I'd like to do the same for the iPhone instead of using an engine. (No offense to anyone, if it sounds like one somehow. Sneaky )

I've been looking everywhere for code that showed how to load and then draw images (PNG with transparency support, anyone?) specifically with OpenGL-ES, nor properly explained where/what/how the main loop was.
(However I did look at the Scene/Layer management in Cocos2d-iPhone and kudos, it looks like a very neat feature to work with, but still didn't really get to fish out what the main loop was.)
(Also, about the PNG Transparrency, I actually know this is possible, so it's not a question. Already read a topic about it Rasp)

The reason why I'd rather not browse through the Documentation the whole time is because I've got more school work coming up, and because of that I'd rather learn straightforward things I can use quickly so I can always go through the documentation later to make sure I don't miss out on anything.
This is just something I _REALLY_ want to learn next to school. (No, it's not a school project.)

And honestly I've thought about getting a book..But without a penny to spend (sadly + long story,) I can't get any other help than what's been my best friend for a whole while without disappointing me; the internet.

So.. Can anyone please help me with these questions? Preferably with some source code to go with? I learn a LOT faster by seeing example(s)..

And since I've seen this questions be asked a few times without the answer that helped anyone, I'm pretty sure a lot of people would find it useful to know these, and I'm really sorry if this has been posted somewhere as I haven't been able to find anything at all. Been searching google and many dev forums for weeks now.. Sad

(Also small side-question. Last week I googled on what the status for SDL on the iPhone was. Is there any way that by the time SDL does happen properly/stable on the iPhone, that it'd be distributable through Appstore (free or paid, not sure if either has a different policy?) without being rejected by apple because of some violation in terms? Also..Since SDL is as far as I know software based, won't it take more CPU thus draining the battery? Sorry if I got the software-only based part wrong, I recall it from a topic of quite a time back.)
Quote this message in a reply
Posts: 457
Joined: 2003.08
Post: #2
I don't know too much about OpenGL, but Stanford had an iPhone development course, and their lecture slides and such are on the web. Take a look at Lecture 16. You might want to save it on to your computer as I don't know how long these will be up there.

Quote this message in a reply
Posts: 13
Joined: 2008.06
Post: #3

Thanks for the link, it does contain a lot of information and helpful things, but sadly I cannot find anything answering any of question I asked above, sorry.
(Unless the PVR way is the only way to go with loading images, and I'm fairly certain it's not as I saw people use different methods..)
Quote this message in a reply
Posts: 1,066
Joined: 2004.07
Post: #4
Have you looked in the sample code on the iPhone developer site? There are at least three OpenGL samples which cover 2D and 3D rendering. Should be enough to get you up and running.

As for the main loop, there really isn't a main loop. At least not when you are doing things the "Apple way" as shown in those samples. Basically you set up an NSTimer that will tick every 1/60th of a second. When it ticks, you do your updating and drawing.

As for the textures, that's something you'll find in the GLSprite sample on the iPhone site. In fact, if you look at that sample, it will likely show you exactly how to get up and running with OpenGL with loading and displaying textures.
Quote this message in a reply
Posts: 13
Joined: 2008.06
Post: #5

I didn't think of the main loop being that way, now things make MUCH more sense, thank you!

And I do understand how the loading/drawing images goes, partly.. I still can't seem to find where the x and y positions of the image that gets drawn on-screen.

As far as I can see, shouldn't it be in here?
// Sets up an array of values to use as the sprite vertices.
const GLfloat spriteVertices[] =
-0.5f, -0.5f,
0.5f, -0.5f,
-0.5f,  0.5f,
0.5f,  0.5f,

// Sets up an array of values for the texture coordinates.
const GLshort spriteTexcoords[] =
0, 0,
1, 0,
0, 1,
1, 1,

// Updates the OpenGL view when the timer fires
- (void)drawView
    // Make sure that you are drawing to the current context
    [EAGLContext setCurrentContext:context];
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    //glRotatef(3.0f, 0.0f, 0.0f, 1.0f);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];

Is there an article or something else that explains more about those two arrays above holding information about the picture 'coordinates'?
I'm not really used to 3d as I'm still learning my way, any help is appreciated. Smile
Quote this message in a reply
Posts: 61
Joined: 2009.01
Post: #6
Hey, you can check out the code I made for a Sprite class that loads and creates OpenGL textures from a file.


The working code is about 6 posts down.

Note that it isn't the best way to draw Sprites if you're looking for efficiency, but it will do the trick if you're not going crazy. It suits my purposes just fine, which is one very large backdrop, about 200 stationary 50x50 tile images, and 100-200 animating sprites.
Quote this message in a reply
Posts: 133
Joined: 2008.05
Post: #7
There is definitely a main run loop, that's what UIApplicationMain(...) creates when its called in your main function.

Creating a timer simply schedules a timer into the main run loop, so each time the main run loop starts over, it checks to see if it has any timers scheduled and whether or not their ready to fire.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Cube drawing: VBOs, instanced geometry, octrees Symbol$ 5 13,501 Dec 23, 2011 01:43 PM
Last Post: Skorche
  OpenGL Differences between iPhone Sim and Real iPhone SparkyNZ 5 10,095 Apr 13, 2011 11:40 AM
Last Post: SparkyNZ
  Drawing menu versus drawing game johncmurphy 4 6,334 Jan 6, 2010 09:14 AM
Last Post: AnotherJake
  OpenGL ES : Drawing Text... savage 8 14,390 Jan 4, 2010 08:59 PM
Last Post: alerus
  openGL n00b looking for FBO drawing examples or direction auptown 5 10,443 Nov 4, 2009 02:16 AM
Last Post: Eskema