Dealing with GLUT and OpenGL...

Apprentice
Posts: 11
Joined: 2008.08
Post: #1
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
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #2
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.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2008.08
Post: #3
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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
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
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2008.08
Post: #5
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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
there's a lot of good documentation you're supposed to read Rasp
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2008.08
Post: #7
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
Quote this message in a reply
Member
Posts: 53
Joined: 2007.08
Post: #8
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...ion_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.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #9
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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
The code I wrote was all set to be copied & pasted, there was no call to mangle it beyond usefulness and workingness >Sad
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2008.08
Post: #11
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
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Is Dealing with Opengl ES 2 worth the trouble? Mattonaise 9 8,333 Aug 23, 2011 08:56 PM
Last Post: Mattonaise
  Dealing with inverted textures in OpenGL johncmurphy 7 7,243 Jun 15, 2009 08:11 AM
Last Post: Skorche
  OpenGL and GLUT on MacOSX Salvietta 1 2,423 Sep 19, 2008 12:03 PM
Last Post: OneSadCookie
  Dealing with Battery Low Warning During Full Screen AnotherJake 8 5,840 Feb 8, 2008 12:26 PM
Last Post: OneSadCookie
  Xcode OpenGL GLUT help please! cloudcrono 11 8,763 Sep 23, 2006 04:58 PM
Last Post: jpalm