diff --git a/game.sex b/game.sex index 98730b3..133b935 100644 --- a/game.sex +++ b/game.sex @@ -99,44 +99,44 @@ (draw-tmx-layer (. state map) "FIH" (& (. state cam))) (EndMode2D) - (DrawText "WASD/arrows: move wheel: zoom click: back to menu" + (DrawText "WASD/arrows: move wheel: zoom ESC: back to menu" 10 10 20 RAYWHITE) - (DrawText (TextFormat "FIH objects: %d" - (tmx-object-count (. state map) "FIH")) - 10 30 20 RAYWHITE) - (EndDrawing)) (pub fn process-input-game ((state (* game-state))) void - (if (!= (. state map) NULL) + (if (== (. state map) NULL) + (return)) + + (var speed float (* 300.0 (GetFrameTime))) + + (if (|| (IsKeyPressed KEY-LEFT) (IsKeyPressed KEY-A)) + (-= (. state fih x) TILE-SIZE)) + (if (|| (IsKeyPressed KEY-RIGHT) (IsKeyPressed KEY-D)) + (+= (. state fih x) TILE-SIZE)) + (if (|| (IsKeyPressed KEY-UP) (IsKeyPressed KEY-W)) + (-= (. state fih y) TILE-SIZE)) + (if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S)) + (+= (. state fih y) TILE-SIZE)) + + (var map-width float (cast (tmx-pixel-width (. state map)) float)) + (var map-height float (cast (tmx-pixel-height (. state map)) float)) + (if (< (. state cam target x) 0.0) + (= (. state cam target x) 0.0)) + (if (> (. state cam target x) map-width) + (= (. state cam target x) map-width)) + (if (< (. state cam target y) 0.0) + (= (. state cam target y) 0.0)) + (if (> (. state cam target y) map-height) + (= (. state cam target y) map-height)) + + (var wheel float (GetMouseWheelMove)) + (if (!= wheel 0.0) (do - (var speed float (* 300.0 (GetFrameTime))) + (+= (. state cam zoom) (* wheel 0.5)) + (if (< (. state cam zoom) 0.5) + (= (. state cam zoom) 0.5)) + (if (> (. state cam zoom) 8.0) + (= (. state cam zoom) 8.0)))) - (if (|| (IsKeyPressed KEY-LEFT) (IsKeyPressed KEY-A)) - (-= (. state fih x) TILE-SIZE)) - (if (|| (IsKeyPressed KEY-RIGHT) (IsKeyPressed KEY-D)) - (+= (. state fih x) TILE-SIZE)) - (if (|| (IsKeyPressed KEY-UP) (IsKeyPressed KEY-W)) - (-= (. state fih y) TILE-SIZE)) - (if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S)) - (+= (. state fih y) TILE-SIZE)) - - (var map-width float (cast (tmx-pixel-width (. state map)) float)) - (var map-height float (cast (tmx-pixel-height (. state map)) float)) - (if (< (. state cam target x) 0.0) - (= (. state cam target x) 0.0)) - (if (> (. state cam target x) map-width) - (= (. state cam target x) map-width)) - (if (< (. state cam target y) 0.0) - (= (. state cam target y) 0.0)) - (if (> (. state cam target y) map-height) - (= (. state cam target y) map-height)) - - (var wheel float (GetMouseWheelMove)) - (if (!= wheel 0.0) - (do - (+= (. state cam zoom) (* wheel 0.5)) - (if (< (. state cam zoom) 0.5) - (= (. state cam zoom) 0.5)) - (if (> (. state cam zoom) 8.0) - (= (. state cam zoom) 8.0))))))) + (if (IsKeyReleased KEY-ESCAPE) + (.change-page (. state app-state) (sum-type page menu)))) diff --git a/main.sex b/main.sex index 27b39d4..18cc5c5 100644 --- a/main.sex +++ b/main.sex @@ -26,7 +26,8 @@ ((page page) ;; TODO: fix macro expansion in fields!!! ;; Should've been (tag-type page). Fuck!!!! - (target-page (enum pageǂ)))) + (target-page (enum pageǂ)) + (running bool))) (import menu) (import game) @@ -37,6 +38,9 @@ (target (tag-type page))) void (= (. self target-page) target)) + (pub fn quit ((self (* app-state))) void + (= (. self running) false)) + (fn change-page- ((self (* app-state))) void (if (== (. self page tag) (. self target-page)) ;; Don't change page to itself @@ -66,6 +70,7 @@ InitWindow WIN-WIDTH WIN-HEIGHT "Catch-Phase") (call-and-check IsAudioDeviceReady "failed to init audio" 2 InitAudioDevice) + (SetExitKey -1) (SetTargetFPS 60)) (fn deinit () void @@ -77,8 +82,11 @@ (init) (if (!= error 0) (return error)) - (var state _ #(app-state : (mk-menu (& state)) (sum-type page menu))) - (while (! (WindowShouldClose)) + (var state _ #(app-state : (mk-menu (& state)) + (sum-type page menu) + true)) + (while (&& (! (WindowShouldClose)) + (. state running)) (main-loop (cast (& state) (* void)))) (return 0)) @@ -88,6 +96,8 @@ #+emscripten (pub fn main ((argc int) (argv [* const char])) int (init) - (var state _ #(app-state : (mk-menu (& state)) (sum-type page menu))) + (var state _ #(app-state : (mk-menu (& state)) + (sum-type page menu) + true)) (emscripten-set-main-loop-arg main-loop (cast (& state) (* void)) 0 true) (return 0)) diff --git a/menu.sex b/menu.sex index ca80972..8923982 100644 --- a/menu.sex +++ b/menu.sex @@ -1,37 +1,63 @@ (include raylib.h) +(include raygui.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)) + ((app-state (* app-state)) + (bg Texture2D) + (btn-to-game int) + (btn-exit int))) #-emscripten (define WIN-WIDTH 1440) #-emscripten (define WIN-HEIGHT 900) #+emscripten (define WIN-WIDTH 960) #+emscripten (define WIN-HEIGHT 600) +(define BTN-WIDTH 320.0) +(define BTN-HEIGHT 72.0) + +(pub fn mk-menu ((app-state (* app-state))) page + (var state _ (mem-alloc menu-state)) + (= (. state app-state) app-state) + (= (. state btn-to-game) 0) + (= (. state btn-exit) 0) + (var img Image (LoadImage "res/img/menu.png")) + (ImageResize (& img) WIN-WIDTH WIN-HEIGHT) + (= (. state bg) (LoadTextureFromImage img)) + (return (page/menu state))) + +(pub fn rm-menu ((state (* menu-state))) void + (free state) + (return)) + (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) + (DrawTexture (. state bg) 0 0 WHITE) + + (= (. state btn-to-game) + (GuiButton #(Rectangle : 80.0 + (- WIN-HEIGHT 350.0) + BTN-WIDTH + BTN-HEIGHT) "To game")) + + (= (. state btn-exit) + (GuiButton #(Rectangle : 80.0 + (- WIN-HEIGHT 240.0) + BTN-WIDTH + BTN-HEIGHT) "Quit")) + (EndDrawing)) (pub fn process-input-menu ((state (* menu-state))) void - (if (IsMouseButtonReleased MOUSE-LEFT-BUTTON) - (.change-page (. state app-state) (sum-type page game)))) + (if (== (. state btn-to-game) 1) + (do + (= (. state btn-to-game) 0) + (.change-page (. state app-state) (sum-type page game)))) + + (if (== (. state btn-exit) 1) + (do + (= (. state btn-exit) 0) + (.quit (. state app-state))))) diff --git a/res/img/menu.png b/res/img/menu.png new file mode 100644 index 0000000..22ff892 Binary files /dev/null and b/res/img/menu.png differ