Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
yay committed Sep 3, 2024
1 parent 343feb8 commit a16f12c
Show file tree
Hide file tree
Showing 4 changed files with 1,032 additions and 770 deletions.
94 changes: 4 additions & 90 deletions learn_metal/02-argbuffers-no-sdl/02-argbuffers-no-sdl.odin
Original file line number Diff line number Diff line change
Expand Up @@ -13,63 +13,11 @@ import MTL "vendor:darwin/Metal"
import CA "vendor:darwin/QuartzCore"

main :: proc() {
// print_property_list(intrinsics.objc_find_class("NSWindow"))
// assert(NS.objc_getProtocol("NSCopying") != nil)
// print_protocol_list()
// fmt.println(NS.objc_getClassList(nil, 0))
m := NS.class_getClassMethod(intrinsics.objc_find_class("NSWindow"), intrinsics.objc_find_selector("init"))
assert(m != nil)
fmt.println("arg type", NS.method_copyArgumentType(m, 0))
fmt.println("arg type", NS.method_copyArgumentType(m, 1))
fmt.println("num args", NS.method_getNumberOfArguments(m))

// err := metal_main()
// if err != nil {
// fmt.eprintln(err->localizedDescription()->odinString())
// os.exit(1)
// }
}

print_property_list :: proc(cls: NS.Class) {
if cls == nil { return }
out_count: uint
property_list := NS.class_copyPropertyList(cls, &out_count)
cls_name := NS.class_getName(cls)
// NS.class_setVersion(cls, 22)
cls_version := NS.class_getVersion(cls)
if property_list != nil && out_count > 0 {
fmt.printf("Found %v properties in class %v (version %v):\n\n", out_count, cls_name, cls_version)
for i: uint = 0; i < out_count; i += 1 {
attributes := strings.clone_from_cstring(NS.property_getAttributes(property_list[i]), context.temp_allocator)
attribute_list := strings.split(attributes[:], ",", context.temp_allocator)
property_name := NS.property_getName(property_list[i])
fmt.printf("%v property #%v:\n\tname: %v\n\tattributes: %v\n", cls_name, i, property_name, attributes)

for j := 0; j < len(attribute_list); j += 1 {
attribute_name := strings.clone_to_cstring(attribute_list[j][:1], context.temp_allocator)
value := NS.property_copyAttributeValue(property_list[i], attribute_name)
fmt.printf("\t\t%v: %v\n", attribute_list[j][:1], value)
}
}
} else {
fmt.printf("Invalid class name (%v) or no properties found.\n", cls_name)
}
free(property_list)
fmt.printf("---- ---- ----\n")
print_property_list(NS.class_getSuperclass(cls))
}

print_protocol_list :: proc() {
out_count: uint
protocol_list := NS.objc_copyProtocolList(&out_count)
if out_count > 0 {
fmt.printf("%v protocols are known to the runtime:\n", out_count)
list := protocol_list[:out_count]
for item in list {
fmt.println(NS.protocol_getName(item))
}
err := metal_main()
if err != nil {
fmt.eprintln(err->localizedDescription()->odinString())
os.exit(1)
}
free(protocol_list)
}

metal_main :: proc() -> (err: ^NS.Error) {
Expand All @@ -88,51 +36,17 @@ metal_main :: proc() -> (err: ^NS.Error) {
NS.Float(math.floor(f64(screen_rect.size.height - window_size.height) / 2)),
}

// window := NS.Window.alloc()
CustomWindowClass := NS.objc_allocateClassPair(intrinsics.objc_find_class("NSWindow"), "CustomWindow", 0)
assert(CustomWindowClass != nil)
assert(NS.class_getProperty(CustomWindowClass, "acceptsFirstResponder") != nil)
// assert(NS.sel_isEqual(intrinsics.objc_find_selector("performKeyEquivalent"), intrinsics.objc_find_selector("performKeyEquivalent")) == true)
fmt.println("SEL NAME", NS.sel_getName(intrinsics.objc_find_selector("performKeyEquivalent")))
// acceptsFirstResponder :: proc "c" () -> NS.BOOL { return NS.YES }
// sel := intrinsics.objc_find_selector("acceptsFirstResponder")
// assert(NS.class_respondsToSelector(CustomWindowClass, sel) == true)
sel := intrinsics.objc_find_selector("performKeyEquivalent:")
assert(NS.class_respondsToSelector(CustomWindowClass, sel) == true)
// NS.class_addMethod(CustomWindowClass, sel, auto_cast acceptsFirstResponder, "B@:")
performKeyEquivalent :: proc "c" (self: NS.id, event: ^NS.Event) -> NS.BOOL {
return NS.YES
}
NS.class_addMethod(CustomWindowClass, intrinsics.objc_find_selector("performKeyEquivalent:"), auto_cast performKeyEquivalent, "B@:@")
NS.objc_registerClassPair(CustomWindowClass)
window := cast(^NS.Window)(NS.class_createInstance(CustomWindowClass, size_of(NS.Window)))
// {
// value := false
// if NS.object_setInstanceVariable(window, "acceptsFirstResponder", &value) == nil {
// fmt.panicf("Ivar '%v' undefined.\n", "acceptsFirstResponder")
// }
// }
// {
// value: bool
// if NS.object_getInstanceVariable(window, "acceptsFirstResponder", &value) == nil {
// fmt.panicf("Ivar '%v' undefined.\n", "acceptsFirstResponder")
// }
// }
// {
// // assert(NS.class_getInstanceVariable(CustomWindowClass, "acceptsFirstResponder") != nil)
// cls := intrinsics.objc_find_class("NSWindow")
// out_count: uint
// fmt.println("Instance size:", NS.class_getInstanceSize(cls))
// ivar_list := NS.class_copyIvarList(cls, &out_count)
// defer free(ivar_list)
// fmt.println("Ivar count:", out_count)
// if out_count > 0 {
// list := ivar_list[:out_count]
// for &ivar in list {
// fmt.printf("\t%v\n", NS.ivar_getName(ivar))
// }
// }
// }
defer window->release()

window->initWithContentRect({window_origin, window_size}, { .Resizable, .Closable, .Titled, .Miniaturizable }, .Buffered, false)
Expand Down
Binary file modified learn_metal/02-argbuffers-no-sdl/app
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit a16f12c

Please sign in to comment.