Browse Source

Get 2D rendering and keybinds set up

master
Macoy Madson 5 months ago
parent
commit
8abd240ff4
  1. 6
      .gitmodules
  2. 1
      Dependencies/SDL
  3. 2
      Dependencies/gamelib
  4. 1
      Dependencies/stb
  5. 59
      src/Presentation.cake

6
.gitmodules

@ -4,3 +4,9 @@
[submodule "Dependencies/gamelib"]
path = Dependencies/gamelib
url = gitea@macoy.me:macoy/gamelib.git
[submodule "Dependencies/SDL"]
path = Dependencies/SDL
url = https://github.com/libsdl-org/SDL
[submodule "Dependencies/stb"]
path = Dependencies/stb
url = https://macoy.me/code/macoy/stb

1
Dependencies/SDL

@ -0,0 +1 @@
Subproject commit f6c2c22d38afbbfbb1c5b7caab29f3302a57e9f1

2
Dependencies/gamelib

@ -1 +1 @@
Subproject commit 5d167c0220bfc3031c54983ee4b0373b0c39c207
Subproject commit 27180123011a633809f75e66e3d1949c6d0f00bd

1
Dependencies/stb

@ -0,0 +1 @@
Subproject commit 18a9ccbbbbb179ab3d991afa79e60b48253e1597

59
src/Presentation.cake

@ -3,5 +3,64 @@
(add-cakelisp-search-directory "Dependencies/cakelisp/runtime")
(add-cakelisp-search-directory "src")
(import
;; GameLib
"SDL.cake" "DynamicArray.cake")
(define-keybind s-quit-keybind (array SDL_SCANCODE_Q keybind-modifier-flags-ctrl))
(var s-key-states sdl-key-states (array 0))
(defmacro preslog (format string &optional &rest arguments any)
(if arguments
(scope
(tokenize-push output
(fprintf stderr (token-splice format) (token-splice-rest arguments tokens))))
(scope
(tokenize-push output
(fprintf stderr (token-splice format)))))
(return true))
(defun main (&return int)
(SDL_GL_SetAttribute SDL_GL_CONTEXT_MAJOR_VERSION 4)
(SDL_GL_SetAttribute SDL_GL_CONTEXT_MINOR_VERSION 6)
(SDL_SetHint SDL_HINT_RENDER_VSYNC "1")
(var window (* SDL_Window) null)
(unless (sdl-initialize-for-2d (addr window) "Presentation" 1920 1080)
(preslog "Failed to initialize SDL\n")
(return 1))
(defer (sdl-shutdown window))
;; -1 = pick driver that is compatible with what we want
(var renderer (* SDL_Renderer) (SDL_CreateRenderer window -1 SDL_RENDERER_ACCELERATED))
(unless renderer
(sdl-print-error)
(return 1))
(defer (SDL_DestroyRenderer renderer))
;; current-key-states is owned by SDL, but we own last-frame-states
(defer (dynarray-free (field s-key-states last-frame-states)))
(var exit-reason (* (const char)) null)
(while true
(var event SDL_Event)
(while (SDL_PollEvent (addr event))
(when (= (field event type) SDL_QUIT)
(set exit-reason "Window event")))
(var num-keys int 0)
(set (field s-key-states this-frame-states) (SDL_GetKeyboardState (addr num-keys)))
(when (keybind-tapped (addr s-quit-keybind) (addr s-key-states))
(set exit-reason "Quit keybind pressed"))
(SDL_UpdateWindowSurface window)
(dynarray-set-length (field s-key-states last-frame-states) num-keys)
(memcpy (field s-key-states last-frame-states) (field s-key-states this-frame-states) num-keys)
(when exit-reason
(break)))
(when exit-reason
(preslog "Exited reason: %s\n" exit-reason))
(return 0))

Loading…
Cancel
Save