From: Robert Lowe Date: Sun, 28 Sep 2014 01:59:41 +0000 (-0400) Subject: Beginning refactoring for better bitmap loading X-Git-Url: http://4ch.mooo.com/gitweb/?a=commitdiff_plain;h=dfaa5ddeb4ea4e1edaf1dbaa6c2a3773ea8a918d;p=16.git Beginning refactoring for better bitmap loading --- diff --git a/16/modex16/BITMAP.C b/16/modex16/BITMAP.C new file mode 100644 index 00000000..108d8c30 --- /dev/null +++ b/16/modex16/BITMAP.C @@ -0,0 +1,130 @@ +#include +#include +#include "bitmap.h" +#include "modex16.h" + +static struct pcxHeader { + byte id; + byte version; + byte encoding; + byte bpp; + word xmin; + word ymin; + word xmax; + word ymax; + word hres; + word vres; + byte pal16[48]; + byte res1; + word bpplane; + word palType; + word hScreenSize; + word vScreenSize; + byte padding[54]; +}; + + +static void loadPcxStage1(FILE *file, bitmap_t *result) { + struct pcxHeader head; + long bufSize; + int index; + byte count, val; + long int pos; + + /* read the header */ + fread(&head, sizeof(char), sizeof(struct pcxHeader), file); + + /* make sure this is 8bpp */ + if(head.bpp != 8) { + printf("I only know how to handle 8bpp pcx files!\n"); + fclose(file); + exit(-2); + } + + /* allocate the buffer */ + result->width = head.xmax - head.xmin + 1; + result->height = head.ymax - head.ymin + 1; + bufSize = result->width * result->height; + result->data = malloc(bufSize); + if(!result->data) { + printf("Could not allocate memory for bitmap data."); + fclose(file); + exit(-1); + } + + /* save the position of the pixel data */ + pos = ftell(file); + + /* handle the palette */ + fseek(file, -769, SEEK_END); + val = fgetc(file); + result->palette = modexNewPal(); + if(head.version == 5 && val == 12) { + /* use the vga palette */ + for(index=0; !feof(file) && index < PAL_SIZE; index++) { + val = fgetc(file); + result->palette[index] = val >> 2; + } + } else { + /* use the 16 color palette */ + for(index=0; index<48; index++) { + result->palette[index] = head.pal16[index]; + } + } + + /* go back to the pixel data */ + fseek(file, -pos, SEEK_END); +} + + +bitmap_t +bitmapLoadPcx(char *filename) { + FILE *file; + bitmap_t result; + struct pcxHeader head; + long bufSize; + int index; + byte count, val; + + /* open the PCX file for reading */ + file = fopen(filename, "rb"); + if(!file) { + printf("Could not open %s for reading.\n", filename); + exit(-2); + } + + /* load the first part of the pcx file */ + loadPcxStage1(file, &result); + + /* read the buffer in */ + index = 0; + do { + /* get the run length and the value */ + count = fgetc(file); + if(0xC0 == (count & 0xC0)) { /* this is the run count */ + count &= 0x3f; + val = fgetc(file); + } else { + val = count; + count = 1; + } + + /* write the pixel the specified number of times */ + for(; count && index < bufSize; count--,index++) { + result.data[index] = val; + } + } while(index < bufSize); + + + fclose(file); + + return result; +} + + +tileset_t +bitmapLoadPcxTiles(char *filename, word twidth, word theight) { + tileset_t ts; + + return ts; +} diff --git a/16/modex16/BITMAP.H b/16/modex16/BITMAP.H new file mode 100644 index 00000000..4c4af98e --- /dev/null +++ b/16/modex16/BITMAP.H @@ -0,0 +1,24 @@ +/* + * Functions and types for loading and manipulating bitmaps. + */ +#ifndef BITMAP_H +#define BITMAP_H +#include "types.h" +typedef struct { + byte *data; + word width; + word height; + byte *palette; +} bitmap_t; + +typedef struct { + byte **data; + word ntiles; /* the number of tiles */ + word twidth; /* width of the tiles */ + word theight; /* height of the tiles */ + byte *palette; /* palette for the tile set */ +} tileset_t; + +bitmap_t bitmapLoadPcx(char *filename); +tileset_t bitmapLoadPcxTiles(char *filename, word twidth, word theight); +#endif diff --git a/16/modex16/makefile b/16/modex16/makefile index bdf9d948..b11f89a0 100644 --- a/16/modex16/makefile +++ b/16/modex16/makefile @@ -1,18 +1,18 @@ FLAGS=-0 all: test.exe pcxtest.exe test2.exe scroll.exe -scroll.exe: scroll.obj modex16.obj dos_kb.obj - wcl $(FLAGS) scroll.obj modex16.obj dos_kb.obj +scroll.exe: scroll.obj modex16.obj dos_kb.obj bitmap.obj + wcl $(FLAGS) scroll.obj modex16.obj dos_kb.obj bitmap.obj scroll.obj: scroll.c wcl $(FLAGS) -c scroll.c -test.exe: test.obj modex16.obj - wcl $(FLAGS) test.obj modex16.obj +test.exe: test.obj modex16.obj bitmap.obj + wcl $(FLAGS) test.obj modex16.obj bitmap.obj -test2.exe: test2.obj modex16.obj - wcl $(FLAGS) test2.obj modex16.obj +test2.exe: test2.obj modex16.obj bitmap.obj + wcl $(FLAGS) test2.obj modex16.obj bitmap.obj -pcxtest.exe: pcxtest.obj modex16.obj - wcl $(FLAGS) pcxtest.obj modex16.obj +pcxtest.exe: pcxtest.obj modex16.obj bitmap.obj + wcl $(FLAGS) pcxtest.obj modex16.obj bitmap.obj test.obj: test.c modex16.h wcl $(FLAGS) -c test.c @@ -25,10 +25,13 @@ pcxtest.obj: pcxtest.c modex16.h modex16.obj: modex16.h modex16.c wcl $(FLAGS) -c modex16.c - + dos_kb.obj: dos_kb.h dos_kb.c - wcl $(FLAGS) -c dos_kb.c + wcl $(FLAGS) -c dos_kb.c +bitmap.obj: bitmap.h bitmap.c + wcl $(FLAGS) -c bitmap.c + clean: del *.obj del *.exe diff --git a/16/modex16/modex16.h b/16/modex16/modex16.h index 08e7b360..f45356b5 100644 --- a/16/modex16/modex16.h +++ b/16/modex16/modex16.h @@ -5,17 +5,12 @@ #define MODEX16_H #include #include "types.h" +#include "bitmap.h" /* -========================== Types & Macros ==========================- */ #define PAGE_OFFSET(x,y) (((y)<<6)+((y)<<4)+((x)>>2)) #define PLANE(x) (1<< (x&3)) #define SELECT_ALL_PLANES() outpw(0x03c4, 0xff02) -typedef struct { - byte *data; - word width; - word height; - byte *palette; -} bitmap_t; typedef struct { byte far* data; /* the data for the page */ diff --git a/16/modex16/pcxtest.c b/16/modex16/pcxtest.c index d9099381..51b8427b 100644 --- a/16/modex16/pcxtest.c +++ b/16/modex16/pcxtest.c @@ -1,5 +1,6 @@ #include #include "modex16.h" +#include "bitmap.h" word far* clock= (word far*) 0x046C; /* 18.2hz clock */ diff --git a/16/modex16/pcxtest.exe b/16/modex16/pcxtest.exe index cf5180df..a57b981c 100644 Binary files a/16/modex16/pcxtest.exe and b/16/modex16/pcxtest.exe differ diff --git a/16/modex16/scroll.exe b/16/modex16/scroll.exe index 83854efe..a259aeed 100755 Binary files a/16/modex16/scroll.exe and b/16/modex16/scroll.exe differ diff --git a/16/modex16/test.exe b/16/modex16/test.exe index e32a4751..507cd456 100644 Binary files a/16/modex16/test.exe and b/16/modex16/test.exe differ diff --git a/16/modex16/test2.exe b/16/modex16/test2.exe index 6476ebc1..0aa470b9 100644 Binary files a/16/modex16/test2.exe and b/16/modex16/test2.exe differ