|
1
|
+import Cocoa
|
|
2
|
+
|
|
3
|
+struct GrCocoaSurface {
|
|
4
|
+ var root: grSurface
|
|
5
|
+ var window: NSWindow?
|
|
6
|
+ var bitmapRep: NSBitmapImageRep?
|
|
7
|
+ var imageView: NSImageView?
|
|
8
|
+
|
|
9
|
+ init(root: grSurface) {
|
|
10
|
+ self.root = root
|
|
11
|
+ }
|
|
12
|
+
|
|
13
|
+ mutating func initializeSurface(bitmap: inout grBitmap) -> Bool {
|
|
14
|
+ let width = Int(bitmap.width)
|
|
15
|
+ let height = Int(bitmap.rows)
|
|
16
|
+
|
|
17
|
+ self.bitmapRep = NSBitmapImageRep(
|
|
18
|
+ bitmapDataPlanes: nil,
|
|
19
|
+ pixelsWide: width,
|
|
20
|
+ pixelsHigh: height,
|
|
21
|
+ bitsPerSample: 8,
|
|
22
|
+ samplesPerPixel: 4,
|
|
23
|
+ hasAlpha: true,
|
|
24
|
+ isPlanar: false,
|
|
25
|
+ colorSpaceName: .deviceRGB,
|
|
26
|
+ bitmapFormat: [],
|
|
27
|
+ bytesPerRow: width * 4,
|
|
28
|
+ bitsPerPixel: 32
|
|
29
|
+ )
|
|
30
|
+
|
|
31
|
+ self.window = NSWindow(
|
|
32
|
+ contentRect: NSMakeRect(0, 0, CGFloat(width), CGFloat(height)),
|
|
33
|
+ styleMask: [.titled, .closable, .resizable],
|
|
34
|
+ backing: .buffered,
|
|
35
|
+ defer: false
|
|
36
|
+ )
|
|
37
|
+ self.window?.title = "FreeType Cocoa Demo"
|
|
38
|
+
|
|
39
|
+ self.imageView = NSImageView(frame: NSMakeRect(0, 0, CGFloat(width), CGFloat(height)))
|
|
40
|
+ self.imageView?.image = NSImage(size: NSSize(width: width, height: height))
|
|
41
|
+ self.window?.contentView?.addSubview(self.imageView!)
|
|
42
|
+ self.window?.makeKeyAndOrderFront(nil)
|
|
43
|
+
|
|
44
|
+ self.root.bitmap = bitmap
|
|
45
|
+ return true
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ mutating func releaseSurface() {
|
|
49
|
+ self.window?.close()
|
|
50
|
+ self.bitmapRep = nil
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ func refreshRectangle(x: Int, y: Int, width: Int, height: Int) {
|
|
54
|
+ // update bitmap data here
|
|
55
|
+
|
|
56
|
+ self.imageView?.image?.addRepresentation(self.bitmapRep!)
|
|
57
|
+ let rect = NSRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(width), height: CGFloat(height))
|
|
58
|
+ self.imageView?.setNeedsDisplay(rect)
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ func listenEvent(eventMask: Int32, event: UnsafeMutablePointer<grEvent>) -> Int32 {
|
|
62
|
+ // event handling here
|
|
63
|
+ return 0
|
|
64
|
+ }
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+// Expose functions to C
|
|
68
|
+@_cdecl("gr_cocoa_device_init")
|
|
69
|
+func gr_cocoa_device_init() -> Int32 {
|
|
70
|
+ // Initialization code for Cocoa
|
|
71
|
+ return 0
|
|
72
|
+}
|
|
73
|
+
|
|
74
|
+@_cdecl("gr_cocoa_device_done")
|
|
75
|
+func gr_cocoa_device_done() {
|
|
76
|
+ // Cleanup code for Cocoa
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+@_cdecl("gr_cocoa_surface_init")
|
|
80
|
+func gr_cocoa_surface_init(surface: UnsafeMutablePointer<grSurface>?, bitmap: UnsafeMutablePointer<grBitmap>?) -> Int32 {
|
|
81
|
+ guard let surface = surface, let bitmap = bitmap else { return 0 }
|
|
82
|
+ var cocoaSurface = GrCocoaSurface(root: surface.pointee)
|
|
83
|
+ return cocoaSurface.initializeSurface(bitmap: &bitmap.pointee) ? 1 : 0
|
|
84
|
+}
|
|
85
|
+
|
|
86
|
+@_cdecl("gr_cocoa_surface_done")
|
|
87
|
+func gr_cocoa_surface_done(surface: UnsafeMutablePointer<grSurface>?) {
|
|
88
|
+ guard let surface = surface else { return }
|
|
89
|
+ var cocoaSurface = GrCocoaSurface(root: surface.pointee)
|
|
90
|
+ cocoaSurface.releaseSurface()
|
|
91
|
+}
|
|
92
|
+
|
|
93
|
+@_cdecl("gr_cocoa_surface_refresh_rectangle")
|
|
94
|
+func gr_cocoa_surface_refresh_rectangle(surface: UnsafeMutablePointer<grSurface>?, x: Int32, y: Int32, width: Int32, height: Int32) {
|
|
95
|
+ guard let surface = surface else { return }
|
|
96
|
+ let cocoaSurface = GrCocoaSurface(root: surface.pointee)
|
|
97
|
+ cocoaSurface.refreshRectangle(x: Int(x), y: Int(y), width: Int(width), height: Int(height))
|
|
98
|
+}
|
|
99
|
+
|
|
100
|
+@_cdecl("gr_cocoa_surface_listen_event")
|
|
101
|
+func gr_cocoa_surface_listen_event(surface: UnsafeMutablePointer<grSurface>?, eventMask: Int32, event: UnsafeMutablePointer<grEvent>?) -> Int32 {
|
|
102
|
+ guard let surface = surface, let event = event else { return 0 }
|
|
103
|
+ let cocoaSurface = GrCocoaSurface(root: surface.pointee)
|
|
104
|
+ return cocoaSurface.listenEvent(eventMask: eventMask, event: event)
|
|
105
|
+} |