Qbasicnews.com

Full Version: PUT and BSV loading using Allegro and C++
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've been trying to load PUT files and BSV files using Allegro and C++. I got the BSV loading function to work, but the put function I can't figure out. Any help is appreciated.

BSV loading:
Code:
BITMAP *load_bsv(char *filename, RGB *palette) {
    PACKFILE *f;
    BITMAP *bmp = NULL;
    char temp[6];
    int w, h, i;

    if (!(f = pack_fopen(filename, F_READ))) return NULL;
    
    // check if it's a bsaved file
    if (pack_getc(f) != 0xfd) goto error;

    // skip source seg:off and length
    pack_fread(temp, 6, f);
    if (pack_feof(f)) goto error;

    // get sprite dimensions
    w = pack_igetw(f) / 8;
    h = pack_igetw(f);

    bmp = create_bitmap_ex(8, w, h);
    if (!bmp) goto error;
    clear_to_color(bmp, makecol16(255, 0, 255));

    // load the sprite data
    for (i = 0; i < h; i++) pack_fread(bmp->line[i], w, f);

    
    // check if there's an appended palette
    if ((!pack_feof(f)) && (palette)) {
        load appended data as palette data
        for (i = 0; i < 256; i++) {
            pack_fread(palette[i], 3, f);
            if (pack_feof(f)) break;
        }
    }
    
    error:
        pack_fclose(f);
        return bmp;
}

PUT loading function:
Code:
BITMAP *load_put(char *filename, RGB *palette) {
    clear(screen);

    FILE *put_file = fopen(filename, "r");
    if (!put_file) {
        textprintf(screen, font, 0, 0, gfx.WHITE, "Error opening PP256 file '%s' for input.\n", filename);
        return NULL;
    }

    // read the first pp256 image to find dimensions of each image
    short w, h;
    fseek(put_file, 7, SEEK_SET);
    fread(&w, 2, 1, put_file);
    w >>= 3;
    fread(&h, 2, 1, put_file);
    fseek(put_file, 0, SEEK_END);
    int numimgs = (ftell(put_file) - 7) / (w * h);
        
    textprintf(screen, font, 0, 0, gfx.WHITE, "width: %d height: %d images: %d", w, h, numimgs);
    BITMAP *bmp = create_bitmap(w, h * numimgs);
    fseek(put_file, 7, SEEK_SET);
    int imgsize = w * h;
    //char data[imgsize];
    for (int i = 0; i < numimgs; i++) {
        fseek(put_file, 4, SEEK_CUR);
        fread(bmp->, 1, imgsize, put_file);
        //fwrite(data, 1, imgsize, bmp_file);
    }
    fclose(put_file);
    return bmp;
}
Finally! I figured it out. Here is a PUT file loader for allegro games. If anyone finds it useful, feel free to use it.

Code:
BITMAP *load_put(char *filename, RGB *palette) {
    
    FILE *put_file = fopen(filename, "r");
    if (!put_file) { textprintf(screen, font, 0, 0, gfx.WHITE, "Error opening PP256 file '%s' for input.\n", filename); return NULL; }
    short w1, h1;
    fseek(put_file, 7, SEEK_SET); fread(&w1, 2, 1, put_file); w1 >>= 3;
    fread(&h1, 2, 1, put_file); fseek(put_file, 0, SEEK_END);
    int numimgs = (ftell(put_file) - 7) / (w1 * h1);
    fclose(put_file);

    PACKFILE *f;
    BITMAP *bmp = NULL;
    char temp[6];
    if (!(f = pack_fopen(filename, F_READ))) return NULL;
    // check if it is a bsaved img
    if (pack_getc(f) != 0xfd) { pack_fclose(f); return NULL; }
    // skip source seg:off and length
    pack_fread(temp, 6, f);
    if (pack_feof(f)) { pack_fclose(f); return NULL; }
    
    // get sprite dimensions
    int w = pack_igetw(f) / 8; int h = pack_igetw(f);
    
    if (!(bmp = create_bitmap_ex(8, w, numimgs * h))) { pack_fclose(f); return NULL; }
    clear_to_color(bmp, makecol8(255, 0, 255));
    
    int c = 0, rx = 0, ry = 0, rc = 0, imgs = 0;
    while (!pack_feof(f)) {
        pack_fread(&c, 1, f);
        if (c != 0) putpixel(bmp, rx, ry, c);
        rx++;
        if (rx == w) {
            rx = 0; ry++; rc++;
            if (rc == h) {
                rc = 0; imgs++;
                pack_fread(&c, 4, f);
            }
        }
    }
    
    pack_fclose(f);
    return bmp;
}
Cool! If you want to be _extra specially_ cool, though Wink, you can write a little bit of code to register the file type with Allegro and make it automagically work with load_bitmap(). I forget what the function's called (maybe register_ ... something??), but it's in the docs.
Yeah, that would be cool, but I don't want to mess with PUT files or BSV ever again.

Not many people would want to continue using .PUT and .BSV files. I would think most of them would want to convert them to BMPs. It would be fun to try to incorporate something like that though, so I'll see what I can do.

here is a program I made to convert .PUT and .BMP files
http://www.rpg-dev.net/typosoft/put2bmp.zip