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 

Game background is flashing, character leaving trails

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



Joined: 19 Jul 2008
Posts: 15

PostPosted: Sat Jul 19, 2008 1:28 am    Post subject: Game background is flashing, character leaving trails Reply with quote

Hello :)
I'm really new to PSP programming, but I've done a little C++ before.
Anyway, I'm having trouble with my game.
I'm trying to display a background and be able to move a character on the screen.
So I made the background and the character in photoshop, saved as PNG.
The character is transparent at some pixels.
But the background is flashing and the character leaves a trail after itself.
It's not removing the previously drawn character.
Here's the code:

Code:
// C-Application, png test

#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>
#include "graphics.h"

#define printf pspDebugScreenPrintf
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))

PSP_MODULE_INFO("Png image display in C", 0, 1, 1);
/* 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 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 main()
{
    char buffer[200];
    Image* ourImage;
    pspDebugScreenInit();
    SetupCallbacks();
    initGraphics();
    SceCtrlData pad;
    int x=480/2;
    int y=272/2;
    int y2=0;
    int x2=0;
   
    sprintf(buffer, "Image.png");
    ourImage = loadImage(buffer);
    if (!ourImage)
    {
       printf("Image load failed!\n");
    }
    else
    {
         sceDisplayWaitVblankStart();
         blitAlphaImageToScreen(0 ,0 ,480 , 272, ourImage, 0, 0);
         flipScreen();
    }
    while(1)
    {
        sceCtrlReadBufferPositive(&pad, 1);
           
        sprintf(buffer, "Eye.png");
        ourImage = loadImage(buffer);
        if (!ourImage)
        {
           printf("Image load failed!\n");
        }
        else
        {
             sceDisplayWaitVblankStart();
             blitAlphaImageToScreen(0 ,0 ,16 , 16, ourImage, x, y);
             flipScreen();
        }
       
        if(pad.Buttons & PSP_CTRL_DOWN)
                       y++;
               
        if(pad.Buttons & PSP_CTRL_UP)
                       y--;
                       
        if(pad.Buttons & PSP_CTRL_RIGHT)
                       x++;
               
        if(pad.Buttons & PSP_CTRL_LEFT)
                       x--;
    }
    sceKernelSleepThread();
    return 0;
}


Background:

PataPon style :)
Character:




Do you know how to fix this?
Back to top
View user's profile Send private message MSN Messenger
hibbyware



Joined: 28 Mar 2007
Posts: 78

PostPosted: Sat Jul 19, 2008 2:44 am    Post subject: Reply with quote

Blit the background every loop not just once before the loop,

Also you should load the images once before your while loop not multiple times within the while loop,
Back to top
View user's profile Send private message
Acegikmo



Joined: 19 Jul 2008
Posts: 15

PostPosted: Sat Jul 19, 2008 3:42 am    Post subject: Reply with quote

How would I write the code then?
I'm not sure how the image loading works
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Jul 19, 2008 3:58 am    Post subject: Reply with quote

Code:
    sprintf(buffer, "Image.png");
    backImage = loadImage(buffer);
    if (!backImage)
    {
       printf("Background Image load failed!\n");
    }
    sprintf(buffer, "Eye.png");
    eyeImage = loadImage(buffer);
    if (!eyeImage)
    {
       printf("Eye Image load failed!\n");
    }

    while(backImage && eyeImage)
    {
        blitImageToScreen(0 ,0 ,480 , 272, backImage, 0, 0);
        blitAlphaImageToScreen(0 ,0 ,16 , 16, eyeImage, x, y);
        sceDisplayWaitVblankStart();
        flipScreen();
       
        sceCtrlReadBufferPositive(&pad, 1);
        if(pad.Buttons & PSP_CTRL_DOWN)
                       y++;
               
        if(pad.Buttons & PSP_CTRL_UP)
                       y--;
                       
        if(pad.Buttons & PSP_CTRL_RIGHT)
                       x++;
               
        if(pad.Buttons & PSP_CTRL_LEFT)
                       x--;
    }
Back to top
View user's profile Send private message AIM Address
Acegikmo



Joined: 19 Jul 2008
Posts: 15

PostPosted: Sat Jul 19, 2008 4:04 am    Post subject: Reply with quote

Thanks, I'll try that :)
Back to top
View user's profile Send private message MSN Messenger
Acegikmo



Joined: 19 Jul 2008
Posts: 15

PostPosted: Sat Jul 19, 2008 4:30 am    Post subject: Reply with quote

Works great!
What method is the best way to make a character fire bullets?
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Jul 19, 2008 4:50 am    Post subject: Reply with quote

Acegikmo wrote:
Works great!
What method is the best way to make a character fire bullets?


Same as the eye - just start the coords at the player position + an offset for where his gun is, then move the bullet in a constant direction at a constant speed until it hits something or reaches its limit. If it's something more than just a dot, you'll need different versions of the image that show the bullet in the proper rotation for the direction it's headed. If it were something like a fireball, you might have different images that make it pulse or rotate. You'd just cycle through those images as the thing moved.
Back to top
View user's profile Send private message AIM Address
Acegikmo



Joined: 19 Jul 2008
Posts: 15

PostPosted: Sat Jul 19, 2008 7:08 am    Post subject: Reply with quote

I just don't know how to do it.
One bullet is fine.
One piece of code to loop through all the time.
But multiple bullets is a problem; I don't know where to start.

Should I make a Structure like this?

Code:
typedef struct
    {
            int x=0;
            int y=0;
            bool on=0;
    } Bullet;


Could you make a simple example where the character drops a bomb that falls and disappears when it reaches the bottom of the screen?

Here's the code so far:


Code:
int main()
{
    char buffer[200];
    Image* backImage;
    Image* eyeImage;
    pspDebugScreenInit();
    SetupCallbacks();
    initGraphics();
    SceCtrlData pad;
    int ex=480/2;
    int ey=272/2;
//    typedef struct
//    {
//            int x=0;
//            int y=0;
//            bool on=0;
//    } Bullet;
   
    sprintf(buffer, "Image.png");
    backImage = loadImage(buffer);
    if (!backImage)
    {
       printf("Background Image load failed!\n");
    }
    sprintf(buffer, "Eye.png");
    eyeImage = loadImage(buffer);
    if (!eyeImage)
    {
       printf("Eye Image load failed!\n");
    }
    double i=0;
    while(backImage && eyeImage)
    {
        blitImageToScreen(0 ,0 ,480 , 272, backImage, 0, 0);
        blitAlphaImageToScreen(0 ,0 ,16 , 16, eyeImage, ex, ey);
        sceDisplayWaitVblankStart();
        flipScreen();
        sceCtrlReadBufferPositive(&pad, 1);
        if(pad.Buttons & PSP_CTRL_DOWN)
                       ey+=1;
               
        if(pad.Buttons & PSP_CTRL_UP)
                       ey-=1;
                       
        if(pad.Buttons & PSP_CTRL_RIGHT)
                       ex+=1;
               
        if(pad.Buttons & PSP_CTRL_LEFT)
                       ex-=1;
    }
    sceKernelSleepThread();
    return 0;
}
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Jul 19, 2008 8:18 am    Post subject: Reply with quote

Code:
#define NUM_BOMBS 4

struct bomb {
    int x, y;
    /* extend as you add stuff */
};

/* global so that you can access from any function */
int ex=480/2;
int ey=272/2;
struct bomb bombs[NUM_BOMBS];

int main()
{
    char buffer[200];
    Image* backImage;
    Image* eyeImage;
    Image* bombImage;
    int all_images;
    SceCtrlData pad;
    unsigned int old_buttons = 0;
    int current_bomb = 0;

    pspDebugScreenInit();
    SetupCallbacks();
    initGraphics();

    /* init game objects */
    for (i=0; i<NUM_BOMBS; i++) {
        bombs[i].x = bombs[i].y = -1;
    }

    /* init images */
    sprintf(buffer, "Image.png");
    backImage = loadImage(buffer);
    if (!backImage)
    {
       printf("Background Image load failed!\n");
    }
    sprintf(buffer, "Eye.png");
    eyeImage = loadImage(buffer);
    if (!eyeImage)
    {
       printf("Eye Image load failed!\n");
    }
    sprintf(buffer, "Bomb.png");
    bombImage = loadImage(buffer);
    if (!bombImage)
    {
       printf("Bomb Image load failed!\n");
    }
    all_images = backImage && eyeImage && bombImage;

    while(all_images)
    {
        int i;
        unsigned int new_buttons;

        /* get inputs */
        sceCtrlReadBufferPositive(&pad, 1);
        new_buttons = pad.Buttons ^ old_buttons;
        old_buttons = pad.Buttons;

        /* process inputs */
        if(pad.Buttons & PSP_CTRL_SELECT)
            break; /* exit look when press select */
        if(pad.Buttons & PSP_CTRL_DOWN)
                       ey+=1;
        if(pad.Buttons & PSP_CTRL_UP)
                       ey-=1;
        if(pad.Buttons & PSP_CTRL_RIGHT)
                       ex+=1;
        if(pad.Buttons & PSP_CTRL_LEFT)
                       ex-=1;
        if(new_buttons & PSP_CTRL_CROSS && pad.Buttons & PSP_CTRL_CROSS)
        {
            /* if you "fire" more than NUM_BOMBS, the first one(s) will just
              disappear from where they are and become the new bomb(s) */
            bombs[current_bomb].x = ex;
            bombs[current_bomb].y = ey+8;
            current_bomb = (current_bomb+1) % NUM_BOMBS;
        }

        /* process game objects */
        for (i=0; i<NUM_BOMBS; i++) {
            if (bombs[i].x >= 0) {
                bombs[i].y++;
                if (bombs[i].y > (272-16))
                    bombs[i].x = bombs[i].y = -1;
                /* you would do some kind of collision detection in an else here */
            }
        }

        /* draw stuff */
        blitImageToScreen(0 ,0 ,480 , 272, backImage, 0, 0);
        blitAlphaImageToScreen(0 ,0 ,16 , 16, eyeImage, ex, ey);
        for (i=0; i<NUM_BOMBS; i++)
            if (bombs[i].x >= 0)
                blitAlphaImageToScreen(0 ,0 ,16 , 16, bombImage, bombs[i].x, bombs[i].y);

        sceDisplayWaitVblankStart();
        flipScreen();
    }
    sceKernelSleepThread();
    return 0;
}
Back to top
View user's profile Send private message AIM Address
Acegikmo



Joined: 19 Jul 2008
Posts: 15

PostPosted: Sat Jul 19, 2008 10:05 am    Post subject: Reply with quote

Great, thanks :)
Just a few questions.

What does this mean?

bombs[i].x = bombs[i].y = -1;

And what's this?

unsigned int old_buttons = 0;
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Jul 19, 2008 12:35 pm    Post subject: Reply with quote

Acegikmo wrote:
Great, thanks :)
Just a few questions.

What does this mean?

bombs[i].x = bombs[i].y = -1;

And what's this?

unsigned int old_buttons = 0;


The first is setting the bomb x/y coords to something that can never happen as a marker that the bomb is not in use. We can skip it if the coord is less than 0.

The second is part of a better way to check your buttons. Instead of just looking at pad.Buttons, you want to XOR the previous buttons with the current ones to make new_buttons - a variable that tells you which buttons just changed. That way you only have to look at a button when it changes. For example in the code

Code:
if(new_buttons & PSP_CTRL_CROSS && pad.Buttons & PSP_CTRL_CROSS)


That says look at CROSS only if it changed, and that the current value is pressed. That way we only fire once each time you press X. So we start the previous buttons (old_buttons) out at 0. Notice that each time through the loop, it is set to pad.Buttons for the next time through the loop.
Back to top
View user's profile Send private message AIM Address
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