9 changed files with 314 additions and 69 deletions
@ -0,0 +1,71 @@ |
|||
(skip-build) |
|||
|
|||
(import &comptime-only "../Dependencies/cakelisp/runtime/Macros.cake") |
|||
|
|||
;; Creates forward declarations in header files. |
|||
;; Example usage: |
|||
;; (forward-declare (namespace Ogre (class item) (struct my-struct))) |
|||
;; Outputs namespace Ogre { class item; struct my-struct;} |
|||
(defgenerator forward-declare () |
|||
;; TODO: Support global vs local? |
|||
(var is-global bool true) |
|||
(var output-dest (& (<> std::vector StringOutput)) |
|||
(? is-global (field output header) (field output source))) |
|||
|
|||
(var end-invocation-index int (FindCloseParenTokenIndex tokens startTokenIndex)) |
|||
(var start-body-index int (+ 2 startTokenIndex)) |
|||
(var current-index int start-body-index) |
|||
(var namespace-stack (<> std::vector int)) |
|||
(while (< current-index end-invocation-index) |
|||
(var current-token (& (const Token)) (at current-index tokens)) |
|||
;; Invocations |
|||
(when (= TokenType_OpenParen (field current-token type)) |
|||
(var invocation-token (& (const Token)) (at (+ 1 current-index) tokens)) |
|||
(cond |
|||
((= 0 (on-call (field invocation-token contents) compare "namespace")) |
|||
(unless (< (+ 3 current-index) end-invocation-index) |
|||
(ErrorAtToken invocation-token "missing name or body arguments") |
|||
(return false)) |
|||
(var namespace-name-token (& (const Token)) (at (+ 2 current-index) tokens)) |
|||
(addStringOutput output-dest "namespace" |
|||
StringOutMod_SpaceAfter (addr invocation-token)) |
|||
(addStringOutput output-dest (field namespace-name-token contents) |
|||
StringOutMod_None (addr namespace-name-token)) |
|||
(addLangTokenOutput output-dest StringOutMod_OpenBlock (addr namespace-name-token)) |
|||
(on-call namespace-stack push_back (FindCloseParenTokenIndex tokens current-index))) |
|||
|
|||
((or (= 0 (on-call (field invocation-token contents) compare "class")) |
|||
(= 0 (on-call (field invocation-token contents) compare "struct"))) |
|||
(unless (< (+ 2 current-index) end-invocation-index) |
|||
(ErrorAtToken invocation-token "missing name argument") |
|||
(return false)) |
|||
(var type-name-token (& (const Token)) (at (+ 2 current-index) tokens)) |
|||
(unless (ExpectTokenType "forward-declare" type-name-token TokenType_Symbol) |
|||
(return false)) |
|||
(addStringOutput output-dest (field invocation-token contents) |
|||
StringOutMod_SpaceAfter (addr invocation-token)) |
|||
(addStringOutput output-dest (field type-name-token contents) |
|||
StringOutMod_None (addr type-name-token)) |
|||
(addLangTokenOutput output-dest StringOutMod_EndStatement (addr type-name-token))) |
|||
(true |
|||
(ErrorAtToken invocation-token "unknown forward-declare type") |
|||
(return false)))) |
|||
|
|||
(when (= TokenType_CloseParen (field current-token type)) |
|||
(for-in close-block-index int namespace-stack |
|||
(when (= close-block-index current-index) |
|||
(addLangTokenOutput output-dest StringOutMod_CloseBlock |
|||
(addr (at current-index tokens)))))) |
|||
;; TODO: Support function calls so we can do this recursively? |
|||
;; (set current-index |
|||
;; (getNextArgument tokens current-index end-invocation-index)) |
|||
(incr current-index)) |
|||
(return true)) |
|||
|
|||
(defmacro command-add-string-argument () |
|||
(destructure-arguments new-argument-index) |
|||
(quick-token-at new-argument new-argument-index) |
|||
(tokenize-push output (on-call (field linkCommand arguments) push_back |
|||
(array ProcessCommandArgumentType_String |
|||
(token-splice (addr new-argument))))) |
|||
(return true)) |
@ -0,0 +1,90 @@ |
|||
(set-cakelisp-option cakelisp-src-dir "Dependencies/cakelisp/src") |
|||
|
|||
(import &comptime-only "Macros.cake") |
|||
|
|||
(c-import "stdio.h" |
|||
"SDL.h" |
|||
"SDL_syswm.h") |
|||
|
|||
(defun-local print-sdl-error () |
|||
(printf "SDL_Error: %s\n" (SDL_GetError))) |
|||
|
|||
(forward-declare (struct SDL_Window)) |
|||
|
|||
(defun sdl-initialize (window-out (* (* SDL_Window)) &return bool) |
|||
(when (< (SDL_Init SDL_INIT_VIDEO) 0) |
|||
(print-sdl-error) |
|||
(return false)) |
|||
|
|||
(set (deref window-out) |
|||
(SDL_CreateWindow "Gamelib" |
|||
SDL_WINDOWPOS_UNDEFINED SDL_WINDOWPOS_UNDEFINED 1920 1080 |
|||
(bit-or SDL_WINDOW_RESIZABLE SDL_WINDOW_OPENGL))) |
|||
(unless (deref window-out) |
|||
(print-sdl-error) |
|||
(return false)) |
|||
;; Must explicitly create the GL context for Ogre |
|||
(unless (SDL_GL_CreateContext (deref window-out)) |
|||
(print-sdl-error) |
|||
(return false)) |
|||
(return true)) |
|||
|
|||
(defun sdl-shutdown (window (* SDL_Window)) |
|||
(SDL_DestroyWindow window) |
|||
(SDL_Quit)) |
|||
|
|||
;; TODO: Automatically promote this if no main is defined. Separate target instead? |
|||
(defun sdl-main (&return int) |
|||
(printf "Hello, SDL!\n") |
|||
(var window (* SDL_Window) nullptr) |
|||
(unless (sdl-initialize (addr window)) (return 1)) |
|||
|
|||
;; (var window-surface (* SDL_Surface) (SDL_GetWindowSurface window)) |
|||
|
|||
(var exit-reason (* (const char)) nullptr) |
|||
(while (not exit-reason) |
|||
(var event SDL_Event) |
|||
(while (SDL_PollEvent (addr event)) |
|||
(when (= (field event type) SDL_QUIT) |
|||
(set exit-reason "Window event"))) |
|||
(SDL_UpdateWindowSurface window)) |
|||
|
|||
(when exit-reason |
|||
(printf "Exiting. Reason: %s\n" exit-reason)) |
|||
|
|||
(sdl-shutdown window) |
|||
(return 0)) |
|||
|
|||
;; |
|||
;; Building |
|||
;; |
|||
|
|||
(set-module-option build-time-compiler "/usr/bin/clang++") |
|||
;; Include cakelisp source for DynamicLoader.hpp |
|||
(set-module-option build-time-compile-arguments |
|||
"-Wall" "-Wextra" "-Wno-unused-parameter" |
|||
"-g" "-c" 'source-input "-o" 'object-output "-fPIC" |
|||
"-IDependencies/SDL/buildSDLBuild/include/SDL2") |
|||
|
|||
(defun-comptime sdl-link-hook (manager (& ModuleManager) |
|||
linkCommand (& ProcessCommand) |
|||
linkTimeInputs (* ProcessCommandInput) numLinkTimeInputs int |
|||
&return bool) |
|||
;; TODO: Expose this option somehow? |
|||
(var is-debug-build bool true) |
|||
|
|||
(printf "SDL: Adding %s link arguments\n" (? is-debug-build "debug" "release")) |
|||
|
|||
(if is-debug-build |
|||
;; TODO: Actually add debug build |
|||
(block |
|||
(command-add-string-argument "-LDependencies/SDL/buildSDLBuild/lib") |
|||
(command-add-string-argument "-lSDL2") |
|||
(command-add-string-argument "-Wl,-rpath,.:../Dependencies/SDL/buildSDLBuild/lib")) |
|||
(block |
|||
(command-add-string-argument "-LDependencies/sdl-next/build/Release/lib") |
|||
(command-add-string-argument "-lSDL2") |
|||
(command-add-string-argument "-Wl,-rpath,.:../Dependencies/SDL/buildSDLBuild/lib"))) |
|||
|
|||
(return true)) |
|||
(add-compile-time-hook pre-link sdl-link-hook) |
@ -0,0 +1,49 @@ |
|||
(set-cakelisp-option cakelisp-src-dir "Dependencies/cakelisp/src") |
|||
|
|||
(import "../src/OgreCore.cake" |
|||
"../src/SDL.cake") |
|||
(c-import "SDL.h" "SDL_syswm.h") |
|||
|
|||
(defun main (&return int) |
|||
(var window (* SDL_Window) nullptr) |
|||
(unless (sdl-initialize (addr window)) |
|||
(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) |
|||
(ogre-initialize-sdl) |
|||
(var monkey-mesh mesh-handle (ogre-load-mesh "Suzanne.mesh")) |
|||
(var monkey-node scene-node (ogre-node-from-item monkey-mesh)) |
|||
|
|||
(var exit-reason (* (const char)) nullptr) |
|||
|
|||
(var x float 0.f) |
|||
(var y float 0.f) |
|||
(var z float 0.f) |
|||
|
|||
;; Main loop |
|||
(while true |
|||
(unless (ogre-handle-window-events) |
|||
(set exit-reason "Window closed") |
|||
(break)) |
|||
|
|||
(ogre-node-set-position (addr monkey-node) x y z) |
|||
(set x (+ x 0.01f)) |
|||
|
|||
(unless (ogre-render-frame) |
|||
(set exit-reason "Failed to render frame") |
|||
(break))) |
|||
|
|||
(ogre-shutdown) |
|||
(when exit-reason |
|||
(printf "Exit reason: %s\n" exit-reason)) |
|||
(return 0)) |
|||
|
|||
(set-cakelisp-option executable-output "test/ogreSDLApp") |
|||
|
|||
;; TODO: Somehow inherit this from SDL.cake? |
|||
(set-module-option build-time-compiler "/usr/bin/clang++") |
|||
;; Include cakelisp source for DynamicLoader.hpp |
|||
(set-module-option build-time-compile-arguments |
|||
"-Wall" "-Wextra" "-Wno-unused-parameter" |
|||
"-g" "-c" 'source-input "-o" 'object-output "-fPIC" |
|||
"-IDependencies/SDL/buildSDLBuild/include/SDL2") |
Loading…
Reference in new issue