Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
osx_sys_cursor.mm
Go to the documentation of this file.
1 /* Copyright (c) 2012 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #import "precompiled.h"
24 #import "lib/sysdep/cursor.h"
25 
26 #import <AppKit/NSCursor.h>
27 #import <AppKit/NSImage.h>
28 #import <ApplicationServices/ApplicationServices.h>
29 
30 //TODO: make sure these are threadsafe
31 Status sys_cursor_create(int w, int h, void* bgra_img, int hx, int hy, sys_cursor* cursor)
32 {
33  NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc]
34  initWithBitmapDataPlanes:0 pixelsWide:w pixelsHigh:h
35  bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
36  colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:w*4 bitsPerPixel:0];
37  if (!bitmap)
38  {
39  debug_printf(L"sys_cursor_create: Error creating NSBitmapImageRep!\n");
40  return ERR::FAIL;
41  }
42 
43  u8* planes[5];
44  [bitmap getBitmapDataPlanes:planes];
45  const u8* bgra = static_cast<const u8*>(bgra_img);
46  u8* dst = planes[0];
47  for (int i = 0; i < w*h*4; i += 4)
48  {
49  dst[i] = bgra[i+2];
50  dst[i+1] = bgra[i+1];
51  dst[i+2] = bgra[i];
52  dst[i+3] = bgra[i+3];
53  }
54 
55  NSImage* image = [[NSImage alloc] init];
56  if (!image)
57  {
58  [bitmap release];
59  debug_printf(L"sys_cursor_create: Error creating NSImage!\n");
60  return ERR::FAIL;
61  }
62 
63  [image addRepresentation:bitmap];
64  [bitmap release];
65  NSCursor* impl = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(hx, hy)];
66  [image release];
67 
68  if (!impl)
69  {
70  debug_printf(L"sys_cursor_create: Error creating NSCursor!\n");
71  return ERR::FAIL;
72  }
73 
74  *cursor = static_cast<sys_cursor>(impl);
75 
76  return INFO::OK;
77 }
78 
80 {
81  NSCursor* impl = static_cast<NSCursor*>(cursor);
82  [impl release];
83  return INFO::OK;
84 }
85 
87 {
88  static u8 empty_bgra[] = {0, 0, 0, 0};
89  sys_cursor_create(1, 1, reinterpret_cast<void*>(empty_bgra), 0, 0, cursor);
90  return INFO::OK;
91 }
92 
94 {
95  NSCursor* impl = static_cast<NSCursor*>(cursor);
96  [impl set];
97  return INFO::OK;
98 }
99 
101 {
102  return INFO::OK;
103 }
104 
#define u8
Definition: types.h:39
const Status OK
Definition: status.h:386
void * sys_cursor
Definition: cursor.h:30
Status sys_cursor_create(int w, int h, void *bgra_img, int hx, int hy, sys_cursor *cursor)
Create a cursor from the given color image.
Definition: android.cpp:72
Status sys_cursor_reset()
reset any cached cursor data.
Definition: android.cpp:116
Status sys_cursor_create_empty(sys_cursor *cursor)
Create a transparent cursor (used to hide the system cursor).
Definition: android.cpp:85
i64 Status
Error handling system.
Definition: status.h:171
Status sys_cursor_set(sys_cursor cursor)
override the current system cursor.
Definition: android.cpp:93
const Status FAIL
Definition: status.h:406
Status sys_cursor_free(sys_cursor cursor)
destroy the indicated cursor and frees its resources.
Definition: android.cpp:105
void debug_printf(const wchar_t *fmt,...)
write a formatted string to the debug channel, subject to filtering (see below).
Definition: debug.cpp:142