From 608de57358f57af259c7ad3b86659c6fa34a58f5 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 23 Jan 2014 15:24:57 +0100 Subject: [PATCH] Cleanup. --- .gitignore | 2 ++ src/cocoa_platform.h | 2 +- src/cocoa_window.m | 17 +++++------ src/input.c | 21 ++++++-------- src/internal.h | 2 +- src/win32_window.c | 68 +++++++++++++++++++++----------------------- src/x11_window.c | 31 +++++++++----------- tests/cursor.c | 46 ++++++++++++++++-------------- 8 files changed, 93 insertions(+), 96 deletions(-) diff --git a/.gitignore b/.gitignore index 07ff42be..3e206b4a 100644 --- a/.gitignore +++ b/.gitignore @@ -52,6 +52,8 @@ tests/*.app tests/*.exe tests/accuracy tests/clipboard +tests/cursor +tests/cursoranim tests/defaults tests/empty tests/events diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index b69bb69a..b9acb66e 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -130,7 +130,7 @@ typedef struct _GLFWmonitorNS //------------------------------------------------------------------------ typedef struct _GLFWcursorNS { - id handle; + id object; } _GLFWcursorNS; diff --git a/src/cocoa_window.m b/src/cocoa_window.m index b4ac29f2..e825014c 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -46,7 +46,7 @@ static void setModeCursor(_GLFWwindow* window) if (window->cursorMode == GLFW_CURSOR_NORMAL) { if (window->cursor) - [(NSCursor*) window->cursor->ns.handle set]; + [(NSCursor*) window->cursor->ns.object set]; else [[NSCursor arrowCursor] set]; } @@ -1201,7 +1201,7 @@ void _glfwPlatformApplyCursorMode(_GLFWwindow* window) CGAssociateMouseAndMouseCursorPosition(true); } -int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int cx, int cy, +int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int xhot, int yhot, int format, const void* data) { NSImage* image; @@ -1223,18 +1223,18 @@ int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int cx if (rep == nil) return GL_FALSE; - memcpy([rep bitmapData], data, 4 * width * height); + memcpy([rep bitmapData], data, width * height * 4); image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)]; [image addRepresentation: rep]; - cursor->ns.handle = [[NSCursor alloc] initWithImage:image - hotSpot:NSMakePoint(cx, cy)]; + cursor->ns.object = [[NSCursor alloc] initWithImage:image + hotSpot:NSMakePoint(xhot, yhot)]; [image release]; [rep release]; - if (cursor->ns.handle == nil) + if (cursor->ns.object == nil) return GL_FALSE; return GL_TRUE; @@ -1242,7 +1242,8 @@ int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int cx void _glfwPlatformDestroyCursor(_GLFWcursor* cursor) { - [(NSCursor*) cursor->ns.handle release]; + if (cursor->ns.object) + [(NSCursor*) cursor->ns.object release]; } void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) @@ -1250,7 +1251,7 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) if (window->cursorMode == GLFW_CURSOR_NORMAL && window->ns.cursorInside) { if (cursor) - [(NSCursor*) cursor->ns.handle set]; + [(NSCursor*) cursor->ns.object set]; else [[NSCursor arrowCursor] set]; } diff --git a/src/input.c b/src/input.c index 477cf030..22f0f6eb 100644 --- a/src/input.c +++ b/src/input.c @@ -353,7 +353,7 @@ GLFWAPI void glfwSetCursorPos(GLFWwindow* handle, double xpos, double ypos) _glfwPlatformSetCursorPos(window, xpos, ypos); } -GLFWAPI GLFWcursor* glfwCreateCursor(int width, int height, int cx, int cy, +GLFWAPI GLFWcursor* glfwCreateCursor(int width, int height, int xhot, int yhot, int format, const void* data) { _GLFWcursor* cursor; @@ -361,16 +361,15 @@ GLFWAPI GLFWcursor* glfwCreateCursor(int width, int height, int cx, int cy, _GLFW_REQUIRE_INIT_OR_RETURN(NULL); cursor = calloc(1, sizeof(_GLFWcursor)); - - if (!_glfwPlatformCreateCursor(cursor, width, height, cx, cy, format, data)) - { - free(cursor); - return NULL; - } - cursor->next = _glfw.cursorListHead; _glfw.cursorListHead = cursor; + if (!_glfwPlatformCreateCursor(cursor, width, height, xhot, yhot, format, data)) + { + glfwDestroyCursor((GLFWcursor*) cursor); + return NULL; + } + return (GLFWcursor*) cursor; } @@ -385,14 +384,12 @@ GLFWAPI void glfwDestroyCursor(GLFWcursor* handle) // Make sure the cursor is not being used by any window { - _GLFWwindow* window = _glfw.windowListHead; + _GLFWwindow* window; - while (window) + for (window = _glfw.windowListHead; window; window = window->next) { if (window->cursor == cursor) glfwSetCursor((GLFWwindow*) window, NULL); - - window = window->next; } } diff --git a/src/internal.h b/src/internal.h index e9b70206..2e2d3e5e 100644 --- a/src/internal.h +++ b/src/internal.h @@ -588,7 +588,7 @@ int _glfwPlatformExtensionSupported(const char* extension); */ GLFWglproc _glfwPlatformGetProcAddress(const char* procname); -int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int cx, int cy, +int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int xhot, int yhot, int format, const void* data); void _glfwPlatformDestroyCursor(_GLFWcursor* cursor); diff --git a/src/win32_window.c b/src/win32_window.c index d9271f2f..0f91fa27 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1223,63 +1223,60 @@ void _glfwPlatformApplyCursorMode(_GLFWwindow* window) } } -int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int cx, int cy, +int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int xhot, int yhot, int format, const void* data) { - HDC hdc; - HBITMAP hBitmap, hMonoBitmap; + HDC dc; + HBITMAP bitmap, mask; BITMAPV5HEADER bi; ICONINFO ii; - DWORD *buffer = 0; - BYTE *image = (BYTE*) data; - int i, size = width * height; - - ZeroMemory(&bi, sizeof(BITMAPV5HEADER)); + DWORD* target = 0; + BYTE* source = (BYTE*) data; + int i; + ZeroMemory(&bi, sizeof(bi)); bi.bV5Size = sizeof(BITMAPV5HEADER); bi.bV5Width = width; bi.bV5Height = -height; bi.bV5Planes = 1; bi.bV5BitCount = 32; bi.bV5Compression = BI_BITFIELDS; - bi.bV5RedMask = 0x00FF0000; - bi.bV5GreenMask = 0x0000FF00; - bi.bV5BlueMask = 0x000000FF; - bi.bV5AlphaMask = 0xFF000000; + bi.bV5RedMask = 0x00ff0000; + bi.bV5GreenMask = 0x0000ff00; + bi.bV5BlueMask = 0x000000ff; + bi.bV5AlphaMask = 0xff000000; - hdc = GetDC(NULL); + dc = GetDC(NULL); + bitmap = CreateDIBSection(dc, (BITMAPINFO*) &bi, DIB_RGB_COLORS, + (void**) &target, NULL, (DWORD) 0); + ReleaseDC(NULL, dc); - hBitmap = CreateDIBSection(hdc, (BITMAPINFO*) &bi, DIB_RGB_COLORS, (void**) &buffer, - NULL, (DWORD) 0); - - ReleaseDC(NULL, hdc); - - if (hBitmap == NULL) + if (!bitmap) return GL_FALSE; - hMonoBitmap = CreateBitmap(width, height, 1, 1, NULL); - - if (hMonoBitmap == NULL) + mask = CreateBitmap(width, height, 1, 1, NULL); + if (!mask) { - DeleteObject(hBitmap); + DeleteObject(bitmap); return GL_FALSE; } - for (i = 0; i < size; i++, buffer++, image += 4) - *buffer = (image[3] << 24) | (image[0] << 16) | (image[1] << 8) | image[2]; + for (i = 0; i < width * height; i++, target++, source += 4) + *target = (source[3] << 24) | (source[0] << 16) | (source[1] << 8) | source[2]; - ii.fIcon = FALSE; - ii.xHotspot = cx; - ii.yHotspot = cy; - ii.hbmMask = hMonoBitmap; - ii.hbmColor = hBitmap; + ZeroMemory(&ii, sizeof(ii)); + ii.fIcon = FALSE; + ii.xHotspot = xhot; + ii.yHotspot = yhot; + ii.hbmMask = mask; + ii.hbmColor = bitmap; cursor->win32.handle = (HCURSOR) CreateIconIndirect(&ii); - DeleteObject(hBitmap); - DeleteObject(hMonoBitmap); + DeleteObject(bitmap); + DeleteObject(mask); - if (cursor->win32.handle == NULL) + if (!cursor->win32.handle) return GL_FALSE; return GL_TRUE; @@ -1287,7 +1284,8 @@ int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int cx void _glfwPlatformDestroyCursor(_GLFWcursor* cursor) { - DestroyIcon((HICON) cursor->win32.handle); + if (cursor->win32.handle) + DestroyIcon((HICON) cursor->win32.handle); } void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) @@ -1302,7 +1300,7 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) if (cursor) SetCursor(cursor->win32.handle); else - SetCursor(LoadCursor(NULL, IDC_ARROW)); + SetCursor(LoadCursorW(NULL, IDC_ARROW)); } } diff --git a/src/x11_window.c b/src/x11_window.c index d4324c19..f44e2e6d 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -1356,30 +1356,26 @@ void _glfwPlatformApplyCursorMode(_GLFWwindow* window) } } -int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int cx, int cy, +int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int xhot, int yhot, int format, const void* data) { - XcursorImage* cursorImage; - XcursorPixel* buffer; - unsigned char* image = (unsigned char*) data; - int i, size = width * height; + int i; - cursorImage = XcursorImageCreate(width, height); - - if (cursorImage == NULL) + XcursorImage* native = XcursorImageCreate(width, height); + if (native == NULL) return GL_FALSE; - cursorImage->xhot = cx; - cursorImage->yhot = cy; + native->xhot = xhot; + native->yhot = yhot; - buffer = cursorImage->pixels; + unsigned char* source = (unsigned char*) data; + XcursorPixel* target = native->pixels; - for (i = 0; i < size; i++, buffer++, image += 4) - *buffer = (image[3] << 24) | (image[0] << 16) | (image[1] << 8) | image[2]; + for (i = 0; i < width * height; i++, target++, source += 4) + *target = (source[3] << 24) | (source[0] << 16) | (source[1] << 8) | source[2]; - cursor->x11.handle = XcursorImageLoadCursor(_glfw.x11.display, cursorImage); - - XcursorImageDestroy(cursorImage); + cursor->x11.handle = XcursorImageLoadCursor(_glfw.x11.display, native); + XcursorImageDestroy(native); if (cursor->x11.handle == None) return GL_FALSE; @@ -1389,7 +1385,8 @@ int _glfwPlatformCreateCursor(_GLFWcursor* cursor, int width, int height, int cx void _glfwPlatformDestroyCursor(_GLFWcursor* cursor) { - XFreeCursor(_glfw.x11.display, cursor->x11.handle); + if (cursor->x11.handle) + XFreeCursor(_glfw.x11.display, cursor->x11.handle); } void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) diff --git a/tests/cursor.c b/tests/cursor.c index 4e673871..7bb074b5 100644 --- a/tests/cursor.c +++ b/tests/cursor.c @@ -37,11 +37,15 @@ static int W = 640; static int H = 480; static int delay = 0; -static GLFWwindow* windows[2] = {NULL, NULL}; +static GLFWwindow* windows[2] = { NULL, NULL }; static GLFWwindow* activeWindow = NULL; static GLFWcursor* cursor = NULL; -static struct { int key; double time; } commands[] = { +static struct +{ + int key; + double time; +} commands[] = { {GLFW_KEY_H, 0}, {GLFW_KEY_C, 0}, {GLFW_KEY_D, 0}, @@ -54,8 +58,11 @@ static struct { int key; double time; } commands[] = { static int CommandCount = sizeof(commands) / sizeof(commands[0]); -static struct { int w, h; } cursorSize[] = { - {24, 24}, {13, 37}, {5, 53}, {43, 64}, {300, 300} +static struct +{ + int w, h; +} cursorSize[] = { + { 24, 24 }, { 13, 37 }, { 5, 53 }, { 43, 64 }, { 300, 300 } }; static int SizeCount = sizeof(cursorSize) / sizeof(cursorSize[0]); @@ -88,11 +95,11 @@ static void command_callback(int key) int x, y, i = 0; unsigned char *image = malloc(4 * w * h); - for (y = 0; y < h; y++) + for (y = 0; y < h; y++) { - for (x = 0; x < w; x++) + for (x = 0; x < w; x++) { - image[i++] = 0xFF; + image[i++] = 0xff; image[i++] = 0; image[i++] = 255 * y / h; image[i++] = 255 * x / w; @@ -103,8 +110,9 @@ static void command_callback(int key) currentSize = (currentSize + 1) % SizeCount; free(image); } + + break; } - break; case GLFW_KEY_D: { @@ -113,8 +121,9 @@ static void command_callback(int key) glfwDestroyCursor(cursor); cursor = NULL; } + + break; } - break; case GLFW_KEY_S: { @@ -122,32 +131,25 @@ static void command_callback(int key) glfwSetCursor(activeWindow, cursor); else printf("The cursor is not created\n"); + + break; } - break; case GLFW_KEY_N: - { glfwSetCursor(activeWindow, NULL); - } - break; + break; case GLFW_KEY_1: - { glfwSetInputMode(activeWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); - } - break; + break; case GLFW_KEY_2: - { glfwSetInputMode(activeWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - } - break; + break; case GLFW_KEY_3: - { glfwSetInputMode(activeWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - } - break; + break; } }