فهرست منبع

Added GLFWimage struct.

Camilla Berglund 11 سال پیش
والد
کامیت
8fa9cc0de3
8فایلهای تغییر یافته به همراه74 افزوده شده و 55 حذف شده
  1. 19 6
      include/GLFW/glfw3.h
  2. 12 11
      src/cocoa_window.m
  3. 2 3
      src/input.c
  4. 1 2
      src/internal.h
  5. 8 7
      src/win32_window.c
  6. 6 5
      src/x11_window.c
  7. 22 18
      tests/cursor.c
  8. 4 3
      tests/cursoranim.c

+ 19 - 6
include/GLFW/glfw3.h

@@ -863,6 +863,23 @@ typedef struct GLFWgammaramp
     unsigned int size;
 } GLFWgammaramp;
 
+/*! @brief Image data.
+ *
+ *  @ingroup window
+ */
+typedef struct GLFWimage
+{
+    /*! The width, in pixels, of this image.
+     */
+    int width;
+    /*! The height, in pixels, of this image.
+     */
+    int height;
+    /*! The pixel data of this image, arranged left-to-right, top-to-bottom.
+     */
+    unsigned char* pixels;
+} GLFWimage;
+
 
 /*************************************************************************
  * GLFW API functions
@@ -1936,13 +1953,9 @@ GLFWAPI void glfwSetCursorPos(GLFWwindow* window, double xpos, double ypos);
 
 /*! @brief Creates a cursor.
  *
- *  @param[in] width The desired cursor width.
- *  @param[in] height The desired cursor height.
+ *  @param[in] image The desired cursor image.
  *  @param[in] xhot The desired x-coordinate of the cursor hotspot.
  *  @param[in] yhot The desired y-coordinate of the cursor hotspot.
- *  @param[in] format Not used.
- *  @param[in] data The cursor image data in RGBA8 format, packed in rows from
- *  top to bottom.
  *
  *  @return A new cursor ready to use or `NULL` if an error occurred. If you
  *  don't destroy the cursor by calling `glfwDestroyCursor` it will be destroyed
@@ -1952,7 +1965,7 @@ GLFWAPI void glfwSetCursorPos(GLFWwindow* window, double xpos, double ypos);
  *
  *  @ingroup input
  */
-GLFWAPI GLFWcursor* glfwCreateCursor(int width, int height, int xhot, int yhot, int format, const void* data);
+GLFWAPI GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot);
 
 /*! @brief Destroys a cursor.
  *

+ 12 - 11
src/cocoa_window.m

@@ -1201,37 +1201,38 @@ void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
         CGAssociateMouseAndMouseCursorPosition(true);
 }
 
-int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int xhot, int yhot,
-                              int format, const void* data)
+int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
+                              const GLFWimage* image,
+                              int xhot, int yhot)
 {
-    NSImage* image;
+    NSImage* native;
     NSBitmapImageRep* rep;
 
     rep = [[NSBitmapImageRep alloc]
         initWithBitmapDataPlanes:NULL
-                      pixelsWide:width
-                      pixelsHigh:height
+                      pixelsWide:image->width
+                      pixelsHigh:image->height
                    bitsPerSample:8
                  samplesPerPixel:4
                         hasAlpha:YES
                         isPlanar:NO
                   colorSpaceName:NSCalibratedRGBColorSpace
                     bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
-                     bytesPerRow:width * 4
+                     bytesPerRow:image->width * 4
                     bitsPerPixel:32];
 
     if (rep == nil)
         return GL_FALSE;
 
-    memcpy([rep bitmapData], data, width * height * 4);
+    memcpy([rep bitmapData], image->pixels, image->width * image->height * 4);
 
-    image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];
-    [image addRepresentation: rep];
+    native = [[NSImage alloc] initWithSize:NSMakeSize(image->width, image->height)];
+    [native addRepresentation: rep];
 
-    cursor->ns.object = [[NSCursor alloc] initWithImage:image
+    cursor->ns.object = [[NSCursor alloc] initWithImage:native
                                                 hotSpot:NSMakePoint(xhot, yhot)];
 
-    [image release];
+    [native release];
     [rep release];
 
     if (cursor->ns.object == nil)

+ 2 - 3
src/input.c

@@ -353,8 +353,7 @@ GLFWAPI void glfwSetCursorPos(GLFWwindow* handle, double xpos, double ypos)
     _glfwPlatformSetCursorPos(window, xpos, ypos);
 }
 
-GLFWAPI GLFWcursor* glfwCreateCursor(int width, int height, int xhot, int yhot,
-                                     int format, const void* data)
+GLFWAPI GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot)
 {
     _GLFWcursor* cursor;
 
@@ -364,7 +363,7 @@ GLFWAPI GLFWcursor* glfwCreateCursor(int width, int height, int xhot, int yhot,
     cursor->next = _glfw.cursorListHead;
     _glfw.cursorListHead = cursor;
 
-    if (!_glfwPlatformCreateCursor(cursor, width, height, xhot, yhot, format, data))
+    if (!_glfwPlatformCreateCursor(cursor, image, xhot, yhot))
     {
         glfwDestroyCursor((GLFWcursor*) cursor);
         return NULL;

+ 1 - 2
src/internal.h

@@ -588,8 +588,7 @@ int _glfwPlatformExtensionSupported(const char* extension);
  */
 GLFWglproc _glfwPlatformGetProcAddress(const char* procname);
 
-int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int xhot, int yhot,
-                              int format, const void* data);
+int _glfwPlatformCreateCursor(_GLFWcursor* cursor, const GLFWimage* image, int xhot, int yhot);
 
 void _glfwPlatformDestroyCursor(_GLFWcursor* cursor);
 

+ 8 - 7
src/win32_window.c

@@ -1223,21 +1223,22 @@ void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
     }
 }
 
-int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int xhot, int yhot,
-                              int format, const void* data)
+int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
+                              const GLFWimage* image,
+                              int xhot, int yhot)
 {
     HDC dc;
     HBITMAP bitmap, mask;
     BITMAPV5HEADER bi;
     ICONINFO ii;
     DWORD* target = 0;
-    BYTE* source = (BYTE*) data;
+    BYTE* source = (BYTE*) image->pixels;
     int i;
 
     ZeroMemory(&bi, sizeof(bi));
     bi.bV5Size        = sizeof(BITMAPV5HEADER);
-    bi.bV5Width       = width;
-    bi.bV5Height      = -height;
+    bi.bV5Width       = image->width;
+    bi.bV5Height      = -image->height;
     bi.bV5Planes      = 1;
     bi.bV5BitCount    = 32;
     bi.bV5Compression = BI_BITFIELDS;
@@ -1254,14 +1255,14 @@ int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int xh
     if (!bitmap)
         return GL_FALSE;
 
-    mask = CreateBitmap(width, height, 1, 1, NULL);
+    mask = CreateBitmap(image->width, image->height, 1, 1, NULL);
     if (!mask)
     {
         DeleteObject(bitmap);
         return GL_FALSE;
     }
 
-    for (i = 0;  i < width * height;  i++, target++, source += 4)
+    for (i = 0;  i < image->width * image->height;  i++, target++, source += 4)
         *target = (source[3] << 24) | (source[0] << 16) | (source[1] << 8) | source[2];
 
     ZeroMemory(&ii, sizeof(ii));

+ 6 - 5
src/x11_window.c

@@ -1356,22 +1356,23 @@ void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
     }
 }
 
-int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int xhot, int yhot,
-                              int format, const void* data)
+int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
+                              const GLFWimage* image,
+                              int xhot, int yhot)
 {
     int i;
 
-    XcursorImage* native = XcursorImageCreate(width, height);
+    XcursorImage* native = XcursorImageCreate(image->width, image->height);
     if (native == NULL)
         return GL_FALSE;
 
     native->xhot = xhot;
     native->yhot = yhot;
 
-    unsigned char* source = (unsigned char*) data;
+    unsigned char* source = (unsigned char*) image->pixels;
     XcursorPixel* target = native->pixels;
 
-    for (i = 0;  i < width * height;  i++, target++, source += 4)
+    for (i = 0;  i < image->width * image->height;  i++, target++, source += 4)
         *target = (source[3] << 24) | (source[0] << 16) | (source[1] << 8) | source[2];
 
     cursor->x11.handle = XcursorImageLoadCursor(_glfw.x11.display, native);

+ 22 - 18
tests/cursor.c

@@ -88,29 +88,33 @@ static void command_callback(int key)
 
         case GLFW_KEY_C:
         {
-            if (cursor == NULL)
-            {
-                int w = cursorSize[currentSize].w;
-                int h = cursorSize[currentSize].h;
-                int x, y, i = 0;
-                unsigned char *image = malloc(4 * w * h);
+            int x, y;
+            GLFWimage image;
+            unsigned char* pixels;
+
+            if (cursor)
+              break;
+
+            image.width = cursorSize[currentSize].w;
+            image.height = cursorSize[currentSize].h;
 
-                for (y = 0;  y < h;  y++)
+            pixels = malloc(4 * image.width * image.height);
+            image.pixels = pixels;
+
+            for (y = 0;  y < image.height;  y++)
+            {
+                for (x =  0;  x < image.width;  x++)
                 {
-                    for (x = 0;  x < w;  x++)
-                    {
-                        image[i++] = 0xff;
-                        image[i++] = 0;
-                        image[i++] = 255 * y / h;
-                        image[i++] = 255 * x / w;
-                    }
+                    *pixels++ = 0xff;
+                    *pixels++ = 0;
+                    *pixels++ = 255 * y / image.height;
+                    *pixels++ = 255 * x / image.width;
                 }
-
-                cursor = glfwCreateCursor(w, h, w / 2, h / 2, 0, image);
-                currentSize = (currentSize + 1) % SizeCount;
-                free(image);
             }
 
+            cursor = glfwCreateCursor(&image, image.width / 2, image.height / 2);
+            currentSize = (currentSize + 1) % SizeCount;
+            free(image.pixels);
             break;
         }
 

+ 4 - 3
tests/cursoranim.c

@@ -66,10 +66,11 @@ static float star(int x, int y, float t)
 static GLFWcursor* load_frame(float t)
 {
     int i = 0, x, y;
+    const GLFWimage image = { SIZE, SIZE, buffer };
 
-    for (y = 0; y < SIZE; y++)
+    for (y = 0;  y < image.width;  y++)
     {
-        for (x = 0; x < SIZE; x++)
+        for (x = 0;  x < image.height;  x++)
         {
             buffer[i++] = 255;
             buffer[i++] = 255;
@@ -78,7 +79,7 @@ static GLFWcursor* load_frame(float t)
         }
     }
 
-    return glfwCreateCursor(SIZE, SIZE, SIZE / 2, SIZE / 2, 0, buffer);
+    return glfwCreateCursor(&image, image.width / 2, image.height / 2);
 }
 
 int main(void)