cvsuser     04/09/06 09:35:14

  Modified:    .        MANIFEST
  Added:       runtime/parrot/library/SDL Button.imc StopWatch.imc
  Log:
  added SDL::Button and SDL::StopWatch
  
  Revision  Changes    Path
  1.1                  parrot/runtime/parrot/library/SDL/Button.imc
  
  Index: Button.imc
  ===================================================================
  =head1 NAME
  
  SDL::Button - A multi state SDL Button
  
  =head1 SYNOPSIS
  
      # the image to use for the button
      $P0 = new PerlString
      $P0 = "filename/to/image.png"
      
      # create the button
      $I0 = find_type 'SDL::Button'
      button = new $I0, $P0
  
      # set the position
      button.'xpos'( 10 )
      button.'ypos'( 10 )
      
      # set the number of states    
      button.'states'( 2 )
  
      # activate the second status (first is 0)
      button = 1
      
      # draw the button
      button."draw"( screen )
  
  =head1 DESCRIPTION
  
  A button uses an image containing several images representing different
  states of a button. You can change the button status at any time, the
  button will then be drawn differently.
  Please have a look at F<examples/sdl/minesweeper/smiley.png> for an example.
  
  =head1 METHODS
  
  An SDL::Button object has the following methods:
  
  =over 4
  
  =cut
  
  .namespace ['SDL::Button']
  
  .sub __onload @LOAD
  
      $I0 = find_type 'SDL::Button'
      if $I0 > 1 goto END
      newclass $P0, 'SDL::Button'
      addattribute $P0, "image"
      addattribute $P0, "status"
      addattribute $P0, "states"
      addattribute $P0, "rect"
      addattribute $P0, "clicked"
      addattribute $P0, "actions"
  END:
  .end
  
  =item button = new ID, name
  
  =cut
  
  .sub __init method
      .param pmc name
      
      $I0 = classoffset self, 'SDL::Button'
      
      # image
      $I1 = find_type 'SDL::Image'
      $P0 = new $I1, name
      setattribute self, $I0, $P0
      
      # status
      inc $I0
      $P0 = new PerlInt
      $P0 = 0
      setattribute self, $I0, $P0
  
      # states
      inc $I0
      $P0 = new PerlInt
      $P0 = 1
      setattribute self, $I0, $P0
  
      # rect
      inc $I0
      $I1 = find_type 'SDL::Rect'
      $P0 = new $I1
      setattribute self, $I0, $P0
  
      # clicked
      inc $I0
      $P0 = new PerlInt
      $P0 = 0
      setattribute self, $I0, $P0
  
      # actions
      inc $I0
      $P0 = new PerlArray
      setattribute self, $I0, $P0
  .end
  
  =item __set_integer_native
  
  =cut
  
  .sub __set_integer_native method
      .param int val
      
      $I0 = classoffset self, 'SDL::Button'
      inc $I0
      $P0 = getattribute self, $I0
      $P0 = val
  .end
  
  =item __get_integer
  
  =cut
  
  .sub __get_integer method
      $I0 = classoffset self, 'SDL::Button'
      inc $I0
      $P0 = getattribute self, $I0
      $I0 = $P0
      .pcc_begin_return
      .return $I0
      .pcc_end_return
  .end
  
  =item states( count )
  
  =cut
  
  .sub states method
      .param int count
      
      $I0 = classoffset self, 'SDL::Button'
      add $I0, 2
      $P0 = getattribute self, $I0
      $P0 = count
  .end
  
  =item pos( x, y )
  
  =cut
  
  .sub pos method
      .param int x
      .param int y
  
      $I0 = classoffset self, 'SDL::Button'
      add $I0, 3
      $P0 = getattribute self, $I0
  
      $P0.'x'( x )
      $P0.'y'( y )
  .end
  
  =item size( width, height )
  
  =cut
  
  .sub size method
      .param int w
      .param int h
  
      $I0 = classoffset self, 'SDL::Button'
      add $I0, 3
      $P0 = getattribute self, $I0
      
      $P0.'width'( w )
      $P0.'height'( h )
  .end
  
  =item draw( screen )
  
  =cut
  
  .sub draw method
      .param pmc screen
      .local pmc image
      .local int status
      .local int states
      .local pmc drect
      .local pmc srect
      .local pmc clicked
      
      $I0 = classoffset self, 'SDL::Button'
  
      image = getattribute self, $I0
  
      inc $I0
      $P0 = getattribute self, $I0
      status = $P0
  
      inc $I0
      $P0 = getattribute self, $I0
      states = $P0
  
      inc $I0
      drect = getattribute self, $I0
  
      inc $I0
      clicked = getattribute self, $I0
      
      $I1 = find_type 'SDL::Rect'
      srect = new $I1
  
      $I1 = drect.'height'()
      srect.'height'( $I1 )
      $I1 = drect.'width'()
      srect.'width'( $I1 )
  
      cmod $I0, status, states
      $I0 *= $I1
      srect.'x'( $I0 )
      
      $I1 = drect.'height'()
      $I0 = clicked
      $I0 *= $I1
      srect.'y'( $I0 )
  
      screen.'blit'( image, srect, drect )
      screen.'update_rect'( drect )
  .end
  
  =item click( x, y )
  
  =cut
  
  .sub click method
      .param int x
      .param int y
      .param int b
      .param pmc arg
      .local pmc rect
      .local pmc clicked
  
      $I0 = classoffset self, 'SDL::Button'
      add $I0, 3
      rect = getattribute self, $I0
      inc $I0
      clicked = getattribute self, $I0
  
      $I0 = rect.'x'()
      if x < $I0 goto OUT
      $I1 = rect.'width'()
      $I0 += $I1
      if x >= $I0 goto OUT
  
      $I0 = rect.'y'()
      if y < $I0 goto OUT
      $I1 = rect.'height'()
      $I0 += $I1
      if y >= $I0 goto OUT
  
      $I0 = clicked
      if $I0 goto RAISE
  
      clicked = 1
      branch OK
  RAISE:
      if b == 1 goto END
      self."raise"( arg )
      clicked = 0
      branch OK
  OUT:
      $I0 = clicked
      unless $I0 goto END
  
      clicked = 0
  
  OK:
      .pcc_begin_return
      .return 1
      .pcc_end_return
  
  END:
      .pcc_begin_return
      .return 0
      .pcc_end_return
  .end
  
  =item raise( arg )
  
  =cut
  
  .sub raise method
      .param pmc arg
      .local int status
      .local pmc action
      
      $I0 = classoffset self, 'SDL::Button'
  
      inc $I0
      $P0 = getattribute self, $I0
      status = $P0
      
      add $I0, 4
      action = getattribute self, $I0
  
      $P0 = action[status]
      
      print "raise action "
      print status
      print "\n"
      
      $P0( arg )
  .end
  
  =item setAction( status, callback )
  
  =cut
  
  .sub setAction method
      .param int status
      .param pmc cb
      .local pmc action
      
      $I0 = classoffset self, 'SDL::Button'
      add $I0, 5
      action = getattribute self, $I0
  
      action[status] = cb
  .end
  
  =back
  
  =head1 AUTHOR
  
  Jens Rieks E<lt>parrot at jensbeimsurfen dot deE<gt> is the author
  and maintainer.
  Please send patches and suggestions to the Perl 6 Internals mailing list.
  
  =head1 COPYRIGHT
  
  Copyright (c) 2004, The Perl Foundation.
  
  =cut
  
  
  
  1.1                  parrot/runtime/parrot/library/SDL/StopWatch.imc
  
  Index: StopWatch.imc
  ===================================================================
  =head1 NAME
  
  SDL::StopWatch - A stopwatch using SDL::LCD
  
  =head1 SYNOPSIS
  
      # create the stopwatch
      $I0 = find_type 'SDL::StopWatch'
      watch = new $I0, screen
      
      # set its position
      watch.'xpos'( 5 )
      watch.'ypos'( 5 )
  
      # start it
      watch.'start'()
      
  =head1 DESCRIPTION
  
  You can start, stop and reset this stopwatch. You do not need
  to draw it while it is running, this is done by an internal
  timer.
  
  =head1 METHODS
  
  An SDL::StopWatch object has the following methods:
  
  =over 4
  
  =cut
  
  .include "timer.pasm"
  .include "iterator.pasm"
  .namespace ['SDL::StopWatch']
  
  .sub __onload @LOAD
      $I0 = find_type 'SDL::StopWatch'
      if $I0 > 1 goto END
  
      load_bytecode "library/SDL/LCD.imc"
      $P0 = getclass 'SDL::LCD'
      $P0 = subclass $P0, 'SDL::StopWatch'
      addattribute $P0, "time"
      addattribute $P0, "precision"
      addattribute $P0, "start"
      addattribute $P0, "screen"
  END:
  .end
  
  =item __init( screen )
  
  Creates a StopWatch object.
  
  The stopwatch will be drawn onto the specified screen.
  
  =cut
  
  .sub __init method
      .param pmc screen
  
      $I0 = classoffset self, 'SDL::StopWatch'
      
      $P0 = new PerlNum
      $P0 = 0
      setattribute self, $I0, $P0
  
      inc $I0
      $P0 = new PerlNum
      $P0 = 0.1
      setattribute self, $I0, $P0
  
      inc $I0
      $P0 = new PerlNum
      $P0 = 0
      setattribute self, $I0, $P0
  
      inc $I0
      setattribute self, $I0, screen
  
      self.'_digits'( 10 )
      self = "000:00:00"
  .end
  
  =item reset()
  
  Resets the stopwatch.
  
  =cut
  
  .sub reset method
      .local pmc total
      .local pmc precision
      .local pmc start
  
      # read the attributes
      $I0 = classoffset self, 'SDL::StopWatch'    
      total = getattribute self, $I0
      inc $I0
      precision = getattribute self, $I0
      inc $I0
      start = getattribute self, $I0
  
      total = 0
      start = 0
  .end
  
  =item start()
  
  Starts the stopwatch.
  
  =cut
  
  .sub start method
      .local pmc total
      .local pmc precision
      .local pmc start
  
      # read the attributes
      $I0 = classoffset self, 'SDL::StopWatch'    
      total = getattribute self, $I0
      inc $I0
      precision = getattribute self, $I0
      inc $I0
      start = getattribute self, $I0
  
      if start != 0 goto END
  
      total = 0
      time $N0
      start = $N0
      
      addWatch( self )
  END:
  .end
  
  =item stop()
  
  Stops the stopwatch.
  
  =cut
  
  .sub stop method
      .local pmc total
      .local pmc precision
      .local pmc start
      
      # read the attributes
      $I0 = classoffset self, 'SDL::StopWatch'    
      total = getattribute self, $I0
      inc $I0
      precision = getattribute self, $I0
      inc $I0
      start = getattribute self, $I0
  
      if start == 0 goto END
  
      time $N0
      $N1 = start
      $N0 = $N0 - $N1
      
      $N1 = total
      $N0 += $N1
  
      $N1 = precision
      $N0 /= $N1    
      
      total = $N0 
      start = 0
  
      removeWatch( self )
  END:
  .end
  
  =item current_time()
  
  Returns the measured time, multiplied by the
  reciprocal of the precision value.
  
  =cut
  
  .sub current_time method
      .local pmc total
      .local pmc precision
      .local pmc start
      .local int ret
      
      # read the attributes
      $I0 = classoffset self, 'SDL::StopWatch'    
      total = getattribute self, $I0
      inc $I0
      precision = getattribute self, $I0
      inc $I0
      start = getattribute self, $I0
  
      ret = total
      if start == 0 goto END
  
      time $N0
      $N1 = start
      $N0 = $N0 - $N1
      
      $N1 = total
      $N0 += $N1
  
      $N1 = precision
      $N0 /= $N1
      ret = $N0
  END:
      .pcc_begin_return
      .return ret
      .pcc_end_return
  .end
  
  =item draw()
  
  Updates the stopwatch and draws it.
  
  It is drawn onto the screen consigned to the constructor.
  
  =cut
  
  .sub draw method
      $I0 = self.'current_time'()
      
      cmod $I5, $I0, 10
      $I0 /= 10
      cmod $I4, $I0, 10
      $I0 /= 10
      cmod $I3, $I0, 6
      $I0 /= 6
      cmod $I2, $I0, 10
      $I0 /= 10
      cmod $I1, $I0, 6
      $I0 /= 6
  
      $S0 = $I0
      $S1 = $I1
      $S2 = $I2
      $S3 = $I3
      $S4 = $I4
      $S5 = $I5
      
      concat $S0, ":"
      concat $S0, $S1
      concat $S0, $S2
      concat $S0, ":"
      concat $S0, $S3
      concat $S0, $S4
      concat $S0, "."
      concat $S0, $S5
      
      self = $S0
      
      .local pmc screen
      
      $I0 = classoffset self, "SDL::StopWatch"
      add $I0, 3
      screen = getattribute self, $I0
      $P0 = find_global "SDL::LCD", "draw"
      $P0( screen )
  .end
  
  .namespace ["SDL::StopWatch::Timer"]
  
  .sub __onload @LOAD
      # XXX: an old array will be overwritten when loading this file again
      $P0 = new PerlArray
      store_global "SDL::StopWatch::Timer", "array", $P0
      
      $P0 = new SArray
      $P1 = find_global "SDL::StopWatch::Timer", "tick"
      $P0 = 8
      $P0[0] = .PARROT_TIMER_NSEC
      $P0[1] = 0.1
      $P0[2] = .PARROT_TIMER_HANDLER
      $P0[3] = $P1
      $P0[4] = .PARROT_TIMER_REPEAT
      $P0[5] = -1
      $P0[6] = .PARROT_TIMER_RUNNING
      $P0[7] = 0
  
      $P0 = new .Timer, $P0
      store_global "SDL::StopWatch::Timer", "timer", $P0
  .end
  
  .sub tick
      .local pmc timer
      .local pmc array
      
      timer = find_global "SDL::StopWatch::Timer", "timer"
      array = find_global "SDL::StopWatch::Timer", "array"
      
      $I0 = array
      if $I0 == 0 goto DISABLE
  
      array = new .Iterator, array
      array = .ITERATE_FROM_START
  LOOP:
      unless array goto END
      $P0 = shift array
  
      $P0.'draw'()
          
      branch LOOP
      
  DISABLE:
      timer[.PARROT_TIMER_RUNNING] = 0
  
  END:
      .pcc_begin_return
      .pcc_end_return
  .end
  
  .sub addWatch
      .param pmc obj
      .local pmc timer
      .local pmc array
      
      timer = find_global "SDL::StopWatch::Timer", "timer"
      array = find_global "SDL::StopWatch::Timer", "array"
  
      push array, obj
      timer[.PARROT_TIMER_RUNNING] = 1
  .end
  
  .sub removeWatch
      .param pmc obj
      .local pmc timer
      .local pmc array
      
      timer = find_global "SDL::StopWatch::Timer", "timer"
      array = find_global "SDL::StopWatch::Timer", "array"
  
      # XXX: stops all watches ATM; just remove the timer from the array
      timer[.PARROT_TIMER_RUNNING] = 0
  .end
  
  =back
  
  =head1 AUTHOR
  
  Jens Rieks E<lt>parrot at jensbeimsurfen dot deE<gt> is the author
  and maintainer.
  Please send patches and suggestions to the Perl 6 Internals mailing list.
  
  =head1 COPYRIGHT
  
  Copyright (c) 2004, The Perl Foundation.
  
  =cut
  
  
  
  1.726     +2 -0      parrot/MANIFEST
  
  Index: MANIFEST
  ===================================================================
  RCS file: /cvs/public/parrot/MANIFEST,v
  retrieving revision 1.725
  retrieving revision 1.726
  diff -u -w -r1.725 -r1.726
  --- MANIFEST  4 Sep 2004 01:14:51 -0000       1.725
  +++ MANIFEST  6 Sep 2004 16:35:14 -0000       1.726
  @@ -2594,6 +2594,7 @@
   runtime/parrot/library/Data/Dumper/Default.imc    [devel]
   runtime/parrot/library/SDL.imc                    [devel]
   runtime/parrot/library/SDL/App.imc                [devel]
  +runtime/parrot/library/SDL/Button.imc             [devel]
   runtime/parrot/library/SDL/Color.imc              [devel]
   runtime/parrot/library/SDL/Constants.imc          [devel]
   runtime/parrot/library/SDL/Event.imc              [devel]
  @@ -2604,6 +2605,7 @@
   runtime/parrot/library/SDL/LCD.png                [devel]
   runtime/parrot/library/SDL/Rect.imc               [devel]
   runtime/parrot/library/SDL/Sprite.imc             [devel]
  +runtime/parrot/library/SDL/StopWatch.imc          [devel]
   runtime/parrot/library/SDL/Surface.imc            [devel]
   runtime/parrot/library/Stream/Base.imc            [devel]
   runtime/parrot/library/Stream/Buffer.imc          [devel]
  
  
  

Reply via email to