Ahmet Göksu pushed to branch gsoc-2024-ahmet at FreeType / FreeType Demo Programs
Commits:
-
c3f0a57e
by Ahmet Goksu at 2024-08-05T03:00:54+03:00
6 changed files:
- + graph/cocoa/grcocoa.h
- + graph/cocoa/grcocoa.mm
- + graph/cocoa/grcocoa.swift
- + graph/cocoa/rules.mk
- graph/grinit.c
- graph/rules.mk
Changes:
1 | +#ifndef GRCOCOA_H_
|
|
2 | +#define GRCOCOA_H_
|
|
3 | + |
|
4 | +#include "../grobjs.h"
|
|
5 | +#include "../grdevice.h"
|
|
6 | +#include <Cocoa/Cocoa.h>
|
|
7 | + |
|
8 | +int gr_cocoa_device_init(void);
|
|
9 | +void gr_cocoa_device_done(void);
|
|
10 | +int gr_cocoa_surface_init(grSurface *surface, grBitmap *bitmap);
|
|
11 | +void gr_cocoa_surface_done(grSurface *surface);
|
|
12 | +void gr_cocoa_surface_refresh_rectangle(grSurface *surface, int x, int y, int w, int h);
|
|
13 | +int gr_cocoa_surface_listen_event(grSurface *surface, int event_mask, grEvent *event);
|
|
14 | + |
|
15 | +extern grDevice gr_cocoa_device;
|
|
16 | + |
|
17 | +#endif /* GRCOCOA_H_ */ |
1 | +#include "grcocoa.h"
|
|
2 | +#include "graph.h"
|
|
3 | +#include <Cocoa/Cocoa.h>
|
|
4 | +#include <objc/runtime.h>
|
|
5 | + |
|
6 | +typedef struct {
|
|
7 | + grSurface root;
|
|
8 | + NSWindow *window;
|
|
9 | + NSBitmapImageRep *bitmapRep;
|
|
10 | + NSImageView *imageView;
|
|
11 | +} grCocoaSurface;
|
|
12 | + |
|
13 | +static int gr_cocoa_device_init(void) {
|
|
14 | + // initialization for Cocoa
|
|
15 | + return 0;
|
|
16 | +}
|
|
17 | + |
|
18 | +static void gr_cocoa_device_done(void) {
|
|
19 | + // cleanup
|
|
20 | +}
|
|
21 | + |
|
22 | +static int gr_cocoa_surface_init(grSurface *surface, grBitmap *bitmap) {
|
|
23 | + grCocoaSurface *cocoa_surface = (grCocoaSurface *)surface;
|
|
24 | + int width = bitmap->width;
|
|
25 | + int height = bitmap->rows;
|
|
26 | + |
|
27 | + cocoa_surface->bitmapRep = [[NSBitmapImageRep alloc]
|
|
28 | + initWithBitmapDataPlanes:NULL
|
|
29 | + pixelsWide:width
|
|
30 | + pixelsHigh:height
|
|
31 | + bitsPerSample:8
|
|
32 | + samplesPerPixel:4
|
|
33 | + hasAlpha:YES
|
|
34 | + isPlanar:NO
|
|
35 | + colorSpaceName:NSDeviceRGBColorSpace
|
|
36 | + bitmapFormat:0
|
|
37 | + bytesPerRow:width * 4
|
|
38 | + bitsPerPixel:32];
|
|
39 | + |
|
40 | + cocoa_surface->window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, height)
|
|
41 | + styleMask:(NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable)
|
|
42 | + backing:NSBackingStoreBuffered
|
|
43 | + defer:NO];
|
|
44 | + [cocoa_surface->window setTitle:@"FreeType Cocoa Demo"];
|
|
45 | + cocoa_surface->imageView = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 0, width, height)];
|
|
46 | + [cocoa_surface->imageView setImage:[[NSImage alloc] initWithSize:NSMakeSize(width, height)]];
|
|
47 | + [[cocoa_surface->window contentView] addSubview:cocoa_surface->imageView];
|
|
48 | + [cocoa_surface->window makeKeyAndOrderFront:nil];
|
|
49 | + |
|
50 | + surface->bitmap = *bitmap;
|
|
51 | + return 1;
|
|
52 | +}
|
|
53 | + |
|
54 | +static void gr_cocoa_surface_done(grSurface *surface) {
|
|
55 | + grCocoaSurface *cocoa_surface = (grCocoaSurface *)surface;
|
|
56 | + [cocoa_surface->window close];
|
|
57 | + [cocoa_surface->bitmapRep release];
|
|
58 | +}
|
|
59 | + |
|
60 | +static void gr_cocoa_surface_refresh_rectangle(grSurface *surface, int x, int y, int w, int h) {
|
|
61 | + grCocoaSurface *cocoa_surface = (grCocoaSurface *)surface;
|
|
62 | + // update the bitmap data here
|
|
63 | + unsigned char* bitmap_data = [cocoa_surface->bitmapRep bitmapData];
|
|
64 | + for (int row = y; row < y + h; row++) {
|
|
65 | + for (int col = x; col < x + w; col++) {
|
|
66 | + int pixel_index = (row * cocoa_surface->bitmapRep.bytesPerRow) + (col * 4);
|
|
67 | + //update the pixel data
|
|
68 | + bitmap_data[pixel_index] = 255; // Red
|
|
69 | + bitmap_data[pixel_index + 1] = 255; // Green
|
|
70 | + bitmap_data[pixel_index + 2] = 255; // Blue
|
|
71 | + bitmap_data[pixel_index + 3] = 255; // Alpha
|
|
72 | + }
|
|
73 | + }
|
|
74 | + |
|
75 | + [[cocoa_surface->imageView image] addRepresentation:cocoa_surface->bitmapRep];
|
|
76 | + [cocoa_surface->imageView setNeedsDisplay:YES];
|
|
77 | +}
|
|
78 | + |
|
79 | +static int gr_cocoa_surface_listen_event(grSurface *surface, int event_mask, grEvent *event) {
|
|
80 | + // event handling here
|
|
81 | + // return 0 for now
|
|
82 | + return 0;
|
|
83 | +}
|
|
84 | + |
|
85 | +static grPixelMode gr_cocoa_pixel_modes[] = {
|
|
86 | + gr_pixel_mode_rgb24
|
|
87 | +};
|
|
88 | + |
|
89 | +grDevice gr_cocoa_device = {
|
|
90 | + sizeof(grCocoaSurface),
|
|
91 | + "cocoa",
|
|
92 | + gr_cocoa_device_init,
|
|
93 | + gr_cocoa_device_done,
|
|
94 | + gr_cocoa_surface_init,
|
|
95 | + gr_cocoa_surface_done,
|
|
96 | + gr_cocoa_surface_refresh_rectangle,
|
|
97 | + gr_cocoa_surface_listen_event,
|
|
98 | + -1,
|
|
99 | + NULL
|
|
100 | +}; |
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 | +} |
1 | +#**************************************************************************
|
|
2 | +#*
|
|
3 | +#* Cocoa specific rules file, used to compile the Cocoa graphics driver
|
|
4 | +#* to the graphics subsystem
|
|
5 | +#*
|
|
6 | +#**************************************************************************
|
|
7 | + |
|
8 | +# Compiler and Flags
|
|
9 | +SWIFT := swiftc
|
|
10 | +SWIFTFLAGS := -import-objc-header $(GRAPH)/cocoa/grcocoa.h
|
|
11 | + |
|
12 | + |
|
13 | +# Cocoa specific settings
|
|
14 | +ifeq ($(shell uname -s), Darwin)
|
|
15 | + SWIFT_OBJS += $(OBJ_DIR_2)/grcocoa.o
|
|
16 | + FRAMEWORKS = -framework Cocoa
|
|
17 | + LDFLAGS += $(FRAMEWORKS)
|
|
18 | +endif
|
|
19 | + |
|
20 | +# Targets
|
|
21 | +all: cocoa
|
|
22 | + |
|
23 | +cocoa: $(SWIFT_OBJS)
|
|
24 | + @echo "Building Cocoa driver"
|
|
25 | + |
|
26 | +$(OBJ_DIR_2)/grcocoa.o: $(GRAPH)/cocoa/grcocoa.swift
|
|
27 | + $(SWIFT) $(SWIFTFLAGS) -c $< -o $@
|
|
28 | + |
|
29 | +clean:
|
|
30 | + rm -rf $(OBJ_DIR_2)/*.o |
... | ... | @@ -29,6 +29,10 @@ |
29 | 29 | #include "mac/grmac.h"
|
30 | 30 | #endif
|
31 | 31 | |
32 | +#ifdef DEVICE_COCOA
|
|
33 | +#include "cocoa/grcocoa.h"
|
|
34 | +#endif
|
|
35 | + |
|
32 | 36 | #ifdef DEVICE_ALLEGRO
|
33 | 37 | #include "allegro/gralleg.h"
|
34 | 38 | #endif
|
... | ... | @@ -38,7 +38,6 @@ GRAPH_OBJS := $(OBJ_DIR_2)/gblblit.$(O) \ |
38 | 38 | $(OBJ_DIR_2)/grswizzle.$(O)
|
39 | 39 | |
40 | 40 | |
41 | - |
|
42 | 41 | # Default value for COMPILE_GRAPH_LIB;
|
43 | 42 | # this value can be modified by the system-specific graphics drivers.
|
44 | 43 | #
|