diff --git a/Makefile b/Makefile index f9c8526..2119ecc 100644 --- a/Makefile +++ b/Makefile @@ -25,14 +25,14 @@ else RAYLIB_LIBS := -lGL -lm -lpthread -ldl -lrt -lX11 endif -OBJECTS := main.o +OBJECTS := main.o menu.o game.o all: catch-phase catch-phase: $(OBJECTS) $(RAYLIB_LIB) $(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(RAYLIB_LIB) $(RAYLIB_LIBS) -main.o: main.sex | $(OBJ_DIR) +%.o: %.sex $(SEXC) $< -c -o $@ -- $(CFLAGS) $(INCLUDES) # raylib ------------------------------------------------------------ diff --git a/game.sex b/game.sex new file mode 100644 index 0000000..4cad74a --- /dev/null +++ b/game.sex @@ -0,0 +1,37 @@ +(include raylib.h) +(pub import pages) +(import main) +(import utils) + +(pub record game-state + ((app-state (* app-state)))) + +(pub fn mk-game ((app-state (* app-state))) page + (var state _ (mem-alloc game-state)) + (= (. state app-state) app-state) + (return (page/game state))) + +(pub fn rm-game ((state (* game-state))) void + (free state) + (return)) + +#-emscripten (define WIN-WIDTH 1440) +#-emscripten (define WIN-HEIGHT 900) +#+emscripten (define WIN-WIDTH 960) +#+emscripten (define WIN-HEIGHT 600) + +(pub fn render-game ((state (* game-state))) void + (BeginDrawing) + (ClearBackground BLACK) + (DrawTriangleGradient + #(Vector2 : (+ (/ WIN-WIDTH 2) 100) (- (/ WIN-HEIGHT 2) 100.0f)) + #(Vector2 : (- (/ WIN-WIDTH 2) 100) (- (/ WIN-HEIGHT 2) 100.0f)) + #(Vector2 : (/ WIN-WIDTH 2) (+ (/ WIN-HEIGHT 2) 100)) + #(Color : 255 255 0 255) + #(Color : 255 0 255 255) + #(Color : 0 255 255 255)) + (EndDrawing)) + +(pub fn process-input-game ((state (* game-state))) void + (if (IsMouseButtonReleased MOUSE-LEFT-BUTTON) + (.change-page (. state app-state) (sum-type page menu)))) diff --git a/main.sex b/main.sex index 3e85ea7..eb5490f 100644 --- a/main.sex +++ b/main.sex @@ -1,3 +1,6 @@ +(pub import pages) +(pub import utils) + (include raylib.h) #-emscripten (define WIN-WIDTH 1440) @@ -16,17 +19,35 @@ (= error ,code) (return))))) -(fn update () void - (BeginDrawing) - (ClearBackground BLACK) - (DrawTriangleGradient - #(Vector2 : (- (/ WIN-WIDTH 2) 100) (+ (/ WIN-HEIGHT 2) 100.0f)) - #(Vector2 : (+ (/ WIN-WIDTH 2) 100) (+ (/ WIN-HEIGHT 2) 100.0f)) - #(Vector2 : (/ WIN-WIDTH 2) (- (/ WIN-HEIGHT 2) 100)) - RED - GREEN - BLUE) - (EndDrawing)) +(pub record app-state + ((page page))) + +(import menu) +(import game) + +(ops app-state + ;; TODO: add force arg to override changing to self protection + (pub fn change-page ((self (* app-state)) + (target (tag-type page))) void + (if (= (. self page tag) target) + ;; Don't change page to itself + (return)) + (match (. self page) + ((menu (state)) (rm-menu state)) + ((game (state)) (rm-game state))) + (if (== target (sum-type page menu)) + (= (. self page) (mk-menu self))) + (if (== target (sum-type page game)) + (= (. self page) (mk-game self))))) + +(fn main-loop ((state (* app-state))) void + (match (. state page) + ((menu (state)) + (render-menu state) + (process-input-menu state)) + ((game (state)) + (render-game state) + (process-input-game state)))) (fn init () void (TraceLog LOG-INFO "hello from Catch-Phase! initializing resources...") @@ -45,8 +66,9 @@ (init) (if (!= error 0) (return error)) + (var state _ #(app-state : (mk-menu (& state)))) (while (! (WindowShouldClose)) - (update)) + (main-loop (& state))) (return 0)) #+emscripten @@ -55,5 +77,6 @@ #+emscripten (pub fn main ((argc int) (argv [* const char])) int (init) - (emscripten-set-main-loop update 0 true) + (var state _ #(app-state : (mk-menu (& state)))) + (emscripten-set-main-loop-arg (cast (* void) (& state)) main-loop 0 true) (return 0)) diff --git a/menu.sex b/menu.sex new file mode 100644 index 0000000..ca80972 --- /dev/null +++ b/menu.sex @@ -0,0 +1,37 @@ +(include raylib.h) +(pub import pages) +(import main) +(import utils) + +(pub record menu-state + ((app-state (* app-state)))) + +(pub fn mk-menu ((app-state (* app-state))) page + (var state _ (mem-alloc menu-state)) + (= (. state app-state) app-state) + (return (page/menu state))) + +(pub fn rm-menu ((state (* menu-state))) void + (free state) + (return)) + +#-emscripten (define WIN-WIDTH 1440) +#-emscripten (define WIN-HEIGHT 900) +#+emscripten (define WIN-WIDTH 960) +#+emscripten (define WIN-HEIGHT 600) + +(pub fn render-menu ((state (* menu-state))) void + (BeginDrawing) + (ClearBackground BLACK) + (DrawTriangleGradient + #(Vector2 : (- (/ WIN-WIDTH 2) 100) (+ (/ WIN-HEIGHT 2) 100.0f)) + #(Vector2 : (+ (/ WIN-WIDTH 2) 100) (+ (/ WIN-HEIGHT 2) 100.0f)) + #(Vector2 : (/ WIN-WIDTH 2) (- (/ WIN-HEIGHT 2) 100)) + RED + GREEN + BLUE) + (EndDrawing)) + +(pub fn process-input-menu ((state (* menu-state))) void + (if (IsMouseButtonReleased MOUSE-LEFT-BUTTON) + (.change-page (. state app-state) (sum-type page game)))) diff --git a/pages.sex b/pages.sex new file mode 100644 index 0000000..fd38e4b --- /dev/null +++ b/pages.sex @@ -0,0 +1,6 @@ +(record menu-state) +(record game-state) + +(pub sum page + ((menu ((state (* menu-state)))) + (game ((state (* game-state)))))) diff --git a/utils.sex b/utils.sex new file mode 100644 index 0000000..ab447ce --- /dev/null +++ b/utils.sex @@ -0,0 +1,10 @@ +(pub include stdlib.h) + +(pub defmacro (mem-alloc type) + `(cast (malloc (sizeof ,type)) (* ,type))) + +(pub defmacro (sum-type sum type) + (cat sum (cat 'ǂ type))) + +(pub defmacro (tag-type sum-type) + `(enum ,(cat sum-type 'ǂ)))