Skip to content
Snippets Groups Projects
Commit 0905103b authored by Ahmet Göksu's avatar Ahmet Göksu
Browse files

make rules works, a draft swift code developed

parent d3ee6a39
No related branches found
No related tags found
No related merge requests found
#ifndef GRCOCOA_H_
#define GRCOCOA_H_
#include "../grobjs.h"
#include "../grdevice.h"
#include <Cocoa/Cocoa.h>
int gr_cocoa_device_init(void);
void gr_cocoa_device_done(void);
int gr_cocoa_surface_init(grSurface *surface, grBitmap *bitmap);
void gr_cocoa_surface_done(grSurface *surface);
void gr_cocoa_surface_refresh_rectangle(grSurface *surface, int x, int y, int w, int h);
int gr_cocoa_surface_listen_event(grSurface *surface, int event_mask, grEvent *event);
extern grDevice gr_cocoa_device;
#endif /* GRCOCOA_H_ */
import Cocoa
struct GrCocoaSurface {
var root: grSurface
var window: NSWindow?
var bitmapRep: NSBitmapImageRep?
var imageView: NSImageView?
init(root: grSurface) {
self.root = root
}
mutating func initializeSurface(bitmap: inout grBitmap) -> Bool {
let width = Int(bitmap.width)
let height = Int(bitmap.rows)
self.bitmapRep = NSBitmapImageRep(
bitmapDataPlanes: nil,
pixelsWide: width,
pixelsHigh: height,
bitsPerSample: 8,
samplesPerPixel: 4,
hasAlpha: true,
isPlanar: false,
colorSpaceName: .deviceRGB,
bitmapFormat: [],
bytesPerRow: width * 4,
bitsPerPixel: 32
)
self.window = NSWindow(
contentRect: NSMakeRect(0, 0, CGFloat(width), CGFloat(height)),
styleMask: [.titled, .closable, .resizable],
backing: .buffered,
defer: false
)
self.window?.title = "FreeType Cocoa Demo"
self.imageView = NSImageView(frame: NSMakeRect(0, 0, CGFloat(width), CGFloat(height)))
self.imageView?.image = NSImage(size: NSSize(width: width, height: height))
self.window?.contentView?.addSubview(self.imageView!)
self.window?.makeKeyAndOrderFront(nil)
self.root.bitmap = bitmap
return true
}
mutating func releaseSurface() {
self.window?.close()
self.bitmapRep = nil
}
func refreshRectangle(x: Int, y: Int, width: Int, height: Int) {
// update bitmap data here
self.imageView?.image?.addRepresentation(self.bitmapRep!)
let rect = NSRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(width), height: CGFloat(height))
self.imageView?.setNeedsDisplay(rect)
}
func listenEvent(eventMask: Int32, event: UnsafeMutablePointer<grEvent>) -> Int32 {
// event handling here
return 0
}
}
// Expose functions to C
@_cdecl("gr_cocoa_device_init")
func gr_cocoa_device_init() -> Int32 {
// Initialization code for Cocoa
return 0
}
@_cdecl("gr_cocoa_device_done")
func gr_cocoa_device_done() {
// Cleanup code for Cocoa
}
@_cdecl("gr_cocoa_surface_init")
func gr_cocoa_surface_init(surface: UnsafeMutablePointer<grSurface>?, bitmap: UnsafeMutablePointer<grBitmap>?) -> Int32 {
guard let surface = surface, let bitmap = bitmap else { return 0 }
var cocoaSurface = GrCocoaSurface(root: surface.pointee)
return cocoaSurface.initializeSurface(bitmap: &bitmap.pointee) ? 1 : 0
}
@_cdecl("gr_cocoa_surface_done")
func gr_cocoa_surface_done(surface: UnsafeMutablePointer<grSurface>?) {
guard let surface = surface else { return }
var cocoaSurface = GrCocoaSurface(root: surface.pointee)
cocoaSurface.releaseSurface()
}
@_cdecl("gr_cocoa_surface_refresh_rectangle")
func gr_cocoa_surface_refresh_rectangle(surface: UnsafeMutablePointer<grSurface>?, x: Int32, y: Int32, width: Int32, height: Int32) {
guard let surface = surface else { return }
let cocoaSurface = GrCocoaSurface(root: surface.pointee)
cocoaSurface.refreshRectangle(x: Int(x), y: Int(y), width: Int(width), height: Int(height))
}
@_cdecl("gr_cocoa_surface_listen_event")
func gr_cocoa_surface_listen_event(surface: UnsafeMutablePointer<grSurface>?, eventMask: Int32, event: UnsafeMutablePointer<grEvent>?) -> Int32 {
guard let surface = surface, let event = event else { return 0 }
let cocoaSurface = GrCocoaSurface(root: surface.pointee)
return cocoaSurface.listenEvent(eventMask: eventMask, event: event)
}
#**************************************************************************
#*
#* Cocoa specific rules file, used to compile the Cocoa graphics driver
#* to the graphics subsystem
#*
#**************************************************************************
# Compiler and Flags
SWIFT := swiftc
SWIFTFLAGS := -import-objc-header $(GRAPH)/cocoa/grcocoa.h
# Cocoa specific settings
ifeq ($(shell uname -s), Darwin)
SWIFT_OBJS += $(OBJ_DIR_2)/grcocoa.o
FRAMEWORKS = -framework Cocoa
LDFLAGS += $(FRAMEWORKS)
endif
# Targets
all: cocoa
cocoa: $(SWIFT_OBJS)
@echo "Building Cocoa driver"
$(OBJ_DIR_2)/grcocoa.o: $(GRAPH)/cocoa/grcocoa.swift
$(SWIFT) $(SWIFTFLAGS) -c $< -o $@
clean:
rm -rf $(OBJ_DIR_2)/*.o
...@@ -29,6 +29,10 @@ ...@@ -29,6 +29,10 @@
#include "mac/grmac.h" #include "mac/grmac.h"
#endif #endif
#ifdef DEVICE_COCOA
#include "cocoa/grcocoa.h"
#endif
#ifdef DEVICE_ALLEGRO #ifdef DEVICE_ALLEGRO
#include "allegro/gralleg.h" #include "allegro/gralleg.h"
#endif #endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment