iDevGames Forums
Dealing with GLUT and OpenGL... - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Dealing with GLUT and OpenGL... (/thread-2458.html)



Dealing with GLUT and OpenGL... - RingoEST - Aug 14, 2008 09:44 PM

Hello!

i've been messing on a PONG clone for a little while now, and i was wondering, is there any way to use GLUT's keyboard functions without having to enter:

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow(”Windows Name”);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardup);
glutIdleFunc(idle);
glutMainLoop();
return EXIT_SUCCESS;
}

into the main.m in ones project...
i want to write the whole thing in OpenGL and Cocoa, and use GLUT for only keyboard input, not for creating the window and so on, is there a way around this?
or if someone could point out a better way to register keyboard/mouse input without glut?
PS! not touching SDL Grin

Thank You for any reply in advance! Smile

---
Ringo


Dealing with GLUT and OpenGL... - ThemsAllTook - Aug 14, 2008 10:09 PM

If you're using Cocoa for anything, you might as well use it for keyboard input. It does a significantly better job of it than GLUT.


Dealing with GLUT and OpenGL... - RingoEST - Aug 15, 2008 01:58 AM

wow that's a good idea, i'm particularly new to programming so i don't know all the features Cocoa and OpenGL have to offer, Thank You for pointing that out Smile
searched around and looked at examples, but, i still can't quite figure out how to use it, simple PONG game, meaning movement is only to the right and left.

- (void)rightKeyDown: (NSEvent *)theEvent
{

}

- (void)rightKeyUp: (NSEvent *)theEvent
{
}

- (void)leftKeyDown: (NSEvent *)theEvent
{
}

- (void)leftKeyUp: (NSEvent *)theEvent
{
}

but i don't know what to write between the curlys, all the examples are rather confusing at the moment, should get to sleep -_-

Thank You in advance Smile

---
Ringo


Dealing with GLUT and OpenGL... - OneSadCookie - Aug 15, 2008 02:41 AM

Code:
- (void)keyDown:(NSEvent *)event
{
    NSString *chars = [event charactersIgnoringModifiers];
    if ([chars length] == 0) return;
    unichar c = [chars characterAtIndex:0];
    switch (c)
    {
    case NSUpArrowFunctionKey:
        keys[UP] = YES;
        break;
    // ... etc...
    }
}

- (void)keyUp:(NSEvent *)event
{
    // the same, except
          keys[X] = NO;
}

Then when you're updating your game state, check keys[UP], etc.

Code typed direct to web, excuse typos and misremembered method names Rasp


Dealing with GLUT and OpenGL... - RingoEST - Aug 15, 2008 02:54 AM

YES! this is exactly what i needed, i'm going to try it out right now, Thank You very much, i didn't know (wasn't written anywhere -_-) what NS statements correspond to what key, i mean, NSUpArrowFunctionKey, how am i supposed to know it's that? GrinGrin

Thank You! Smile


Dealing with GLUT and OpenGL... - OneSadCookie - Aug 15, 2008 04:19 AM

there's a lot of good documentation you're supposed to read Rasp


Dealing with GLUT and OpenGL... - RingoEST - Aug 15, 2008 05:35 AM

yeah i know XD

anyways, i'm still a bit confused, how do i actually "connect" the program and the code of the keyboard, i mean, in my PONG game, when i press the right arrow key for example, i want the x of the players paddle to raise by one, making the paddle move right...
basically what i'm asking is, does my overly stupid code work? XD

- (void)keyDown:(NSEvent *)event
{
switch(mainPlayer)
{
case NSRightArrowFunctionKey:
mainPlayer += 1;
break;
case NSLeftArrowFunctionKey:
mainPlayer -= -1;
break;
default:
break;
}
}



where

// players paddle
glPushMatrix();
glTranslatef(mainPlayer, 0.0f, 0.0f);
glBegin(GL_QUADS);
glColor3d(0, 1, 0.2);
glVertex2f(50, 30);
glVertex2f(80, 30);
glVertex2f(80, 38);
glVertex2f(50, 38);
glEnd();
glPopMatrix();
glFlush();

where int mainPlayer = 0;


Thank You in advance Smile

PS! i don't really understand all the terms yet, for example, what is the switch () statement anyways? case is logical, but yeah, i haven't found any documentation that explains it either...

---
Ringo


Dealing with GLUT and OpenGL... - Terrydil - Aug 15, 2008 07:27 AM

Check this out to try to understand the switch statement: http://www.cprogramming.com/tutorial/c/lesson5.html . You may want to check out the other tutorials on that site (or another) to make sure you are familiar with the basic stuff (like switch statements) that come up a lot.

Basically, you use it to test for various values of an argument. You could also do this using a bunch of if/else statements.

So the switch statement you are using is not what you are looking for. You are probably wanting to test the event variable, and then move the position of the player depending on whether its a left key press or a right key press. Right now you are testing the mainPlayer variable. You may want to check out http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/Introduction/chapter_1_section_1.html to make sure that you understand exactly how events work in Cocoa.

Also, make sure you are checking your signs when changing the position of the player.


Dealing with GLUT and OpenGL... - AnotherJake - Aug 15, 2008 07:56 AM

Hairball just did Pong in Cocoa/OpenGL a few weeks ago. I put up an improved example of how it can be done. You might want to check it out to get some ideas: http://www.idevgames.com/forum/showthread.php?t=15317


Dealing with GLUT and OpenGL... - OneSadCookie - Aug 15, 2008 01:38 PM

The code I wrote was all set to be copied & pasted, there was no call to mangle it beyond usefulness and workingness >Sad


Dealing with GLUT and OpenGL... - RingoEST - Aug 16, 2008 12:24 AM

Terrydil: Yes i will Smile
i really need to get myself together one day and start reading through apple's dev site documentation if i ever want to become a good enough programmer -_-

and Thank You for the links, the switch statement was what i was looking for by the way, since i didn't understand it XD
anyways, Thank You! Smile

Anotherjake: well that code cleared out my problems, i'm going to get right into clarifying everything that's written there after i post this, biiiiiiig big Thank You!

OneSadCookie: SORRY!!! Sad
i'm a baaaad person Sad