Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Cocoa Newbie needs help with Game

Cocoa Newbie needs help with Game
Thread Tools
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Feb 8, 2001, 09:34 PM
 
hello all,
i am new to Cocoa programming and need a few answers. First of all, how would I go about making a full screen game so that I can have access to the individual pixels of the screen, or have access to some sort of low level drawing functions (line, polygon, circle etc.) through Cocoa. And secondly, since I am fairly proficient with Java, is it possible to use some of my Java code in it?
Any help would be greatly appreciated along with links to tutorials and such.

Thanks in advance
     
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Feb 9, 2001, 05:45 AM
 
You could use NSBezier to draw lines and circles and all sorts of crazy shapes. I don't know how to talk to the graphics server directly but I know that it can be done because I have seen programs that did so... I just don't remember what they were . Anyway, if you want to do 3D you could always use OpenGL instead of trying to write your own 3d API. (yikes!)

Anyway, if you want to write to the screen without using the "window" concept then I have no idea if you can do that, but you can always make a full-screen window and then the things to look at would be NSView and NSOpenGLView and other classes like that.
     
Forum Regular
Join Date: Dec 2000
Status: Offline
Reply With Quote
Feb 9, 2001, 12:10 PM
 
Look in
/Developer/Examples/GLUTExamples/skyfly/

That's some OpenGL code for a full-screen game.

You can use OpenGL even if you're not interested in 3d stuff (although there may be better routines I don't know about) just by using 2d shapes (they don't distort).

At any rate, /Developer/Examples/GLUTExamples/GameGLUT/ should have exacly the drawing routines you're looking for.
     
Fresh-Faced Recruit
Join Date: Feb 2001
Location: OR
Status: Offline
Reply With Quote
Feb 9, 2001, 03:29 PM
 
It seems the only possible way to creat a full screen NSView is inside a window. In the NSView doc it says a view must be inside a window to show itself. Someone please correct me if I'm wrong. Anyhow, I've created one and it seems to work pretty good; here's the steps:

1. Create a window and initialize it in the code (not Interface Builder). Set the size of the window to the same size as the screen, and choose NSBorderlessWindowMask as the styleMask. This creates a window without a title bar.
2. Next, set the window level to NSScreenSaverWindowLevel, which will place it in front of the the menu bar.
3. Create a custom NSView the same size as the window, and place it inside the window.
4. Next make the window the key window and move it to the front.
5. Now you're done! Now just draw your view in your custom view object.

The code should look something like this, I'm not in OS X so I can't check it:
Code:
// Create the new window mainWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES]; // Place window in front of everything. [mainWindow setLevel:NSScreenSaverWindowLevel]; // Create the view. (Don't forget to change "MyView" to your custom view.) mainView = [[MyView alloc] initWithFrame:[mainWindow border]]; // Place the view inside the window. Setting it as the content view will make it the "main" view. [mainWindow setContentView:mainView]; // Bring the window to the front and make it active. This also makes the window visible. [mainWindow makeKeyAndOrderFront:self];
I hope I remembered everything. I'll try to check this code later today once I get access to OSX. Does anyone else know of a better way to do this? Oh, btw, this code would be placed in some kind of controller object. I have an example of this app at home, so if you would like it, just email me at rbates@mac.com. Good luck.

Ryan
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Feb 9, 2001, 03:54 PM
 
thanks you guys,
since im already in the process of writing my own 3D API (its more fun to figure it out yourself), i think im going to stay away from openGL. But the examples really helped me out.

Thanks again.
-Loren
     
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Feb 9, 2001, 04:54 PM
 
That sounds like a good setup. For more examples on how to mess around with windows try looking at the source code CPU from softrak on the Stepwise website.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Feb 9, 2001, 05:09 PM
 
Writing your own? Yikes!

I tried that a while back.

Good luck, it's a lot of work :-)
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Fresh-Faced Recruit
Join Date: Feb 2001
Location: OR
Status: Offline
Reply With Quote
Feb 9, 2001, 08:10 PM
 
I'm having a problem with the code I wrote in my last post. The "mainWindow" will not become the key or main Window. When I call "canBecomeKeyWindow" or "canBecomeMainWindow" it returns NO. Is there something I have to do after I initialize the window? Like link the window to the NSApp? I've looked through the NSApplication and NSWindow docs and cannot find anything like this. Has anyone else managed to create a window without using IB?

BTW, sorry if I'm getting off topic here.
Thanks for any replies.

Ryan
     
Fresh-Faced Recruit
Join Date: Feb 2001
Location: OR
Status: Offline
Reply With Quote
Feb 10, 2001, 12:03 AM
 
I think I've figured it out, but I still don't know a solution for it. It seems a window with an NSBorderlessWindowMask cannot become the main/key window! If you replace it with NSTitledWindowMask then it works fine. How am I supposed to get the key events? Is a borderless window automaticly the key window? Do I have to create a custom NSWindow or custom NSApplication object? That could get messy really fast. Any help?

Ryan
     
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Feb 10, 2001, 04:44 AM
 
I don't know if a borderless window is automatically key, but you can easily find out.

if([NSApp keyWindow] == yourMainWindow)
NSBeep();

IF it beeps then you know that your window is already key. Don't use the isKey method in NSWindow; it doesn't always work correctly.
     
Fresh-Faced Recruit
Join Date: Feb 2001
Location: OR
Status: Offline
Reply With Quote
Feb 10, 2001, 11:54 AM
 
Yep, I've done all of those tests, and the NSApp returns nil for both key and main window; but when I do:

if ([[NSApp windows] objectAtIndex:0] == mainWindow)
NSBeep()

it beeps, so I know the window is connected to the app. Any other suggestions?

Ryan
     
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Feb 11, 2001, 10:00 AM
 
Maybe your problem is that you set it at the screensaver level. Try setting it above the menu but not as high as screensaver level. I don't think that screensavers become key. Maybe that level is so high the cursor is under it so, by normal rules, it can't become key.
     
Fresh-Faced Recruit
Join Date: Feb 2001
Location: OR
Status: Offline
Reply With Quote
Feb 11, 2001, 03:17 PM
 
Nope, that's not it either. When I let it go to the default level, it still cannot become key. It's simply that when the window doesn't have any attributes(title bar, etc.) it cannot become the main window. Thanks for the suggestions.

BTW, the cursor is still above the screen saver level. Of course, you can always hide the it though.


Ryan
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:20 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2