GameLib is a collection of libraries for creating applications in Cakelisp.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 lines
2.9 KiB

(set-cakelisp-option cakelisp-src-dir "Dependencies/cakelisp/src")
(set-cakelisp-option executable-output "SDLOgreApp")
(import "Ogre.cake"
"SDL.cake")
;; TODO: Should this happen automatically, because import automatically adds current working dir?
;; Should it add working dir?
(add-c-search-directory-module ".")
(c-import "SDL.h" "SDL_syswm.h" "SDL_timer.h")
(defun main (&return int)
(var window (* SDL_Window) null)
(unless (sdl-initialize-for-3d (addr window) "SDL + Ogre" 1920 1080)
(return 1))
;; Ogre uses exceptions for error handling, so we can't gracefully close without getting all that
;; stuff set up (which I don't really want to do; it belongs in Gamelib)
(unless (ogre-initialize-sdl)
(return 1))
(var monkey-mesh mesh-handle (ogre-load-mesh "Monkey_Mesh.mesh"))
(var monkey-node scene-node (ogre-node-from-item monkey-mesh))
(ogre-create-light)
(var exit-reason (* (const char)) null)
(var x float 0.f)
(var y float 0.f)
(var z float 0.f)
(var move-speed float 10.f)
(var counter-num-ticks-per-second (const Uint64) (SDL_GetPerformanceFrequency))
(var last-frame-perf-count Uint64 (* 0.016f counter-num-ticks-per-second))
;; Main loop
(while (not exit-reason)
(var event SDL_Event)
(while (SDL_PollEvent (addr event))
(when (= (field event type) SDL_QUIT)
(set exit-reason "Window closed")))
;; Note: this requires SDL_PollEvent in order to be up-to-date
(var currentKeyStates (* (const Uint8)) (SDL_GetKeyboardState null))
(when (at SDL_SCANCODE_ESCAPE currentKeyStates)
(set exit-reason "Escape pressed"))
(var delta-position ([] 3 float) (array 0))
(when (at SDL_SCANCODE_RIGHT currentKeyStates)
(set (at 0 delta-position) (+ (at 0 delta-position) move-speed)))
(when (at SDL_SCANCODE_LEFT currentKeyStates)
(set (at 0 delta-position) (- (at 0 delta-position) move-speed)))
(when (at SDL_SCANCODE_UP currentKeyStates)
(set (at 1 delta-position) (+ (at 1 delta-position) move-speed)))
(when (at SDL_SCANCODE_DOWN currentKeyStates)
(set (at 1 delta-position) (- (at 1 delta-position) move-speed)))
(var current-counter-ticks Uint64 (SDL_GetPerformanceCounter))
(var frame-diff-ticks Uint64 (- current-counter-ticks last-frame-perf-count))
(var delta-time float (/ frame-diff-ticks
(type-cast counter-num-ticks-per-second float)))
(printf "%lu %f\n" frame-diff-ticks delta-time)
(set x (+ x (* delta-time (at 0 delta-position))))
(set y (+ y (* delta-time (at 1 delta-position))))
(set z (+ z (* delta-time (at 2 delta-position))))
(ogre-node-set-position (addr monkey-node) x y z)
(set last-frame-perf-count (SDL_GetPerformanceCounter))
(unless (ogre-render-frame)
(set exit-reason "Failed to render frame")
(break)))
(ogre-shutdown)
(sdl-shutdown window)
(when exit-reason
(printf "Exit reason: %s\n" exit-reason))
(return 0))