Help with animation sequence...

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

Moderators: cheriff, TyRaNiD

Post Reply
JesusXP
Posts: 79
Joined: Tue Jan 17, 2006 11:16 am
Location: Ontario, Canada

Help with animation sequence...

Post by JesusXP »

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

#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&#40;"Game Beta - v2 - Animation Seq.", 0, 1, 1&#41;;

#define printf pspDebugScreenPrintf
#define RGB&#40;r,g,b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;&#41;
#define MAX&#40;X,Y&#41; &#40;&#40;X&#41;>&#40;Y&#41;?&#40;X&#41;&#58;&#40;Y&#41;&#41;
#define NEXT_FRAME 32

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;&#123;
	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;&#123;
	int cbid;
	
	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;
	
	sceKernelSleepThreadCB&#40;&#41;;
	
	return 0;
&#125;

/* Sets up the callback thread and returns its threat id */
int SetupCallbacks&#40;void&#41;&#123;
	int thid;
	
	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 main&#40;&#41;&#123;
	// Set the power frequency
	scePowerSetClockFrequency&#40;333, 333, 166&#41;;
	
	SetupCallbacks&#40;&#41;;
	pspDebugScreenInit&#40;&#41;;
	initGraphics&#40;&#41;;
	pspAudioInit&#40;&#41;;
	
	SceCtrlData pad;
	char hero_buffer&#91;200&#93;;
	/* char enemy_buffer&#91;200&#93;; */
	Image* ourHero;
	/* Image* ourEnemy; */
		
	MP3_Init&#40;1&#41;;
	MP3_Load&#40;"laser_1.mp3"&#41;;
		
	sprintf&#40;hero_buffer, "walkseq.png"&#41;;
	ourHero = loadImage&#40;hero_buffer&#41;;
	
	/* animation variables */
	int leftSeq&#91;4&#93;=&#123;32, 64, 96, 128&#125;;
	int rightSeq&#91;4&#93;=&#123;160, 192, 224, 256&#125;;
	int frame = 0;
	int pos = rightSeq&#91;frame&#93;;
	
	
	/* sprintf&#40;enemy_buffer, "enemyDude.png"&#41;;
	ourEnemy = loadImage&#40;enemy_buffer&#41;; */
	
	if&#40;!ourHero&#41;&#123;
		//Image load failed
		printf&#40;"Image load failed!\n"&#41;;
	&#125;/*else if&#40;!ourEnemy&#41;&#123;
		//Image load failed
		printf&#40;"Image load failed!\n"&#41;;
	&#125;*/else&#123;
		int hero_x = 0/*, enemy_x = 150*/;
		int hero_y = 0/*, enemy_y = 0*/;
		int bullet_x, bullet_y = 0;
				
		sceDisplayWaitVblankStart&#40;&#41;;
		
		while&#40;1&#41;&#123;
			clearScreen&#40;0&#41;;
			fillScreenRect&#40;RGB&#40;255, 255, 255&#41;, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT&#41;;
			blitAlphaImageToScreen&#40;pos-32, 0, pos, 32, ourHero, hero_x, hero_y&#41;;
			/* blitAlphaImageToScreen&#40;0, 0, 32, 32, ourEnemy, enemy_x, enemy_y&#41;; */
			
			
			sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
			
			//meDude controls
			if&#40;pad.Buttons & PSP_CTRL_LEFT&#41;&#123;
				if&#40;hero_x>0&#41;&#123;hero_x--;&#125;
				pos = leftSeq&#91;frame&#93;;
				
				if&#40;frame < 4&#41;&#123;
					frame++;
				&#125;else&#123;
					frame = 0;
				&#125;
				
			&#125;

			if&#40;pad.Buttons & PSP_CTRL_RIGHT&#41;&#123;
				if&#40;hero_x<&#40;480-32&#41;&#41;&#123;hero_x++;&#125;
				pos = rightSeq&#91;frame&#93;;
				
				if&#40;frame < 4&#41;&#123;
					frame++;
				&#125;else&#123;
					frame = 0;
				&#125;
					
			&#125;
			
			if&#40;pad.Buttons & PSP_CTRL_UP&#41;&#123;
				if&#40;hero_y>0&#41;&#123;hero_y--;&#125;
			&#125;
			
			if&#40;pad.Buttons & PSP_CTRL_DOWN&#41;&#123;
				if&#40;hero_y<&#40;272-32&#41;&#41;&#123;hero_y++;&#125;
			&#125;
			
			
		/* 	if&#40;pad.Buttons & PSP_CTRL_SQUARE&#41;&#123;
				if&#40;enemy_x>0&#41;&#123;enemy_x--;&#125;
			&#125; */

		/*	if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;&#123;
				if&#40;enemy_x<&#40;480-32&#41;&#41;&#123;enemy_x++;&#125;
			&#125; */
			
		/*	if&#40;pad.Buttons & PSP_CTRL_TRIANGLE&#41;&#123;
				if&#40;enemy_y>0&#41;&#123;enemy_y--;&#125;
			&#125; */
			
			if&#40;pad.Buttons & PSP_CTRL_CROSS&#41;&#123;
				MP3_Play&#40;&#41;; 
				bullet_x = hero_x + 32;
				bullet_y = hero_y + 16;
				while&#40;bullet_x < 480&#41;&#123;
					putPixelScreen&#40;0, bullet_x, bullet_y&#41;;
					bullet_x++;
					flipScreen&#40;&#41;;
					
				&#125;
					
			&#125; 
			
			//if the MP3 finishes, stop it.
			if&#40;MP3_EndOfStream&#40;&#41; == 1&#41;&#123;
			MP3_Stop&#40;&#41;;
			&#125;  
			
			sceDisplayWaitVblank&#40;&#41;;
			flipScreen&#40;&#41;;
		&#125;	
	
	&#125;
	
	/* Stop MP3 & Free Memory */
	/*MP3_Stop&#40;&#41;;*/
	MP3_FreeTune&#40;&#41;;
	
	sceKernelSleepThread&#40;&#41;;
	return 0;
&#125;
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

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.
JesusXP
Posts: 79
Joined: Tue Jan 17, 2006 11:16 am
Location: Ontario, Canada

Post by JesusXP »

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...
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..
Loco-san
Posts: 1
Joined: Thu Feb 09, 2006 2:11 pm

Post by Loco-san »

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

#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&#40;"Game Beta v2 - Animation", 0, 1, 1&#41;;

#define printf pspDebugScreenPrintf
#define RGB&#40;r,g,b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;&#41;
#define MAX&#40;X,Y&#41; &#40;&#40;X&#41;>&#40;Y&#41;?&#40;X&#41;&#58;&#40;Y&#41;&#41;
#define NEXT_FRAME 32

int done = 0;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;&#123;
   done = 1;
   return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;&#123;
   int cbid;
   
   cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
   sceKernelRegisterExitCallback&#40;cbid&#41;;
   
   sceKernelSleepThreadCB&#40;&#41;;
   
   return 0;
&#125;

/* Sets up the callback thread and returns its threat id */
int SetupCallbacks&#40;void&#41;&#123;
   int thid;
   
   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 main&#40;&#41;&#123;
   // Set the power frequency
   //scePowerSetClockFrequency&#40;333, 333, 166&#41;;
   
   SetupCallbacks&#40;&#41;;
   pspDebugScreenInit&#40;&#41;;
   initGraphics&#40;&#41;;
   //pspAudioInit&#40;&#41;;
   
   SceCtrlData pad;
   //char hero_buffer&#91;200&#93;; // stores the path string to the png not the actual graphics
   /* char enemy_buffer&#91;200&#93;; */
   Image* ourHero; // pointer to where the hero graphics are stored in memory
   /* Image* ourEnemy; */
         
   //MP3_Init&#40;1&#41;;
   //MP3_Load&#40;"laser_1.mp3"&#41;;
      
   // you can get rid of the buffers and sprintfs unless you need
   // to create the path string in code for some reason

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

         if&#40;pad.Buttons & PSP_CTRL_RIGHT&#41;&#123;
            if&#40;hero_x<&#40;480-32&#41;&#41;&#123;hero_x++;&#125;
            pos = rightSeq&#91;frame&#93;;
            
            if&#40;frame < 3&#41;&#123;
               frame++;
            &#125;else&#123;
               frame = 0;
            &#125;
               
         &#125;
         
         if&#40;pad.Buttons & PSP_CTRL_UP&#41;&#123;
            if&#40;hero_y>0&#41;&#123;hero_y--;&#125;
         &#125;
         
         if&#40;pad.Buttons & PSP_CTRL_DOWN&#41;&#123;
            if&#40;hero_y<&#40;272-32&#41;&#41;&#123;hero_y++;&#125;
         &#125;
         if&#40;pad.Buttons & PSP_CTRL_START&#41; done = 1;
         
         
      /*    if&#40;pad.Buttons & PSP_CTRL_SQUARE&#41;&#123;
            if&#40;enemy_x>0&#41;&#123;enemy_x--;&#125;
         &#125; */

      /*   if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;&#123;
            if&#40;enemy_x<&#40;480-32&#41;&#41;&#123;enemy_x++;&#125;
         &#125; */
         
      /*   if&#40;pad.Buttons & PSP_CTRL_TRIANGLE&#41;&#123;
            if&#40;enemy_y>0&#41;&#123;enemy_y--;&#125;
         &#125; */
         
         if&#40;pad.Buttons & PSP_CTRL_CROSS&#41;&#123;
            //MP3_Play&#40;&#41;;
            bullet_x = hero_x + 32;
            bullet_y = hero_y + 16;
            while&#40;bullet_x < 480&#41;&#123;
               putPixelScreen&#40;0, bullet_x, bullet_y&#41;;
               bullet_x++;
               flipScreen&#40;&#41;;

            &#125;

         &#125;

         //if the MP3 finishes, stop it.
         //if&#40;MP3_EndOfStream&#40;&#41; == 1&#41;&#123;
         //MP3_Stop&#40;&#41;;
         //&#125; 
         
         //sceDisplayWaitVblank&#40;&#41;;
         sceDisplayWaitVblankStart&#40;&#41;;
         flipScreen&#40;&#41;;
      &#125;   
   
   &#125;
   
   /* Stop MP3 & Free Memory */
   /*MP3_Stop&#40;&#41;;*/
   //MP3_FreeTune&#40;&#41;;

   sceKernelExitGame&#40;&#41;;

   //sceKernelSleepThread&#40;&#41;;
   return 0;
&#125;
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
JesusXP
Posts: 79
Joined: Tue Jan 17, 2006 11:16 am
Location: Ontario, Canada

Post by JesusXP »

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