Drawing an image or shape to the screen?

Discussion in 'Mac Programming' started by Gandolfmatt, Feb 20, 2010.

  1. Gandolfmatt macrumors member

    Joined:
    Jul 18, 2008
    #1
    Anyone know how to draw an image or shape to the screen in Obj-C?

    Thanks,
    -Gan
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You don't draw "to the screen" as such. You use the view system to draw (assuming you don't want to go down the potentially more complex route of OpenGL).

    I would recommend you read all of The Cocoa Drawing Guide
     
  3. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #3
    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.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #5
    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
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    ??? 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...
     
  7. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #7
    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/libr...w.html#//apple_ref/doc/uid/TP40002978-CH7-SW4

    Aha! Got it by following this:
    http://developer.apple.com/mac/libr...html#//apple_ref/doc/uid/TP30000890-CH205-SW1
     
  8. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #8
    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
     
  9. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  10. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #10
    It appears that everything works fine except drawing images, those appear upside down. Also I think rotation was flipped.
    Anyway to fix those?


    -Gan
     
  11. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
  12. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #12
    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
     
  13. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #13
    @properties require Objective-C 2.0 which only became available in 10.5, so you'll have to write your own accessors. Have fun ;)
     
  14. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #14
    Oh um... How do I do that? (And what exactly is an accessor?)

    Thanks,
    -Gan
     
  15. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #15
    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];
    }
     
  16. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #16
    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.
     
  17. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #17
    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.
     
  18. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #18
    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
     
  19. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #19
    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
     
  20. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #20
    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
     
  21. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #21
    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
     
  22. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #22
    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.
     
  23. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #23
    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
     
  24. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #24
    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...
     
  25. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #25
    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
     

Share This Page