--
--	Flipbook animation example for Occade
--	Copyright (C) 2009 Adam Sampson
--
--	This program is free software; you can redistribute it and/or
--	modify it under the terms of the GNU General Public License as
--	published by the Free Software Foundation, either version 2 of the
--	License, or (at your option) any later version.
--
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--	General Public License for more details.
--
--	You should have received a copy of the GNU General Public License
--	along with this program.  If not, see
--	<http://www.gnu.org/licenses/>.
--

#INCLUDE "occade.module"
#INCLUDE "rasterio.module"

-- The size of each frame.
VAL INT FRAME.WIDTH IS 32:
VAL INT FRAME.HEIGHT IS 32:

--{{{  PROC my.sprite
PROC my.sprite (SHARED OCCADE! occade)
  OCCADE.SPRITE! sprite:
  RASTER image, frame:
  INT num.frames:

  SEQ
    -- Start the sprite.
    occade.start.sprite (occade, sprite, -1)

    -- Load the image into a RASTER.
    read.raster ("images/numbers.png", image)

    -- Work out how many frames we loaded.
    num.frames := (SIZE image[0]) / FRAME.WIDTH

    -- Create a new frame-sized RASTER. (A RASTER is really just a mobile 2D
    -- array, with the outer dimension being the Y axis.)
    frame := MOBILE [FRAME.HEIGHT][FRAME.WIDTH]INT

    INITIAL INT frame.num IS 0:
    WHILE TRUE
      SEQ
        -- Copy the appropriate frame out of the loaded image.
        VAL INT x.offset IS FRAME.WIDTH * frame.num:
        SEQ y = 0 FOR FRAME.HEIGHT
          SEQ x = 0 FOR FRAME.WIDTH
            frame[y][x] := image[y][x.offset + x]

        -- Set the sprite's image from the frame.
        sprite[req] ! load.raster; frame
        sprite[resp] ? CASE return.raster; frame

        -- Put the sprite in the middle of the screen and make it visible.
        sprite[req] ! move; 320; 240; TRUE
        sprite[req] ! visible; TRUE

        -- Move to the next frame number (wrapping round at num.frames).
        frame.num := (frame.num + 1) \ num.frames

        -- Wait for a bit.
        occade.delay (100000)
:
--}}}

--{{{  PROC main
PROC main ()
  SHARED OCCADE! occade:
  INITIAL OCCADE.PARAMS params IS occade.default.params:
  SEQ
    params[width] := 640
    params[height] := 480
    occade.start (occade, params, "Flipbook animation demo")

    my.sprite (occade)
:
--}}}
