Saturday, April 9, 2016

Monkey-X - Tinting a Image using pixmap - code example


Tinting an image is adding the color r,g,b to the original r,g,b value.

Import mojo

Class theimage
    Field x:Int
    Field y:Int
    Field image:Image
    Field pixels:Int[]
    Field width:Int
    Field height:Int
    Method New(    x:Int,y:Int,
                width:Int,
                height:Int)
        pixels = New Int[width*height]
        image = CreateImage(width,height)
        Self.x = x
        Self.y = y
        Self.width = width
        Self.height = height
        makeimage
        image.WritePixels(pixels, 0, 0, width, height, 0)
    End Method
    Method makeimage()
        Seed = 10
        For Local i:Int = 0 Until width*height
            Local col:Int=Rnd(255)
            pixels[i] = argb(col,col,col)
        Next
    End Method
    Method tintimage(r:Int,g:Int,b:Int)
        makeimage
        For Local i:Int = 0 Until width*height
            Local r2:Int = getred(pixels[i])
            Local g2:Int = getgreen(pixels[i])
            Local b2:Int = getblue(pixels[i])
            r2+=r
            g2+=g
            b2+=b
            r2 = Clamp(r2,0,255)
            g2 = Clamp(g2,0,255)
            b2 = Clamp(b2,0,255)
            pixels[i] = argb(r2,g2,b2)
        Next
        image.WritePixels(pixels, 0, 0, width, height, 0)        
    End Method
    Method draw()
        DrawImage image,x,y
    End Method
End Class    

Global im1:theimage
Global im2:theimage

Class MyGame Extends App    
    Field tintred:Int = 100
    Field tintgreen:Int = 0
    Field tintblue:Int = 0
    Method OnCreate:Int()
        SetUpdateRate(60)
        im1 = New theimage(10,20,200,200)
        im2 = New theimage(330,20,200,200)
        '
        ' Here we tint the image
        '
        im2.tintimage(tintred,tintgreen,tintblue)
    End Method
    Method OnUpdate()
        If Rnd(320)<2
            DebugLog Millisecs()
            Seed = Millisecs()
            tintred = Rnd(0,255)
            tintgreen = Rnd(0,255)
            tintblue = Rnd(0,255)
            im2.tintimage(tintred,tintgreen,tintblue)
        End If
    End Method
    Method OnRender:Int()
        Cls
        im1.draw
        im2.draw
        SetColor 255,255,255
        DrawText "Monkey-X - Tinting a image (pixels).",0,0
        DrawText     "Red: "+tintred+
                    " Green: "+tintgreen+
                    " BLue: "+tintblue,
                    10,300
        SetColor tintred,tintgreen,tintblue
        DrawRect 400,300,50,50
    End Method    
End Class

Function Main:Int()
    New MyGame()
End Function

Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255)
   Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b          
End Function

Function getred:Int(rgba:Int)    
    Return((rgba Shr 16) & $FF)    
End Function
              
Function getgreen:Int(rgba:Int)    
    Return((rgba Shr 8) & $FF)    
End Function
    
Function getblue:Int(rgba:Int)    
    Return(rgba & $FF)    
End Function
    
Function getalpha:Int(rgba:Int)    
    Return ((rgba Shr 24) & $FF)    
End Function

Monday, April 4, 2016

Monkey-X - Red Clouds (drunken walk) - code example


Import mojo

Class themap
    Field map:Int[][]
    Field mapwidth:Int
    Field mapheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Method New(    mapwidth:Int,
                mapheight:Int)
        tilewidth = DeviceWidth()/Float(mapwidth)
        tileheight = DeviceHeight()/Float(mapheight)
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        map = New Int[mapwidth][]
           For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        makemap        
    End Method
    Method makemap()
        Local x:Int=mapwidth/2
        Local y:Int=mapheight/2
        Local dx:Int[]=[0,1,0,-1]
        Local dy:Int[]=[-1,0,1,0]
        For Local i=0 To mapwidth*mapheight
              Local m : Int = map[x][y]+16
              If m>255 Then m=255 
            map[x][y] = m 
            Local d:Int=Rnd(0,4)
            x+=dx[d]
            y+=dy[d]
              If     x<1 Or y<1 Or 
                  x>mapwidth-3 Or 
                  y>mapheight-3 Then 
                  x=mapwidth/2 
                  y=mapheight/2 
              End If
        Next        
    End Method
    Method draw()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            Local col:Int=map[x][y]    
            If col > 0        
                SetColor col,0,0
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth+1,
                            tileheight+1
            end if
        Next
        Next
    End Method
End Class

Global mymap:themap

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
        Local date := GetDate()
        Seed = date[5]
        mymap = New themap(320,256)
    End Method
    Method OnUpdate()        
        If KeyHit(KEY_SPACE) = True
            mymap = New themap(320,356)
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        mymap.draw
        SetColor 255,255,255
        DrawText     "Monkey-X - Red Clouds Example.",
                    0,0
        DrawText "Press Space to Render new",0,20
    End Method
End Class


Function Main()
    New MyGame()
End Function

Sunday, April 3, 2016

MonkeyX - Wait/Busy animation - code example


Import mojo

Class waitanim
    Field angle:Int
    Field x:Int,y:Int
    Field size:Int
    Method New(x:Int,y:Int,size:Int)
        Self.x = x
        Self.y = y
        Self.size = size
    End Method
    Method update()
        angle+=4
        If angle>359 Then angle=0
    End Method
    Method draw()
        SetColor 150,150,150
        For Local i=0 To 360
            DrawOval     x+(Cos(i)*size),
                        y+(Sin(i)*size),4,4
        Next
        SetColor 220,220,220
        For Local i=0 To 48
            Local da:Int=angle+i
            If da>359 Then da = da-359
            DrawOval     x+(Cos(da)*size),
                        y+(Sin(da)*size),6,6
        Next
    End Method
End Class

Global mywait:waitanim

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)        
        mywait = New waitanim(    DeviceWidth/2,
                                DeviceHeight/2,
                                24)
    End Method
    Method OnUpdate()
        mywait.update        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText     "Wait/Busy Animation "+
                    "thing - code example.",
                    0,0
        mywait.draw
    End Method
End Class


Function Main()
    New MyGame()
End function

Saturday, April 2, 2016

MonkeyX - Tunneling/Rooms/Blurring map generator - code example


What the code does is tunnel through the map and sometimes store a coord from the path.
This location then after a amount of steps gets a room placed on top. The blurring can be
1, 2 or 3. 1 having no edges, just lines and rooms. and 3 going around to each cell and 
placing a lower value if possible. This creates the look of caverns.

Import mojo

Class themap
    Field x:Int
    Field y:Int
    Field mapwidth:Int
    Field mapheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Field map:Int[][]
    'this holds the offsets the we
    'use to tunnel. 0=upwards;1=right ect
    'use like : x+=dx[0]
    Field dx:Int[]=[0,1,0,-1]
    Field dy:Int[]=[-1,0,1,0]
    Field blur:Int
    Method New(    mapwidth:Int,
                mapheight:Int,
                blur:Int)
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        Self.blur = blur
        tilewidth = DeviceWidth()/mapwidth
        tileheight = DeviceHeight()/mapheight
        x = mapwidth/2
        y = mapheight/2
        map = New Int[mapwidth][]
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next            
        tunnel()
           mapblur()
    End Method
    Method mapblur()
        For Local i=0 To mapwidth*mapheight
            Local x1:Int=Rnd(2,mapwidth-4)
            Local y1:Int=Rnd(2,mapheight-4)
            If map[x1][y1] > 0
                For Local y2=y1-1 To y1+1
                For Local x2=x1-1 To x1+1
                    If map[x2][y2] = 0
                        map[x2][y2] = map[x1][y1] / 2
                    End If
                Next
                Next
            End If
        Next
    End Method
    Method tunnel()
        ' direction of tunneling
        Local dir:Int=1
        'make room countdown
        Local mrt:Int=10
        ' location on the path
        ' where next room is to be made
        Local rx:Int,ry:Int
        For Local i=0 Until ((mapwidth*mapheight)/10)
            map[x][y] = blur
            If Rnd(0,5) < 2
                mrt-=1
                dir = newdir(dir)
                
                If mrt = 0
                If rx>3 And ry>3
                If rx<mapwidth-3 And ry<mapheight-3
                    For Local y1 = ry-3 To ry+3
                    For Local x1 = rx-3 To rx+3
                        map[x1][y1] = blur
                    Next
                    Next
                End If
                End If
                mrt=10
                End If
                If Rnd(0,20)<2 Or mrt=0
                    rx = x
                    ry = y
                    mrt = 10
                End If
            End If
            If     x>3 And x<mapwidth-3 And 
                y>3 And y<mapheight-3 Then
                x+=dx[dir]
                y+=dy[dir]
            Else
                x = mapwidth/2
                y = mapheight/2
            End If
            
        Next
    End Method
    Method newdir:Int(dir:Int)
        Local exitloop:Bool=False
        Local cnt:Int=0
        While exitloop=False
            Local d:Int=Rnd(0,4)
            cnt+=1
            If cnt>500 Then Return 0
            If d<>dir
                ' is nothing ahead taken
                If map[x+dx[d]][y+dy[d]] = 0
                If map[x+(dx[d]*2)][y+(dy[d]*2)] = 0
                ' do not go in opposite direction
                Select d
                    Case 0
                    If dir<>2 Then 
                        Return d
                    End If
                    Case 1
                    If dir<>3 Then
                        Return d
                    End If
                    Case 2
                    If dir<>0 Then
                        Return d
                    End If
                    Case 3
                    If dir<>1 Then
                        Return d
                    End If
                End Select
                End If
                End If
            End If
        Wend
    End Method
    Method draw()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If map[x][y] > 0
                Local d:Int=distance(    mapwidth/2,
                                        mapheight/2,
                                        x,
                                        y)

                Local col:Int=0+(1500/(d+1))
                SetColor col,col,col
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth-1,
                            tileheight-1            
            End If
        Next
        Next
    End Method
    Method distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
        Return Abs(x2-x1)+Abs(y2-y1)
    End Method
End Class

Global mymap:themap

Class MyGame Extends App
    Field cnt:Int
    Field mw:Int
    Field mh:Int
    Field bl:Int
    Method OnCreate()
        SetUpdateRate(60)
        Local date := GetDate()
        Seed = date[5]        
        mymap = New themap(Rnd(32,150),Rnd(32,150),Rnd(1,10))
    End Method
    Method OnUpdate()        
        cnt+=1
        If cnt>200
            mw = Rnd(32,150)
            mh = Rnd(32,150)
            'the blurring(edging)
            bl = Rnd(1,4)
            mymap = New themap(mw,mh,bl)
            cnt=0
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        mymap.draw
        SetColor 255,255,255
        DrawText     "Mapw:"+mw+
                    " Maph:"+mh+" Blur:"+
                    bl,0,20
        DrawText     "Map gen - Tunneling and "+
                    "path room placement and "+
                    "blurring.",0,0
    End Method
End Class


Function Main()
    New MyGame()
End Function

MonkeyX - Tunneling and Rooms map generator - code example


I read through a article and it mentioned prefabs and tunneling. I made this 
to see if I could do anything new. Not very special or so. It basically tunnels in a 
random direction and sometimes stores a position on the path where it after a number of
loops places a room.


Import mojo

Class themap
    Field x:Int
    Field y:Int
    Field mapwidth:Int
    Field mapheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Field map:Int[][]
    'this holds the offsets the we
    'use to tunnel. 0=upwards;1=right ect
    'use like : x+=dx[0]
    Field dx:Int[]=[0,1,0,-1]
    Field dy:Int[]=[-1,0,1,0]
    Method New(    mapwidth:Int,
                mapheight:Int)
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        tilewidth = DeviceWidth()/mapwidth
        tileheight = DeviceHeight()/mapheight
        x = mapwidth/2
        y = mapheight/2
        map = New Int[mapwidth][]
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next            
        tunnel()
    End Method
    Method tunnel()
        ' direction of tunneling
        Local dir:Int=1
        'make room countdown
        Local mrt:Int=10
        ' location on the path
        ' where next room is to be made
        Local rx:Int,ry:Int
        For Local i=0 Until ((mapwidth*mapheight)/10)
            map[x][y]=1
            If Rnd(0,5) < 2
                mrt-=1
                dir = newdir(dir)
                
                If mrt = 0
                If rx>3 And ry>3
                If rx<mapwidth-3 And ry<mapheight-3
                    For Local y1 = ry-3 To ry+3
                    For Local x1 = rx-3 To rx+3
                        map[x1][y1]=1
                    Next
                    Next
                End If
                End If
                mrt=10
                End If
                If Rnd(0,20)<2 Or mrt=0
                    rx = x
                    ry = y
                    mrt = 10
                End If
            End If
            If     x>3 And x<mapwidth-3 And 
                y>3 And y<mapheight-3 Then
                x+=dx[dir]
                y+=dy[dir]
            Else
                x = mapwidth/2
                y = mapheight/2
            End If
            
        Next
    End Method
    Method newdir:Int(dir:Int)
        Local exitloop:Bool=False
        Local cnt:Int=0
        While exitloop=False
            Local d:Int=Rnd(0,4)
            cnt+=1
            If cnt>500 Then Return 0
            If d<>dir
                ' is nothing ahead taken
                If map[x+dx[d]][y+dy[d]] = 0
                If map[x+(dx[d]*2)][y+(dy[d]*2)] = 0
                ' do not go in opposite direction
                Select d
                    Case 0
                    If dir<>2 Then 
                        Return d
                    End If
                    Case 1
                    If dir<>3 Then
                        Return d
                    End If
                    Case 2
                    If dir<>0 Then
                        Return d
                    End If
                    Case 3
                    If dir<>1 Then
                        Return d
                    End If
                End Select
                End If
                End If
            End If
        Wend
    End Method
    Method draw()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If map[x][y] > 0
                Local d:Int=distance(    mapwidth/2,
                                        mapheight/2,
                                        x,
                                        y)

                Local col:Int=0+(1500/(d+1))
                SetColor col,col,col
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth-1,
                            tileheight-1
            End If
        Next
        Next
    End Method
    Method distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
        Return Abs(x2-x1)+Abs(y2-y1)
    End Method
End Class

Global mymap:themap

Class MyGame Extends App
    Field cnt:Int
    Method OnCreate()
        SetUpdateRate(60)
        Local date := GetDate()
        Seed = date[5]        
        mymap = New themap(Rnd(32,150),Rnd(32,150))
    End Method
    Method OnUpdate()        
        cnt+=1
        If cnt>100
            mymap = New themap(Rnd(32,150),Rnd(32,150))
            cnt=0
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        mymap.draw
    End Method
End Class


Function Main()
    New MyGame()
End Function

MonkeyX - Map Island Dungeon Generator exp. - code example


Import mojo

Class themap
    Field mapwidth:Int
    Field mapheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Field map:Int[][]
    Field cmode:Int=Rnd(1,4)
    Method New(    mapwidth:Int,
                mapheight:Int)
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        tilewidth = Float(DeviceWidth())/Float(mapwidth)
        tileheight = Float(DeviceHeight())/Float(mapheight)    
        map = New Int[mapwidth][]
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        map[mapwidth/2][mapheight/2] = 255
        For Local i=0 To (mapwidth*mapheight)/4
        placeblock
        Next
        mapblur
    End Method
    Method mapblur()
        For Local i=0 To mapwidth*mapheight
            Local x1:Int=Rnd(2,mapwidth-4)
            Local y1:Int=Rnd(2,mapheight-4)
            If map[x1][y1] > 0
                For Local y2=y1-1 To y1+1
                For Local x2=x1-1 To x1+1
                    If map[x2][y2] = 0
                        map[x2][y2] = map[x1][y1] / 2
                    End If
                Next
                Next
            End If
        Next
    End Method
    Method placeblock()
        Local x:Int=Rnd(0,mapwidth)
        Local y:Int=Rnd(0,mapheight)
        Local fitsx:Int=Rnd(3,12)
        Local fitsy:Int=Rnd(3,12)
        If Rnd(0,10) < 2 Then
            If Rnd(1,3) = 1
                fitsx = Rnd(6,22)                
                fitsy = 3
            Else
                fitsy = Rnd(6,22)
                fitsx = 3
            End If
        End If
        If mapfit(x,y,fitsx,fitsy) = True
            fitmap(x,y,fitsx,fitsy)
        End If
        
    End Method
    Method fitmap(x:Int,y:Int,w:Int,h:Int)
        For Local y2 = y Until y+h
        For Local x2 = x Until x+w
            map[x2][y2] = 255
        Next
        Next        
    End Method
    Method mapfit:Bool(x:Int,y:Int,w:Int,h:Int)
        If x+w > mapwidth-3 Then Return False
        If y+h > mapheight-3 Then Return False
        If x<3 Then Return False
        If y<3 Then Return False
        For Local y2 = y Until y+h
        For Local x2 = x Until x+w
            If map[x2][y2] > 0 Then Return False
        Next
        Next
        For Local y2 = y+1 Until y+h-1
            If map[x-1][y2] > 0 Then Return True
            If map[x+w][y2] > 0 Then Return True
        Next        
        For Local x2 = x+1 Until x+w-1
            If map[x2][y-1] > 0 Then Return True
            If map[x2][y+h] > 0 Then Return True
        Next
        End Method
    Method draw()
        For Local y:Float=0 Until mapheight Step 1
        For Local x:Float=0 Until mapwidth Step 1
            If map[x][y] > 0
            Local d:Int = distance(x,y,320,240)
            d = d / 2.5        
            SetColor map[x][y],d,0
            DrawRect    x*tilewidth,
                        y*tileheight,
                        tilewidth+1,
                        tileheight+1
            End If
        Next
        Next
    End Method
End Class

Global mymap:themap

Class MyGame Extends App
    Field refresh:Int
    Method OnCreate()
        SetUpdateRate(60)
        mymap = New themap(100,100)
    End Method
    Method OnUpdate()
        refresh+=1
        If refresh>120
            mymap = New themap(Rnd(100,200),Rnd(100,200))
            refresh=0
        Endif        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        mymap.draw
    End Method
End Class


Function Main()
    New MyGame()
End Function

Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
    Return Abs(x2-x1)+Abs(y2-y1)
End Function

MonkeyX - Heightmap/Texture/Image Generator - code example


Ported from a example by AdamStrange from a thread on the Blitzmax forum. 
Link


Import mojo

Class themap
    Field mapwidth:Int
    Field mapheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Field map:Float[][]
    Method New(    mapwidth:Int,
                mapheight:Int,
                numpoints:Int)
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        tilewidth = DeviceWidth()/Float(mapwidth)
        tileheight = DeviceHeight()/Float(mapheight)
        map = New Float[mapwidth][]
        For Local i = 0 Until mapwidth
            map[i] = New Float[mapheight]
        Next
        addheightmappoints(numpoints)
        For Local i=0 Until numpoints
        expandheightmap
        next
    End Method
    Method expandheightmap()
        For Local n=0 To 100000
            Local x1:Int=Rnd(1,mapwidth-1)
            Local y1:Int=Rnd(1,mapheight-1)
            If map[x1][y1] > 0
                For Local y2=y1-1 To y1+1
                For Local x2=x1-1 To x1+1
                    If map[x1][y1] > map[x2][y2]
                        map[x2][y2] = 
                                        (map[x1][y1]+
                                        map[x2][y2])*Rnd(0.49,0.5)
                    End If
                Next
                Next 
            End If
        Next
    End Method
    Method addheightmappoints(count:Int)
        For Local i=0 Until count
            Local x:Int=Rnd(    mapwidth/2-(mapwidth/3),
                                mapwidth/2+(mapwidth/3))
            Local y:Int=Rnd(    mapheight/2-(mapheight/3),
                                mapheight/2+(mapheight/3))
            map[x][y] = Rnd(64,200)
        Next
    End Method
    Method drawmap()
        For Local y:Float=0 Until mapheight Step 1
        For Local x:Float=0 Until mapwidth Step 1
            SetColor map[x][y],0,0
            DrawRect     x*tilewidth,
                        y*tileheight,
                        tilewidth+1,
                        tileheight+1
        Next
        Next
    End Method
    Method clearheightmap()
    End Method
End Class

Global mymap:themap 

Class MyGame Extends App
    Field nm:Int
    Method OnCreate()
        SetUpdateRate(60)
        Local date := GetDate()
        Seed = date[5]        
        mymap = New themap(    Rnd(50,150),
                            Rnd(50,150),
                            Rnd(2,20))
    End Method
    Method OnUpdate()        
        nm+=1
        If nm>100
            mymap = New themap(    Rnd(50,150),
                                Rnd(50,150),
                                Rnd(2,20))
            nm=0
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        mymap.drawmap
        SetColor 255,255,255
        DrawText     "MonkeyX - Heightmap/"+
                    "texture/image generator",
                    0,0
    End Method
End Class


Function Main()
    New MyGame()
End Function