WritePixels help

Dubbsta

Active Member
Joined
Jul 13, 2017
im not sure what the use for createimage , writepixels and readpixels are for or how to use them but this didnt work for me...cant make much sense of the docs and dont know how to set the argb can someone please enlighten me thx.

is this how to change pixels on an image?

Cerberus X:
Strict
Import mojo





Class test Extends App

 Field pad:Image
 Field pix:Int[40*40]
 Field img:Image
 
    Method OnCreate:Int()
        SetUpdateRate(60)
         pad = LoadImage("pad.png")
         img = CreateImage(40,40)
        Return 0
    End
    
    
    
    
    Method OnUpdate:Int()
        ReadPixels(pix,40,40,40,40)
    
        Return 0
    End
    
    
    
    
    Method OnRender:Int()
        Cls 0,0,0
        
        DrawImage(pad,40,40)
        
        
        img.GrabImage(100,100,40,40)
        Return 0
    End
    
    
    
End





Function Main:Int()
    
    New test()

    Return 0
End
 

adamredwoods

Member
3rd Party Tool Dev
Joined
Jun 23, 2017
I forget if ReadPixels() needs to be in OnRender or not.
Once you have your data in pix, you can manipulate the 32-bit (which is the size of int) pixel. I usually use bit manipulation to do that.

Then img.WritePixels(), and DrawImage(img).

 

Dubbsta

Active Member
Joined
Jul 13, 2017
what am i missing? it seems like it should work but doesn't. i get invalid pixel rectangle

dont know if im using it right but just to test it i have a log image , copied a 40 * 40 piece of it and tried to draw it on a blank image i created
is this not how it works ?

Cerberus X:
Strict

Import mojo


Class gamename Extends App
 Field log:Image
 Field x:Float =50
 Field y:Float = 50
 Field pix:Int[40*40]
 Field img:Image
 
        Method OnCreate:int()
            SetUpdateRate(60)
            
            log = LoadImage("log1.png")
            img = CreateImage(40,40)
            
            Return 0
        End
        
        
        Method OnUpdate:int()
        
            Return 0
        End
        
        
        Method OnRender:int()
            Cls 0,0,0
            
            DrawImage(log,x,y)
            
            ReadPixels(pix,x,y,40,40)
            
            img.WritePixels(pix,100,100,40,40)
            
            
            
            Return 0
        End
End


Function Main:int()

    New gamename()
    
    Return 0
    
End
 

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
You started your WritePixel method at the wrong x and y coordinate.

Exchange your Render method with this:
Cerberus X:
        Method OnRender:int()
            Cls 0,0,250
            
            DrawImage(log,0,0)
            
            ReadPixels(pix,MouseX(),MouseY(),40,40)
            
            img.WritePixels(pix,0,0,40,40)
            DrawImage(img,0,0)
            
            
            Return 0
        End
 

Dubbsta

Active Member
Joined
Jul 13, 2017
thx mike. just so i understand writepixels x,y must stay 0,0?
if so would the method have been better written (writepixels(w:int,h:int,x:float = 0 ,y:float = 0 )
so to just deal with width and height? not asking to change just curious thx

also meant to put this in beginner if it makes sense to
 

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
Sometimes you want to update only a smaller portion of an image, then you need an offset. With your example, you don't need it.
 
Top Bottom