#!/usr/bin/env sdl_falcon
/*
   FALCON - SDL MODULE Samples

   FILE: sdl_events.fal

   Show minimal management of events.
   This small program also shows how Falcon coroutines and SDL events
   can interact.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Sun, 16 Mar 2008 21:15:18 +0100
   Last modified because:

   -------------------------------------------------------------------
   (C) Copyright 2008: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
   In order to use this file in its compiled form, this source or
   part of it you have to read, understand and accept the conditions
   that are stated in the LICENSE file that comes boundled with this
   package.
*/

load sdlmixer
load sdlttf
load sdlimage

//const prefix="/Volumes/Falcon SDL binding module/Resources/"
const prefix="./"

object MyEventManager
   quitRequest = Semaphore()
   surf1 = SDLRect( 0,20,640,80)
   surf2 = SDLRect( 0,90,640,150)

   subs = [
      "sdl_MouseMotion",
      "sdl_Quit",
      "sdl_Expose",
      "UserEvent" ]

   init
      for event in self.subs: subscribe( event, self )
   end

   function on_sdl_MouseMotion( state, x, y, xrel, yrel )
      static
         saved = SDL.CreateRGBSurface( SDL.HWSURFACE, 640, 60, 32 )
         global font, screen, color
         screen.BlitSurface( self.surf1, saved, nil )
      end

      color.r, color.g, color.b = 20, 240, 240
      saved.BlitSurface( nil, screen, self.surf1 )
      dataImage = font.Render_Blended( @ "X=$x, Y=$y, XRel=$xrel, YRel=$yrel", color )
      dataImage.BlitSurface( nil, screen, self.surf1 )
      screen.UpdateRect()
      
      >> "X=",x, " Y=",y, " Xrel=", xrel, " YRel=",yrel, "       \r"
   end

   function on_sdl_Quit( state, x, y, xrel, yrel )
      > strReplicate( " ", 60 )
      > "== Quit! =="
      self.quitRequest.post(2)
   end

   function on_sdl_Expose()
      global screen
      screen.UpdateRect()
   end

   function on_UserEvent( code, item )
      static
         saved = SDL.CreateRGBSurface( SDL.HWSURFACE, 640, 60, 32 )
         global font, screen, color
         screen.BlitSurface( self.surf2, saved, nil )
      end
      saved.BlitSurface( nil, screen, self.surf2 )
      if item
         dataImage = font.Render_Blended( item.toString(), color )
         dataImage.BlitSurface( nil, screen, self.surf2 )
      end
      screen.UpdateRect()
   end
end


//==============================================
// Coroutine to display a blinking cursor
//

function blinker( screen )
   r = SDLRect( 280, 200, 80, 80 )
   black = screen.MapRGBA( 0, 0, 0, 0 )
   white = screen.MapRGBA( 255, 255, 255, 255 )
   color = white

   time = 0
   send = "Reset..."
   while not MyEventManager.quitRequest.wait( 0.5 )
      color = (color == white ? black : white )
      screen.FillRect( r, color )
      screen.UpdateRect( r )
      time ++
      if time % 6 == 0
         send = send ? "" : "Time ticks " + time
         broadcast( "UserEvent", 0, send  )
      end
   end
end

//==============================================
// Main program
//

try

   autoSDL = SDL.InitAuto( SDL.INIT_VIDEO )
   SDL.LoadBMP("falcon-ico.bmp").SetIcon()
   SDL.WM_SetCaption( "Falcon SDL Event Handler Test" )
   screen = SDL.SetVideoMode( 640, 480 )

   // draw a cloured band - first hardware
   try
      band = SDL.CreateRGBSurface( SDL.HWSURFACE, 640, 1, 32 )
   catch SDLError
      > "Hardware surface creation failed, try with software creation."
      // then software -- let it fail if it fails
      band = SDL.CreateRGBSurface( SDL.SWSURFACE, 640, 1, 32 )
   end

   launch blinker( screen )

   // MUSIC ====================

   MIX.OpenAudio( 44100, AUDIO.S16, 2, 4096 )
   music = MIX.LoadMUS( prefix + "fading.ogg" )
   music.Play( loops| -1, fadeIn| 6 )

   // FONT ======================
   autoFont = TTF.InitAuto()
   font = TTF.OpenFont( prefix + "Vera.ttf", 24)
   font.SetFontStyle( TTF.STYLE_BOLD || TTF.STYLE_ITALIC )

   color = SDLColor( 20, 240, 240)
   color.r, color.g, color.b = 20, 240, 240

   screen.UpdateRect()

   pixels = band.pixels
   for bid in [0:640]
      pixels[bid] = band.MapRGBA( 150, bid%128, (bid+128)%256,  250 )
   end


   rect = SDLRect( 0,0,640,1)
   for y in [0:480]
      rect.y = y
      band.BlitSurface( nil, screen, rect )
   end
   screen.UpdateRect()

   // make a solid rectangle

   // make a solid rectangle
   launch blinker( screen )

   > "Trying to load a BMP image:\n"
   img = IMAGE.Load( prefix + "FalconLogo1.bmp" )
   img.BlitSurface( nil, screen, SDLRect(0, 390, 640, 480 ) )
   screen.UpdateRect()

   > "Success! - Now start moving the mouse."
   > "Click the X on the bar to quit the app."

   SDL.StartEvents()
   count = 0
   while true
      if MyEventManager.quitRequest.wait(1): break
      >> count++, "\r"
   end
   SDL.StopEvents()

   > "Complete."
catch in e
   > "Test failed: "
   > e
end

/* end of file */
