• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Showcase Writepixel Mojo2 Demo

Paul59

Active member
CX Code Contributor
Joined
Dec 13, 2018
Messages
384
voronoi.gif


Shows how to write pixels using a databuffer with mojo 2 (a bit more interesting than the official example I re-wrote). Modify the number of control points or the control point radius, angle step or colour values and the image transparency, to get different effects.

The program creates a simple Voronoi diagram, where each point is coloured according to the nearest 'control point'. The control points are rotated to give interesting visual effects - they could be transformed using trig functions or bitwise operations to give a broader range of effects.



Cerberus:
Strict
Import mojo2
Import brl.databuffer

Const XSIZE:Int = 320
Const YSIZE:Int = 240
Const INTSIZE:Int = 4
Const NUMPOINTS:Int = 8

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

Global pixels:DataBuffer = New DataBuffer(INTSIZE * XSIZE * YSIZE)

Class ControlPoint

    Field x:Float
    Field y:Float
    Field cx:Float
    Field cy:Float
    Field abgr:Int
    Field angle:Float
    Field angleStep:Float
    Field radius:Float
        
    Method New(xx:Float , yy:Float, r:Int, g:Int, b:Int)
        cx = xx
        cy = yy
        abgr = r + (g Shl 8) + (b Shl 16) + (255 Shl 24)
        angle = Rnd(360)
        radius = Rnd(40, 80)
        x = cx + radius * Cos(angle)
        y = cy + radius * Sin(angle)
        angleStep = Rnd(0.5, 1)
    End
    
    Method Update:Void()   
        x = cx + radius * Cos(angle)
        y = cy + radius * Sin(angle)
        angle += angleStep   
        
    End

End

Class MyGame Extends App

    Field myImage:Image
    Field myCanvas:Canvas
     Field cp:ControlPoint[NUMPOINTS]

    
    Method OnCreate:Int()
        Local date := GetDate()
        Seed = date[3] * 3600000 + date[4] * 60000 + date[5] * 1000 + date[6] 
          
        SetUpdateRate(60)
        myCanvas = New Canvas()
  
        ' Create a 320 x 200 image with top left handle
        myImage =  New Image(XSIZE, YSIZE, 0.5, 0.5)
        
        ' add control points
        For Local i:Int = 0 Until NUMPOINTS
            cp[i] = New ControlPoint(Rnd(XSIZE), Rnd(YSIZE), Int(Rnd(256)), Int(Rnd(256)), int(Rnd(256)))   
        Next
        ' initial fill       
        UpdatePixels()
        Return 0
    End
 
     Method UpdatePixels:Void()
    
        Local pos:Int = 0
        For Local y:Int = 0 Until YSIZE
               For Local x:Int = 0 Until XSIZE
                Local dist:Float = 123456789
                Local index:Int
                'get nearest cp to this pixel
                For Local i:Int = 0 Until NUMPOINTS
                    Local a:Float = x - cp[i].x
                    Local b:Float  = y - cp[i].y
                    Local hyp:Float = (a * a) + (b * b)'Sqrt((a * a) + (b * b))
                    If hyp <= dist Then
                        dist = hyp
                        index = i
                    Endif
                Next
                ' set this pixel to nearest control point's colour
                pixels.PokeInt(pos, cp[index].abgr)
                pos += INTSIZE
            Next
        Next
                
        ' write databuffer to image
        myImage.WritePixels(0, 0, XSIZE, YSIZE, pixels)     
    
         ' update control points
         For Local i: Int = 0 Until NUMPOINTS
             cp[i].Update()
         Next
        

     End
 
    Method OnRender:Int()   
        ' draw a scaled version of the image, no canvas clear, high transparency
        myCanvas.SetColor(1, 1, 1, 0.025)
        myCanvas.DrawImage(myImage, DeviceWidth()/2, DeviceHeight()/2, 0, 2, 2)
        myCanvas.Flush()
        Return 0
    End
    
    Method OnUpdate:Int()
        UpdatePixels()
        Return 0
    End
End
 
Back
Top Bottom