- Joined
- Jan 2, 2020
- Messages
- 1,414
I'm doing a water level and really need pixels for doing a few things.
I got everything working with Mojo1 but now I'm using Mojo2 so I built an example for you to show what I mean and I made it on purpose to be as close to Mojo1 as possible.
So when we use Mojo1 to draw to a image, we'll do this :
And it works perfectly in every way.
Let's try to do the same thing with Mojo2.
It will stay opaque or have strange alpha levels. Try for instance to change the alpha so the white rectangle becomes more or less transparent by setting alpha.
I got everything working with Mojo1 but now I'm using Mojo2 so I built an example for you to show what I mean and I made it on purpose to be as close to Mojo1 as possible.
So when we use Mojo1 to draw to a image, we'll do this :
And it works perfectly in every way.
Code:
Import mojo
Class MyApp Extends App
Field myImage:Image
Method OnCreate()
End
Method OnRender()
If Not myImage
' ------------ CREATE MYIMAGE ---------------
Local w=256,h=256
myImage=CreateImage(w,h,1,Image.MidHandle)
' ------ DRAW INSIDE MYIMAGE, NORMALLY -------
Cls 128,0,255
SetColor 255,128,0
DrawRect 16,16,w-32,h-32
SetColor 255,255,255
DrawText "Hello World",w/2,h/2,.5,.5
' - DRAW INSIDE MYIMAGE, INDIVIDUALLY PIXELS -
' The raw format is : rrggbb & aa, where 0=transparent, 255 = opaque)
Local buf:=New Int[w*2*h]
ReadPixels buf,0,0,w,h ,w,w*2 ' First read existing pixels in case we want to use it in next step
For Local i=0 Until h Step 4
For Local j=0 Until w*2
buf[i*w*2+j]=buf[i*w*2+j] + $ff00ff | $ff Shl 24 ' < Change ($rrggbb & $aa) here to change what to draw. We draw only on every forth row so we can see some of the original contents
Next
Next
myImage.WritePixels buf,0,0,w,h ,w,w*2
End
' -----------------------------------
Cls
SetColor 255,255,255
DrawText "Testing...",0,0
DrawImage myImage,MouseX/2.0,MouseY/2.0
End
End
Function Main()
New MyApp
End
Let's try to do the same thing with Mojo2.
It will stay opaque or have strange alpha levels. Try for instance to change the alpha so the white rectangle becomes more or less transparent by setting alpha.
Cerberus:
Strict
Import mojo2
Import brl.databuffer
Import cerberus.maths
Class MyApp Extends App
Field myCanvas:Canvas
Field myImage:Image
Field myData:DataBuffer
Field imageCanvas:Canvas
Field patternSeed:Float = 1
Method OnCreate:Int()
' ------------ CREATE MYIMAGE ---------------
myCanvas = New Canvas()
myImage = New Image(256, 256)
imageCanvas = New Canvas(myImage)
myData = New DataBuffer(16 * 16 * 4)
Return 0
End
Method OnRender:Int()
' ------ DRAW INSIDE MYIMAGE, NORMALLY -------
patternSeed += 0.1
For Local x:Int = 0 Until 128
For Local y:Int = 0 Until 128
If (x & y & Int(patternSeed))
imageCanvas.SetColor(Sin(Millisecs() * 0.1 ) * 0.5 + 0.5, Cos(Millisecs() * 0.1) * 0.5 + 0.5, 0.5)
Else
imageCanvas.SetColor(0, 0, 0)
Endif
imageCanvas.DrawRect(x * 2, y * 2, 2, 2)
Next
Next
' - DRAW INSIDE MYIMAGE, INDIVIDUALLY PIXELS -
' Paint the upper left 16 x 16 so we can see some of the original contents
For Local i:Int = 0 Until 16 * 16 * 4 Step 4
Local r:Int = 255 ' Change red here (we use 0-255 in raw format, not 0.0-1.0)
Local g:Int = 255 ' Change green here
Local b:Int = 255 ' Change blue here
Local a:Int = 255 ' Change alpha here (0 = transparent, 255 = opaque, this does not work yet in Mojo2)
Local rgba:Int = a Shl 24 | b Shl 16 | g Shl 8 | r
myData.PokeInt(i, rgba)
Next
imageCanvas.Flush()
myImage.WritePixels(0, 0, 16, 16, myData)
' -----------------------------------
' now render to main canvas
myCanvas.Clear()
myCanvas.DrawImage(myImage, MouseX(), MouseY())
myCanvas.Flush()
Return 0
End
End
Function Main:Int()
New MyApp()
Return 0
End