newb: Trying to get a sprites drawn on the screen

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

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

newb: Trying to get a sprites drawn on the screen

Post by Ghoti »

Hi folks,

I try to render a sprite on the screen. I used the example of the sprite in the SDK. I use the same .raw picture but for some reason mine won't render. Am i forgetting something? The app doesn't crash since my buttons still work and i change the clear code into another color and that worked also only the sprite won't render.

what is wrong?

Code: Select all

/*

  Generated by Sony PSP Application AppWizard

  Version : 2.0.0

  File : main.c

  Description : main file for psp projet

*/


// INCLUDES

#include <psptypes.h>
#include <pspmoduleinfo.h>
#include <pspthreadman.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspge.h>
#include <pspgu.h>
#include <pspkernel.h>
#include <math.h>


// MODULE INITIALISATION

PSP_MODULE_INFO&#40;"Sample", 0x0000, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;PSP_THREAD_ATTR_USER&#41;;

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;
extern unsigned char ball_start&#91;&#93;;

/* Define printf, just to make typing easier */
#define printf	pspDebugScreenPrintf

#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;
#define SIN_ITERATOR 20

struct Vertex
&#123;
	float u, v;
	unsigned int color;
	float x,y,z;
&#125;;

struct Vertex vertices&#91;1&#93;;
int done = 0;
int love = 0;
int pressed = 0;

ScePspFMatrix4 projection;
ScePspFMatrix4 view;
ScePspFMatrix4 world;

void matrix_identity&#40;float* matrix&#41;;
void matrix_projection&#40;float* matrix, float fovy, float aspect, float near, float far&#41;;


/* Exit callback */
int exit_callback&#40;void&#41;
&#123;
	sceKernelExitGame&#40;&#41;;

	return 0;
&#125;

/* Callback thread */
void CallbackThread&#40;void *arg&#41;
&#123;
	int cbid;

	
	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;

	sceKernelSleepThreadCB&#40;&#41;;
&#125;



/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;

	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;

int InitApp&#40;void&#41;&#123;

	// init the Graphical Unit
	
	sceGuInit&#40;&#41;;
	sceGuStart&#40;GU_DIRECT,list&#41;;
	sceGuDrawBuffer&#40;GU_PSM_8888,&#40;void*&#41;0,512&#41;;
	sceGuDispBuffer&#40;SCR_WIDTH,SCR_HEIGHT,&#40;void*&#41;0x88000,512&#41;;
	sceGuDepthBuffer&#40;&#40;void*&#41;0x110000,512&#41;;
	sceGuOffset&#40;2048 - &#40;SCR_WIDTH/2&#41;,2048 - &#40;SCR_HEIGHT/2&#41;&#41;;
	sceGuViewport&#40;2048,2048,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuDepthRange&#40;0xc350,0x2710&#41;;
	sceGuScissor&#40;0,0,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuEnable&#40;GU_SCISSOR_TEST&#41;;
	sceGuAlphaFunc&#40;GU_GREATER,0,0xff&#41;;
	sceGuEnable&#40;GU_ALPHA_TEST&#41;;
	sceGuDepthFunc&#40;GU_GEQUAL&#41;;
	sceGuEnable&#40;GU_DEPTH_TEST&#41;;
	sceGuFrontFace&#40;GU_CW&#41;;
	sceGuShadeModel&#40;GU_SMOOTH&#41;;
	sceGuEnable&#40;GU_CULL_FACE&#41;;
	sceGuEnable&#40;GU_TEXTURE_2D&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;

	sceDisplayWaitVblankStart&#40;&#41;;
	sceGuDisplay&#40;GU_TRUE&#41;;

	vertices&#91;0&#93;.x = 0;
	vertices&#91;0&#93;.y = 0;
	vertices&#91;0&#93;.z = 0;
	vertices&#91;1&#93;.x = 100;
	vertices&#91;1&#93;.y = 100;
	vertices&#91;1&#93;.z = 100;


	return 0;
&#125;

int Controls&#40;void&#41;&#123;
	 
	SceCtrlData pad;

	sceCtrlReadBufferPositive&#40;&pad, 1&#41;; 

	if &#40;pad.Buttons != 0&#41;&#123;
			if &#40;pad.Buttons & PSP_CTRL_CROSS&#41;&#123;
				sceGuTerm&#40;&#41;;
				sceKernelExitGame&#40;&#41;;
			&#125;
	&#125;
	return 0;
 &#125;

int SetupWorld&#40;void&#41;&#123;

	matrix_identity&#40;&#40;float*&#41;&projection&#41;;
	matrix_projection&#40;&#40;float*&#41;&projection,75.0f,16.0/9.0f,0.5f,1000.0f&#41;;
	sceGuSetMatrix&#40;GU_PROJECTION,&projection&#41;;

	matrix_identity&#40;&#40;float*&#41;&view&#41;;
	sceGuSetMatrix&#40;GU_VIEW,&view&#41;;

	matrix_identity&#40;&#40;float*&#41;&world&#41;;
	sceGuSetMatrix&#40;GU_MODEL,&world&#41;;

	sceGuAmbientColor&#40;0xffffffff&#41;;
	
	return 0;
 &#125;

int Draw&#40;void&#41;&#123;
	
	sceGuStart&#40;GU_DIRECT,list&#41;;

	SetupWorld&#40;&#41;;
	
	sceGuTexMode&#40;GU_PSM_5551,0,0,0&#41;;
	sceGuTexImage&#40;0,32,32,32,ball_start&#41;; // width, height, buffer width, tbp
	sceGuTexFunc&#40;GU_TFX_MODULATE,GU_TCC_RGBA&#41;; // NOTE&#58; this enables reads of the alpha-component from the texture, otherwise blend/test won't work
	sceGuTexFilter&#40;GU_NEAREST,GU_NEAREST&#41;;
	sceGuTexWrap&#40;GU_CLAMP,GU_CLAMP&#41;;
	sceGuTexScale&#40;1,1&#41;;
	sceGuTexOffset&#40;0,0&#41;;
	sceGuAmbientColor&#40;0xffffffff&#41;;

	sceGuClearColor&#40;0xff000000&#41;;
	sceGuClearDepth&#40;0&#41;;
	sceGuClear&#40;GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT&#41;;

	sceGuDrawArray&#40;GU_SPRITES,GU_TEXTURE_32BITF|GU_VERTEX_32BITF,2,0,vertices&#41;;

	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;

	sceDisplayWaitVblankStart&#40;&#41;;
	sceGuSwapBuffers&#40;&#41;;
	
	return 0;
 &#125;



int main&#40;void&#41;
&#123;
	SetupCallbacks&#40;&#41;;
	pspDebugScreenInit&#40;&#41;;

	InitApp&#40;&#41;;
	//SetupWorld&#40;&#41;;
	

 /*print some text on the screen
	printf&#40;"\nHey mooie schoonheid ik houd van jou!!!\n"&#41;;
	printf&#40;"En hou jij ook van mij?\n"&#41;;
	printf&#40;"\nAls je van me houd moet je op X drukken!\n"&#41;;*/
	
	sceCtrlSetSamplingCycle&#40;0&#41;;
	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;
	
	
	while&#40;!done&#41;&#123;

	// Get control input
		Controls&#40;&#41;;
	// Draw the scene
		Draw&#40;&#41;;
    	
	&#125;
	sceGuTerm&#40;&#41;;
	
	//sceKernelSleepThread&#40;&#41;;
	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

void matrix_identity&#40;float* matrix&#41;
&#123;
	matrix&#91;&#40;0<<2&#41;+0&#93; = 1.0f;
	matrix&#91;&#40;0<<2&#41;+1&#93; = 0.0f;
	matrix&#91;&#40;0<<2&#41;+2&#93; = 0.0f;
	matrix&#91;&#40;0<<2&#41;+3&#93; = 0.0f;

	matrix&#91;&#40;1<<2&#41;+0&#93; = 0.0f;
	matrix&#91;&#40;1<<2&#41;+1&#93; = 1.0f;
	matrix&#91;&#40;1<<2&#41;+2&#93; = 0.0f;
	matrix&#91;&#40;1<<2&#41;+3&#93; = 0.0f;

	matrix&#91;&#40;2<<2&#41;+0&#93; = 0.0f;
	matrix&#91;&#40;2<<2&#41;+1&#93; = 0.0f;
	matrix&#91;&#40;2<<2&#41;+2&#93; = 1.0f;
	matrix&#91;&#40;2<<2&#41;+3&#93; = 0.0f;

	matrix&#91;&#40;3<<2&#41;+0&#93; = 0.0f;
	matrix&#91;&#40;3<<2&#41;+1&#93; = 0.0f;
	matrix&#91;&#40;3<<2&#41;+2&#93; = 0.0f;
	matrix&#91;&#40;3<<2&#41;+3&#93; = 1.0f;
&#125;

void matrix_projection&#40;float* matrix, float fovy, float aspect, float near, float far&#41;
&#123;
	matrix_identity&#40;matrix&#41;;

	float angle = &#40;fovy / 2.0f&#41; * &#40;M_PI/180.0f&#41;;
	float cotangent = cosf&#40;angle&#41; / sinf&#40;angle&#41;;

	matrix&#91;&#40;0<<2&#41;+0&#93; = cotangent / aspect;
	matrix&#91;&#40;1<<2&#41;+1&#93; = cotangent;
	matrix&#91;&#40;2<<2&#41;+2&#93; = &#40;far + near&#41; / &#40;near - far&#41;;
	matrix&#91;&#40;3<<2&#41;+2&#93; = 2.0f * &#40;far * near&#41; / &#40;near - far&#41;;
	matrix&#91;&#40;2<<2&#41;+3&#93; = -1;
	matrix&#91;&#40;3<<2&#41;+3&#93; = 0.0f;
&#125;

float sinf&#40;float v&#41;
&#123;
	float res,w;
	int t;
	float fac;
	int i=&#40;int&#41;&#40;&#40;v&#41;/&#40;2.0f*M_PI&#41;&#41;;
	v-=i*2.0f*M_PI;

	fac=1.0f;
	res=0.0f;
	w=v;
	for&#40;t=1;t<SIN_ITERATOR;&#41;
	&#123;
		res+=fac*w;
		w*=v*v;
		t++;
		fac/=t;
		t++;
		fac/=t;

		res-=fac*w;
		w*=v*v;
		t++;
		fac/=t;
		t++;
		fac/=t;
	&#125;
	return res;
&#125;

float cosf&#40;float v&#41;
&#123;
	float res,w;
	int t;
	float fac;
	int i=&#40;int&#41;&#40;&#40;v&#41;/&#40;2.0f*M_PI&#41;&#41;;
	v-=i*2.0f*M_PI;

	fac=1.0f;
	res=0.0f;
	w=1.0f;
	for&#40;t=0;t<SIN_ITERATOR;&#41;
	&#123;
		res+=fac*w;
		w*=v*v;
		t++;
		fac/=t;
		t++;
		fac/=t;

		res-=fac*w;
		w*=v*v;
		t++;
		fac/=t;
		t++;
		fac/=t;
	&#125;
	return res;
&#125;
sandberg
Posts: 90
Joined: Wed Oct 05, 2005 1:25 am
Location: Denmark

Post by sandberg »

Frist you only declare space for 1 vertex ? You should make sure you allocate enough space for all your vertices and you should align it to a 16 byte boundary. See my change in red below.
struct Vertex
{
float u, v;
unsigned int color;
float x,y,z;
};

struct Vertex __attribute__((aligned(16))) vertices[2];
You don't set the u and v coordinates for the vertex or the color. See my change in red below.
vertices[0].x = 0;
vertices[0].y = 0;
vertices[0].z = 0;

vertices[0].u = 0;
vertices[0].v = 0;
vertices[0].color = 0xFFFFFFFF;

vertices[1].x = 100;
vertices[1].y = 100;
vertices[1].z = 100;

vertices[1].u = 32;
vertices[1].v = 32;
vertices[1].color = 0xFFFFFFFF;
Try to see if that helps.
Br, Sandberg
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Hi thanks first of all for the reply but it didn't work.

first some question about the code you gave me: I thought that var[1] means that you have var[0] and var[1] (I come from visual basic code.) is this a special case or is this always so?

I didn't specify the uv and color but i assumed if i didn't that it would assign a default value guess this is not the case.

last, why do i have to align it to a 16 byte boundary? in the sprite.c example given with the SDK it is also not declare as such.

anyway in the sdk the do this:

Code: Select all

vertices = sceGuGetMemory&#40;NUM_SLICES * NUM_ROWS * 2 * sizeof&#40;struct Vertex&#41;&#41;;
		
What exactly does this and do i need it also?
sandberg
Posts: 90
Joined: Wed Oct 05, 2005 1:25 am
Location: Denmark

Post by sandberg »

Ghoti wrote:Hi thanks first of all for the reply but it didn't work.

first some question about the code you gave me: I thought that var[1] means that you have var[0] and var[1] (I come from visual basic code.) is this a special case or is this always so?
This is different in C. The number between the [] is the number of elements in the array.
Ghoti wrote:I didn't specify the uv and color but i assumed if i didn't that it would assign a default value guess this is not the case.
Nope. and even if it was, how should it know which texture coordinates you wanted to use :)
Ghoti wrote:last, why do i have to align it to a 16 byte boundary? in the sprite.c example given with the SDK it is also not declare as such.

anyway in the sdk the do this:

Code: Select all

vertices = sceGuGetMemory&#40;NUM_SLICES * NUM_ROWS * 2 * sizeof&#40;struct Vertex&#41;&#41;;
		
What exactly does this and do i need it also?
You don't have to. The sceGuGetMemory allocated the memory from your current displaylist (which is list in your case). You can use that, but make sure that you only use it between your sceGuStart and sceGuFinish functioncalls.

You also need to tell sceGuDrawArray how your vertices are layed out. In your case you tell it that you have texture coordinates and vertices (both 32 bit). You need to modify your call as shown below.

sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_2D,2,0,vertices);

And you all need to tell if your need 2D or 3D transforms. If you don't specify that it will explicit use 3D transforms, which is not what you want.
Br, Sandberg
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Tnxy :D:D it works and thank you for the explanation it helps alot to understand it all better.
sandberg
Posts: 90
Joined: Wed Oct 05, 2005 1:25 am
Location: Denmark

Post by sandberg »

Ghoti wrote:Tnxy :D:D it works and thank you for the explanation it helps alot to understand it all better.
No problem :)
Br, Sandberg
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

Just as a note, vertices do not have to be aligned to a qword-boundary, only images & palettes need that.
GE Dominator
TheBrooster
Posts: 16
Joined: Wed Jun 01, 2005 8:55 am

Post by TheBrooster »

Vertices don't have to be aligned, I will give you that. But it is a good habit to get into as when the VFPU vector calls ( add, sub, mul etc. ) are working they will require alignment.
Post Reply