add initial pages support

This commit is contained in:
2026-11-03 19:35:23 +03:00
parent f6efe2956e
commit 6f2367455a
6 changed files with 128 additions and 15 deletions

View File

@@ -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 ------------------------------------------------------------

37
game.sex Normal file
View File

@@ -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))))

View File

@@ -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))

37
menu.sex Normal file
View File

@@ -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))))

6
pages.sex Normal file
View File

@@ -0,0 +1,6 @@
(record menu-state)
(record game-state)
(pub sum page
((menu ((state (* menu-state))))
(game ((state (* game-state))))))

10
utils.sex Normal file
View File

@@ -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 'ǂ)))