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 > new to Cocoa... best way to implement a custom (textured) 2D view?

new to Cocoa... best way to implement a custom (textured) 2D view?
Thread Tools
Dedicated MacNNer
Join Date: May 2003
Location: Atlanta, GA
Status: Offline
Reply With Quote
Aug 31, 2003, 06:52 PM
 
Hey all,

I'm new to Cocoa (and Obj-C) but I'm very proficient in both Java and C.... so for my first real project (aside from hello world and such) I've decided to do an OSX version of tetrinet...

www.tetrinet.org

I'm getting the protocol info from gtetrinet... so it's a derivative work (and will therefore be free), but the rest is being entirely re-done from scratch by me in Obj-C.

I have enough of the backend stuff done now (field coordinates, placing/rotating/moving of blocks in space and collision detection) that I'm ready to start working on an interface.

My question is this. I want to create a view (in Cocoa) that displays the following:

1) a background image

2) a "block" bitmap for each location where a part of block should appear. these need to be scaled from a larger image (say 128x128) to support multiple resolutions.

This view needs to sit inside a regular application window. Ideally, I want it to only update when I send the view class an update message containing data on the filed I want it to draw. This way 1 view class would work for every tetris field, regardless of size/shape.

My quesiton is, what would be the best way to do this? At first I thought a custom NSView would be the way to go, but I'm not sure it allows nearly the amount of draw control I need. Is there any way to create a custom direct quartz view? I also thought about using a (flat) OpenGL view and just texturing the front "face" of my blocks, but this seems like entirely too much work.

I coded a tetris game in Java for a course final at GA Tech long ago, and I remember just creating a sub-window w/ swing and drawing a filled rectangle for each little "block" in the tetris field, updating as necessary... I'd like to be able to do something this simple, but I'm willing to explore the OpenGL option if necessary.

Also, at a later time (once I get the basics done) I plan to add effects; for example a block bomb might overlay an explosion video on a section of the field as the blocks scatter. So whatever option I choose needs to have some future flexibility as far as drawing is concerned.

Sorry this was so long. Basically I want to know what classes/interfaces I should be looking at to do my actual rendering on screen.

Thanks!
Alex

G7 Software: home Tetrinet Aqua
-----
"Utopia" 1Ghz TiBook SuperDrive w/ 1Gb RAM.
     
Forum Regular
Join Date: Jul 2001
Location: Québec, Canada
Status: Offline
Reply With Quote
Aug 31, 2003, 09:27 PM
 
May I suggest you take a look to the small game example provided by Apple: BlastApp. It should sit right at /Developer/Examples/Java/AppKit/.

It is programmed in Java Cocoa and it does in fact use an NSView... and is fun to play with.
     
Dedicated MacNNer
Join Date: May 2003
Location: Atlanta, GA
Status: Offline
Reply With Quote
Sep 1, 2003, 01:08 AM
 
Originally posted by Michel Fortin:
May I suggest you take a look to the small game example provided by Apple: BlastApp. It should sit right at /Developer/Examples/Java/AppKit/.

It is programmed in Java Cocoa and it does in fact use an NSView... and is fun to play with.
I'm not using Java though...

Sorry if I wasn't clear. I KNOW a great deal of Java, which has helped me pick up Obj-C pretty quickly (a lot of the same concepts).

I think I'm going to go with a custom NSView for now...
Alex

G7 Software: home Tetrinet Aqua
-----
"Utopia" 1Ghz TiBook SuperDrive w/ 1Gb RAM.
     
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Sep 3, 2003, 11:29 PM
 
Hate to be unhelpful, but you may want to look at OpenGL for your graphics rendering, instead of just Quartz. Speed is always good.
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Sep 4, 2003, 02:25 AM
 
Originally posted by Thinine:
Hate to be unhelpful, but you may want to look at OpenGL for your graphics rendering, instead of just Quartz. Speed is always good.
"Premature optimization is the root of all evil"
Tony Hoare

If you don't already know OpenGL I'd say there's no need to use it. You're not after 90+ FPS, are you? Good use of NSView can actually be pretty fast (if you're not updating the entire view with each move) and the drawing code isn't that complicated. NSView is easier and you can get the basics working much faster. If you find that you really need more speed later on, there won't be that much code to rewrite anyway.

Also, you'll learn more Cocoa stuff if you use NSView =)
     
Senior User
Join Date: Apr 2001
Location: Bolingbrook, IL, USA
Status: Offline
Reply With Quote
Sep 5, 2003, 05:29 PM
 
Though it might be overkill, I'd also like to throw in Cocoa Sprite Kit. It can handle a lot of the stuff you'd have to do to make a game.

A custom NSView would work too though.

Make sure you only redraw the parts you're changing though.

Good luck.
     
   
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 02:21 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