the way im trying to do it is with a 256x256 png with all the ascii chars. I can load and find my way through that with no problem, but when it comes to the gu stuff im a bit lost. Here's what i have so far, but it doesnt seem to actually draw any text..
FontEngine.c
Code: Select all
void InitFont()
{
FontTexture *FTex;
FTex = readFontPNGTexture("font2.png");
readwidths();
sceGuInit();
sceGuStart(GU_DIRECT, list);
sceGuDrawBuffer(GU_PSM_8888,(void*)0,BUF_WIDTH);
sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,(void*)0x88000,BUF_WIDTH);
sceGuDepthBuffer((void*)0x110000,BUF_WIDTH);
sceGuOffset(2048 - (SCR_WIDTH/2),2048 - (SCR_HEIGHT/2));
sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT);
sceGuDepthRange(0xc350,0x2710);
sceGuScissor(0,0,SCR_WIDTH,SCR_HEIGHT);
sceGuEnable(GU_SCISSOR_TEST);
sceGuDisable(GU_DEPTH_TEST);
sceGuShadeModel(GU_SMOOTH);
sceGuEnable(GU_BLEND);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
sceGuEnable(GU_TEXTURE_2D);
sceGuTexMode(GU_PSM_8888, 0, 0, 0);
sceGuTexImage(0, 256, 256, 256, FTex->data);
sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
sceGuTexEnvColor(0x0);
sceGuTexOffset(0.0f, 0.0f);
sceGuTexScale(1.0f / 256.0f, 1.0f / 128.0f);
sceGuTexWrap(GU_REPEAT, GU_REPEAT);
sceGuTexFilter(GU_NEAREST, GU_NEAREST);
sceGuFinish();
sceGuSync(0,0);
sceGuDisplay(GU_TRUE);
}
Code: Select all
void pTextScreen(int x, int y, char * text, unsigned int color)
{
int length = strlen(text);
Vertex *vertices = sceGuGetMemory(sizeof(Vertex) * 2 * length);
sceGuStart(GU_DIRECT, list);
for (i = 0; i < strlen(text); i += 2)
{
ascii_num = (int)text[i];
Y_Pos_Tex = ((int)(ascii_num / 16)) * 16;
X_Pos_Tex = (ascii_num % 16) * 16;
vertices[i].u = (float)X_Pos_Tex;
vertices[i].v = (float)Y_Pos_Tex;
vertices[i].c = color;
vertices[i].x = (float)x;
vertices[i].y = (float)y;
vertices[i].z = 0.0f;
vertices[i + 1].u = (float)((X_Pos_Tex + 16) - (16 - fontwidths[ascii_num]));
vertices[i + 1].v = (float)(Y_Pos_Tex + 16);
vertices[i + 1].c = color;
vertices[i + 1].x = (float)((x + 16) - (16 - fontwidths[ascii_num]));
vertices[i + 1].y = (float)(y + 16);
vertices[i + 1].z = 0.0f;
x += fontwidths[ascii_num];
}
sceGuDrawArray(GU_SPRITES,
GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_2D,
length * 2, 0, vertices
);
sceGuFinish();
sceGuSync(0, 0);
}
Code: Select all
FontTexture* readFontPNGTexture(char *pngfile)
{
FontTexture* fonttexture;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type, x, y;
u32 *line;
fonttexture = (FontTexture*) malloc(sizeof(FontTexture));
FILE *fp = fopen(pngfile,"rb");
if (!fp)
{
return NULL;
}
//fread (signature, 1, 8, fp);
//if (png_sig_cmp(signature, 0, 8) != 0)
//{
// fclose(fp);
// return NULL;
//}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
fclose(fp);
return NULL;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(fp);
return NULL;
}
png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info)
{
png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)NULL);
fclose(fp);
return NULL;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 0);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY &&
bit_depth < 8) png_set_gray_1_2_4_to_8(png_ptr);
if (png_get_valid(png_ptr, info_ptr,
PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
png_set_strip_16(png_ptr);
png_set_packing(png_ptr);
png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
fonttexture->imageWidth = width;
fonttexture->imageHeight = height;
fonttexture->textureWidth = 512;
fonttexture->textureHeight = 512;
fonttexture->data = (u32*) memalign(16, fonttexture->textureWidth * fonttexture->textureHeight * sizeof(u32));
if (!fonttexture->data) return NULL;
line = (u32*) malloc(width * 4);
for (y = 0; y < height; y++) {
png_read_row(png_ptr, (u8*) line, png_bytep_NULL);
for (x = 0; x < width; x++) {
u32 color = line[x];
fonttexture->data[x + y * fonttexture->textureWidth] = color;
}
}
free(line);
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
return fonttexture;
}
FontEngine.h
Code: Select all
#include "graphics.h"
void readwidths();
void pTextScreen(int x, int y, char * text, unsigned int color);
void blitAlphaImageScreen(int sx, int sy, int width, int height, Image* source, int dx, int dy);
void InitFont();
void writewidths();
typedef struct
{
int textureWidth;
int textureHeight;
int imageWidth;
int imageHeight;
u32* data;
} FontTexture;
typedef struct {
float u, v;
unsigned int c;
float x, y, z;
} Vertex;
FontTexture* readFontPNGTexture(char *pngfile);
and i know the readFontPNGTexture() function works because ive used it to load a png and call a function in graphics.h and it worked fine.
i realize this is a lot of nonsense to sort through to try and find someone else's error, but i really would appreciate any help. I'm really trying hard to learn this =\