Skip to content

Commit

Permalink
Cocoa Metal example (no SDL).
Browse files Browse the repository at this point in the history
  • Loading branch information
yay committed Aug 28, 2024
1 parent 7e69b2b commit 9c5e073
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
**.exp
tetroid.lib
build.bat
ols.json

.vscode
.idea

# orca samples

Expand Down
53 changes: 32 additions & 21 deletions learn_metal/02-argbuffers-no-sdl/02-argbuffers-no-sdl.odin
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ main :: proc() {
}

metal_main :: proc() -> (err: ^NS.Error) {
NS.scoped_autoreleasepool()
@static quit := false

app := NS.Application.sharedApplication()
app->setActivationPolicy(.Regular) // without this window is not brought to foreground on launch
Expand All @@ -40,6 +40,11 @@ metal_main :: proc() -> (err: ^NS.Error) {
window->initWithContentRect({window_origin, window_size}, { .Resizable, .Closable, .Titled, .Miniaturizable }, .Buffered, false)
window->setTitle(NS.MakeConstantString("Use left/right arrow keys to rotate the triangle"))
window->makeKeyAndOrderFront(nil)
window->setDelegate(NS.window_delegate_register_and_alloc({
windowWillClose = proc(^NS.Notification) {
quit = true
},
}, "MainWindowDelegate", context))

app->finishLaunching()

Expand Down Expand Up @@ -74,24 +79,29 @@ metal_main :: proc() -> (err: ^NS.Error) {
command_queue := device->newCommandQueue()
defer command_queue->release()

for {
event := app->nextEventMatchingMask(NS.EventMaskAny, NS.Date.distantFuture(), NS.DefaultRunLoopMode, true)

@static angle: f32

event_type := event->type()
#partial switch event_type {
case .KeyDown, .KeyUp:
code := NS.kVK(event->keyCode())
#partial switch code {
case .LeftArrow:
angle -= 0.02
case .RightArrow:
angle += 0.02
@static angle: f32

for !quit {
{
NS.scoped_autoreleasepool()
event := app->nextEventMatchingMask(NS.EventMaskAny, NS.Date.distantFuture(), NS.DefaultRunLoopMode, true)
event_type := event->type()
#partial switch event_type {
case .KeyDown, .KeyUp:
code := NS.kVK(event->keyCode())
#partial switch code {
case .Escape:
quit = true
case .LeftArrow:
angle -= 0.02
case .RightArrow:
angle += 0.02
}
fmt.println(event_type, code)
case:
fmt.println(event_type, event->locationInWindow(), event->modifierFlags())
}
fmt.println(event_type, code)
case:
fmt.println(event_type, event->locationInWindow(), event->modifierFlags())
app->sendEvent(event)
}

frame_data := (^Frame_Data)(frame_data_buffer->contentsPointer())
Expand Down Expand Up @@ -129,9 +139,10 @@ metal_main :: proc() -> (err: ^NS.Error) {
command_buffer->presentDrawable(drawable)
command_buffer->commit()

app->sendEvent(event)
app->updateWindows()
}

return nil
}

Frame_Data :: struct {
Expand All @@ -158,8 +169,8 @@ build_shaders :: proc(device: ^MTL.Device) -> (library: ^MTL.Library, pso: ^MTL.
};
v2f vertex vertex_main(device const Vertex_Data* vertex_data [[buffer(0)]],
device const Frame_Data* frame_data [[buffer(1)]],
uint vertex_id [[vertex_id]]) {
device const Frame_Data* frame_data [[buffer(1)]],
uint vertex_id [[vertex_id]]) {
float a = frame_data->angle;
float3x3 rotation_matrix = float3x3(sin(a), cos(a), 0.0, cos(a), -sin(a), 0.0, 0.0, 0.0, 1.0);
float3 position = float3(vertex_data->positions[vertex_id]);
Expand Down
2 changes: 1 addition & 1 deletion learn_metal/02-argbuffers/02-argbuffers.odin
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ metal_main :: proc() -> (err: ^NS.Error) {
defer command_queue->release()

SDL.ShowWindow(window)
for quit := false; !quit; {
for quit := false; !quit; {
for e: SDL.Event; SDL.PollEvent(&e); {
#partial switch e.type {
case .QUIT:
Expand Down

0 comments on commit 9c5e073

Please sign in to comment.