diff samples/sdl.tp @ 291:38bbbf74b735

Initial work on event support in SDL module
author Michael Pavone <pavone@retrodev.com>
date Tue, 22 Jul 2014 23:35:08 -0700
parents b01d7c1b4edd
children 2b045d5b673b
line wrap: on
line diff
--- a/samples/sdl.tp	Tue Jul 22 23:33:12 2014 -0700
+++ b/samples/sdl.tp	Tue Jul 22 23:35:08 2014 -0700
@@ -3,6 +3,13 @@
 		video
 		timer
 	] from: (sdl subsystems)
+	import: [
+		quit
+		keyDown
+		keyUp
+		mouseDown
+		mouseUp
+	] from: (sdl eventTypes)
 	main <- {
 		if: (sdl init: (video or timer)) = 0 {
 			(sdl createWindow: "SDL Test" pos: 0 0 size: 640 480 flags: 0u32) value: :window {
@@ -11,16 +18,37 @@
 					render clear
 					(sdl loadBMP: "944.bmp") value: :surf {
 						(surf asTexture: render) value: :tex {
-							sdl clearError
-							if: (tex copy) != 0 {
-								print: "Failed to copy texture to renderer: " . (sdl getError) . "\n"
+							surf free
+							angle <- 45.0f32
+							continue? <- true
+							while: { continue? } do: {
+								tex copy
+								tex copyTo: (sdl rect: 160 120 size: 320 224)
+								tex copyRect: (sdl rect: 80 60 size: 160 120) To: (sdl rect: 40 30 size: 320 224)
+								tex copyTo: (sdl rect: 320 240 size: 320 224) rotated: angle
+								render present
+								angle <- angle + 1.0f32
+
+								event <- option none
+								while: {
+									event <- sdl pollEvent
+									event value?
+								} do: {
+									event value: :ev {
+										if: (ev type) = quit {
+											continue? <- false
+										} else: {
+											if: (ev type) = keyDown || (ev type) = keyUp {
+												print: "Key event for: " . (ev keyChar) . ", pressed?: " . (ev pressed?) . "\n"
+											} else: {
+												if: (ev type) = mouseDown || (ev type) = mouseUp {
+													print: "Mouse event at: " . (ev x) . ", " . (ev y) . " for button " . (ev button) . "\n"
+												}
+											}
+										}
+									} none: {}
+								}
 							}
-							surf free
-							tex copyTo: (sdl rect: 160 120 size: 320 224)
-							tex copyRect: (sdl rect: 80 60 size: 160 120) To: (sdl rect: 40 30 size: 320 224)
-							tex copyTo: (sdl rect: 320 240 size: 320 224) rotated: 45.0
-							render present
-							sdl delay: 3000u32
 							tex destroy
 						} none: {
 							surf free