view 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 source
#{
	import: [
		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 {
				(window createRenderer: -1 flags: ((window renderOpts) accelerated)) value: :render {
					render drawColor!: (sdl r: 0u8 g: 0u8 b: 255u8)
					render clear
					(sdl loadBMP: "944.bmp") value: :surf {
						(surf asTexture: render) value: :tex {
							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: {}
								}
							}
							tex destroy
						} none: {
							surf free
						}
					} none: {
					}
					render destroy
				} none: {
					print: "Failed to create renderer\n"
				}
				window destroy
			} none: {
				print: "Failed to create window\n"
				1
			}
		} else: {
			print: "Failed to initialize SDL\n"
			1
		}
	}
}