Drawing an image or shape to the screen?


Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
I've already read that though perhaps I should rephrase my question with a bit more info:
I'm an iPhone programmer working to take previous knowledge and apply it to program for the mac. How would I set up a UIView with the ability to detect mouse and keyboard input as well as draw UIImages and shapes such as rectangles, ovals, and lines?
I have all the code to do this on the iPhone so if I were to be able to have a UIView, would most code work for the mac?
It'd be great if you guys could point me to a tutorial or bit of code that sets things up. If it were just remotely similar to what I'm use to I could take it from there.


Thanks,
-Gan
P.S. I'm use to CoreGraphics.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,510
24
London
You can't have a UIView on the Mac at all. You must use NSView. Drawing (assuming you were using Quartz) is pretty similar. Event handling too.
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Coding for the mac in an NSView is incredibly similar to the iPhone, though I have one problem.
How do I connect my CustomView to my NSView? Never really used Interface Builder and it appears quite strange.


-Gan
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,510
24
London
Coding for the mac in an NSView is incredibly similar to the iPhone, though I have one problem.
How do I connect my CustomView to my NSView? Never really used Interface Builder and it appears quite strange.


-Gan
??? You shouldn't be trying to connect a view to another view. The NSView should be your custom view. Drag/drop the header for the custom view in and then change the class of the NSView instance to your custom class. I'm pretty sure there is Interface Builder documentation included with the developer tools...
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Ah sorry, by NSView I meant my NSView class which is titled GameView.
I'm having a horrid time fighting with IB. Googled for a while on how to do this with minimal luck. It appeared that I had successfully connected my CustomView to my GameView class but then used the some basic code to draw a rectangle and nothing was drawn to the screen.
Can any of you guys post some instructions on how to connect a CustomView to an NSView class you have in the project?


-Gan
P.S. This tells me how to use it but doesn't give me instructions on making one:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaViewsGuide/SubclassingNSView/SubclassingNSView.html#//apple_ref/doc/uid/TP40002978-CH7-SW4

Aha! Got it by following this:
http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/A_Tour_of_Xcode/020-Xcode_Workflow_Tutorial/qt_projects.html#//apple_ref/doc/uid/TP30000890-CH205-SW1
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Hey guys, I've successfully ported my whole iPhone Game Template to mac but there's one issue:
How do I set the drawing origin to top left instead of bottom left?


Thanks,
-Gan
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
It appears that everything works fine except drawing images, those appear upside down. Also I think rotation was flipped.
Anyway to fix those?


-Gan
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
So far so good. I've been able to convert my iPhone app to the mac quite easily. Than I was able to get it to compile for both 10.6 and 10.5.
Now I'm trying to get it to compile to 10.4. All the code works except there's a massive amount of errors on the @property lines. Do any of you know how to fix this?

Thanks,
-Gan
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
@properties require Objective-C 2.0 which only became available in 10.5, so you'll have to write your own accessors. Have fun ;)
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
You should read up on memory management. Basically, you've been spoiled with @properties :p

Example:

Code:
// .h
@property (readwrite, copy) NSString *name;

// .m
@synthesize name;
Becomes:

Code:
// .h
- (void)setName:(NSString *)aName;
- (NSString *)name;

// .m
- (void)setName:(NSString *)aName
{
    if (name != aName) {
        [name release];
        name = [aName copy];
    }
}

- (NSString *)name
{
    return [[name retain] autorelease];
}
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Youch. That'd take a ton of recoding. Anyways, already read the memory management documentation and am managing it manually instead of with Garbage Collector. Was able to get my app down to a solid 16mb of ram usage the whole time.
Just in case you guys were wondering what you helped me do:
Simple Rpg 10.5+

Should I go for 10.4 and do all that work? Would it be worth it?

Thanks,
-Gan
P.S. The reason my application looks like an iPhone app is because I ported it directly over without much changing of the interface.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,510
24
London
Should I go for 10.4 and do all that work? Would it be worth it?
No, not really. 10.5/10.6 have over 65% of the market (according to OmniGroup statistics). The time and effort you spend coding for 10.4, not to mention any bugs you introduce in you setter/getter methods (which will then impact your 10.5/10.6 versions unless you use conditional compilation statements) will not see much return as people not willing to pay to update to at least 10.5 are very unlikely to pay for your software.
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Ah, thanks. :) I think I'll leave 10.4 alone then.
I'm looking into Cocoa Async Socket for networking. I'm going to try to make a mac server and mac+iPhone client. This will be for a game so it'd need fast packet sending...
Is Cocoa Async Socket a good choice? Do you guys know anything better?

Thanks,
-Gan
 

chown33

Moderator
Staff member
Aug 9, 2009
8,379
4,366
Pumpkindale
I'm looking into Cocoa Async Socket for networking. I'm going to try to make a mac server and mac+iPhone client. This will be for a game so it'd need fast packet sending...
Is Cocoa Async Socket a good choice? Do you guys know anything better?
AsyncSocket should be fine.

You might have to provide more details on your protocol, if you expect a more detailed response. AsyncSocket on the loopback interface easily exceeds 100 MB/sec on a Core Solo mini (the puniest of all Intel-based Macs).

You could also use BLIP:
http://groups.google.com/group/blip-protocol
http://jens.mooseyard.com/2008/05/blip-come-n-get-it/

Google search terms:
jens alfke blip
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Thanks. :) Async Socket seems to only give me the server. Is it simple to make the client? (Sorry if this seems like an unneeded question, I have yet to read the documentation when I get home)

Thanks,
-Gan
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Alright, I have my Async server set up and a client that connects to it.
Everything works perfect except for some odd reason the server isn't receiving the data sent from the client.
Code:
case NSStreamEventHasSpaceAvailable:
			event = @"NSStreamEventHasSpaceAvailable";
			if (theStream == oStream)
			{
				//send data
				//uint8_t buffer[11] = "I send this";				
				//int len;
				
				NSString *welcomeMsg = @"Yo Yo Yo";
				NSData *welcomeData = [welcomeMsg dataUsingEncoding:NSASCIIStringEncoding];
				
				len = [oStream write:[welcomeData bytes] maxLength:sizeof([welcomeData bytes])];
				if (sizeof([welcomeData bytes]) > 0)
				{
					NSLog(@"Command send");
					[oStream close];
				}
			}
			break;
Anything wrong with my code?


Thanks,
-Gan
 

chown33

Moderator
Staff member
Aug 9, 2009
8,379
4,366
Pumpkindale
Code:
len = [oStream write:[welcomeData bytes] maxLength:sizeof([welcomeData bytes])];
sizeof([welcomeData bytes]) strikes me as wrong. You might want to check it and see if it makes sense.

Or just fix it. There's an NSData method that returns the length. Use that.


It also seems to me that closing ostream may be premature. I don't recall if it closes immediately (bad in this case), or only after it's done writing any previously buffered content.
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Tried it, still doesn't work. I'm going to play with it a bit more. If you guys see anything else that could be wrong, please post.

Thanks,
-Gan
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,510
24
London
Can you explain what you think you are checking with this line:

Code:
if (sizeof([welcomeData bytes]) > 0)
You might as well have written

Code:
if (true)
Hint: len (the return from the oStream write method) has meaning but you are ignoring it...
 

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
case NSStreamEventHasSpaceAvailable:
event = @"NSStreamEventHasSpaceAvailable";
if (theStream == oStream)
{
//send data
uint8_t buffer[11] = "I send this";
int len;

len = [oStream write:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
NSLog(@"Command send");
[oStream close];
}
}
break;
This appears to be the correct code, yet it still doesn't work. How can I check to make sure the packet is actually getting to the server?


Thanks,
-Gan