Create a Layer in Cocoa from a picture

I am quite new to programing with Cocoa(only a few Weeks).
And I want to do a begining to a simple game.
I make a NSView with the class "Picture"(a subbclasse from NSView).
First( game):
I just want to set a picture on the the NSView.
So that I can choose it¥s position.

This is not yet a real game but it will be.

Second (game):
Now I want to set some pictures on the NSView.
I want to have a grid in a array. And You will enter a Letter In the array for the right picture.
Something like this:
NSArray(("G","G","G","G")
("G","T","G","C")
("G","T","T","G"))
(In this case G will be Grass and T will be Tree)
Every picture is the size of 32*32.

I don¥t know how to set anything in NSView and I have just start to learn Object C. I have the basis for C( i have work with Php, basic, litle Java, MySQL).
Please help me how to do the first game

Tree and Grass could be any object, such as @"T" or @"G". But it might be an idea to have aTile class that contains infomation about the tile including the image. Then you could just write for(...)for(...){ [[[[map objectAtIndex:x] objectAtIndex:y] image] compositeTo...

I don¥t get it to work. I have made a NSView subbclasse named MyPicture.
I make a NSView object in the window and set it to MyPicture.
I make the files for MyPicture. I write in MyPicture.h:
[SOURCECODE]
#import <Cocoa/Cocoa.h>

has no place in the interface. As I understand it, Obj-C objects are initialised to contain all zeroes. If you want a new object to be different from this, you'll probably want to write your own init method.

However you do it, you'll want the actual code in the implementation bit rather than the interface.

As I said before, I'm not a Cocoa expert, but IIRC there's something you can tell NSView (or a subclass thereof) that makes it actually display whatever's in it. It almost certainly isn't [myPicture setNeedsDisplay:YES], but I think it's something along those lines.

That might not be it at all though; please post more details of how it's "not working". Does it not compile, or compile but crash, or compile and run without showing you the image?

As I said before, I'm not a Cocoa expert, but IIRC there's something you can tell NSView (or a subclass thereof) that makes it actually display whatever's in it. It almost certainly isn't [myPicture setNeedsDisplay:YES], but I think it's something along those lines.

That might not be it at all though; please post more details of how it's "not working". Does it not compile, or compile but crash, or compile and run without showing you the image?

Yeah. You're right. I haven't worked with NSImage for a while but I'm pretty good with file I/O and string parsing.

Simon, it seems like you maybe want to spend a little more time on some of the basics of Cocoa class definitions, and the uses of certain key methods.

You do not have to initialize your image instance at the time you declare it. You can do that anytime before you actually draw it (but not in the interface). You might consider browsing some of the Hooptie source, which I imagine does a lot of what you are interested in. Personally I've not use NSImage yet.

This will return a NSPoint that is the center of the view. I haven't tested this code, but it should work. Like Feanor said, however, you should probably learn to walk before you try to run. I'll stop with the bad analogies now.

Simon, I do believe Good Master Thimbleby has the code you'll want, though there is a caveat: you should generally invoke NSImage methods within the drawRect: method of your chosen NSView so you can be sure focus is locked on the view you want to draw into. (*blink* Anyone care to parse that sentence into something a bit more readable?)

As long as you don't want to do any orientation tricks (such as rotating or flipping) with your bitmaps, you can use something along the lines of:
[sourcecode]
[myImage compositeToPoint:NSMakePoint(0,0) operation:NSCompositeCopy];
[/sourcecode]
within drawRect:, which will place myImage at the origin (0,0) of the locked view.

To composite the image somewhere else, you can either substitute your own NSPoint (as per jabber's post), or replace the (x,y) tuple in NSMakePoint with different values.

If you replace the NSCompositeCopy with NSCompositeSourceOver, alpha blending will be performed, which is useful if you have sprites overlaid upon or moving across terrain (a passing fad, I say, all these moving sprites and whatnot).

In any case, try putting together some test applications before you move on to building a tile engine. Once you're comfortable with the basic syntax of Objective-C, you may wish to peruse the following links:NSView, NSImage, and Cocoa Drawing & Imaging.

Quote:Originally posted by quillbit Simon, I do believe Good Master Thimbleby has the code you'll want, though there is a caveat: you should generally invoke NSImage methods within the drawRect: method of your chosen NSView so you can be sure focus is locked on the view you want to draw into. (*blink* Anyone care to parse that sentence into something a bit more readable?)

I'll try! When drawing in Cocoa, you must have your "focus" set on something. The focus being what you want to draw to. If you do your drawing in -drawRect, you can be sure that what you are drawing to is your view. If you don't do your drawing in -drawRect, then the image(s) will never appear.

If, for some reason, you must draw outside of -drawRect, you can call -[NSView lockFocus] and then -[NSView unlockFocus] when you are done.

A quick look at the NSView documentation tells me that all subclasses of NSView must implement their own drawRect method. So, probably best to do that... load your image in either the init method or some other one you make up (and remember to call ), and then draw the image when you're inside the drawRect method.

Try not to put the image loading code inside the drawRect method, though, unless you do something clever that ensures the picture only gets loaded once.