forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with drawing text (sprites)

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
sturatt



Joined: 13 Jul 2006
Posts: 46

PostPosted: Wed Jul 26, 2006 4:05 pm    Post subject: Help with drawing text (sprites) Reply with quote

This all started when i wanted to use a custom font. at first i tried to make a text drawer using graphic.h's blit alpha image to screen, but it was too slow. So i embarked on my journey to try and learn enough GU to be able to draw text on the screen.

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:
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:

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:

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;
}


and yes i realize that ^^ is almost exactly like the graphics.h loadImage but i wanted to go through with the libpng documentation and find out what it was actually doing.


FontEngine.h
Code:
#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);


fontwidths[] is a 256 int array that has a width for each char.
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 =\
Back to top
View user's profile Send private message
sturatt



Joined: 13 Jul 2006
Posts: 46

PostPosted: Thu Jul 27, 2006 5:14 am    Post subject: Reply with quote

maybe it has something to do with my sceGuTexMode(GU_PSM_8888, 0, 0, 0); and with my sceGuDrawArray call. i dont actually know if my texture is 32bits, would that stop it from drawing?

also, if i changed GU_VERTEX_32BITF in the sceGuDrawArray function to GU_VERTEX_16BIT could i just use my ints instead of typecasting them as floats?
Back to top
View user's profile Send private message
subbie



Joined: 05 May 2005
Posts: 122

PostPosted: Fri Jul 28, 2006 4:14 am    Post subject: Reply with quote

You try doing a cache flush on your verticie list before sending it off to the GU?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group