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

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



Joined: 17 Jan 2006
Posts: 80
Location: Ontario, Canada

PostPosted: Tue Feb 28, 2006 7:11 pm    Post subject: Help with animation sequence... Reply with quote

Hi guys,

I may not be getting this completely becuase im tired, but I think i need more direction anyways. Right now I have a png, 256x32 WxL. Im trying to load this in memory, and display it as a sequence of frames. There are four frames in each direction. The first four, are the sections of walking left, and the second four are walking right. As you can probably tell, im trying to only display the section of frame where my character is located.

Code:

#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <psppower.h>
#include <pspdisplay.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>

#include "graphics.h"
#include "mp3player.h"

PSP_MODULE_INFO("Game Beta - v2 - Animation Seq.", 0, 1, 1);

#define printf pspDebugScreenPrintf
#define RGB(r,g,b) ((r)|((g)<<8)|((b)<<16))
#define MAX(X,Y) ((X)>(Y)?(X):(Y))
#define NEXT_FRAME 32

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common){
   sceKernelExitGame();
   return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp){
   int cbid;
   
   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);
   
   sceKernelSleepThreadCB();
   
   return 0;
}

/* Sets up the callback thread and returns its threat id */
int SetupCallbacks(void){
   int thid;
   
   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0){
      sceKernelStartThread(thid, 0, 0);
   }
   
   return thid;
}

int main(){
   // Set the power frequency
   scePowerSetClockFrequency(333, 333, 166);
   
   SetupCallbacks();
   pspDebugScreenInit();
   initGraphics();
   pspAudioInit();
   
   SceCtrlData pad;
   char hero_buffer[200];
   /* char enemy_buffer[200]; */
   Image* ourHero;
   /* Image* ourEnemy; */
      
   MP3_Init(1);
   MP3_Load("laser_1.mp3");
      
   sprintf(hero_buffer, "walkseq.png");
   ourHero = loadImage(hero_buffer);
   
   /* animation variables */
   int leftSeq[4]={32, 64, 96, 128};
   int rightSeq[4]={160, 192, 224, 256};
   int frame = 0;
   int pos = rightSeq[frame];
   
   
   /* sprintf(enemy_buffer, "enemyDude.png");
   ourEnemy = loadImage(enemy_buffer); */
   
   if(!ourHero){
      //Image load failed
      printf("Image load failed!\n");
   }/*else if(!ourEnemy){
      //Image load failed
      printf("Image load failed!\n");
   }*/else{
      int hero_x = 0/*, enemy_x = 150*/;
      int hero_y = 0/*, enemy_y = 0*/;
      int bullet_x, bullet_y = 0;
            
      sceDisplayWaitVblankStart();
      
      while(1){
         clearScreen(0);
         fillScreenRect(RGB(255, 255, 255), 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
         blitAlphaImageToScreen(pos-32, 0, pos, 32, ourHero, hero_x, hero_y);
         /* blitAlphaImageToScreen(0, 0, 32, 32, ourEnemy, enemy_x, enemy_y); */
         
         
         sceCtrlReadBufferPositive(&pad, 1);
         
         //meDude controls
         if(pad.Buttons & PSP_CTRL_LEFT){
            if(hero_x>0){hero_x--;}
            pos = leftSeq[frame];
            
            if(frame < 4){
               frame++;
            }else{
               frame = 0;
            }
            
         }

         if(pad.Buttons & PSP_CTRL_RIGHT){
            if(hero_x<(480-32)){hero_x++;}
            pos = rightSeq[frame];
            
            if(frame < 4){
               frame++;
            }else{
               frame = 0;
            }
               
         }
         
         if(pad.Buttons & PSP_CTRL_UP){
            if(hero_y>0){hero_y--;}
         }
         
         if(pad.Buttons & PSP_CTRL_DOWN){
            if(hero_y<(272-32)){hero_y++;}
         }
         
         
      /*    if(pad.Buttons & PSP_CTRL_SQUARE){
            if(enemy_x>0){enemy_x--;}
         } */

      /*   if(pad.Buttons & PSP_CTRL_CIRCLE){
            if(enemy_x<(480-32)){enemy_x++;}
         } */
         
      /*   if(pad.Buttons & PSP_CTRL_TRIANGLE){
            if(enemy_y>0){enemy_y--;}
         } */
         
         if(pad.Buttons & PSP_CTRL_CROSS){
            MP3_Play();
            bullet_x = hero_x + 32;
            bullet_y = hero_y + 16;
            while(bullet_x < 480){
               putPixelScreen(0, bullet_x, bullet_y);
               bullet_x++;
               flipScreen();
               
            }
               
         }
         
         //if the MP3 finishes, stop it.
         if(MP3_EndOfStream() == 1){
         MP3_Stop();
         } 
         
         sceDisplayWaitVblank();
         flipScreen();
      }   
   
   }
   
   /* Stop MP3 & Free Memory */
   /*MP3_Stop();*/
   MP3_FreeTune();
   
   sceKernelSleepThread();
   return 0;
}
Back to top
View user's profile Send private message MSN Messenger
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Tue Feb 28, 2006 7:47 pm    Post subject: Reply with quote

You didn't exactly tell what your problem is, but at a quick look, I noticed that you got the parameters for blitAlphaImage wrong (unless you changed the function from the psp tutorial).

In the tutorial introducing this function it says: "The second two parameters are for width and height respectively. Since our image is 32 pixels by 32 pixels, we are using 32 for both of these values."

So it should be blitAlphaImageToScreen(pos-32, 0, 32, 32, ourHero, hero_x, hero_y);

Other than that (and the fact that your animation isn't timed in any way, so will most probably play too fast) the only thing I don't quite get, is why you first clrear the screen with 0 (black) and afterwards fill the whole screen with a white rect (thus clearing the screen with white). One or the other should be removed.
Back to top
View user's profile Send private message Visit poster's website
JesusXP



Joined: 17 Jan 2006
Posts: 80
Location: Ontario, Canada

PostPosted: Wed Mar 01, 2006 3:58 pm    Post subject: Reply with quote

Quote:
You didn't exactly tell what your problem is


sorry, the problem is, it doesnt display just the section of the image i want to display, which is a 32 pixel length section at a time. Right now, it displays a strip of about 4 frames, and it does switch which one it is showing in the far left corner however, if i press the right direction it freezes my psp, and I cant escape back to main menu...

Quote:
why you first clrear the screen with 0 (black) and afterwards fill the whole screen with a white rect


yeah, im pretty much a newb. Im sorry, I didnt even realize that part. The reason being, my sprite backgrounds are white. Is there a way to make the alpha color (transparent color) the white, or first pixel of the sprite? should i clear the screen and make it white somehow? how would I do that? put the hex value of white?

oh, and in the blit function, pos is at first equal to 32, and each time i press a direction, it switches to a frame of sectioned off of 32 pixels, thats at least what im trying t odo..
Back to top
View user's profile Send private message MSN Messenger
Loco-san



Joined: 09 Feb 2006
Posts: 1

PostPosted: Wed Mar 01, 2006 5:31 pm    Post subject: Reply with quote

Raph was right, your blit was wrong. I fixed that, and the other major problem was your frame counter. it becomes out of range. when <4 it still adds 1 more then becomes 4 and accessed in the rightSeq arrays, trying to grab the last frame from the memory past what you allocated (probably where it freezes). I fixed it the lazy way. I cleaned up some of the code for you, but much more cleaning needs to be done :P sorry I had to comment out the mp3 stuff I dont have the code on me.

Code:

#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <psppower.h>
#include <pspdisplay.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>

#include "graphics.h"
//#include "mp3player.h"

// your title was too long it gave a warning
PSP_MODULE_INFO("Game Beta v2 - Animation", 0, 1, 1);

#define printf pspDebugScreenPrintf
#define RGB(r,g,b) ((r)|((g)<<8)|((b)<<16))
#define MAX(X,Y) ((X)>(Y)?(X):(Y))
#define NEXT_FRAME 32

int done = 0;

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common){
   done = 1;
   return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp){
   int cbid;
   
   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);
   
   sceKernelSleepThreadCB();
   
   return 0;
}

/* Sets up the callback thread and returns its threat id */
int SetupCallbacks(void){
   int thid;
   
   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0){
      sceKernelStartThread(thid, 0, 0);
   }
   
   return thid;
}

int main(){
   // Set the power frequency
   //scePowerSetClockFrequency(333, 333, 166);
   
   SetupCallbacks();
   pspDebugScreenInit();
   initGraphics();
   //pspAudioInit();
   
   SceCtrlData pad;
   //char hero_buffer[200]; // stores the path string to the png not the actual graphics
   /* char enemy_buffer[200]; */
   Image* ourHero; // pointer to where the hero graphics are stored in memory
   /* Image* ourEnemy; */
         
   //MP3_Init(1);
   //MP3_Load("laser_1.mp3");
     
   // you can get rid of the buffers and sprintfs unless you need
   // to create the path string in code for some reason

   //sprintf(hero_buffer, "walkseq.png");
   ourHero = loadImage("walkseq.png");
   
   /* animation variables */
   int rightSeq[4]={32, 64, 96, 128};
   int leftSeq[4]={160, 192, 224, 256};
   int frame = 0;
   int pos = rightSeq[frame];
   
   
   /* sprintf(enemy_buffer, "enemyDude.png");
   ourEnemy = loadImage(enemy_buffer); */
   
   if(!ourHero){
      //Image load failed
      printf("Image load failed!\n");
   }/*else if(!ourEnemy){
      //Image load failed
      printf("Image load failed!\n");
   }*/else{
      int hero_x = 0/*, enemy_x = 150*/;
      int hero_y = 0/*, enemy_y = 0*/;
      int bullet_x, bullet_y = 0;
           
      sceDisplayWaitVblankStart();
     
      while(!done){
         clearScreen(0);
         //fillScreenRect(RGB(255, 255, 255), 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
         blitAlphaImageToScreen(pos-32, 0, 32, 32, ourHero, hero_x, hero_y);
         /* blitAlphaImageToScreen(0, 0, 32, 32, ourEnemy, enemy_x, enemy_y); */
                 
         sceCtrlReadBufferPositive(&pad, 1);
         
         //meDude controls
         if(pad.Buttons & PSP_CTRL_LEFT){
            if(hero_x>0){hero_x--;}
            pos = leftSeq[frame];
           
            if(frame < 3){
               frame++;
            }else{
               frame = 0;
            }
           
         }

         if(pad.Buttons & PSP_CTRL_RIGHT){
            if(hero_x<(480-32)){hero_x++;}
            pos = rightSeq[frame];
           
            if(frame < 3){
               frame++;
            }else{
               frame = 0;
            }
               
         }
         
         if(pad.Buttons & PSP_CTRL_UP){
            if(hero_y>0){hero_y--;}
         }
         
         if(pad.Buttons & PSP_CTRL_DOWN){
            if(hero_y<(272-32)){hero_y++;}
         }
         if(pad.Buttons & PSP_CTRL_START) done = 1;
         
         
      /*    if(pad.Buttons & PSP_CTRL_SQUARE){
            if(enemy_x>0){enemy_x--;}
         } */

      /*   if(pad.Buttons & PSP_CTRL_CIRCLE){
            if(enemy_x<(480-32)){enemy_x++;}
         } */
         
      /*   if(pad.Buttons & PSP_CTRL_TRIANGLE){
            if(enemy_y>0){enemy_y--;}
         } */
         
         if(pad.Buttons & PSP_CTRL_CROSS){
            //MP3_Play();
            bullet_x = hero_x + 32;
            bullet_y = hero_y + 16;
            while(bullet_x < 480){
               putPixelScreen(0, bullet_x, bullet_y);
               bullet_x++;
               flipScreen();

            }

         }

         //if the MP3 finishes, stop it.
         //if(MP3_EndOfStream() == 1){
         //MP3_Stop();
         //}
         
         //sceDisplayWaitVblank();
         sceDisplayWaitVblankStart();
         flipScreen();
      }   
   
   }
   
   /* Stop MP3 & Free Memory */
   /*MP3_Stop();*/
   //MP3_FreeTune();

   sceKernelExitGame();

   //sceKernelSleepThread();
   return 0;
}


this works, just needs a delay like Raph also mentioned. if you cant figure it out maybe I will help you later.

for the transparency, you need to set that in the graphics program you are creating your .png in. I made one real quick that works fine (but graphics.h doesnt blend, only 100% transparent is skipped from drawing) but I dont have a place to upload it right now
Back to top
View user's profile Send private message
JesusXP



Joined: 17 Jan 2006
Posts: 80
Location: Ontario, Canada

PostPosted: Thu Mar 02, 2006 11:33 am    Post subject: Reply with quote

thanks Loco-san!! haha, Your always helping me on irc too.

I really appreciate it man! As soon as I get time, Im gonna try it out and report back to you!!

Thanks again.
Back to top
View user's profile Send private message MSN Messenger
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