sblendorio / petscii-bbs

A Java framework for building highly customizable PETSCII-enabled BBS, accessible from Commodore 64/128
Mozilla Public License 2.0
121 stars 16 forks source link

Here is a method to write a PETmate json file, maybe you can add it to the repo #28

Closed deadline-cxn closed 5 years ago

deadline-cxn commented 5 years ago

Below you will find the function for reading in a PETmate (https://nurpax.github.io/petmate/) json file and write it out. It takes into account arbitrary width and height, and can be written to x,y on the screen. Hope this helps some people, please add it in to the repo if you can.

I attached an example json file that was created from PETmate, which you can also include in the repo if you'd like. art.zip

Cheers! Deadline of CityXen ( https://github.com/cityxen )

import java.util.HashMap; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import java.io.FileReader;

/****/

private void PETmateJSON(String File) throws Exception { PETmateJSON(File,0,0); };
private void PETmateJSON(String File, Integer XLoc, Integer YLoc) throws Exception {

        // By Deadline of CityXen (cityxen.net)

    gotoXY(XLoc,YLoc);

    HashMap<Integer,Integer> codehash =new HashMap<Integer,Integer>();

    for(int i = 0; i < 32; i++)    { codehash.put(i,i+64);  }
    for(int i = 64; i < 96; i++)   { codehash.put(i,i+128); }
    for(int i = 96; i < 128; i++)  { codehash.put(i,i+64);  }
    for(int i = 128; i < 160; i++) { codehash.put(i,i-64);  }
    for(int i = 160; i < 192; i++) { codehash.put(i,i-128); }
    for(int i = 224; i < 256; i++) { codehash.put(i,i-64);  }

    HashMap<Integer,Integer> colorhash=new HashMap<Integer,Integer>();

    colorhash.put(0,Colors.BLACK);
    colorhash.put(1,Colors.WHITE);
    colorhash.put(2,Colors.RED);
    colorhash.put(3,Colors.CYAN);
    colorhash.put(4,Colors.PURPLE);
    colorhash.put(5,Colors.GREEN);
    colorhash.put(6,Colors.BLUE);
    colorhash.put(7,Colors.YELLOW);
    colorhash.put(8,Colors.ORANGE);
    colorhash.put(9,Colors.BROWN);
    colorhash.put(10,Colors.LIGHT_RED);
    colorhash.put(11,Colors.GREY1);
    colorhash.put(12,Colors.GREY2);
    colorhash.put(13,Colors.LIGHT_GREEN);
    colorhash.put(14,Colors.LIGHT_BLUE);
    colorhash.put(15,Colors.GREY3);

    Object jpars = new JSONParser().parse(new FileReader(File));
    JSONObject json = (JSONObject) jpars;
    JSONArray framebufs = (JSONArray) json.get("framebufs");
    JSONArray screencodes = (JSONArray) ((JSONObject) framebufs.get(0)).get("screencodes");
    JSONArray colors = (JSONArray) ((JSONObject) framebufs.get(0)).get("colors");
    String charset = (String) ((JSONObject)framebufs.get(0)).get("charset");
    Long width  = (Long) ((JSONObject)framebufs.get(0)).get("width");
    Long height = (Long) ((JSONObject)framebufs.get(0)).get("height");
    // System.out.println("\nFile: "+File+" (CHARSET: ["+charset+"] WIDTH: ["+width+"] HEIGHT: ["+height+"])");
    if(charset.equals((String)"upper")) { write(Keys.UPPERCASE,Keys.CASE_LOCK); }
    else { write(Keys.LOWERCASE,Keys.CASE_LOCK); }
    Integer outcolor;
    Integer lastcolor=null;
    Integer outcode;
    Integer compcode;
    int linecounter=0;
    int linecounter2=0;
    int heightcounter=0;
    int DrawSize=screencodes.size();
    for (int i = 0; i < DrawSize; i++) {
        if(width<40) {
            if(linecounter==width) {
                write(Keys.DOWN);
                for(int zz=0;zz<width;zz++) {
                    write(Keys.LEFT);
                }
                linecounter=0;
            }
        }
        outcolor = colorhash.get((int) (long) colors.get(i));
        if(lastcolor!=outcolor) {
            write(outcolor);
            lastcolor=outcolor;
        }
        outcode  = (int)(long)screencodes.get(i);
        if(outcode>127) write(Keys.REVON);
        else write(Keys.REVOFF);
        compcode = codehash.get(outcode);
        if(compcode!=null) {
            outcode=compcode;
        }
        write(outcode);
        linecounter++;
        linecounter2++;
        if(linecounter2 == width) {
            linecounter2=0;
            heightcounter++;
            if(heightcounter>height) {
                break;
            }
        }
    }
};
deadline-cxn commented 5 years ago

I just realized that the REVON and REVOFF can be optimized a little better to not write on every character, similar to how I did the lastcolor variable. So something like lastreverse variable condition should be added for the reverse status.

deadline-cxn commented 5 years ago

Here is an image example of it in action: LiveCapture You can also overlay smaller sized petscii files using the gotoXY() function like so: LiveCapture2

deadline-cxn commented 5 years ago

Okay so here is the updated method that has the reverseStatus optimization added so it doesn't write the reverse code every character. Only when the reverse status changes will it write the reverse status out.

private void PETmateJSON(final String File) throws Exception { PETmateJSON(File, 0, 0); };
private void PETmateJSON(final String File, final Integer XLoc, final Integer YLoc) throws Exception {

    gotoXY(XLoc, YLoc);

    final HashMap<Integer, Integer> codehash = new HashMap<Integer, Integer>();

    for (int i = 0; i < 32; i++)    { codehash.put(i, i + 64); }
    for (int i = 64; i < 96; i++)   { codehash.put(i, i + 128); }
    for (int i = 96; i < 128; i++)  { codehash.put(i, i + 64); }
    for (int i = 128; i < 160; i++) { codehash.put(i, i - 64); }
    for (int i = 160; i < 192; i++) { codehash.put(i, i - 128); }
    for (int i = 224; i < 256; i++) { codehash.put(i, i - 64); }

    final HashMap<Integer, Integer> colorhash = new HashMap<Integer, Integer>();

    colorhash.put(0, Colors.BLACK);
    colorhash.put(1, Colors.WHITE);
    colorhash.put(2, Colors.RED);
    colorhash.put(3, Colors.CYAN);
    colorhash.put(4, Colors.PURPLE);
    colorhash.put(5, Colors.GREEN);
    colorhash.put(6, Colors.BLUE);
    colorhash.put(7, Colors.YELLOW);
    colorhash.put(8, Colors.ORANGE);
    colorhash.put(9, Colors.BROWN);
    colorhash.put(10, Colors.LIGHT_RED);
    colorhash.put(11, Colors.GREY1);
    colorhash.put(12, Colors.GREY2);
    colorhash.put(13, Colors.LIGHT_GREEN);
    colorhash.put(14, Colors.LIGHT_BLUE);
    colorhash.put(15, Colors.GREY3);

    final Object jpars = new JSONParser().parse(new FileReader(File));
    final JSONObject json = (JSONObject) jpars;
    final JSONArray framebufs = (JSONArray) json.get("framebufs");
    final JSONArray screencodes = (JSONArray) ((JSONObject) framebufs.get(0)).get("screencodes");
    final JSONArray colors = (JSONArray) ((JSONObject) framebufs.get(0)).get("colors");
    final String charset = (String) ((JSONObject) framebufs.get(0)).get("charset");
    final Long width = (Long) ((JSONObject) framebufs.get(0)).get("width");
    final Long height = (Long) ((JSONObject) framebufs.get(0)).get("height");

    if (charset.equals((String) "upper")) {
        write(Keys.UPPERCASE, Keys.CASE_LOCK);
    } else {
        write(Keys.LOWERCASE, Keys.CASE_LOCK);
    }

    Integer outcolor;
    Integer lastcolor = null;
    Boolean reverseStatus = false;
    Integer outcode;
    Integer compcode;
    int linecounter = 0;
    int linecounter2 = 0;
    int heightcounter = 0;
    Integer DrawSize = screencodes.size();

    for (int i = 0; i < DrawSize; i++) {
        if(width<40) {
            if(linecounter == width) {
                write(Keys.DOWN);
                for(int zz=0;zz<width;zz++) {
                    write(Keys.LEFT);
                }
                linecounter=0;
            }
        }
        outcolor = colorhash.get((int) (long) colors.get(i));

        if(lastcolor!=outcolor) {
            write(outcolor);
            lastcolor=outcolor;
        }

        outcode  = (int)(long)screencodes.get(i);

        if(outcode>127) {
            if(reverseStatus == false) {
                write(Keys.REVON);
                reverseStatus = true;
            }
        }
        else {
            if(reverseStatus == true) {
                write(Keys.REVOFF);
                reverseStatus = false;
            }
        }

        compcode = codehash.get(outcode);
        if(compcode!=null) {
            outcode=compcode;
        }

        write(outcode);

        linecounter++;
        linecounter2++;
        if(linecounter2 == width) {
            linecounter2=0;
            heightcounter++;
            if(heightcounter>height) {
                break;
            }
        }
    }
};
sblendorio commented 5 years ago

https://github.com/sblendorio/petscii-bbs/blob/master/src/main/java/eu/sblendorio/bbs/tenants/PetsciiArtGallery.java

Just added it to the codebase. Thank you dude!