• 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

Snippet Define sprite version 2

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,414
This is another way to define sprites inside the code, very old fashioned but I'm quiet fond of it.
I'm not promoting using pixels like this in realtime situations but it's good example to understand how they are used.

Actually you can get quiet a nice realtime action using this. HTML5, iOS and Desktop Linux/Windows/macOS allows okayish but keep your action down to around a maximum of 640x400 pixels per frame, if you want 60 fps.

For Android... you need to tweak to get the same speed as the other platforms. if you use Mojo1 then you can get speed increase from 2 fps to 60fps by changing line 118 in mojo.android.java from Bind() to Bind2(). I'm sure there's a similar tweak for Mojo2 but I have not found that tweak yet myself as I focus on other things.

But for just defining graphics in code and as long as you keep it non-realtime.. it's really nice with or without tweaks.

Code:
Strict
Import mojo2
Import brl.databuffer

Function Main:Int()
    New Game
    Return 0
End
        
Class Game Extends App
    
    Field canvas:Canvas, pixels:DataBuffer = New DataBuffer(16 * 16 * 4), sprite:Image[8]
    Global spritedata:Int[][] = [[0,0,0,2,0,2,0,2,0,2,0,2,0,2,0,0],
                                 [0,0,0,0,2,0,2,0,2,0,2,0,2,0,0,0],
                                 [0,0,2,0,0,0,0,0,0,0,0,0,0,0,2,0],
                                 [0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0],
                                 [0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0],
                                 [0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0],
                                 [0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0],
                                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                 [0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0],
                                 [0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0],
                                 [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0],
                                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

    Method OnCreate:Int()
        canvas=New Canvas
        sprite[0] = New Image(16,16,0,0,0)
        canvas.SetRenderTarget sprite[0]
        canvas.Clear
        canvas.Flush   
        sprite[0] = New Image(64,64,0,0,0)
        canvas.SetRenderTarget sprite[0]

        For Local i:Int = 0 Until 16 * 16
            Local x:Int = i Mod 16, y:Int = i / 16
            If spritedata[y][x] = 0
                pixels.PokeInt x*4+y*16*4, $00000000
            Elseif spritedata[y][x] = 1
                pixels.PokeInt x*4+y*16*4, $0aff0000
            Elseif spritedata[y][x] = 2
                pixels.PokeInt x*4+y*16*4, $0000ff00
            End If
        Next
        sprite[0].WritePixels 0,0,16,16,pixels
        canvas.Flush     
        canvas.SetRenderTarget Null
        Return 0
    End
    
    Method OnRender:Int()
        canvas.Clear   
        canvas.DrawImage sprite[0], 100,100   
        canvas.Flush
       Return 0
    End

    Method copysprite:Void(src:Image, dst:Image)
        canvas.SetRenderTarget src
        Local w:Int = 64, h:Int = 64
        For Local ii:Int = 0 Until h
            For Local i:Int = 0 Until w
                canvas.ReadPixels i,ii,0,0,pixels    ' Read pixel
                pixels.PokeInt 0,pixels.PeekInt(0) & $ffffffff
                dst.WritePixels i,ii,0,0,pixels        ' Write pixel
                Local rgb:Int = pixels.PeekInt(0)
            Next
        Next
        canvas.SetRenderTarget Null
    End

    Method plotpixel:Void(img:Image,x:Int,y:Int,c:Int,a:Float)
        canvas.SetRenderTarget img
        canvas.ReadPixels x,y,0,0,pixels
        Local g:Int=pixels.PeekInt(0) & $ffffffff       
        Local r0:Int = ((g & $00ff0000) Shr 16)
        Local g0:Int = ((g & $0000ff00) Shr 8)
        Local b0:Int = ((g & $000000ff))
        Local r1:Int = ((c & $00ff0000) Shr 16)
        Local g1:Int = ((c & $0000ff00) Shr 8)
        Local b1:Int = ((c & $000000ff))
        Local ac:Float = 255
        Local rc:Float = r1*a+r0*(1.0-a)
        Local gc:Float = g1*a+g0*(1.0-a)
        Local bc:Float = b1*a+b0*(1.0-a)
        Local n:Int = (ac Shl 24) + (rc Shl 16) + (gc Shl 8) + bc
        pixels.PokeInt 0,n
        img.WritePixels x,y,0,0,pixels
        canvas.SetRenderTarget Null
    End

End
 
  • Like
Reactions: mag
Back
Top Bottom