changeset 274:a923b5b7da3d

Initial work on an SDL2 binding
author Michael Pavone <pavone@retrodev.com>
date Sun, 20 Jul 2014 00:13:01 -0700
parents 0dc7322590da
children d83647152485
files compile modules/sdl.tp samples/sdl.tp
diffstat 3 files changed, 142 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/compile	Sun Jul 20 00:12:43 2014 -0700
+++ b/compile	Sun Jul 20 00:13:01 2014 -0700
@@ -16,4 +16,4 @@
 bin=`echo $1 | sed 's/\.tp//'`
 shift
 echo gcc $@ -o $bin $cname runtime/object.c -lgc
-gcc $@ -o $bin $cname runtime/object.c -lgc -ldl
+gcc $@ -o $bin $cname runtime/object.c -lgc -ldl -lSDL2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/sdl.tp	Sun Jul 20 00:13:01 2014 -0700
@@ -0,0 +1,121 @@
+{
+	_constant <- macro: :name cname {
+		quote: (llMessage: name withVars: {
+			uintret <- obj_uint32 ptr
+		} andCode: {
+			uintret <- make_object: (addr_of: obj_uint32_meta) NULL 0
+			uintret num!: cname
+			uintret
+		})
+	}
+	_subsystems <- #{
+		_constant: timer SDL_INIT_TIMER
+		_constant: audio SDL_INIT_AUDIO
+		_constant: video SDL_INIT_VIDEO
+		_constant: joystick SDL_INIT_JOYSTICK
+		_constant: haptic SDL_INIT_HAPTIC
+		_constant: gameController SDL_INIT_GAMECONTROLLER
+		_constant: events SDL_INIT_EVENTS
+		_constant: everything SDL_INIT_EVERYTHING
+	}
+	
+	_windowOpts <- #{
+		_constant: fullscreen SDL_WINDOW_FULLSCREEN
+		_constant: fullscreenDesktop SDL_WINDOW_FULLSCREEN_DESKTOP
+		_constant: opengl SDL_WINDOW_OPENGL
+		_constant: hidden SDL_WINDOW_HIDDEN
+		_constant: borderless SDL_WINDOW_BORDERLESS
+		_constant: minimized SDL_WINDOW_MINIMIZED
+		_constant: maximized SDL_WINDOW_MAXIMIZED
+		_constant: inputGrabbed SDL_WINDOW_INPUT_GRABBED
+		_constant: allowHighDPI SDL_WINDOW_ALLOW_HIGHDPI
+	}
+	
+	_helper <- #{
+		llMessage: createWindow withVars: {
+			title <- string ptr
+			x <- obj_int32 ptr
+			y <- obj_int32 ptr
+			w <- obj_int32 ptr
+			h <- obj_int32 ptr
+			flags <- obj_uint32 ptr
+			makeWindow <- lambda ptr
+			windowOpaque <- cpointer ptr
+			win <- object ptr
+		} andCode: :title x y w h flags makeWindow {
+			windowOpaque <- make_object: (addr_of: cpointer_meta) NULL 0
+			windowOpaque val!: (SDL_CreateWindow: (title data) (x num) (y num) (w num) (h num) (flags num))
+			if: (windowOpaque val) {
+				win <- ccall: makeWindow 1 (windowOpaque castTo: (object ptr))
+				mcall: value 2 option win
+			} else: {
+				mcall: none 1 option
+			}
+		}
+	}
+	
+	#{
+		includeSystemHeader: "SDL.h"
+		includeSystemHeader: "stdlib.h"
+		
+		llMessage: init withVars: {
+			flags <- obj_uint32 ptr
+			intret <- obj_int32 ptr
+		} andCode: :flags {
+			intret <- make_object: (addr_of: obj_int32_meta) NULL 0
+			intret num!: (SDL_Init: (flags num))
+			if: (intret num) {
+				atexit: SDL_Quit
+			}
+			intret
+		}
+		
+		llMessage: initSubSystem withVars: {
+			flags <- obj_uint32 ptr
+			intret <- obj_int32 ptr
+		} andCode: :flags {
+			intret <- make_object: (addr_of: obj_int32_meta) NULL 0
+			intret num!: (SDL_InitSubSystem: (flags num))
+			intret
+		}
+		
+		llMessage: quitSubSystem withVars: {
+			flags <- obj_uint32 ptr
+		} andCode: :flags {
+			SDL_QuitSubSystem: (flags num)
+			true
+		}
+		
+		createWindow:pos:size:flags <- :title :x y :w h :flags{
+			_helper createWindow: title x y w h flags :ptr {
+				#{
+					includeSystemHeader: "SDL.h"
+					llProperty: window withType: (SDL_Window ptr)
+					
+					llMessage: _ptr_init withVars: {
+						ptr <- cpointer ptr
+					} andCode: :ptr {
+						window <- ptr val
+						self
+					}
+					
+					llMessage: destroy withVars: {
+					} andCode: {
+						SDL_DestroyWindow: window
+						true
+					}
+				} _ptr_init
+			}
+		}
+		
+		llMessage: delay withVars: {
+			ms <- obj_uint32 ptr
+		} andCode: :ms {
+			SDL_Delay: (ms num)
+			true
+		}
+		
+		subsystems <- { _subsystems }
+		windowOpts <- { _windowOpts }
+	}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/samples/sdl.tp	Sun Jul 20 00:13:01 2014 -0700
@@ -0,0 +1,20 @@
+#{
+	import: [
+		video
+		timer
+	] from: (sdl subsystems)
+	main <- {
+		if: (sdl init: (video or timer)) = 0 {
+			(sdl createWindow: "SDL Test" pos: 0 0 size: 640 480 flags: 0u32) value: :window {
+				sdl delay: 3000u32
+				window destroy
+			} none: {
+				print: "Failed to create window\n"
+				1
+			}
+		} else: {
+			print: "Failed to initialize SDL\n"
+			1
+		}
+	}
+}
\ No newline at end of file