Objective-C: crop image

// Create the image from a png file
UIImage *image = [UIImage imageNamed:@"prgBinary.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

// Get size of current image
CGSize size = [image size];

// Frame location in view to show original image
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];

// Create rectangle that represents a cropped image  
// from the middle of the existing image
CGRect rect = CGRectMake(size.width / 4, size.height / 4, (size.width / 2), (size.height / 2));

// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

// Create and show the new image from bitmap data
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(0, 200, (size.width / 2), (size.height / 2))];
[[self view] addSubview:imageView];
[imageView release];

Objective-C: resize and mask an image

- (UIImage*) maskImage:(UIImage *)image {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

UIImage *maskImage = [UIImage imageNamed:@"mask.png"];
CGImageRef maskImageRef = [maskImage CGImage];

// create a bitmap graphics context the size of the image
CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

if (mainViewContentContext==NULL)
	return NULL;

CGFloat ratio = 0;

ratio = maskImage.size.width/ image.size.width;

if(ratio * image.size.height < maskImage.size.height) {
	ratio = maskImage.size.height/ image.size.height;
} 

CGRect rect1  = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
CGRect rect2  = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};

CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);

// Create CGImageRef of the main view bitmap content, and then
// release that bitmap context
CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);

UIImage *theImage = [UIImage imageWithCGImage:newImage];

CGImageRelease(newImage);

// return the image
return theImage;
}

 

Monkey: asteroids game example

Strict

Import mojo

Global game:MyGame

Function Main:Int()
    game = New MyGame
    Return 0
End Function

Const PDM_smlparticle%=0
Const PDM_medparticle%=1
Const PDM_bigparticle%=2
Const PDM_spark%=3

Global ASTEROIDS_NUM% = 6
Global ASTEROIDS_SIZE% = 4

' Store the device width and height
Global SCREEN_WIDTH%
Global SCREEN_HEIGHT%
' Half of SCREEN_WIDTH and HEIGHT
Global SCREEN_WIDTH2%
Global SCREEN_HEIGHT2%

' Used for delta timing movement
Global dt:DeltaTimer

' Screen states
Const STATE_TITLE% = 1
Const STATE_GAME% = 2
Const STATE_GAME_OVER% = 3

Class MyGame Extends App

Field player:Player
Field cg%, cr%, cb%
Field level% = 1
Field score% = 0
Field bestScore% = 0

Field FPS% = 60
' Current game state
Global gameState:Int = STATE_TITLE

Method OnCreate:Int()
    ' Store the device width and height
    SCREEN_WIDTH = DeviceWidth()
    SCREEN_HEIGHT = DeviceHeight()
    SCREEN_WIDTH2 = SCREEN_WIDTH / 2
    SCREEN_HEIGHT2 = SCREEN_HEIGHT / 2
    ' Set the Random seed
    Seed = Millisecs()
    ' Create the delta timer
    dt = New DeltaTimer(FPS)
    SetUpdateRate FPS

    reset()
    Return 0   
End Method

Method OnLoading:Int()
    Cls 0,0,0
    DrawText("Loading", SCREEN_WIDTH2, SCREEN_HEIGHT2, 0.5, 0)
    Return 0
End Method

Method OnUpdate:Int()
    FPSCounter.update()
    dt.UpdateDelta()

    Select gameState
    Case STATE_TITLE
        Asteroid.updateAll()
        If KeyHit(KEY_SPACE)
           setState(STATE_GAME)
        Endif   
    Case STATE_GAME
        player.Update()
        Bullet.updateAll()
        Asteroid.updateAll()
        Particle.updateAll()
        checkCollisions()
        clscolor()
    Case STATE_GAME_OVER
        Asteroid.updateAll()
        Particle.updateAll()   
        If KeyHit(KEY_SPACE)
           setState(STATE_TITLE)
            reset()
        Endif
    End Select

    Return 0
End Method

Method OnRender:Int()
    Select gameState
    Case STATE_TITLE
    Cls 0, 0, 0
    Asteroid.drawAll()
    DrawText ("ASTEROIDS - MONKEY STYLE!", SCREEN_WIDTH2, SCREEN_HEIGHT2, 0.5, 0.5)
    DrawText ("BEST SCORE: "+Self.bestScore, SCREEN_WIDTH2, SCREEN_HEIGHT2 + 30, 0.5, 0.5)
    DrawText ("PRESS <space> TO PLAY", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 60, 0.5, 0.5)
    Case STATE_GAME 
    Cls cr, cg, cb
    player.draw()
    Bullet.drawAll()
    Asteroid.drawAll()
    Particle.drawAll()
    drawHUD()
    Case STATE_GAME_OVER
    Cls 0, 0, 0
    Asteroid.drawAll()
    Particle.drawAll()
    DrawText ("GAME OVER!", SCREEN_WIDTH2, SCREEN_HEIGHT2, 0.5, 0.5)
    DrawText ("SCORE: "+score, SCREEN_WIDTH2, SCREEN_HEIGHT2 + 30, 0.5, 0.5)
    DrawText ("BEST SCORE: "+Self.bestScore, SCREEN_WIDTH2, SCREEN_HEIGHT2 + 60, 0.5, 0.5)
    DrawText ("PRESS <space> TO RETURN TO THE TITLE SCREEN", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 90, 0.5, 0.5)
    End Select
    Return 0
End Method

Method setState:Void(state:Int)
    gameState = state
End

Method clscolor:Void()
    If cr > 0
    cr-=2*dt.delta
    Else
    cr = 0
    Endif
End Method

Method reset:Void()
    Bullet.list.Clear()
    Asteroid.list.Clear()
    Particle.list.Clear()
    cg = 0
    cr = 0
    cb = 0
    level = 1
    ASTEROIDS_NUM = 6
    ASTEROIDS_SIZE = 4
    score = 0
    player = New Player(SCREEN_WIDTH2, SCREEN_HEIGHT2)
    fillAsteroids(ASTEROIDS_NUM, ASTEROIDS_SIZE)   
End Method

Method checkCollisions:Void()
    For Local a:Asteroid = Eachin Asteroid.list

        If dist(player.x, player.y, a.x, a.y) <= a.avgrad
        If cr = 0 Then cr = Rnd(100, 155)
        player.shield-=2
        Endif

        For Local b:Bullet = Eachin Bullet.list 
            If a <> Null Then
                If dist(b.x, b.y, a.x, a.y) <= a.avgrad
                    a.life = a.life - 1
                    b.life = 0
                    For Local t%=1 To 4
                        New Particle(a.x, a.y, Rnd(-8,8), Rnd(-8,8), 0.95, 30, PDM_spark, 255, 192, 64, 16)
                    Next
                    For Local t%=1 To 4
                        New Particle(a.x, a.y, Rnd(-4,4), Rnd(-4,4), 0.95, 60, PDM_smlparticle, 160, 160, 160, 0)
                    Next
                Endif
                If a.life <= 0

                For Local t%=1 To 8
                    New Particle(a.x,a.y,Rnd(-10,10),Rnd(-10,10),0.95,30,PDM_spark,255,192,64,64)
                Next
                For Local t%=1 To 6
                    New Particle(a.x,a.y,Rnd(-6,6),Rnd(-6,6),0.95,30,PDM_medparticle,255,192,64,128)
                Next
                For Local t%=1 To 6
                    New Particle(a.x,a.y,Rnd(-8,8),Rnd(-8,8),0.99,60,PDM_smlparticle,160,160,160,0)
                Next
                For Local t%=1 To 5
                    New Particle(a.x,a.y,Rnd(-6,6),Rnd(-6,6),0.99,60,PDM_medparticle,160,160,160,0)
                Next
                For Local t%=1 To 4
                    New Particle(a.x,a.y,Rnd(-4,4),Rnd(-4,4),0.99,60,PDM_bigparticle,160,160,160,0)
                Next

                If a.size > 1
                    For Local t% = 1 To 2
                        New Asteroid(a.x, a.y, Rnd(-5,5), Rnd(-5,5), a.size-1)
                    Next
                Endif
                Asteroid.list.Remove(a)
                a = Null
                score+=5
                Endif
            Endif
        Next
    Next
    If Asteroid.list.Count() = 0
        level+=1
        ASTEROIDS_SIZE+=1
        ASTEROIDS_NUM+=1
        fillAsteroids(ASTEROIDS_NUM, ASTEROIDS_SIZE)
    Endif
End Method

    Method drawHUD:Void()
        DrawText ("LEVEL: "+level, 0, 0)
        DrawText("SCORE: "+score, SCREEN_WIDTH, 0, 1, 0)

        FPSCounter.draw(SCREEN_WIDTH,SCREEN_HEIGHT, 1, 1)
    End Method

    Method fillAsteroids:Void(num%, size%)
        Local tx#
        Local ty#
        For Local t% = 1 To num
            Repeat
                tx=Rnd(640)
                ty=Rnd(480)
            Until ( tx<280 or="" tx="">360 ) And ( ty<200 or="" ty="">280 )
            New Asteroid(tx, ty, Rnd(-3,3), Rnd(-3,3), size+Rnd(1))
        Next
    End Method
End Class

Class Sprite
    Field xv# ,yv#
    Field x#, y# 
    Field rotation#
End Class

Class Player Extends Sprite
    Field angVel#
    Field velmul#
    Field vel#
    Field acc#
    Field drag#
    'Field xv#,yv#
    Field xa#,ya#
    Field firedel#

    Field ship_angvel#
    Field ship_acc#
    Field ship_velmul#
    Field ship_firedel#

    Field shield#=100

    Method New(x#, y#)
        Self.x = x
        Self.y = y
        ship_angvel = 6
        ship_acc = 0.16
        ship_velmul = -0.0005
        ship_firedel = 4
        shield = 100
    End Method

    Method Update:Void()

        If KeyDown(KEY_UP)
            acc =  ship_acc
            drag = vel * ship_velmul
        Else If KeyDown(KEY_DOWN)
            drag = vel * ship_velmul * 50
        Else
            acc = 0
            drag = 0
        End If 
        If KeyDown(KEY_LEFT)
            rotation+=ship_angvel * dt.delta
        End If
        If KeyDown(KEY_RIGHT)
            rotation-=ship_angvel * dt.delta
        End If

        If KeyDown(KEY_SPACE) And Self.firedel<=0
            Local tang#=Rnd(-4,4)
            New Bullet(x - (Sin(rotation)*8), y-(Cos(rotation)*8), xv - (Sin(rotation + tang ) *12), yv-(Cos(rotation + tang ) *12), 45, 255-Rnd(4), 192+Rnd(-4,4), 64+Rnd(4,4))
            firedel = ship_firedel
        Endif
        firedel-=dt.delta

        xa = (drag * xv) - (Sin(rotation) * acc)
        ya = (drag * yv) - (Cos(rotation) * acc)
        xv = xv + xa *dt.delta
        yv = yv + ya * dt.delta
        x = x + xv * dt.delta
        y = y + yv * dt.delta
        vel = dist(0, 0, xv, yv)
        '   
        If x < 0 x = SCREEN_WIDTH
        If x > SCREEN_WIDTH x = 0
        If y < 0 y = SCREEN_HEIGHT
        If y > SCREEN_HEIGHT y = 0

        If shield <= 0
        For Local t%=1 To 18
            New Particle( x, y,Rnd(-10,10),Rnd(-10,10),0.95,130,PDM_spark,255,192,64,64)
        Next
        For Local t%=1 To 16
            New Particle( x, y,Rnd(-6,6),Rnd(-6,6),0.95,130,PDM_medparticle,255,192,64,128)
        Next
        For Local t%=1 To 16
            New Particle( x, y,Rnd(-8,8),Rnd(-8,8),0.99,160,PDM_smlparticle,160,160,160,0)
        Next
        For Local t%=1 To 15
            New Particle( x, y,Rnd(-6,6),Rnd(-6,6),0.99,160,PDM_medparticle,160,160,160,0)
        Next
        For Local t%=1 To 14
            New Particle( x, y,Rnd(-4,4),Rnd(-4,4),0.99,160,PDM_bigparticle,160,160,160,0)
        Next

        If game.score>game.bestScore
            game.bestScore = game.score
        Endif

        game.setState(STATE_GAME_OVER)
        Endif
    End Method

    Method draw:Void()
        Local x1# = x-(Sin(rotation) * 10)
        Local y1# = y-(Cos(rotation) * 10)
        Local x2# = x-(Sin(rotation + 140 ) * 8)
        Local y2# = y-(Cos(rotation + 140 ) * 8)
        Local x3# = x-(Sin(rotation - 140 ) * 8)
        Local y3# = y-(Cos(rotation - 140 ) * 8)
        SetColor 255, 255, 255
        DrawLine x1, y1, x2, y2
        DrawLine x2, y2, x3, y3
        DrawLine x3, y3, x1, y1

        SetAlpha 0.5
        If shield < 50 Then SetColor 255,0,0
        DrawRect 10,SCREEN_HEIGHT - 15, Self.shield, 10
        SetAlpha 1
        SetColor 255,0,0
    End Method

End Class

Class Bullet Extends Sprite
    Global list:List<bullet> = New List<bullet>

    Field life#
    Field cr%, cg%, cb%

    Method New(x#,y#,xv#,yv#,life#,cr%,cg%,cb%)
        Self.x = x
        Self.y = y
        Self.xv = xv
        Self.yv = yv
        Self.life = life
        Self.cr = cr
        Self.cg = cg
        Self.cb = cb

        list.AddLast Self
    End Method

    Function updateAll:Void()
        If Not list Return
        For Local b:Bullet = Eachin list
            b.update()
            If b.life < 0
                Bullet.list.Remove(b)
            b = Null
        Endif

        Next
    End Function

    Method update:Void()
        x = x + xv * dt.delta
        y = y + yv * dt.delta

        life-=dt.delta
        If x < 0 x = SCREEN_WIDTH
        If x > SCREEN_WIDTH x = 0
        If y < 0 y = SCREEN_HEIGHT
        If y > SCREEN_HEIGHT y = 0
    End Method

    Function drawAll:Void()
        If Not list Return
        For Local b:Bullet = Eachin list
            b.draw()
        Next   
    End Function

    Method draw:Void()
    Local tmul#
    If life <= 15.0
    tmul = life / 15.0
    Else
    tmul = 1.0
    Endif
    SetColor cr*tmul, cg*tmul, cb*tmul
    DrawLine x, y, x + xv, y + yv
    End Method
End Class

Class Asteroid Extends Sprite
    Global list:List<asteroid> = New List<asteroid>

    Field ang#,angvel#
    Field rad#[9]
    Field avgrad#
    Field size%
    Field life%
    Field cr%, cg%, cb% 

    Method New(x#,y#,xv#,yv#,size%)
        Self.x =x 
        Self.y =y 
        Self.xv =xv 
        Self.yv =yv 
        Self.ang =Rnd(360)
        Self.angvel =Rnd(-6,6)
        Self.size=size
        Self.life=size
        Local tcol% = Rnd(-48,48)
        Self.cr=128+tcol
        Self.cg=128+tcol
        Self.cb=128+tcol
        ' Create "Rockiness"
        Self.avgrad =0
        For Local t% = 0 To 7
            Self.rad [t]=size*8.0+Rnd(-size*4.0,size*4.0)
            Self.avgrad =Self.avgrad +Self.rad[t]
        Next
        Self.avgrad =Self.avgrad /6.0
        Self.rad[8] = Self.rad[0]

        list.AddLast Self
    End Method

    Function drawAll:Void()
        If Not list Return
            For Local b:Asteroid = Eachin list
            b.draw()
        Next   
    End Function

    Function updateAll:Void()
        If Not list Return
            For Local b:Asteroid = Eachin list
            b.update() 
        Next
    End Function

    Method update:Void()
        Self.x =Self.x +Self.xv * dt.delta 
        Self.y =Self.y +Self.yv * dt.delta 
        Self.rotation =Self.rotation +Self.angvel * dt.delta 

        If Self.x <-Self.avgrad  Then Self.x =Self.x + SCREEN_WIDTH + Self.avgrad *2
        If Self.x >SCREEN_WIDTH+Self.avgrad  Then Self.x =Self.x - SCREEN_WIDTH - Self.avgrad *2
        If Self.y <-Self.avgrad  Then Self.y =Self.y + SCREEN_HEIGHT + Self.avgrad *2
        If Self.y >SCREEN_HEIGHT+Self.avgrad  Then Self.y =Self.y - SCREEN_HEIGHT - Self.avgrad *2
    End Method

    Method draw:Void()
        Local tmul# = 360.0 / 8.0
        SetColor cr, cg, cb
        For Local t% = 0 To 7
            DrawLine x-(Sin(rotation+(t)*tmul)*rad[t]),y-(Cos(rotation+(t)*tmul)*rad[t]),x-(Sin(rotation+(t+1)*tmul)*rad[t+1]),y-(Cos(rotation+(t+1)*tmul)*rad[t+1])
        Next
    End Method
End Class

Class Particle Extends Sprite
    Global list:List<particle> = New List<particle>

    Field vm#
    Field life#,mlife#
    Field drawmode%
    Field cr%,cg%,cb%
    Field cflash%

    Method New(x#,y#,xv#,yv#,vm#,life#,drawmode%,cr%,cg%,cb%,cflash%)
        Self.x=x
        Self.y=y
        Self.xv=xv
        Self.yv=yv
        Self.vm=vm
        Self.life=life
        Self.mlife=life
        Self.drawmode=drawmode
        Self.cr=cr
        Self.cg=cg
        Self.cb=cb
        Self.cflash=cflash

        list.AddLast Self
    End Method

    Function updateAll:Void()
        If Not list Return
        For Local b:Particle = Eachin list
            b.update() 
        Next
    End Function

    Function drawAll:Void()
        If Not list Return
        For Local b:Particle = Eachin list
            b.draw()
        Next   
        End Function

    Method update:Void()
        Self.x =Self.x +Self.xv *dt.delta
        Self.y =Self.y +Self.yv *dt.delta
        Self.xv =Self.xv *(1.0-(1.0-Self.vm )*dt.delta )
        Self.yv =Self.yv *(1.0-(1.0-Self.vm )*dt.delta )
        Self.life =Self.life -dt.delta
        If Self.life <0 then="" selflife="0" if="" listremoveself="" endif="" end="" method="" draw:void="" local="" tmul="Self.life" selfmlife="" tfls="Rnd(-Self.cflash,Self.cflash)" setcolor="" limit="" selfcrtmul="" tfls0255limit="" selfcgtmul="" selfcb="" tmultfls0255="" select="" selfdrawmode="" case="" pdm_smlparticle="" drawrect="" selfx="" selfy="" 1="" pdm_medparticle="" drawoval="" -1selfy="" -133="" pdm_bigparticle="" -2selfy="" -255="" pdm_spark="" drawline="" selfxv="" selfyv="" class="" fpscounter="" abstract="" global="" fpscount:int="" starttime:int="" totalfps:int="" function="" update:void="" millisecs="" -="" starttime="">= 1000
            totalFPS = fpsCount
            fpsCount = 0
            startTime = Millisecs()
        Else
            fpsCount+=1
        Endif
    End Function

    Function draw:Void(x% = 0, y% = 0, ax# = 0, ay# = 0)
        DrawText("FPS: " + totalFPS, x, y, ax, ay)
    End Function
End Class

' From James Boyd
Class DeltaTimer
    Field targetfps:Float = 60
    Field currentticks:Float
    Field lastticks:Float
    Field frametime:Float
    Field delta:Float

    Method New (fps:Float)
        targetfps = fps
        lastticks = Millisecs()
    End

    Method UpdateDelta:Void()
        currentticks = Millisecs()
        frametime = currentticks - lastticks
        delta = frametime / (1000.0 / targetfps)
        lastticks = currentticks
    End
End

Function dist#(x1#,y1#,x2#,y2#)
    Return Sqrt(Pow((x1-x2),2) + Pow((y1-y2),2))
End Function

Function limit#(value#,low#,high#)
    If value < low Then Return low
    If value > high Then Return high
    Return value
End Function
</particle></particle></asteroid></asteroid></bullet></bullet></space></space>

 

Monkey: virtual stick

Strict
Import mojo

Class VirtualStickTestApp Extends App
    Const PLAYFIELD_WIDTH:Float = 200
    Const PLAYFIELD_HEIGHT:Float = 200
    Const PLAYER_SPEED:Float = 5

    ' our virtual stick
    Field mystick:MyStick

    ' the "player"'s location
    Field playerX:Float = PLAYFIELD_WIDTH/2
    Field playerY:Float = PLAYFIELD_HEIGHT/2
    Field playfieldX:Float
    Field playfieldY:Float = 10

    Method OnCreate:Int()
        mystick = New MyStick
        mystick.SetRing(100, DeviceHeight()-100, 40)
        mystick.SetStick(0, 0, 15)
        mystick.SetDeadZone(0.2)
        mystick.SetTriggerDistance(5)
        playfieldX = DeviceWidth()-PLAYFIELD_WIDTH-10
        SetUpdateRate 30
        Return 0
    End

    Method OnUpdate:Int()
        ' update the stick usage
        UpdateStick()

        ' update the player position
        If mystick.GetVelocity() <> 0 Then
            playerX += mystick.GetDX() * PLAYER_SPEED
            playerY -= mystick.GetDY() * PLAYER_SPEED
            If playerX < 0 Then
                playerX = 0
            Elseif playerX > PLAYFIELD_WIDTH Then
                playerX = PLAYFIELD_WIDTH
            End
            If playerY < 0 Then
                playerY = 0
            Elseif playerY > PLAYFIELD_HEIGHT Then
                playerY = PLAYFIELD_HEIGHT
            End
        End
        Return 0
    End

    Method OnRender:Int()
        Cls(0,0,0)

        mystick.DoRenderRing()
        mystick.DoRenderStick()
        DrawOutlineRect(playfieldX, playfieldY, PLAYFIELD_WIDTH, PLAYFIELD_HEIGHT)
        DrawCircle(playfieldX + playerX, playfieldY + playerY, 5)

        ' some test info
        DrawText("angle="+mystick.GetAngle(), 10, 10)
        DrawText("vel="+mystick.GetVelocity(), 10, 30)
        DrawText("dx="+mystick.GetDX(), 10, 50)
        DrawText("dy="+mystick.GetDY(), 10, 70)
        Return 0
    End

    Method UpdateStick:Void()
        If mystick.GetTouchNumber() < 0 Then
            #if TARGET="android" Then
                For Local i:Int = 0 To 31
                    If TouchHit(i) And mystick.GetTouchNumber() < 0 Then
                        mystick.StartTouch(TouchX(i), TouchY(i), i)
                    End
                End
            #else
                If MouseHit(0) Then
                    mystick.StartTouch(MouseX(), MouseY(), 0)
                End
            #endif
        End

        If mystick.GetTouchNumber() >= 0 Then
            #if TARGET="android" Then
                If TouchDown(mystick.GetTouchNumber()) Then
                    mystick.UpdateTouch(TouchX(mystick.GetTouchNumber()), TouchY(mystick.GetTouchNumber()))
                Else
                    mystick.StopTouch()
                End
            #else
                If MouseDown(0) Then
                    mystick.UpdateTouch(MouseX(), MouseY())
                Else
                    mystick.StopTouch()
                End
            #endif
        End
    End
End

Class MyStick Extends VirtualStick
    Method RenderRing:Void(x:Float, y:Float)
        SetColor 0, 0, 255
        Super.RenderRing(x, y)
        SetColor 255, 255, 255
    End

    Method RenderStick:Void(x:Float, y:Float)
        SetColor 0, 255, 0
        Super.RenderStick(x, y)
        SetColor 255, 255, 255
    End
End

Class VirtualStick
Private
    ' the coordinates and dimensions for the virtual stick's ring (where the user will first touch)
    Field ringX:Float
    Field ringY:Float
    Field ringRadius:Float

    ' the coordinates and dimensions for the stick (what the user is pushing around)
    ' X/Y is relative to the centre of the ring, and positive Y points up
    Field stickX:Float = 0
    Field stickY:Float = 0
    Field stickRadius:Float
    Field stickAngle:Float
    Field stickPower:Float

    ' where the user first touched
    Field firstTouchX:Float
    Field firstTouchY:Float

    ' power must always be >= this, or we return 0
    Field deadZone:Float

    ' we need to move the stick this much before it triggers
    Field triggerDistance:Float = -1
    Field triggered:Bool = False

    ' the index of the touch event that initiated the stick movement
    Field touchNumber:Int = -1

    ' clips the stick to be within the ring, and updates angles, etc.
    Method UpdateStick:Void()
        If touchNumber>=0 Then
            Local length:Float = Sqrt(stickX*stickX+stickY*stickY)
            stickPower = length/ringRadius
            If stickPower > 1 Then stickPower = 1

            If stickPower < deadZone Then
                stickPower = 0
                stickAngle = 0
                stickX = 0
                stickY = 0
            Else
                If stickX = 0 And stickY = 0 Then
                    stickAngle = 0
                    stickPower = 0
                Elseif stickX = 0 And stickY > 0 Then
                    stickAngle = 90
                Elseif stickX = 0 And stickY < 0 Then
                    stickAngle = 270
                Elseif stickY = 0 And stickX > 0 Then
                    stickAngle = 0
                Elseif stickY = 0 And stickX < 0 Then
                    stickAngle = 180
                Elseif stickX > 0 And stickY > 0 Then
                    stickAngle = ATan(stickY/stickX)
                Elseif stickX < 0 Then
                    stickAngle = 180+ATan(stickY/stickX)
                Else
                    stickAngle = 360+ATan(stickY/stickX)
                End
                If length > ringRadius Then
                    stickPower = 1
                    stickX = Cos(stickAngle) * ringRadius
                    stickY = Sin(stickAngle) * ringRadius
                End
            End
        End
    End

Public

    Method GetTouchNumber:Int()
        Return touchNumber
    End

    ' the angle in degrees that the user is pushing, going counter-clockwise from right
    Method GetAngle:Float()
        Return stickAngle
    End

    ' the strength of the movement (0 means dead centre, 1 means at the edge of the ring (or past it)
    Method GetVelocity:Float()
        Return stickPower
    End

    ' based on the angle and velocity, get the DX
    Method GetDX:Float()
        Return Cos(stickAngle) * stickPower
    End

    ' based on the angle and velocity, get the DY
    Method GetDY:Float()
        Return Sin(stickAngle) * stickPower
    End

    ' we just touched the screen at point (x,y), so start "controlling" if we touched inside the ring
    Method StartTouch:Void(x:Float, y:Float, touchnum:Int)
        If touchNumber < 0 Then
            If (x-ringX)*(x-ringX) + (y-ringY)*(y-ringY) <= ringRadius*ringRadius Then
                touchNumber = touchnum
                firstTouchX = x
                firstTouchY = y
                triggered = False
                If triggerDistance <= 0 Then
                    triggered = True
                    stickX = x-ringX
                    stickY = ringY-y
                End
                UpdateStick()
            End
        End
    End

    ' a touch just moved, so we may need to update the stick
    Method UpdateTouch:Void(x:Float, y:Float)
        If touchNumber>=0 Then
            If Not triggered Then
                If (x-firstTouchX)*(x-firstTouchX)+(y-firstTouchY)*(y-firstTouchY) > triggerDistance*triggerDistance Then
                    triggered = True
                End
            End
            If triggered Then
                stickX = x - ringX
                stickY = ringY - y
                UpdateStick()
            End
        End
    End

    ' we just released a touch, which may have been this one
    Method StopTouch:Void()
        If touchNumber>=0 Then
            touchNumber = -1
            stickX = 0
            stickY = 0
            stickAngle = 0
            stickPower = 0
            triggered = False
        End
    End

    Method DoRenderRing:Void()
        RenderRing(ringX, ringY)
    End

    Method DoRenderStick:Void()
        RenderStick(ringX+stickX, ringY-stickY)
    End

    ' draws the stick (may be overridden to do images, etc.)
    Method RenderStick:Void(x:Float, y:Float)
        DrawCircle(x, y, stickRadius)
    End

    ' draws the outside ring (may be overridden to do images, etc.)
    Method RenderRing:Void(x:Float, y:Float)
        DrawCircle(x, y, ringRadius)
    End

    ' set the location and radius of the ring
    Method SetRing:Void(ringX:Float, ringY:Float, ringRadius:Float)
        Self.ringX = ringX
        Self.ringY = ringY
        Self.ringRadius = ringRadius
    End

    ' set the location and radius of the stick
    Method SetStick:Void(stickX:Float, stickY:Float, stickRadius:Float)
        Self.stickX = stickX
        Self.stickY = stickY
        Self.stickRadius = stickRadius
    End

    Method SetDeadZone:Void(deadZone:Float)
        Self.deadZone = deadZone
    End

    Method SetTriggerDistance:Void(triggerDistance:Float)
        Self.triggerDistance = triggerDistance
    End
End

Function Main:Int()
    New VirtualStickTestApp
    Return 0
End

Function DrawOutlineRect:Void(x:Float, y:Float, width:Float, height:Float)
    DrawLine(x, y, x+width, y)
    DrawLine(x, y, x, y+height)
    DrawLine(x+width, y, x+width, y+height)
    DrawLine(x, y+height, x+width, y+height)
End

Monkey: multitouch demo

Strict
Import mojo

Function Main:Int()
    New MyApp
    Return 0
End

Class MyApp Extends App
    Method OnCreate:Int()
        SetUpdateRate(120)
        Return 0
    End

    Method OnUpdate:Int()
        Return 0
    End

    Method OnRender:Int()
        Cls(0,0,0)
        Local dy:Int = 30
        For Local i:Int = 0 To 31
            If TouchDown(i) Then
                If i Mod 2 = 0 Then
                    SetColor(255,0,0)
                Else
                    SetColor(0,0,255)
                End
                DrawCircle(TouchX(i),TouchY(i),50)
                SetColor(255,255,255)
                DrawText(""+i, 10, dy)
                DrawText(""+Int(TouchX(i)), 40, dy)
                DrawText(""+Int(TouchY(i)), 80, dy)
                dy+=20
            End
        End
        DrawText("idx",10,10)
        DrawText("x",40,10)
        DrawText("y",80,10)
        Return 0
    End
End

Monkey: fantomEngine – using text object as a button

Strict

#rem
   Script:      touchTest.monkey
   Description:   Sample Script fantomEngine to display the use of touch events 
   Author:       Michael
#end

Import fantomEngine
Global g:game

'***************************************
Class game Extends App
   Field eng:engine
   Field font1:ftFont
   '------------------------------------------
   Method CreateObjects:Int()

      'Create a box
      Local b:ftObject = eng.CreateBox(100,50,eng.canvasWidth/2,eng.canvasHeight/2-150)
      'Set TouchMode to bounding box(2)    ... 1=Circle
      b.SetTouchMode(2)
      b.SetName("Box")

      'Create a circle
      Local c:ftObject = eng.CreateCircle(50,eng.canvasWidth/2,eng.canvasHeight/2)
      c.SetTouchMode(1)
      c.SetName("Circle")

      'Create a text
      Local t:ftObject = eng.CreateText(font1,"This is a clickable text", eng.canvasWidth/2,eng.canvasHeight/2+150,1)
      t.SetTouchMode(2)
      t.SetName("Text")

      Return 0
   End
   '------------------------------------------
   Method OnCreate:Int()
      SetUpdateRate(60)

      'Store the engine instance
      eng = New engine

      'Load the font bitmap
      font1 = eng.LoadFont("bo_font")             '<========== please change this to a font that you have

      'Create the objects
      CreateObjects()
      Return 0
   End

   '------------------------------------------
   Method OnUpdate:Int()
      Local d:Float = Float(eng.CalcDeltaTime())/60.0
      eng.Update(Float(d))
      'check if you have a touch hit and then do a touch check.
      If TouchHit(0) Then   eng.TouchCheck()
      Return 0
   End

   '------------------------------------------
   Method OnRender:Int()
      Cls
      eng.Render()
      Return 0
   End
End  

'***************************************
Class engine Extends ftEngine
   '------------------------------------------
   Method OnObjectTouch:Int(obj:ftObject, touchId:Int)
      Print ("Object "+obj.GetName()+" was hit!")
      Return 0
   End
End

'***************************************
Function Main:Int()
   g = New game
   Return 0
End

Monkey: a base script for the fantomEngine

Strict

#rem
    Script:     baseScript.monkey
    Description:    Base Script fantomEngine 
    Author:         Michael Hartlef
    Version:      1.0
#end

Import fantomEngine
Global g:game

'***************************************
Class game Extends App
    Field eng:engine
    Field isSuspended:Bool = False
    '------------------------------------------
    Method OnCreate:Int()
        SetUpdateRate(60)
        eng = New engine
        Return 0
    End
    '------------------------------------------
    Method OnUpdate:Int()
        Local d:Float = Float(eng.CalcDeltaTime())/60.0
        If isSuspended = False Then
            eng.Update(Float(d))
        Endif
        Return 0
    End
    '------------------------------------------
    Method OnRender:Int()
        Cls
        eng.Render()
        Return 0
    End
    '------------------------------------------
    Method OnResume:Int()
        isSuspended = False
        SetUpdateRate(60)
        Return 0
    End
    '------------------------------------------
    Method OnSuspend:Int()
        isSuspended = True
        SetUpdateRate(5)
        Return 0
    End
End

'***************************************
Class engine Extends ftEngine
    '------------------------------------------
    Method OnObjectCollision:Int(obj:ftObject, obj2:ftObject)
        Return 0
    End
    '------------------------------------------
    Method OnObjectTimer:Int(timerId:Int, obj:ftObject)
        Return 0
    End
    '------------------------------------------
    Method OnObjectTouch:Int(obj:ftObject, touchId:Int)
        Return 0
    End
    '------------------------------------------
    Method OnObjectTransition:Int(transId:Int, obj:ftObject)
        Return 0
    End
    '------------------------------------------
    Method OnObjectUpdate:Int(obj:ftObject)
        Return 0
    End
    '------------------------------------------
    Method OnLayerTransition:Int(transId:Int, layer:ftLayer)
        Return 0
    End
    '------------------------------------------
    Method OnLayerUpdate:Int(layer:ftLayer)
        Return 0
    End
End

'***************************************
Function Main:Int()
    g = New game
    Return 0
End

 

Monkey: drawing a diffusion effect

'******************************************************************
'* Diffusion Example
'* Author: Richard R Betson
'* Date: 11/25/11
'* Language: Monkey
'* Target: HTML5              
'******************************************************************
' Linsence - Public Domain
'******************************************************************
' Web Site - <a href="http://redeyeware.uphero.com" target="_blank">http://redeyeware.uphero.com</a>
'******************************************************************
' Canvas Wrapper Mod by Impixi - <a href="http://www.monkeycoder.co.nz/Community/posts.php?topic=1885" target="_blank">http://www.monkeycoder.co.nz/Community/posts.php?topic=1885</a>
'******************************************************************

Import monkey
Import mojo
Import canvaswrapper

Global flag=0
Global clra:Int
Global ang_index_1:Int[360],ang_index_2:Int[360],ang_index_3:Int[360],ang_index_4:Int[360]

Function Main()
  New Diffusion
End Function

Class Diffusion Extends App
Field imd:ImageData

Global fps,fp,fps_t
Global mapx:Int[77900]
Global mapx1:Int[77900]
Global mapx2:Int[77900]
Global mapx3:Int[77900]
Global mapx4:Int[77900]

Global clrr:Int[77900]
Global clrb:Int[77900]

Global clrr1:Int[77900]
Global clrb1:Int[77900]

Global ii
Global fxsel_ttl
Global fxsel

Global fred:Int=1,fblue:Int=2
Global qtext:String="Highest Quality",qt:Int=3,widetext:String=""

Global xa:Int=1,xb:Int=319,ya:Int=1,yb:Int=239

  Method OnCreate()
    fxsel_ttl=Millisecs()+16000
    SetUpdateRate(60)
    SetLookUp()
    imd = xCanvas.CreateImageData(640,480)'640,480)
Local i2:Int
  For i2=0 To 359
    ang_index_1[i2]=(160 + (Sin(i2)*40) )
    ang_index_2[i2]=(120 + (Cos(i2)*40) )
    ang_index_3[i2]=(160 + (-Sin(i2)*40) )
    ang_index_4[i2]=(120 + (-Cos(i2)*40) )
  Next

  End Method

  Method OnUpdate()
    If KeyHit(KEY_F1)
    For Local y=0 To 240
      'Local y1:Int=320 *y
      For Local x= 0 To 320
        xCanvas.SetImageDataPixel(imd, x*2,(y*2), 0, 0, 0, 255)
        xCanvas.SetImageDataPixel(imd, (x*2)+1,(y*2) ,0, 0, 0, 255)
        xCanvas.SetImageDataPixel(imd, (x*2),((y*2)+1), 0, 0, 0, 255)
        xCanvas.SetImageDataPixel(imd, ((x*2)+1),((y*2)+1) ,0, 0, 0, 255)
      Next
    Next
'Cls    
      qt=qt+1
      If qt>5 Then qt=0
      If qt=0 Then qtext="Low Quality"
      If qt=1 Then qtext="Med Quality"
      If qt=2 Then qtext="Med-High Quality"
      If qt=3 Then qtext="Highest Quality"
      If qt=4 Then qtext="1/4 Screen H-Quality" 
      If qt=5 Then qtext="1/4 Screen x 4 H-Quality"
    Endif

    If KeyHit(KEY_F2)
      For Local y=0 To 240
        'Local y1:Int=320 *y
        For Local x= 0 To 320
          xCanvas.SetImageDataPixel(imd, x*2,(y*2), 0, 0, 0, 255)
          xCanvas.SetImageDataPixel(imd, (x*2)+1,(y*2) ,0, 0, 0, 255)
          xCanvas.SetImageDataPixel(imd, (x*2),((y*2)+1), 0, 0, 0, 255)
          xCanvas.SetImageDataPixel(imd, ((x*2)+1),((y*2)+1) ,0, 0, 0, 255)
        Next
      Next

      If widetext=""
        ya=60
        yb=180
        widetext="WIDE -"
      Else
        ya=1
        yb=239
        widetext=""
      Endif
    Endif

    If fxsel_ttl<millisecs() fxsel="" fxsel_ttl="Millisecs()+14000" if="">4 Then fxsel=0
      Select fxsel
      Case 0
        fred=1
        fblue=2

      Case 1
        fred=50
        fblue=4
      Case 2
        fred=3
        fblue=3
      Case 3
        fred=2
        fblue=4
      Case 4
        fred=3
        fblue=4
    End Select

    Endif 

  End Method

  Method OnRender()

    fps=fps+1
    If fps_t<millisecs() fp="(fps)" fps_t="1000+Millisecs()" fps="0" endif="" ii="" if="">359 Then ii=0

    Local x2:Int=ang_index_1[ii]
    Local y2:Int=ang_index_2[ii]
    Local x3:Int=ang_index_3[ii]
    Local y3:Int=ang_index_4[ii]

    'Draw Line
    LineB(160,120,x2,y2)
    LineB(160,120,x3,y3)

    For Local zz=0 To 100
      Local xz=Int(Rnd(50))+135
      Local yz=Int(Rnd(50))+95
      clrb1[ ( 320 *yz ) +xz ]=255

    Next

    For Local y=ya To yb
        Local y2:Int=(320 *y)
      For Local x= xa To xb 
        Local y1:Int=y2+x
        'Apply mapping via lookup table
        If fxsel=0
        clrr[  y1  ] = clrr1[ mapx[ y1 ] ]
        clrb[  y1  ] = clrb1[ mapx[ y1 ] ]
        Else If fxsel=1
        clrr[  y1   ] = clrr1[ mapx1[ y1  ] ]
        clrb[  y1   ] = clrb1[ mapx1[ y1  ] ]
        Else If fxsel=2
        clrr[  y1   ] = clrr1[ mapx2[ y1  ] ]
        clrb[  y1   ] = clrb1[ mapx2[ y1  ] ]
        Else If fxsel=3
        clrr[  y1   ] = clrr1[ mapx3[ y1  ] ]
        clrb[  y1   ] = clrb1[ mapx3[ y1  ] ]
        Else If fxsel=4
        clrr[  y1   ] = clrr1[ mapx4[ y1  ] ]
        clrb[  y1   ] = clrb1[ mapx4[ y1  ] ]
        Endif

      Next
    Next

Local sumr,sumb,b,r

    For Local y=ya To yb
      Local y1:Int=320 *y
      Local yn1:Int=y*2
      For Local x= xa To xb
        Local yn2:Int=y1+x
        'Blur Image and Fade Color

        r=clrr[yn2]
        sumr = ((r*4) + clrr[ yn2+1   ] + clrr[ yn2-1   ] + clrr[ ( y1-1 ) + x   ] + clrr[ ( y1+1 ) + x   ] ) Shr 3 

        b=clrb[yn2]
        sumb = ((b*4) + clrb[ yn2+1   ] + clrb[ yn2-1   ] + clrb[ ( y1-1 ) + x   ] + clrb[ ( y1+1 ) + x   ] ) Shr 3

        r=sumr-fred
        b=sumb-fblue

        If r<0 then="" r="" if="">255 Then r=255
        If b<0 then="" b="" if="">255 Then b=255

        clrr1[ yn2 ]=r
        clrb1[ yn2 ]=b

        Local xn1:Int=x*2

        If qt=0
            xCanvas.SetImageDataPixel(imd, xn1,yn1, r, 0, b, 255)         
          Else If qt=1
            xCanvas.SetImageDataPixel(imd, xn1,yn1, r, 0, b, 255)         
            xCanvas.SetImageDataPixel(imd, xn1,yn1+1, r, 0, b, 255)
          Else If qt=2
            xCanvas.SetImageDataPixel(imd, xn1,yn1, r, 0, b, 255)
            xCanvas.SetImageDataPixel(imd, xn1+1,yn1 ,r, 0, b, 255)
            xCanvas.SetImageDataPixel(imd, xn1,yn1+1, r, 0, b, 255)         
          Else If qt=3
            xCanvas.SetImageDataPixel(imd, xn1,yn1, r, 0, b, 255)
            xCanvas.SetImageDataPixel(imd, xn1+1,yn1 ,r, 0, b, 255)
            xCanvas.SetImageDataPixel(imd, xn1,yn1+1, r, 0, b, 255)
            xCanvas.SetImageDataPixel(imd, xn1+1,yn1+1 ,r, 0, b, 255)
          Else If qt=4
            xCanvas.SetImageDataPixel(imd, x,y, r, 0, b, 255)
          Else If qt=5
            xCanvas.SetImageDataPixel(imd, x,y, r, 0, b, 255)
            xCanvas.SetImageDataPixel(imd, (x+319),y ,r, 0, b, 255)
            xCanvas.SetImageDataPixel(imd, x,(y+239), r, 0, b, 255)
            xCanvas.SetImageDataPixel(imd, (x+319),(y+239) ,r, 0, b, 255)
          Endif

Next
Next

    xCanvas.DrawImageData imd, 0, 0
    DrawText("Hit F1/F2 Change Qualtity-Size/Wide Screen: "+widetext+" "+qtext,10,440)
    DrawText("HTML5 Diffusion - Richard Betson, <a href="http://redeyeware.uphero.com" target="_blank">http://redeyeware.uphero.com</a> - FPS:"+fp,10,460)

  End Method

  Function LineB(x1,y1,x2,y2)
    'Ported
    'Bresenham Line Algorithm 
    'Source - GameDev.Net - Mark Feldman
    'Public Domain

    Local deltax = Abs(x2 - x1)
    Local deltay = Abs(y2 - y1) 

    Local numpixels,d,dinc1,dinc2,xinc1,xinc2,yinc1,yinc2,x,y,i

    If deltax >= deltay 
      numpixels = deltax + 1
      d = (2 * deltay) - deltax
      dinc1 = deltay Shl 1
      dinc2 = (deltay - deltax) Shl 1
      xinc1 = 1
      xinc2 = 1
      yinc1 = 0
      yinc2 = 1
    Else
      numpixels = deltay + 1
      d = (2 * deltax) - deltay
      dinc1 = deltax Shl 1
      dinc2 = (deltax - deltay) Shl 1
      xinc1 = 0
      xinc2 = 1
      yinc1 = 1
      yinc2 = 1
    Endif

    If x1 > x2
      xinc1 = -xinc1
      xinc2 = -xinc2
    Endif

    If y1 > y2
      yinc1 = -yinc1
      yinc2 = -yinc2

    Endif

    x = x1
    y = y1

    For i = 1 To numpixels 

      If d < 0
        d = d + dinc1
        x = x + xinc1
        y = y + yinc1
      Else
        d = d + dinc2
        x = x + xinc2
        y = y + yinc2
      Endif

      'Draw line 
      If x>xa And x<xb and="" y="">ya And y<yb clrr1="" 320="" y="0" x="0" endif="" next="" end="" function="" method="" setlookup="" local="" ang="0" for="" lui="0" to="" 4="" 240="" rad="" abs="" y-120="" if="">0
          rad= Sqrt(rad)
        Else
          rad=0
        Endif
        If rad=0 Then rad=1

        Local dx#=((x-160)/(rad))
        Local dy#=((y-120)/(rad))
        If lui=1
          rad= ( ( (  ((Sin(rad*.2)*29.5) ) * (  ((Cos(rad*.81)*9.5) ) ) ) )   )

          dx=(dx*Cos(ang) - dy*Sin(ang))
          dy=(dy*Cos(ang) + dx*Sin(ang))

          Local x1=Int(dx*rad)+(160)
          Local y1=Int(dy*rad)+(120)

          If y1<1 then="" y1="" if="">240 Then y1=240
          If x1<1 then="" x1="" if="">320 Then x1=320

          mapx1[ ( 320 *y ) +x ] = ( 320 *y1 ) +x1'Int(x1)
        Endif

        If lui=2
          'Free Float
          ang=1
          rad= 1-( ( rad- (Sin(rad*900)*-Cos(rad*900)*.5 )*3.1415926 ) *  (-Cos((3.1415926) ) )*.9  )
          Local x1=Int(dx*rad)+(160)
          Local y1=Int(dy*rad)+(120)
          x1= ( x1*Cos(ang) - y1*Sin(ang) )
          y1= ( x1*Sin(ang) + y1*Cos(ang) )

          If y1<1 then="" y1="" if="">240 Then y1=240
          If x1<1 then="" x1="" if="">320 Then x1=320

          mapx2[ ( 320 *y ) +x ] = ( 320 *y1 ) +x1
        Endif

        If lui=3
          rad= rad-(  (Sin(rad*PI)*3)- ((Cos(rad*PI)*3) )  )

          dx=(dx*Cos(ang) - dy*Sin(ang))
          dy=(dy*Cos(ang) + dx*Sin(ang))

          Local x1=Int(dx*rad)+(160)
          Local y1=Int(dy*rad)+(120)

          If y1<1 then="" y1="" if="">240 Then y1=240
          If x1<1 then="" x1="" if="">320 Then x1=320

          mapx3[ ( 320 *y ) +x ] = ( 320 *y1 ) +x1

        Endif
        If lui=0
          ang=-3

          Local yq:Int=y
          If yq<=120
          rad= rad-(  (Sin(yq))    )
          rad= rad-(  (Cos(yq)) )
          Else
          rad= rad-(  (Sin((240-yq)) )    )
          rad= rad-(  (Cos((240-yq)) )    )
          Endif

          dx=(dx*Cos(ang) - dy*Sin(ang))
          dy=(dy*Cos(ang) + dx*Sin(ang))

          Local x1=Int(dx*rad)+(160)
          Local y1=Int(dy*rad)+(120)

          If y1<1 then="" y1="" if="">240 Then y1=240
          If x1<1 then="" x1="" if="">320 Then x1=320

          mapx[ ( 320 *y ) +x ] = ( 320 *y1 ) +x1

        Endif
        If lui=39
          'Orb
          rad= 2-( ( rad- (Sin(rad)*(Cos(rad)) )*3.1415926 ) *  (-Cos((3.1415926) ))*.97  )
          dx=(dx*Cos(ang) - dy*Sin(ang))
          dy=(dy*Cos(ang) + dx*Sin(ang))

          Local x1=Int(dx*rad)+(160)
          Local y1=Int(dy*rad)+(120)

          If y1<1 then="" y1="" if="">240 Then y1=240
          If x1<1 then="" x1="" if="">320 Then x1=320

          mapx3[ ( 320 *y ) +x ] = ( 320 *y1 ) +x1
        Endif
        If lui=4
          'Swirl
           ang=-3
          rad= 1-( ( rad- (Sin(rad)*-Cos(rad)*.9 )*3.1415926 ) *  (-Cos((3.1415926) ) )*.9  )
          dx=(dx*Cos(ang) - dy*Sin(ang))
          dy=(dy*Cos(ang) + dx*Sin(ang))

          Local x1=Int(dx*rad)+(160)
          Local y1=Int(dy*rad)+(120)

          If y1<1 then="" y1="" if="">240 Then y1=240
          If x1<1 then="" x1="" if="">320 Then x1=320

          mapx4[ ( 320 *y ) +x ] = ( 320 *y1 ) +x1
      Endif

      Next
    Next
  Next

  End Method

End Class

Monkey: drawing a mosaic

Import mojo
Global sprite:MyApp

Function Main()
    sprite = New MyApp()
End Function

Class TColor
    Field r:Int,g:Int,b:Int
    Method Color()
        SetColor r,g,b
    End Method

    Method Set(rgb$)
        Self.r=255*(rgb[0]-48)/7
        Self.g=255*(rgb[1]-48)/7
        Self.b=255*(rgb[2]-48)/7
    End Method
End Class

Class MyApp Extends App
    Field pixel:Int[256]
    Field palette:TColor[16]

    Method OnCreate()
        SetUpdateRate 50

        For Local col:Int=0 To 15
            palette[col]=New TColor
            palette[col].Set "000333555777300520742764003025247467020040160370"[col*3..col*3+3]
        Next

        For Local t:Int=0 To 255
            pixel[t]=Rnd(16)
        Next
    End Method

    Method OnRender()
        Local i:Int
        For Local y:Int=0 To 15
            For Local x:Int=0 To 15
                palette[pixel[i]].Color
                DrawRect x*16,y*16,16,16
                i=i+1
            Next
        Next
    End Method

    Method OnUpdate()
    End Method
End Class