Save PNG files

RaspberryDJ

Member
Joined
Jun 3, 2019
I found some old Monkey-X code from somewhere that I never got time to try.
It would be great to incorporate it to Cerberus-X to be able to save png graphics, what do you say?


Example
Cerberus X:
Strict

' Simple mojo2 script.

' Import the mojo framework
Import mojo2
Import png



'*******************************************************************   
' Define the myClass that extends the App Class.   
Class myClass Extends App
    Field canvas:Canvas
    '---------------------------------------------------------------
     ' The OnCreate Method inside the App class is always called at the start of your app.
     ' It is a perfect place to load resources into your app.
    Method OnCreate:Int()
        ' By setting the update rate to 0, there will be always an OnRender call after OnUpdate. No matter how long it takes.
        ' It will also run as fast as your sync rate of your hardware runs.
        SetUpdateRate(60)               
        canvas = New Canvas
        Return 0
    End
    
    '---------------------------------------------------------------
     ' The OnUpdate Method is called at the beginning of a new frame.
     ' The interval it is called depends on how you have set the update rate, which by default is set to 60 times a second.
     ' The mojo framework tries to make sure that it will be able to call OnUpdate this many times in a second. And if needed, skip
     ' calls to OnRender to reach the goal.
    Method OnUpdate:Int()
        If KeyHit(KEY_P)
            SavePNG("screen.png",canvas)
        Endif
        Return 0
    End
    
    '---------------------------------------------------------------
    ' The OnRender method will be called after the OnUpdate method.
    ' It is the place to draw your images or other visual elements. ~n
    Method OnRender:Int()
        ' Clear the canvas with a black color.
        canvas.Clear (0,0.5,0)   
        ' Now draw the multiple text lines. They need to be split into a strng array.
        canvas.DrawText( String("Cerberus X~nHello World!").Split("~n")),DeviceWidth()/2,DeviceHeight()/2,0.5,0.5
        ' Flush the canvas so it becomes visible.       
        canvas.Flush

        Return 0
    End
End

'---------------------------------------------------------------
' The Main function is the starting point of every Cerberus X app.
Function Main:Int()
    ' Create an instance of your class that you have defined above.
    New myClass       
    Return 0
End





Code


Cerberus X:
Import brl.databuffer
Import mojo2.graphics

Import "png.cpp"

Extern
Function _savepng:Void(file:String, w:Int, h:Int, db:DataBuffer)

Public

Function SavePNG:Void(file:String,cnvs:Canvas)
    'Print("width="+cnvs.Width()+"   height="+cnvs.Height())
    Local db:DataBuffer = New DataBuffer(cnvs.Width()*cnvs.Height()*4)
    cnvs.ReadPixels(0,0,cnvs.Width(),cnvs.Height(),db)
    'Print("length db="+db.Length())

    _savepng(file,cnvs.Width(),cnvs.Height(),db)
End


C++:
static String::CString<char> C_STR( const String &t ){ return t.ToCString<char>(); };

void _savepng(String filename, int w, int h, BBDataBuffer *db) {
    std::vector<unsigned char> colorBuffer(w * h * 4, 255);
    int i;
    int i2;
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            i = x + y*w;
            i2 = x + ((h-1)*w)-y*w;
            colorBuffer[i*4]   = db->PeekByte(i2*4); //Red
            colorBuffer[i*4+1] = db->PeekByte(i2*4+1); //Green
            colorBuffer[i*4+2] = db->PeekByte(i2*4+2); //Blue
            colorBuffer[i*4+3] = db->PeekByte(i2*4+3); // alpha
        }
    }
 
    stbi_write_png(C_STR(filename), w, h, 4, &colorBuffer[0], 0);
}
 

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
As you are not able to create these files yourself, I will post a small user module in the modules section when i find time. I won't add it to mojo2 now itself as i consider mojo1 and 2 feature complete.
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
As you are not able to create these files yourself, I will post a small user module in the modules section when i find time. I won't add it to mojo2 now itself as i consider mojo1 and 2 feature complete.
The ability to save a screenshot is a feature of many games (and you did say you were integrating it further up the thread). Not sure what you mean about 'not able to create these files yourself' - I wouldn't know what to do with the C++ code and don't want to create some sort of custom version of CX.
 
Top Bottom