changing mouse icon in cocoa

Member
Posts: 105
Joined: 2007.03
Post: #1
Say that I have an inventory window(opengl view) with pictures (textures quads really)of the player's inventory items. I want to be able to click the item and then change the mouse(or at least make it appear that way) to the newly clicked inventory item. What's the simplest way to achieve this in cocoa?
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #2
I'm going to guess it involves NSCursor.
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #3
Josh Wrote:I'm going to guess it involves NSCursor.

I would guess so as well.

Maybe I'm wrong but if I'm not this should make my question more specific. I would assume that I would need to create a new cursor with an image of the inventory item. If this is the case, then I guess my question would be, while creating an image and after calling [image lockFocus], would I be able to draw directly into the image using a textured quad through opengl?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
No.

But you're loading your textures into OpenGL somehow, right? Are you not using Quartz (via CoreGraphics/ImageIO/Cocoa/whatever) for that already?
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #5
OneSadCookie Wrote:No.

But you're loading your textures into OpenGL somehow, right? Are you not using Quartz (via CoreGraphics/ImageIO/Cocoa/whatever) for that already?

I'm only using opengl. The item that I want to draw is in a texture sprite sheet in opengl.

I just assumed that the only to do it was to initialize a new cursor with an image, but I don't know how to write texture data directly into the image file (I mean I can see how you could do it that way but there must be something easier). But if there is some totally different path I'm overlooking, please let me know.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
I know you have it as a texture, but you must be loading that texture somehow...
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #7
OneSadCookie Wrote:I know you have it as a texture, but you must be loading that texture somehow...

Here's my texture loading routine(so maybe the answer is stdlib?)

Code:
BOOL load_tga(texture_type *texture, char *filename)            // Loads A TGA File Into Memory
{    
    GLubyte        TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};    // Uncompressed TGA Header
    GLubyte        TGAcompare[12];                                // Used To Compare TGA Header
    GLubyte        header[6];                                    // First 6 Useful Bytes From The Header
    GLuint        bytesPerPixel;                                // Holds Number Of Bytes Per Pixel Used In The TGA File
    GLuint        imageSize;                                    // Used To Store The Image Size When Setting Aside Ram
    GLuint        temp;                                        // Temporary Variable
    GLuint        type=GL_RGBA;                                // Set The Default GL Mode To RBGA (32 BPP)
    GLuint        i;
    FILE *file = fopen(filename, "rb");                        // Open The TGA File

    if(    file==NULL ||                                        // Does File Even Exist?
        fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||    // Are There 12 Bytes To Read?
        memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0                ||    // Does The Header Match What We Want?
        fread(header,1,sizeof(header),file)!=sizeof(header))                // If So Read Next 6 Header Bytes
    {
        fclose(file);                                        // If Anything Failed, Close The File
        return FALSE;                                        // Return False
    }

    texture->width  = header[1] * 256 + header[0];            // Determine The TGA Width    (highbyte*256+lowbyte)
    texture->height = header[3] * 256 + header[2];            // Determine The TGA Height    (highbyte*256+lowbyte)
    
    if(    texture->width    <=0    ||                                // Is The Width Less Than Or Equal To Zero
        texture->height    <=0    ||                                // Is The Height Less Than Or Equal To Zero
        (header[4]!=24 && header[4]!=32))                    // Is The TGA 24 or 32 Bit?
    {
        fclose(file);                                        // If Anything Failed, Close The File
        return FALSE;                                        // Return False
    }

    texture->bpp    = header[4];                            // Grab The TGA's Bits Per Pixel (24 or 32)
    bytesPerPixel    = texture->bpp/8;                        // Divide By 8 To Get The Bytes Per Pixel
    imageSize        = texture->width*texture->height*bytesPerPixel;    // Calculate The Memory Required For The TGA Data

    texture->imageData=(GLubyte *)malloc(imageSize);        // Reserve Memory To Hold The TGA Data

    if(    texture->imageData==NULL ||                            // Does The Storage Memory Exist?
        fread(texture->imageData, 1, imageSize, file)!=imageSize)    // Does The Image Size Match The Memory Reserved?
    {
        if(texture->imageData!=NULL)                        // Was Image Data Loaded
            free(texture->imageData);                        // If So, Release The Image Data

        fclose(file);                                        // Close The File
        return FALSE;                                        // Return False
    }

    for(i=0; i<(int)imageSize; i+=bytesPerPixel)        // Loop Through The Image Data
    {                                                        // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
        temp=texture->imageData[i];                            // Temporarily Store The Value At Image Data 'i'
        texture->imageData[i] = texture->imageData[i + 2];    // Set The 1st Byte To The Value Of The 3rd Byte
        texture->imageData[i + 2] = temp;                    // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
    }

    fclose (file);                                            // Close The File

    // Build A Texture From The Data
    glGenTextures(1, &texture[0].texID);                    // Generate OpenGL texture IDs

    glBindTexture(GL_TEXTURE_2D, texture[0].texID);            // Bind Our Texture
    
    //extra
//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);    // Linear Filtered
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);    // Linear Filtered
    
    if (texture[0].bpp==24)                                    // Was The TGA 24 Bits
    {
        type=GL_RGB;                                        // If So Set The 'type' To GL_RGB
    }

    glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);

    return TRUE;                                            // Texture Building Went Ok, Return True
}



typedef struct                // Create A Structure
{
    GLubyte    *imageData;        // Image Data (Up To 32 Bits)
    GLuint    bpp;            // Image Color Depth In Bits Per Pixel.
    GLuint    width;            // Image Width
    GLuint    height;            // Image Height
    GLuint    texID;            // Texture ID Used To Select A Texture
} texture_type;                    // Structure Name
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Cry OMG why Cry

Anyway, once you've decompressed your TGA, you can use the pointer to create an NSBitmapImageRep, which you can use to create an NSImage.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #9
Or you can hide the cursor with [NSCursor hide]; when it's over your OpenGLView and draw the cursor in OpenGL if you like.

Why are you using TGA's? That code looks like a major pain to have written. There are better ways...
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #10
AnotherJake Wrote:Or you can hide the cursor with [NSCursor hide]; when it's over your OpenGLView and draw the cursor in OpenGL if you like.

I don't believe I can do that because I have a view for the actual game, and then another view(two totally different sublasses of nsopenglview) for the inventory.


AnotherJake Wrote:Why are you using TGA's? That code looks like a major pain to have written. There are better ways...

What's the downside to using tgas? Anyways if you can suggest a better format and(or) refer me to some better code I would appreciate it.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #11
Leroy Wrote:I don't believe I can do that because I have a view for the actual game, and then another view(two totally different sublasses of nsopenglview) for the inventory.
Having two different views shouldn't make any difference.

Quote:What's the downside to using tgas? Anyways if you can suggest a better format and(or) refer me to some better code I would appreciate it.
TGAs don't compress well. PNGs and JPEGs are widely recommended here. You can use either their respective libs which will be cross-platform, or you can use one of the many code suggestions and resources found here on the forums (a simple forum search will likely yield lots of results). Among them you will find either Quicktime or Quartz being the most popular Mac-only approaches. There seems to be a nice cross-platform image loading lib floating around lately, but I don't recall the name, nor have I looked at it (yet), sorry.
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #12
AnotherJake Wrote:Having two different views shouldn't make any difference.

If my game view is in the left edge of the screen and my inventory view is in the right edge of the screen, then when I drag an item from the inventory view over to the game view (and in the process cross over the center of the screen which has no opengl view at all) how could I possible use opengl to draw the cursor image over this middle space?
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #13
Ah! I see what you're saying. I didn't think about that at all... I didn't realize you'd be dragging and needing the cursor in-between the two. Yeah, pretty-much nixes the whole OpenGL cursor drawing idea.
Quote this message in a reply
⌘-R in Chief
Posts: 1,247
Joined: 2002.05
Post: #14
OneSadCookie Wrote:Cry OMG why Cry

That brightened my (so far) crappy day.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  itunes: upload new app, but the icon in device has not the red "1" sefiroths 10 8,834 Oct 9, 2012 01:31 AM
Last Post: sefiroths
  Mouse coordinates with Cocoa vnvrymdreglage 13 6,601 Apr 8, 2007 07:26 AM
Last Post: vnvrymdreglage
  Creating a shortcut icon on desktop ?? asobo 14 6,559 Jan 16, 2007 02:25 PM
Last Post: stevejohnson
  Changing cursor in C++/SDL possible? Bonked 1 3,175 Dec 16, 2006 06:40 AM
Last Post: sealfin
  Cocoa: Mouse location while dragging window Fenris 4 4,977 Jul 14, 2005 04:46 PM
Last Post: Fenris