Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
elmindreda committed Mar 20, 2014
1 parent 40c04a7 commit 608de57
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ tests/*.app
tests/*.exe
tests/accuracy
tests/clipboard
tests/cursor
tests/cursoranim
tests/defaults
tests/empty
tests/events
Expand Down
2 changes: 1 addition & 1 deletion src/cocoa_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ typedef struct _GLFWmonitorNS
//------------------------------------------------------------------------
typedef struct _GLFWcursorNS
{
id handle;
id object;
} _GLFWcursorNS;


Expand Down
17 changes: 9 additions & 8 deletions src/cocoa_window.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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;
Expand All @@ -1223,34 +1223,35 @@ 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;
}

void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
{
[(NSCursor*) cursor->ns.handle release];
if (cursor->ns.object)
[(NSCursor*) cursor->ns.object release];
}

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];
}
Expand Down
17 changes: 7 additions & 10 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,24 +353,23 @@ 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;

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

cursor = calloc(1, sizeof(_GLFWcursor));
cursor->next = _glfw.cursorListHead;
_glfw.cursorListHead = cursor;

if (!_glfwPlatformCreateCursor(cursor, width, height, cx, cy, format, data))
if (!_glfwPlatformCreateCursor(cursor, width, height, xhot, yhot, format, data))
{
free(cursor);
glfwDestroyCursor((GLFWcursor*) cursor);
return NULL;
}

cursor->next = _glfw.cursorListHead;
_glfw.cursorListHead = cursor;

return (GLFWcursor*) cursor;
}

Expand All @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
68 changes: 33 additions & 35 deletions src/win32_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,71 +1223,69 @@ 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;

hdc = GetDC(NULL);
bi.bV5RedMask = 0x00ff0000;
bi.bV5GreenMask = 0x0000ff00;
bi.bV5BlueMask = 0x000000ff;
bi.bV5AlphaMask = 0xff000000;

hBitmap = CreateDIBSection(hdc, (BITMAPINFO*) &bi, DIB_RGB_COLORS, (void**) &buffer,
NULL, (DWORD) 0);
dc = GetDC(NULL);
bitmap = CreateDIBSection(dc, (BITMAPINFO*) &bi, DIB_RGB_COLORS,
(void**) &target, NULL, (DWORD) 0);
ReleaseDC(NULL, dc);

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;
}

void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
{
DestroyIcon((HICON) cursor->win32.handle);
if (cursor->win32.handle)
DestroyIcon((HICON) cursor->win32.handle);
}

void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
Expand All @@ -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));
}
}

Expand Down
31 changes: 14 additions & 17 deletions src/x11_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

buffer = cursorImage->pixels;
native->xhot = xhot;
native->yhot = yhot;

for (i = 0; i < size; i++, buffer++, image += 4)
*buffer = (image[3] << 24) | (image[0] << 16) | (image[1] << 8) | image[2];
unsigned char* source = (unsigned char*) data;
XcursorPixel* target = native->pixels;

cursor->x11.handle = XcursorImageLoadCursor(_glfw.x11.display, cursorImage);
for (i = 0; i < width * height; i++, target++, source += 4)
*target = (source[3] << 24) | (source[0] << 16) | (source[1] << 8) | source[2];

XcursorImageDestroy(cursorImage);
cursor->x11.handle = XcursorImageLoadCursor(_glfw.x11.display, native);
XcursorImageDestroy(native);

if (cursor->x11.handle == None)
return GL_FALSE;
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 608de57

Please sign in to comment.