Browse Source

Ported helpful functions from Kitty-Gridlock

This is a good way to grow GameLib - by only bringing things in which
have proved useful on previous projects.
RelativeDependencies
Macoy Madson 10 months ago
parent
commit
754f65a2aa
  1. 37
      src/Math.cake
  2. 81
      src/SDL.cake

37
src/Math.cake

@ -79,6 +79,43 @@
(defun vec3-scale-add-vec3 (vec-to-scale vec3 scale float vec-to-add vec3 &return vec3)
(return (vec3-add vec-to-add (vec3-scale vec-to-scale scale))))
;; Factor 0 to 1
(defun vec2-interpolate (factor float from vec2 to vec2 &return vec2)
(return (array
(interpolate-range (vec-x from) (vec-x to)
0.f 1.f factor)
(interpolate-range (vec-y from) (vec-y to)
0.f 1.f factor))))
;; Factor 0 to 1
(defun vec3-interpolate (factor float from vec3 to vec3 &return vec3)
(return
(array
(interpolate-range (vec-x from) (vec-x to)
0.f 1.f factor)
(interpolate-range (vec-y from) (vec-y to)
0.f 1.f factor)
(interpolate-range (vec-z from) (vec-z to)
0.f 1.f factor))))
(defun vec2-is-zero (vec vec2 &return bool)
(return (and (= 0.f (vec-x vec))
(= 0.f (vec-y vec)))))
(defun vec3-is-zero (vec vec3 &return bool)
(return (and (= 0.f (vec-x vec))
(= 0.f (vec-y vec))
(= 0.f (vec-z vec)))))
(defun vec2-is-within-aabb (point-to-test vec2 upper-left vec2 size vec2 &return bool)
(var bottom-right vec2 (vec2-add upper-left size))
(return
(and
(>= (vec-x point-to-test) (vec-x upper-left))
(>= (vec-y point-to-test) (vec-y upper-left))
(< (vec-x point-to-test) (vec-x bottom-right))
(< (vec-y point-to-test) (vec-y bottom-right)))))
;; Expand vector to each of its components
;; TODO: Change to any, prevent multiple eval?
(defmacro vec-xy (vec symbol)

81
src/SDL.cake

@ -7,6 +7,10 @@
"SDL.h"
"SDL_syswm.h")
;;
;; Core/windowing
;;
(forward-declare (struct SDL_Window)
(struct SDL_AudioSpec))
@ -66,6 +70,65 @@
(SDL_DestroyWindow window)
(SDL_Quit))
;;
;; Graphics
;;
(defun sdl-list-2d-render-drivers (&return bool)
(var num-render-drivers int (SDL_GetNumRenderDrivers))
(unless num-render-drivers
(return false))
(var i int 0)
(each-in-range
num-render-drivers i
(var driver-info SDL_RendererInfo (array 0))
(unless (= 0 (SDL_GetRenderDriverInfo i (addr driver-info)))
(return false))
(SDL_Log "Renderer [%d]: %s\n
\tHardware accelerated: %s\n
\tRender to texture: %s\n
\tMax texture width: %d\n
\tMax texture height: %d\n
\n"
i (field driver-info name)
(? (bit-and (field driver-info flags) SDL_RENDERER_ACCELERATED) "yes" "no")
(? (bit-and (field driver-info flags) SDL_RENDERER_TARGETTEXTURE) "yes" "no")
(field driver-info max_texture_width)
(field driver-info max_texture_height)))
(return true))
(defun-local sdl-texture-from-bmp (filename (* (const char)) renderer (* SDL_Renderer)
&return (* SDL_Texture))
(var surface (* SDL_Surface) (SDL_LoadBMP filename))
(unless surface
(SDL_Log "Failed to load surface from BMP %s\n" filename)
(sdl-print-error)
(return null))
(var texture (* SDL_Texture)
(SDL_CreateTextureFromSurface renderer surface))
;; No need to hold on to surface after texture has been created
(SDL_FreeSurface surface)
(unless texture (sdl-print-error))
(return texture))
(defun-local sdl-texture-from-bmp-color-to-transparent
(filename (* (const char)) renderer (* SDL_Renderer) r char g char b char
&return (* SDL_Texture))
(var surface (* SDL_Surface) (SDL_LoadBMP filename))
(unless surface
(SDL_Log "Failed to load surface from BMP %s\n" filename)
(sdl-print-error)
(return null))
(SDL_SetColorKey surface SDL_TRUE (SDL_MapRGB (path surface > format) r g b))
(var texture (* SDL_Texture)
(SDL_CreateTextureFromSurface renderer surface))
;; No need to hold on to surface after texture has been created
(SDL_FreeSurface surface)
(unless texture (sdl-print-error))
(return texture))
;;
;; Audio
;;
@ -101,6 +164,24 @@
(incr i))
(free device-names))
;;
;; Time
;;
;; Useful for getting a quick idea how long something takes, e.g.:
;; (var start-load-ticks (const Uint64) (SDL_GetPerformanceFrequency))
;; (do-load)
;; (sdl-print-time-delta start-load-ticks "Loading done")
;; ...Will print e.g. "--- Loading done at 0.94 seconds"
(defun-local sdl-print-time-delta (start-num-perf-ticks Uint64 label (* (const char)))
(var performance-num-ticks-per-second (const Uint64) (SDL_GetPerformanceFrequency))
(var current-counter-ticks Uint64 (SDL_GetPerformanceCounter))
(var frame-diff-ticks Uint64 (- current-counter-ticks start-num-perf-ticks))
(var delta-time float (/ frame-diff-ticks
(type-cast performance-num-ticks-per-second float)))
(SDL_Log "--- %s at %f seconds\n" label delta-time))
;;
;; Test

Loading…
Cancel
Save