[CHG] GRRLIB_LoadTextureBMF is still in progress (problem with palette and

some cleaning is needed)
This commit is contained in:
Crayon2000 2009-02-20 06:48:07 +00:00
parent a1edf64aac
commit 4d9046eade
3 changed files with 131 additions and 65 deletions

View file

@ -241,55 +241,73 @@ GRRLIB_texImg GRRLIB_LoadTextureJPG(const unsigned char my_jpg[]) {
return my_texture; return my_texture;
} }
/**
* Print formatted output.
* @param xpos
* @param ypos
* @param tex
* @param color
* @param zoom
* @param text
* @param ... Optional arguments.
*/
void GRRLIB_PrintBMF(f32 xpos, f32 ypos, GRRLIB_bytemapFont bmf, f32 zoom, const char *text, ...) {
unsigned int i, j, x, y, n, size;
char tmp[1024];
va_list argp;
va_start(argp, text);
size = vsprintf(tmp, text, argp);
va_end(argp);
GRRLIB_texImg tex_BMfont = GRRLIB_CreateEmptyTexture(800, 600);
for(i=0; i<size; i++) {
for(j=0; j<bmf.nbChar; j++) {
if(tmp[i] == bmf.charDef[j].character) {
n=0;
for(y=0; y<bmf.charDef[j].height; y++) {
for(x=0; x<bmf.charDef[j].width; x++) {
if(bmf.charDef[j].data[n]) {
GRRLIB_SetPixelTotexImg(xpos + x, ypos + y, tex_BMfont,
bmf.palette[bmf.charDef[j].data[n]]);
}
n++;
}
}
xpos += bmf.charDef[j].width + 1;
break;
}
}
}
GRRLIB_FlushTex(tex_BMfont);
GRRLIB_DrawImg(0, 0, tex_BMfont, 0, 1, 1, 0xFFFFFFFF);
free(tex_BMfont.data);
}
/** /**
* Load a texture from a buffer. * Load a texture from a buffer.
* @param my_bmf the ByteMap font buffer to load. * @param my_bmf the ByteMap font buffer to load.
* @return A GRRLIB_texImg structure filled with BMF informations. * @return A GRRLIB_bytemapFont structure filled with BMF informations.
*/ */
GRRLIB_texImg GRRLIB_LoadTextureBMF(const unsigned char my_bmf[]) { GRRLIB_bytemapFont GRRLIB_LoadTextureBMF(const unsigned char my_bmf[]) {
/* GRRLIB_bytemapFont fontArray;
if s=BMFHEADER then begin fontArray.nbChar = 0;
blockread(f,i,1); fontArray.charDef = NULL;
blockread(f,lineheight,1);
blockread(f,sizeover,1);
blockread(f,sizeunder,1);
blockread(f,addspace,1);
blockread(f,sizeinner,1);
blockread(f,usedcolors,1);
blockread(f,highestcolor,1);
blockread(f,s[1],5);
for i:=1 to ord(s[5]) do begin
blockread(f,rgb[i],3);
rgb[i].r:=rgb[i].r shl 2+3;
rgb[i].g:=rgb[i].g shl 2+3;
rgb[i].b:=rgb[i].b shl 2+3;
end;
blockread(f,i,1); i:=byte(i); s:='';
while i>0 do begin
blockread(f,c,1);
s:=s+c;
dec(i);
end;
blockread(f,i,2);
for i:=pred(i) downto 0 do begin
blockread(f,c,1);
blockread(f,tablo[c],5);
with tablo[c] do
if w or h<>0 then begin
getmem(d,w*h);
blockread(f,d^,w*h);
end;
end;
end;
*/
GRRLIB_texImg my_texture;
int i, j = 0; int i, j = 1;
u8 lineheight; u8 lineheight;
u8 usedcolors, highestcolor; u8 usedcolors, highestcolor;
short int sizeover, sizeunder, addspace, sizeinner, numcolpal, numchars; short int sizeover, sizeunder, addspace, sizeinner, numcolpal;
u32 palette[64]; u16 nbPixels;
if(my_bmf[0]==0xE1 && my_bmf[1]==0xE6 && my_bmf[2]==0xD5 && my_bmf[3]==0x1A) {
lineheight = my_bmf[5]; lineheight = my_bmf[5];
sizeover = my_bmf[6]; sizeover = my_bmf[6];
sizeunder = my_bmf[7]; sizeunder = my_bmf[7];
@ -298,18 +316,43 @@ GRRLIB_texImg my_texture;
usedcolors = my_bmf[10]; usedcolors = my_bmf[10];
highestcolor = my_bmf[11]; highestcolor = my_bmf[11];
numcolpal = 3 * my_bmf[16]; numcolpal = 3 * my_bmf[16];
memset(fontArray.palette, 0, sizeof(*fontArray.palette));
for(i=0; i < numcolpal; i+=3) for(i=0; i < numcolpal; i+=3)
{ // Font palette { // Font palette
palette[j++] = ((my_bmf[i+17]<<24) | (my_bmf[i+18]<<16) | (my_bmf[i+19]<<8) | 0xFF); fontArray.palette[j++] = ((my_bmf[i+17]<<24) | (my_bmf[i+18]<<16) | (my_bmf[i+19]<<8) | 0xFF);
} }
numchars = my_bmf[18 + numcolpal + my_bmf[17 + numcolpal]]; j = 18 + numcolpal + my_bmf[17 + numcolpal];
fontArray.nbChar = (my_bmf[j] | my_bmf[j+1]<<8);
fontArray.charDef = (GRRLIB_bytemapChar *)calloc(fontArray.nbChar, sizeof(GRRLIB_bytemapChar));
for(i=0; i < numchars; i++) j++;
for(i=0; i < fontArray.nbChar; i++) //fontArray.nbChar
{ // Bitmap character definitions { // Bitmap character definitions
fontArray.charDef[i].character = my_bmf[++j];
fontArray.charDef[i].width = my_bmf[++j];
fontArray.charDef[i].height = my_bmf[++j];
fontArray.charDef[i].relx = my_bmf[++j];
fontArray.charDef[i].rely = my_bmf[++j];
fontArray.charDef[i].shift = my_bmf[++j];
nbPixels = fontArray.charDef[i].width * fontArray.charDef[i].height;
//fontArray.charDef[i].data = malloc(nbPixels);
fontArray.charDef[i].data = (u8 *)calloc(nbPixels, 8);
if(nbPixels && fontArray.charDef[i].data) {
memcpy(fontArray.charDef[i].data, &my_bmf[++j], nbPixels);
j += (nbPixels - 1);
} }
}
}
return fontArray;
}
/**
* Free memory.
* @param bmf a GRRLIB_bytemapFont structure.
*/
void GRRLIB_FreeBMF(GRRLIB_bytemapFont bmf)
{
return my_texture; // Need another kind of struct
} }
/** /**
@ -322,9 +365,6 @@ GRRLIB_texImg GRRLIB_LoadTexture(const unsigned char my_img[]) {
if(my_img[0]==0xFF && my_img[1]==0xD8 && my_img[2]==0xFF) { if(my_img[0]==0xFF && my_img[1]==0xD8 && my_img[2]==0xFF) {
return(GRRLIB_LoadTextureJPG(my_img)); return(GRRLIB_LoadTextureJPG(my_img));
} }
else if(my_img[0]==0xE1 && my_img[1]==0xE6 && my_img[2]==0xD5 && my_img[3]==0x1A) {
return GRRLIB_LoadTextureBMF(my_img);
}
/* /*
else if(my_img[0]==0x42 && my_img[1]==0x4D) { else if(my_img[0]==0x42 && my_img[1]==0x4D) {
// Bitmap not supported // Bitmap not supported

View file

@ -33,6 +33,29 @@ typedef struct GRRLIB_texImg{
void *data; /**< pointer to the texture data. */ void *data; /**< pointer to the texture data. */
} GRRLIB_texImg; } GRRLIB_texImg;
/**
* Structure to hold the bytemap character informations.
*/
typedef struct GRRLIB_bytemapChar{
u8 character; /**< Which character. */
u8 width; /**< Character width. */
u8 height; /**< Character height. */
u8 relx; /**< Horizontal offset according to cursor (-128..127). */
u8 rely; /**< Vertical offset according to cursor (-128..127). */
u8 shift; /**< Horizontal cursor shift after drawing the character. */
u8 *data; /**< Character data itself (uncompressed, 8 bits per pixel). */
} GRRLIB_bytemapChar;
/**
* Structure to hold the bytemap font informations.
*/
typedef struct GRRLIB_bytemapFont{
u32 palette[64];
u16 nbChar;
GRRLIB_bytemapChar *charDef;
} GRRLIB_bytemapFont;
extern Mtx GXmodelView2D; extern Mtx GXmodelView2D;
inline void GRRLIB_FillScreen(u32 color); inline void GRRLIB_FillScreen(u32 color);
@ -49,7 +72,8 @@ void GRRLIB_NGoneFilled(Vector v[], u32 color, long n);
GRRLIB_texImg GRRLIB_CreateEmptyTexture(unsigned int, unsigned int); GRRLIB_texImg GRRLIB_CreateEmptyTexture(unsigned int, unsigned int);
GRRLIB_texImg GRRLIB_LoadTexture(const unsigned char my_img[]); GRRLIB_texImg GRRLIB_LoadTexture(const unsigned char my_img[]);
GRRLIB_bytemapFont GRRLIB_LoadTextureBMF(const unsigned char my_bmf[]);
void GRRLIB_FreeBMF(GRRLIB_bytemapFont bmf);
void GRRLIB_InitTileSet(struct GRRLIB_texImg *tex, unsigned int tilew, unsigned int tileh, unsigned int tilestart); void GRRLIB_InitTileSet(struct GRRLIB_texImg *tex, unsigned int tilew, unsigned int tileh, unsigned int tilestart);
@ -57,6 +81,7 @@ inline void GRRLIB_DrawImg(f32 xpos, f32 ypos, GRRLIB_texImg tex, float degrees,
inline void GRRLIB_DrawTile(f32 xpos, f32 ypos, GRRLIB_texImg tex, float degrees, float scaleX, f32 scaleY, u32 color, int frame); inline void GRRLIB_DrawTile(f32 xpos, f32 ypos, GRRLIB_texImg tex, float degrees, float scaleX, f32 scaleY, u32 color, int frame);
void GRRLIB_Printf(f32 xpos, f32 ypos, GRRLIB_texImg tex, u32 color, f32 zoom, const char *text, ...); void GRRLIB_Printf(f32 xpos, f32 ypos, GRRLIB_texImg tex, u32 color, f32 zoom, const char *text, ...);
void GRRLIB_PrintBMF(f32 xpos, f32 ypos, GRRLIB_bytemapFont bmf, f32 zoom, const char *text, ...);
bool GRRLIB_PtInRect(int hotx, int hoty, int hotw, int hoth, int wpadx, int wpady); bool GRRLIB_PtInRect(int hotx, int hoty, int hotw, int hoth, int wpadx, int wpady);
bool GRRLIB_RectInRect(int rect1x, int rect1y, int rect1w, int rect1h, int rect2x, int rect2y, int rect2w, int rect2h); bool GRRLIB_RectInRect(int rect1x, int rect1y, int rect1w, int rect1h, int rect2x, int rect2y, int rect2w, int rect2h);

View file

@ -72,7 +72,7 @@ int main() {
GRRLIB_texImg tex_test_jpg = GRRLIB_LoadTexture(test_jpg); GRRLIB_texImg tex_test_jpg = GRRLIB_LoadTexture(test_jpg);
GRRLIB_texImg tex_test_bmf = GRRLIB_LoadTexture(ocean); GRRLIB_bytemapFont bmf_Font = GRRLIB_LoadTextureBMF(ocean);
GRRLIB_texImg tex_sprite_png = GRRLIB_LoadTexture(sprite); GRRLIB_texImg tex_sprite_png = GRRLIB_LoadTexture(sprite);
GRRLIB_InitTileSet(&tex_sprite_png, 24, 32, 0); GRRLIB_InitTileSet(&tex_sprite_png, 24, 32, 0);
@ -162,6 +162,7 @@ int main() {
GRRLIB_Printf(left, top+300, tex_BMfont3, GRRLIB_WHITE, 1, "IR Y VALUE: %d", (int)ir1.y); GRRLIB_Printf(left, top+300, tex_BMfont3, GRRLIB_WHITE, 1, "IR Y VALUE: %d", (int)ir1.y);
GRRLIB_Printf(left, top+350, tex_BMfont3, 0XFFFFFF50, 1, "TEXT WITH ALPHA"); GRRLIB_Printf(left, top+350, tex_BMfont3, 0XFFFFFF50, 1, "TEXT WITH ALPHA");
GRRLIB_Printf(left, top+400, tex_BMfont5, GRRLIB_LIME, 1, "This font has the 128 ASCII characters"); GRRLIB_Printf(left, top+400, tex_BMfont5, GRRLIB_LIME, 1, "This font has the 128 ASCII characters");
GRRLIB_PrintBMF(left, top+420, bmf_Font, 1, "OCEAN");
} }
GRRLIB_Printf(500, 27, tex_BMfont5, GRRLIB_WHITE, 1, "Current FPS: %d", FPS); GRRLIB_Printf(500, 27, tex_BMfont5, GRRLIB_WHITE, 1, "Current FPS: %d", FPS);
GRRLIB_Render(); GRRLIB_Render();
@ -214,13 +215,13 @@ int main() {
GRRLIB_Exit(); // Be a good boy, clear the memory allocated by GRRLIB GRRLIB_Exit(); // Be a good boy, clear the memory allocated by GRRLIB
// Free some textures // Free some textures
free(tex_test_jpg.data); free(tex_test_jpg.data);
free(tex_test_bmf.data);
free(tex_sprite_png.data); free(tex_sprite_png.data);
free(tex_BMfont1.data); free(tex_BMfont1.data);
free(tex_BMfont2.data); free(tex_BMfont2.data);
free(tex_BMfont3.data); free(tex_BMfont3.data);
free(tex_BMfont4.data); free(tex_BMfont4.data);
free(tex_BMfont5.data); free(tex_BMfont5.data);
GRRLIB_FreeBMF(bmf_Font);
return 0; return 0;
} }