Posted to tcl by ro at Mon Jan 07 19:15:06 GMT 2008view raw
- SDL_Init $SDL_INIT_VIDEO
- set screen [SDL_SetVideoMode 400 600 32 0]
- set pixel_format [$screen cget -format]
- proc color {r g b} {
- global pixel_format
- SDL_MapRGB $pixel_format $r $g $b
- }
- proc check_events {} {
- # if there are SDL events:
- if {[SDL_PollEvent NULL]} {
- process_events
- }
- after 10 check_events ;# this is the big cpu user
- }
- set e [SDL_Event]
- proc process_events {} {
- global e
- while {[SDL_PollEvent $e]} {
- set type [$e cget -type]
- if {$type == $::SDL_MOUSEMOTION} {
- set motion [$e cget -motion]
- set dx [$motion cget -xrel]
- set dy [$motion cget -yrel]
- move_square $dx $dy
- }
- }
- return
- }
- set square_rect [SDL_Rect]
- $square_rect configure -x 100 -y 100 -w 100 -h 100
- proc paint {} {
- global screen square_rect
- SDL_LockSurface $screen
- SDL_FillRect $screen NULL [color 0 0 255] ;# background
- SDL_FillRect $screen $square_rect [color 0 255 0] ;# square
- SDL_UnlockSurface $screen
- SDL_Flip $screen
- }
- proc move_square {dx dy} {
- global square_rect
- #puts "moving by $dx $dy"
- set x [$square_rect cget -x]
- set y [$square_rect cget -y]
- set nx [expr {$x + $dx}]
- set ny [expr {$y + $dy}]
- if {$nx < 0} {set nx 0} ; if {$ny < 0} {set ny 0}
- if {$nx > 300} {set nx 300} ; if {$ny > 500} {set ny 500}
- $square_rect configure -x $nx -y $ny
- paint
- }
- check_events