Graphic distorted.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
zniter81
Posts: 8
Joined: Thu Jun 15, 2006 1:25 am

Graphic distorted.

Post by zniter81 »

hi all,
i have recently try to do some simple graphic thingy.
displaying a .PNG picture and draw a string. displaying the .PNG picture alone work great. drawstring alone also working fine. but when i combine then together, i got distorted graphic.

can any1 guide me.... thanks

below are my code(i modified(in red)) and picture

this is the .PNG alone only it work.
Image

this is the drawString function from pspsdk/sample/gu/text. it work too.
Image

below is the conbine of both (got Distorted.)
Image

void initGraphics()
{
dispBufferNumber = 0;

sceGuInit();
guStart();
sceGuDrawBuffer(GU_PSM_8888,(void*)0,BUF_WIDTH);
sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,(void*)0x88000,BUF_WIDTH);
sceGuClear(GU_COLOR_BUFFER_BIT);
sceGuDepthBuffer((void*)0x110000,BUF_WIDTH);
sceGuOffset(2048 - (SCREEN_WIDTH / 2), 2048 - (SCREEN_HEIGHT / 2));
sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT);
sceGuDepthRange(0xc350,0x2710);
sceGuScissor(0,0,SCR_WIDTH,SCR_HEIGHT);

sceGuEnable(GU_SCISSOR_TEST);
sceGuAlphaFunc(GU_GREATER, 0, 0xff);
sceGuEnable(GU_ALPHA_TEST);
sceGuDepthFunc(GU_GEQUAL);
sceGuDisable(GU_DEPTH_TEST);
sceGuFrontFace(GU_CW);
sceGuShadeModel(GU_SMOOTH);
sceGuEnable(GU_CULL_FACE);
sceGuEnable(GU_TEXTURE_2D);
sceGuEnable(GU_CLIP_PLANES);
sceGuTexMode(GU_PSM_8888, 0, 0, 0);
sceGuTexImage(0, 256, 128, 256, font);
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);
sceGuAmbientColor(0xffffffff);
sceGuEnable(GU_BLEND);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
sceGuFinish();
sceGuSync(0, 0);

sceDisplayWaitVblankStart();
sceGuDisplay(GU_TRUE);
initialized = 1;
}

typedef struct{
float u, v;
float x, y, z;
} Vertex;

void blitAlphaImageToScreen(int sx, int sy, int width, int height, Image* source, int dx, int dy)
{
if (!initialized) return;

sceKernelDcacheWritebackInvalidateAll();
//guStart();
sceGuTexImage(0, source->textureWidth, source->textureHeight, source->textureWidth, (void*) source->data);
float u = 1.0f / ((float)source->textureWidth);
float v = 1.0f / ((float)source->textureHeight);
sceGuTexScale(u, v);

int j = 0;
while (j < width) {
Vertex* vertices = (Vertex*) sceGuGetMemory(2 * sizeof(Vertex));
int sliceWidth = 64;
if (j + sliceWidth > width) sliceWidth = width - j;
vertices[0].u = (float)sx + j;
vertices[0].v = (float)sy;
vertices[0].x = (float)dx + j;
vertices[0].y = (float)dy;
vertices[0].z = (float)0;
vertices[1].u = (float)sx + j + sliceWidth;
vertices[1].v = (float)sy + height;
vertices[1].x = (float)dx + j + sliceWidth;
vertices[1].y = (float)dy + height;
vertices[1].z = (float)0;
sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_2D, 2, 0, vertices);

j += sliceWidth;
}

//sceGuFinish();
//sceGuSync(0, 0);

}

void drawString(const char* text, int x, int y, unsigned int color, int fw) {
int len = (int)strlen(text);
if(!len) {
return;
}

typedef struct {
float s, t;
unsigned int c;
float x, y, z;
} VERT;

VERT* v = sceGuGetMemory(sizeof(VERT) * 2 * len);

int i;
for(i = 0; i < len; i++) {
unsigned char c = (unsigned char)text;
if(c < 32) {
c = 0;
} else if(c >= 128) {
c = 0;
}

int tx = (c & 0x0F) << 4;
int ty = (c & 0xF0);

VERT* v0 = &v[i*2+0];
VERT* v1 = &v[i*2+1];

v0->s = (float)(tx + (fw ? ((16 - fw) >> 1) : ((16 - fontwidthtab[c]) >> 1)));
v0->t = (float)(ty);
v0->c = color;
v0->x = (float)(x);
v0->y = (float)(y);
v0->z = 0.0f;

v1->s = (float)(tx + 16 - (fw ? ((16 - fw) >> 1) : ((16 - fontwidthtab[c]) >> 1)));
v1->t = (float)(ty + 16);
v1->c = color;
v1->x = (float)(x + (fw ? fw : fontwidthtab[c]));
v1->y = (float)(y + 16);
v1->z = 0.0f;

x += (fw ? fw : fontwidthtab[c]);
}

sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_2D, len * 2, 0, v);

}

int main(int argc, char** argv) {
char buffer[200];
Image* ourImage;
char filler[10];
pspDebugScreenInit();

SetupCallbacks();

initGraphics();

Color highlightColor = RGB(200, 200, 200);

sprintf(buffer, "ourImage.png");
ourImage = loadImage(buffer);
if (!ourImage) {
//Image load failed
printf("Image load failed!\n");
sceKernelSleepThread();
return 0;
}

while(1) {
sceGuStart(GU_DIRECT, list);
sceGuClear(GU_COLOR_BUFFER_BIT);

//blitAlphaImageToScreen(0 ,0 ,480 , 272, ourImage, 0, 0);

// No matrixes are needed because the font is drawn with GU_TRANSFORM_2D

drawString("Hello World in red", 0, 0, 0xFF0000FF, 0);
drawString("Hello World in green", 0, 16, 0xFF00FF00, 0);
drawString("Hello World in blue", 0, 32, 0xFFFF0000, 0);

drawString("Hello World with free char width", 0, 64, 0xFFFFFFFF, 0);
drawString("Hello World with block char width 10", 0, 80, 0xFFFFFFFF, 10);
drawString("Hello World with block char width 12", 0, 0, 0xFFFFFFFF, 12);

drawString("Hello World with opacity 100%", 0, 128, 0xFFFFFFFF, 0);
drawString("Hello World with opacity 50%", 0, 144, 0x7FFFFFFF, 0);
drawString("Hello World with opacity 10%", 0, 160, 0x18FFFFFF, 0);

drawString("Hello World with shadow", 2, 194, 0x40FFFFFF, 0);
drawString("Hello World with shadow", 0, 192, 0xFFFFFFFF, 0);

sceGuFinish();
sceGuSync(0, 0);

flipScreen();
sceDisplayWaitVblankStart();

}

sceGuDisplay(GU_FALSE);
sceGuTerm();

return 0;
}
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

You set the texture pointer for the font in the init, but then you render the image in-between. Set the texture pointer for the font before rendering the text instead.
GE Dominator
zniter81
Posts: 8
Joined: Thu Jun 15, 2006 1:25 am

Post by zniter81 »

Thanks.... it did work....
how could i miss that part u mention...
haha but anyway thanks....
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

reverse these
flipScreen();
sceDisplayWaitVblankStart();

to be
sceDisplayWaitVblankStart();
flipscreen();
10011011 00101010 11010111 10001001 10111010
Post Reply