iDevGames Forums
changing mouse icon in cocoa - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: changing mouse icon in cocoa (/thread-3061.html)



changing mouse icon in cocoa - Leroy - Aug 30, 2007 01:58 PM

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?


changing mouse icon in cocoa - Josh - Aug 30, 2007 02:31 PM

I'm going to guess it involves NSCursor.


changing mouse icon in cocoa - Leroy - Aug 30, 2007 02:57 PM

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?


changing mouse icon in cocoa - OneSadCookie - Aug 30, 2007 03:06 PM

No.

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


changing mouse icon in cocoa - Leroy - Aug 30, 2007 03:23 PM

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.


changing mouse icon in cocoa - OneSadCookie - Aug 30, 2007 03:32 PM

I know you have it as a texture, but you must be loading that texture somehow...


changing mouse icon in cocoa - Leroy - Aug 30, 2007 03:38 PM

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



changing mouse icon in cocoa - OneSadCookie - Aug 30, 2007 04:11 PM

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.


changing mouse icon in cocoa - AnotherJake - Aug 30, 2007 04:45 PM

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...


changing mouse icon in cocoa - Leroy - Sep 3, 2007 06:14 PM

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.


changing mouse icon in cocoa - AnotherJake - Sep 3, 2007 07:50 PM

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.


changing mouse icon in cocoa - Leroy - Sep 3, 2007 08:07 PM

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?


changing mouse icon in cocoa - AnotherJake - Sep 3, 2007 08:14 PM

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.


changing mouse icon in cocoa - SethWillits - Sep 4, 2007 07:10 PM

OneSadCookie Wrote:Cry OMG why Cry

That brightened my (so far) crappy day.