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 

newb: Trying to get a sprites drawn on the screen

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



Joined: 31 Dec 2005
Posts: 288

PostPosted: Thu Jan 05, 2006 1:07 am    Post subject: newb: Trying to get a sprites drawn on the screen Reply with quote

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:

/*

  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("Sample", 0x0000, 1, 1);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);

static unsigned int __attribute__((aligned(16))) list[262144];
extern unsigned char ball_start[];

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

#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)
#define SIN_ITERATOR 20

struct Vertex
{
   float u, v;
   unsigned int color;
   float x,y,z;
};

struct Vertex vertices[1];
int done = 0;
int love = 0;
int pressed = 0;

ScePspFMatrix4 projection;
ScePspFMatrix4 view;
ScePspFMatrix4 world;

void matrix_identity(float* matrix);
void matrix_projection(float* matrix, float fovy, float aspect, float near, float far);


/* Exit callback */
int exit_callback(void)
{
   sceKernelExitGame();

   return 0;
}

/* Callback thread */
void CallbackThread(void *arg)
{
   int cbid;

   
   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);

   sceKernelSleepThreadCB();
}



/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}

int InitApp(void){

   // init the Graphical Unit
   
   sceGuInit();
   sceGuStart(GU_DIRECT,list);
   sceGuDrawBuffer(GU_PSM_8888,(void*)0,512);
   sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,(void*)0x88000,512);
   sceGuDepthBuffer((void*)0x110000,512);
   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);
   sceGuAlphaFunc(GU_GREATER,0,0xff);
   sceGuEnable(GU_ALPHA_TEST);
   sceGuDepthFunc(GU_GEQUAL);
   sceGuEnable(GU_DEPTH_TEST);
   sceGuFrontFace(GU_CW);
   sceGuShadeModel(GU_SMOOTH);
   sceGuEnable(GU_CULL_FACE);
   sceGuEnable(GU_TEXTURE_2D);
   sceGuFinish();
   sceGuSync(0,0);

   sceDisplayWaitVblankStart();
   sceGuDisplay(GU_TRUE);

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


   return 0;
}

int Controls(void){
   
   SceCtrlData pad;

   sceCtrlReadBufferPositive(&pad, 1);

   if (pad.Buttons != 0){
         if (pad.Buttons & PSP_CTRL_CROSS){
            sceGuTerm();
            sceKernelExitGame();
         }
   }
   return 0;
 }

int SetupWorld(void){

   matrix_identity((float*)&projection);
   matrix_projection((float*)&projection,75.0f,16.0/9.0f,0.5f,1000.0f);
   sceGuSetMatrix(GU_PROJECTION,&projection);

   matrix_identity((float*)&view);
   sceGuSetMatrix(GU_VIEW,&view);

   matrix_identity((float*)&world);
   sceGuSetMatrix(GU_MODEL,&world);

   sceGuAmbientColor(0xffffffff);
   
   return 0;
 }

int Draw(void){
   
   sceGuStart(GU_DIRECT,list);

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

   sceGuClearColor(0xff000000);
   sceGuClearDepth(0);
   sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);

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

   sceGuFinish();
   sceGuSync(0,0);

   sceDisplayWaitVblankStart();
   sceGuSwapBuffers();
   
   return 0;
 }



int main(void)
{
   SetupCallbacks();
   pspDebugScreenInit();

   InitApp();
   //SetupWorld();
   

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

   // Get control input
      Controls();
   // Draw the scene
      Draw();
       
   }
   sceGuTerm();
   
   //sceKernelSleepThread();
   sceKernelExitGame();
   return 0;
}

void matrix_identity(float* matrix)
{
   matrix[(0<<2)+0] = 1.0f;
   matrix[(0<<2)+1] = 0.0f;
   matrix[(0<<2)+2] = 0.0f;
   matrix[(0<<2)+3] = 0.0f;

   matrix[(1<<2)+0] = 0.0f;
   matrix[(1<<2)+1] = 1.0f;
   matrix[(1<<2)+2] = 0.0f;
   matrix[(1<<2)+3] = 0.0f;

   matrix[(2<<2)+0] = 0.0f;
   matrix[(2<<2)+1] = 0.0f;
   matrix[(2<<2)+2] = 1.0f;
   matrix[(2<<2)+3] = 0.0f;

   matrix[(3<<2)+0] = 0.0f;
   matrix[(3<<2)+1] = 0.0f;
   matrix[(3<<2)+2] = 0.0f;
   matrix[(3<<2)+3] = 1.0f;
}

void matrix_projection(float* matrix, float fovy, float aspect, float near, float far)
{
   matrix_identity(matrix);

   float angle = (fovy / 2.0f) * (M_PI/180.0f);
   float cotangent = cosf(angle) / sinf(angle);

   matrix[(0<<2)+0] = cotangent / aspect;
   matrix[(1<<2)+1] = cotangent;
   matrix[(2<<2)+2] = (far + near) / (near - far);
   matrix[(3<<2)+2] = 2.0f * (far * near) / (near - far);
   matrix[(2<<2)+3] = -1;
   matrix[(3<<2)+3] = 0.0f;
}

float sinf(float v)
{
   float res,w;
   int t;
   float fac;
   int i=(int)((v)/(2.0f*M_PI));
   v-=i*2.0f*M_PI;

   fac=1.0f;
   res=0.0f;
   w=v;
   for(t=1;t<SIN_ITERATOR;)
   {
      res+=fac*w;
      w*=v*v;
      t++;
      fac/=t;
      t++;
      fac/=t;

      res-=fac*w;
      w*=v*v;
      t++;
      fac/=t;
      t++;
      fac/=t;
   }
   return res;
}

float cosf(float v)
{
   float res,w;
   int t;
   float fac;
   int i=(int)((v)/(2.0f*M_PI));
   v-=i*2.0f*M_PI;

   fac=1.0f;
   res=0.0f;
   w=1.0f;
   for(t=0;t<SIN_ITERATOR;)
   {
      res+=fac*w;
      w*=v*v;
      t++;
      fac/=t;
      t++;
      fac/=t;

      res-=fac*w;
      w*=v*v;
      t++;
      fac/=t;
      t++;
      fac/=t;
   }
   return res;
}

_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
sandberg



Joined: 05 Oct 2005
Posts: 90
Location: Denmark

PostPosted: Thu Jan 05, 2006 3:16 am    Post subject: Reply with quote

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.

Quote:

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.

Quote:

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
Back to top
View user's profile Send private message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Thu Jan 05, 2006 3:50 am    Post subject: Reply with quote

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:
vertices = sceGuGetMemory(NUM_SLICES * NUM_ROWS * 2 * sizeof(struct Vertex));
      

What exactly does this and do i need it also?
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
sandberg



Joined: 05 Oct 2005
Posts: 90
Location: Denmark

PostPosted: Thu Jan 05, 2006 4:13 am    Post subject: Reply with quote

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:
vertices = sceGuGetMemory(NUM_SLICES * NUM_ROWS * 2 * sizeof(struct Vertex));
      

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
Back to top
View user's profile Send private message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Thu Jan 05, 2006 4:31 am    Post subject: Reply with quote

Tnxy :D:D it works and thank you for the explanation it helps alot to understand it all better.
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
sandberg



Joined: 05 Oct 2005
Posts: 90
Location: Denmark

PostPosted: Thu Jan 05, 2006 4:38 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
chp



Joined: 23 Jun 2004
Posts: 313

PostPosted: Thu Jan 05, 2006 5:22 am    Post subject: Reply with quote

Just as a note, vertices do not have to be aligned to a qword-boundary, only images & palettes need that.
_________________
GE Dominator
Back to top
View user's profile Send private message
TheBrooster



Joined: 01 Jun 2005
Posts: 16

PostPosted: Wed Feb 01, 2006 2:41 am    Post subject: Reply with quote

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