--screen3.exw--
without type_check
without warning
include win32lib.ew
include res.e 
rFile("eu.bmp")
rFile("pop.wav")

constant
	CXFULL = w32Func( xGetSystemMetrics, {SM_CXSCREEN} ),
	CYFULL = w32Func( xGetSystemMetrics, {SM_CYSCREEN} ),
	Win1   = create( Window, "", 0, 0, 0, CXFULL, CYFULL, {WS_POPUP, WS_DLGFRAME} ),
	Win2   = create( Window, "", Win1, 0, 0, 330, 148, {WS_POPUP, WS_DLGFRAME} ),
	Title5  = create( CText, "_empowered by WIN32LIB_", Win2, 1, 121, 322, 20, 0 ),
	Bitmap2 = create( Bitmap, "", Win2, 0, 0, 324, 120, 0 ),
  
  Mytimer1 = 1,
  Mytimer2 = 2,
  Mytimer3 = 3,
  SND_MEMORY = 4,
  CX = CXFULL-330,
  CY = CYFULL-148

setFont( Title5, "Arial", 12, 3 )

setWindowBackColor( Win1, rgb(0,0,0) )
atom rgbColor
rgbColor = getSysColor( COLOR_BTNFACE )
setWindowBackColor( Win2, rgbColor )

atom posx, posy, xsize, ysize, xdir, ydir, mysound, s_switch
xdir = 1   ydir = 1  s_switch = 1

--reads WAV from resource file to memory.
function loadWavResource( sequence fileName, atom fSize)
  atom        image
  integer     hFile, byte

  hFile = rOpen( fileName, "rb" )
  image = allocate( fSize )
  for i = 0 to fSize-1 do
    byte = getc (hFile)
    poke( image+i, byte )
  end for
  close(hFile)
  return image
end function

procedure center_win(atom win_id)
  sequence loc

  loc=getRect(win_id)
  xsize=loc[3]-loc[1]
  ysize=loc[4]-loc[2]
  posx=floor((w32Func(xGetSystemMetrics,{SM_CXFULLSCREEN})/2)-(xsize/2))
  posy=floor((w32Func(xGetSystemMetrics,{SM_CYFULLSCREEN})/2)-(ysize/2))
  setRect(win_id,posx,posy,xsize,ysize,1 )
end procedure

procedure start_up( integer self, integer event, sequence params )
  atom map, map2

  map=rReadBmp( ".\\Bin\\eu.bmp" )
  map2=createDIB(map)
  setBitmap( Bitmap2, map2 )
  mysound=loadWavResource(".\\Bin\\pop.wav", 2576 )
  center_win(Win2)
  openWindow(Win2,Modal)
  setTimer( Win1, Mytimer1, 25 )  -- 40 pixels/sec. maximum
  setTimer( Win1, Mytimer2, 120000 ) -- 2 minutes
  setTimer( Win1, Mytimer3, 250 )
end procedure

procedure next_pos( integer self, integer event, sequence params )
  integer junk
  integer timerId = params[1]

  if timerId=1 then
    if posx < 1 or posx > CX then 
      junk=w32Func(xPlaySound,{mysound,0,or_bits(SND_MEMORY,SND_ASYNC)})
      xdir = -xdir 
    end if
    if posy < 1 or posy > CY then 
      junk=w32Func(xPlaySound,{mysound,0,or_bits(SND_MEMORY,SND_ASYNC)})
      ydir = -ydir 
    end if
    posx = posx + xdir
    posy = posy - ydir
    setRect(Win2,posx,posy,xsize,ysize,1)
  elsif timerId=2 then
    posx=rand(CX)
    posy=rand(CY)
    setRect(Win2,posx,posy,xsize,ysize,1)
  -- third timer 'blinks' mouse cursor.
  elsif timerId=3 then
    if s_switch=1 then
      junk = w32Func(xShowCursor,{False})
      s_switch=0
    else
      junk = w32Func(xShowCursor,{True})
      s_switch=1
    end if
  end if
end procedure

procedure exit_prog( integer self, integer event, sequence params )
  closeWindow(Win2)
  closeWindow(Win1)
end procedure

procedure blah( integer self, integer event, sequence params )
  exit_prog( self, event, params )
end procedure

setHandler( Win1, w32HOpen, routine_id("start_up") )
setHandler( Win1, w32HTimer, routine_id("next_pos") )
setHandler( Win2, w32HClick, routine_id("exit_prog") )
setHandler( Win2, w32HKeyDown, routine_id("blah") )

WinMain( Win1,Maximize )
--end--
