• 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

Save PNG files

RaspberryDJ

New member
Joined
Jun 3, 2019
Messages
122
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:
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:
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);
}
 
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.
 
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.
 
Dont worry, you will get a module if the code works like on Windows.
 
Back
Top Bottom