Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 58 additions & 6 deletions src/native/framework/app.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# src/native/framework/app.cr
# main framework entry

require "json"
require "signal"
require "../core/state"
Expand Down Expand Up @@ -32,6 +32,11 @@ abstract class NativeApp

def initialize
@renderer = nil
@bg_r = 100
@bg_g = 100
@bg_b = 100
@screen_width = 0
@screen_height = 0
setup_signal_handlers
end

Expand Down Expand Up @@ -87,16 +92,16 @@ abstract class NativeApp
end

def screen_width : Int32
@screen_width ||= 0
@screen_width
end

def screen_height : Int32
@screen_height ||= 0
@screen_height
end

abstract def setup : Nil
abstract def draw : Nil
# stub method

def on_touch_began(x : Float32, y : Float32) : Nil
end

Expand Down Expand Up @@ -146,21 +151,68 @@ abstract class NativeApp
@@current.not_nil!
end

def self.current=(app : NativeApp)
@@current = app
end

def self.start(app_class : NativeApp.class)
app = app_class.new
@@current = app
app.load_saved_state
app.setup

{% if flag?(:android) %}
crystal_android_main(app)
# Android: C engine will call crystal_android_main
# The app is created and ready, just need to run the event loop
app.run
{% elsif flag?(:ios) %}
crystal_ios_main(app)
# iOS: Obj-C engine will call crystal_ios_main
app.run
{% else %}
# Desktop: run directly
app.run
{% end %}
end

def run : Nil
loop do
{% if flag?(:android) %}
# On Android, the C engine handles the event loop
# This is a placeholder - actual loop is in native.c
sleep 1
{% elsif flag?(:ios) %}
# On iOS, the Obj-C engine handles the event loop
sleep 1
{% else %}
# Desktop mode - needs implementation
sleep 0.016
{% end %}
end
end
end

# ==========================================
# ANDROID ENTRY POINT - Called from native.c
# ==========================================
{% if flag?(:android) %}
@[Export("crystal_android_main")]
fun crystal_android_main(state : Void*) : Void
GC.init
app = NativeApp.current
app.renderer = state
app.run
end
{% end %}

# ==========================================
# IOS ENTRY POINT - Called from Objective-C
# ==========================================
{% if flag?(:ios) %}
@[Export("crystal_ios_main")]
fun crystal_ios_main(state : Void*) : Void
GC.init
app = NativeApp.current
app.renderer = state
app.run
end
{% end %}
Loading