Timers / Move sprite to position on screen?

SGDK only sub forum

Moderator: Stef

Post Reply
Kuroto
Interested
Posts: 46
Joined: Sat Mar 02, 2013 4:34 pm

Timers / Move sprite to position on screen?

Post by Kuroto »

Hi all,

What's the best way to move a sprite to a position on the screen?
Is there a way to use some kind of timer?

I tried doing it using for-loops but then you won't see the sprites move, it just gets placed on the right position directly, at least to the bare eye.

Thanks in advance! :)


-Steve
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

You should use the VDP_WaitVSync() method which allow you to wait for the end of frame. This method is very important to help you in synchronizing your game (ans so your sprite movement) on frame unit.
Kuroto
Interested
Posts: 46
Joined: Sat Mar 02, 2013 4:34 pm

Post by Kuroto »

Hi Stef,

I'm a bit confused.


This is my current code:

Code: Select all

    int i = 250;

    for (i; i>60; i--)
    {
        SPR_setPosition(&sprflappy, i, fix16ToInt(posy));
        VDP_waitVSync();
    }


My sprite just stays on the same position until (i assume) the for-loop is finished, then it updates with the new location.

Here's the rom so you know what i mean.

Or is there another way to move a sprite from one location to another?
I'm a bit confused on what WaitVSync really does.

Am i right in thinking that it waits until the next frame (vblank?) before going to the next line of code?

Thanks in advance :)
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

Indeed the waitVSync exactly does that ;)
The only missing part in your code is :

Code: Select all

SPR_update(&sprflappy, 1);
Just before the waitVSync call, this allow to send the specified sprite list to the VDP. You can have a look in the given "sprite" sample in SGDK to see how sprites methods work =)
Kuroto
Interested
Posts: 46
Joined: Sat Mar 02, 2013 4:34 pm

Post by Kuroto »

Allright, thx :)

I did try doing it like that, but it still does the same.

There's a great chance i'm missing something, as i'm actually learning C by coding this.

Any ideas?

I did copy a lot from the sprite example, and i'm now trying to understand what it does. For me that's the best way to learn a new programming language.

Here's the full code:

Code: Select all

#include <genesis.h>
#include "gfx.h"

#define ANIM_STATIC  0
#define ANIM_UP      1
#define ANIM_FALL    2

#define GRAVITY         FIX16(0.4)
#define MIN_POSX        FIX16(10)
#define MAX_POSX        FIX16(280)
#define MAX_POSY        FIX16(154)
#define SLIDESPEED      FIX16(6)
#define FALSE 0

void joyhandler(u16 joy, u16 changed, u16 state);
void titleHandler(u16 joy, u16 changed, u16 state);
void titleScreen();

// forward
static void disableInts();
static void enableInts();

static void handleInput();
static void joyEvent(u16 joy, u16 changed, u16 state);

static void updatePhysic();
static void updateAnim();

// sprite structure
Sprite sprflappy;

fix16 camposx;
fix16 camposy;
fix16 posx;
fix16 posy;
fix16 movx;
fix16 movy;
s16 xorder;
s16 yorder;

volatile int waitflag = FALSE;
int score = 0;

int main( )
{
    titleScreen();

   while(1)
   {

      updateAnim();

      //update sprites
      disableInts();
      SPR_update(&sprflappy, 1);
      enableInts();
      VDP_waitVSync();
   }

   return 0;
}


static void updatePhysic()
{
    //Physics handling goes here
}

static void updateAnim()
{
    SPR_setAnim(&sprflappy, ANIM_STATIC);
}


static void disableInts()
{
    if (!SYS_isInInterrupt()) SYS_disableInts();
}

static void enableInts()
{
    if (!SYS_isInInterrupt()) SYS_enableInts();
}

static void handleInput()
{
    //Input handling goes here
}

void flappyGame() {
    //Main game logic
    //Clear the A Plane to remove the logo and title screen text
    VDP_clearPlan(APLAN, FALSE);
    //stop playing music
    //SND_stopPlay_VGM();

    //move Flappy to the left of the screen to start in the right position
    int i = 250;

    for (i; i>60; i--)
    {

        SPR_setPosition(&sprflappy, i, fix16ToInt(posy));
        SPR_update(&sprflappy, 1);
        VDP_waitVSync();

    }


}

void titleHandler(u16 joy, u16 changed, u16 state) {
    switch (joy) {
    case JOY_1:
        if (state & BUTTON_START) {
            flappyGame();
        }
        break;
    }
}

void titleScreen() {
    //Title screen
    u16 tileIndex = TILE_USERINDEX;
    u16 palette[64];


    //play VGM
    SND_startPlay_VGM(mustitle);

    // set screen width
    VDP_setScreenWidth320();
    VDP_setPaletteColors(0, palette_black, 64);

    //Initialize the joy handler for the title screen
    JOY_init();
    JOY_setEventHandler(titleHandler);

    //draw the background
    VDP_drawImageEx(VDP_PLAN_B, &bgcity, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, tileIndex), 0, 0, TRUE, TRUE);
    tileIndex += bgcity.tileset->numTile;

    //draw the logo
    VDP_drawImageEx(VDP_PLAN_A, &gamelogo, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE, tileIndex), 7, 4, TRUE, TRUE);
    tileIndex += gamelogo.tileset->numTile;

    //Initialize sprite engine
    SPR_init(256);

    //Draw the title screen text
    VDP_drawText("* Press START to play! *", 8, 12);
    VDP_drawText("By Steve Gilissen (KUROTO)", 7, 26);
    VDP_drawText("Music: Chippin' by DEMICKXII", 6, 27);

    camposx = FIX16(0);
    camposy = FIX16(0);
    posx = FIX16(250);
    posy = FIX16(42); //was: MAX_POSY;
    movx = FIX16(0);
    movy = FIX16(0);
    xorder = 0;
    yorder = 0;

    // initialize the sprite (Flappy! :D)
    SPR_initSprite(&sprflappy, &flappy_sprite, fix16ToInt(posx), fix16ToInt(posy), TILE_ATTR(PAL0, TRUE, FALSE, FALSE));
    SPR_update(&sprflappy, 1);

    // prepare the palette
    memcpy(&palette[0], bgcity.palette->data, 16*2);
    memcpy(&palette[16], gamelogo.palette->data, 16*2);
    memcpy(&palette[32], flappy_sprite.palette->data, 16*2);


}
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

The problem comes from the fact you call

Code: Select all

flappyGame(); 
in the JOY event handler. The JOY event handler is called in the V Interrupt method so you will be remain in the V Interrupt forever and automatic processes as sprites, DMA... done by SGDK during VInt won't happen anymore.

It's better to design your code this way :

Code: Select all

 state = INTRO;

 while(TRUE)
 {
   switch(state)
   {
      case INTRO:
        updateIntroAnim();

        //update sprites
        disableInts();
        SPR_update(&sprflappy, 1);
        enableInts();
        VDP_waitVSync();
        break;

      case GAME:
        if (!game_initialized)
        {
           game_init();
           game_initialized = TRUE;
        }

        updateGameAnim();

        //update sprites
        disableInts();
        SPR_update(&sprflappy, 1);
        enableInts();
        VDP_waitVSync();
        break;
    }
 } 
Well actually there is so much way of doing it but that is just to give you the idea...
Kuroto
Interested
Posts: 46
Joined: Sat Mar 02, 2013 4:34 pm

Post by Kuroto »

I'm terribly sorry, but i can't really wrap my head around it i'm afraid :?

What exactly is "state"?
I tried making an INT out of it and defining them, but i don't think that's what i'm supposed to do, right?

Code: Select all

#include <genesis.h>
#include "gfx.h"

#define ANIM_STATIC  0
#define ANIM_UP      1
#define ANIM_FALL    2

#define GRAVITY         FIX16(0.4)
#define MIN_POSX        FIX16(10)
#define MAX_POSX        FIX16(280)
#define MAX_POSY        FIX16(154)
#define SLIDESPEED      FIX16(6)
#define FALSE 0

#define TITLE   0
#define INTRO   1
#define GAME    2


// forward
static void disableInts();
static void enableInts();

static void joyEvent(u16 joy, u16 changed, u16 state);

// sprite structure
Sprite sprflappy;

fix16 camposx;
fix16 camposy;
fix16 posx;
fix16 posy;
fix16 movx;
fix16 movy;
s16 xorder;
s16 yorder;


volatile int waitflag = FALSE;
int score = 0;

int main()

{
//Title screen
    u16 tileIndex = TILE_USERINDEX;
    u16 palette[64];


    //play VGM
    SND_startPlay_VGM(mustitle);

    // set screen width
    VDP_setScreenWidth320();
    VDP_setPaletteColors(0, palette_black, 64);

    //Initialize the joy handler for the title screen
    JOY_init();


    //draw the background
    VDP_drawImageEx(VDP_PLAN_B, &bgcity, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, tileIndex), 0, 0, TRUE, TRUE);
    tileIndex += bgcity.tileset->numTile;

    //draw the logo
    VDP_drawImageEx(VDP_PLAN_A, &gamelogo, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE, tileIndex), 7, 4, TRUE, TRUE);
    tileIndex += gamelogo.tileset->numTile;

    //Initialize sprite engine
    SPR_init(256);

    //Draw the title screen text
    VDP_drawText("* Press START to play! *", 8, 12);
    VDP_drawText("By Steve Gilissen (KUROTO)", 7, 26);
    VDP_drawText("Music: Chippin' by DEMICKXII", 6, 27);

    camposx = FIX16(0);
    camposy = FIX16(0);
    posx = FIX16(250);
    posy = FIX16(42); //was: MAX_POSY;
    movx = FIX16(0);
    movy = FIX16(0);
    xorder = 0;
    yorder = 0;

    // initialize the sprite (Flappy! :D)
    SPR_initSprite(&sprflappy, &flappy_sprite, fix16ToInt(posx), fix16ToInt(posy), TILE_ATTR(PAL0, TRUE, FALSE, FALSE));
    SPR_update(&sprflappy, 1);

    // prepare the palette
    memcpy(&palette[0], bgcity.palette->data, 16*2);
    memcpy(&palette[16], gamelogo.palette->data, 16*2);
    memcpy(&palette[32], flappy_sprite.palette->data, 16*2);
    JOY_setEventHandler(joyEvent);


int state = TITLE;

 while(TRUE)
 {
   switch(state)
   {
      case INTRO:
        //updateIntroAnim();
        //Clear the A Plane to remove the logo and title screen text
        VDP_clearPlan(APLAN, FALSE);
        //stop playing music
        //SND_stopPlay_VGM();

        //move Flappy to the left of the screen to start in the right position
        int i = 250;

        for (i; i>60; i--)
        {
        SPR_setPosition(&sprflappy, i, fix16ToInt(posy));
        }
        //update sprites
        disableInts();
        SPR_update(&sprflappy, 1);
        enableInts();
        VDP_waitVSync();
        break;

      case GAME:

        //updateGameAnim();

        //update sprites
        disableInts();
        SPR_update(&sprflappy, 1);
        enableInts();
        VDP_waitVSync();
        break;


      case TITLE:

        disableInts();
        SPR_update(&sprflappy, 1);
        enableInts();
        VDP_waitVSync();
        break;

    }
 }

return 0;

}




static void disableInts()
{
    if (!SYS_isInInterrupt()) SYS_disableInts();
}

static void enableInts()
{
    if (!SYS_isInInterrupt()) SYS_enableInts();
}



static void joyEvent(u16 joy, u16 changed, u16 state)
{
    // START button state changed
    if (changed & BUTTON_START)
    {
        state = INTRO;
    }

    if (changed & state & (BUTTON_A | BUTTON_B | BUTTON_C))
    {

    }
}


Definitely doing it wrong right now i guess :p

I did check your sprite example again. You're handling all the while(TRUE) loop in int main(); if i'm reading the code correctly.

I'm trying to do the same, but this doesn't seem to work for me.
I'm guessing there's something wrong with the state switch, as the start button is being handled correctly. I tested this by putting a VDP_drawText in the joyEvent handler.
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

Oh well, my "state" var was just an example about how handling it but if you are not comfortable with it you can do the way you want. You just have to avoid to do the process in the JOY event handler and never return from it or you will lock the VInt process and many things won't work anymore (as the sprite engine in SGDK). So you can use the JOY event just to change a variable state and from this state do the job corresponding, that is the idea :)
Kuroto
Interested
Posts: 46
Joined: Sat Mar 02, 2013 4:34 pm

Post by Kuroto »

Hi Stef,

I got the majority of it working. Thanks again for the help! :)

Now i've got a different problem.
I'm now drawing multiple sprites on screen after the loop.

This is the code:

Code: Select all

 while(TRUE)
 {
   switch(gamestate)
   {
      case INTRO:
        //updateIntroAnim();
        //Clear the A Plane to remove the logo and title screen text
        VDP_clearPlan(APLAN, FALSE);


        //move Flappy to the left of the screen to start in the right position
        for (flappyTitlePosition; flappyTitlePosition>0; flappyTitlePosition--)
        {
            SPR_setPosition(&sprites[0], flappyTitlePosition, fix16ToInt(posy));
            SPR_update(&sprites, 2);
            VDP_waitVSync();
        }
        //VDP_waitVSync();
        //stop playing music
        //SND_stopPlay_VGM();

        SPR_initSprite(&sprites[1], &flappy_sprite, 140, 80, TILE_ATTR(PAL0, TRUE, FALSE, FALSE));
        SPR_setAnim(&sprites[1], ANIM_INSTR);
        SPR_update(&sprites, 2);
        VDP_waitVSync();
        JOY_setEventHandler(joyEvent);


        break;
However, it should be an animated sprite (4 frames). The first sprite (which i moved using the loop) is animated just fine, but the second one isn't. It just displays the first frame.

Here's the rom

Any ideas?

Thanks in advance!
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

The SPR_update(..) is enough to get the sprite to animate. You should verify your flappy_sprite definition : the "timer" filed should be != 0 to enable auto animation, also you should have different animation frames in your image but i guess that is the case.
I don't understand your code, are you initializing the sprites[1] each time ? that might be the problem.
Kuroto
Interested
Posts: 46
Joined: Sat Mar 02, 2013 4:34 pm

Post by Kuroto »

Hi Stef,

I'm actually using 1 sprite definition for multiple sprites, as i saw in a different thread on this forum.

However, when i tried that i got several error message when compiling for some reason.

My current code:

Code: Select all

#include <genesis.h>
#include "gfx.h"

#define ANIM_STATIC  0
#define ANIM_SCORE   1
#define ANIM_BRONZE  2
#define ANIM_SILVER  3
#define ANIM_GOLD    4
#define ANIM_PLATIN  5
#define ANIM_INSTR   6

#define GRAVITY         FIX16(0.4)
#define MIN_POSX        FIX16(10)
#define MAX_POSX        FIX16(280)
#define MAX_POSY        FIX16(154)
#define SLIDESPEED      FIX16(6)
#define FALSE 0

#define TITLE   0
#define INTRO   1
#define GAME    2

#define MAX_SPRITE 16

// forward
static void disableInts();
static void enableInts();

static void joyEvent(u16 joy, u16 changed, u16 state);

// sprite structure
//Sprite allsprites[MAX_SPRITE];
//Sprite sprflappy = &allsprites[0];
//Sprite sprinstr = &allsprites[1];
Sprite sprites[MAX_SPRITE];
//Sprite sprflappy = &sprites[0];

fix16 camposx;
fix16 camposy;
fix16 posx;
fix16 posy;
fix16 movx;
fix16 movy;
s16 xorder;
s16 yorder;

int gamestate = TITLE;
int music_status;


volatile int waitflag = FALSE;
int score = 0;
int flappyTitlePosition = 250;
char flappyPosString;

int main()

{
//Title screen
    u16 tileIndex = TILE_USERINDEX;
    u16 palette[64];



    //play VGM
    SND_startPlay_VGM(mustitle);

    // set screen width
    VDP_setScreenWidth320();
    VDP_setPaletteColors(0, palette_black, 64);

    //Initialize the joy handler for the title screen
    JOY_init();


    //draw the background
    VDP_drawImageEx(VDP_PLAN_B, &bgcity, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, tileIndex), 0, 0, TRUE, TRUE);
    tileIndex += bgcity.tileset->numTile;

    //draw the logo
    VDP_drawImageEx(VDP_PLAN_A, &gamelogo, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE, tileIndex), 7, 4, TRUE, TRUE);
    tileIndex += gamelogo.tileset->numTile;

    //Initialize sprite engine
    SPR_init(256);

    //Draw the title screen text
    VDP_drawText("* Press START to play! *", 8, 12);
    VDP_drawText("By Steve Gilissen (KUROTO)", 7, 26);
    VDP_drawText("Music: Chippin' by DEMICKXII", 6, 27);

    camposx = FIX16(0);
    camposy = FIX16(0);
    posx = FIX16(250);
    posy = FIX16(42); //was: MAX_POSY;
    movx = FIX16(0);
    movy = FIX16(0);
    xorder = 0;
    yorder = 0;

    // initialize the sprite (Flappy! :D)
    SPR_initSprite(&sprites[0], &flappy_sprite, fix16ToInt(posx), fix16ToInt(posy), TILE_ATTR(PAL0, TRUE, FALSE, FALSE));
    SPR_update(&sprites[0], 1);


    // prepare the palette
    memcpy(&palette[0], bgcity.palette->data, 16*2);
    memcpy(&palette[16], gamelogo.palette->data, 16*2);
    memcpy(&palette[32], flappy_sprite.palette->data, 16*2);
    JOY_setEventHandler(joyEvent);




 while(TRUE)
 {
   switch(gamestate)
   {
      case INTRO:
        //updateIntroAnim();
        //Clear the A Plane to remove the logo and title screen text
        VDP_clearPlan(APLAN, FALSE);


        //move Flappy to the left of the screen to start in the right position
        for (flappyTitlePosition; flappyTitlePosition>0; flappyTitlePosition--)
        {
            SPR_setPosition(&sprites[0], flappyTitlePosition, fix16ToInt(posy));
            SPR_update(&sprites, 2);
            VDP_waitVSync();
        }
        //VDP_waitVSync();
        //stop playing music
        //SND_stopPlay_VGM();

        SPR_initSprite(&sprites[1], &flappy_sprite, 140, 80, TILE_ATTR(PAL0, TRUE, FALSE, FALSE));
        SPR_setAnim(&sprites[1], ANIM_INSTR);
        SPR_update(&sprites, 2);
        VDP_waitVSync();
        JOY_setEventHandler(joyEvent);


        break;

      case GAME:

        //updateGameAnim();

        //update sprites
        disableInts();
        SPR_update(&sprites[0], 1);
        enableInts();
        VDP_waitVSync();
        break;


      case TITLE:

        disableInts();
        SPR_update(&sprites[0], 1);
        enableInts();
        VDP_waitVSync();
        break;

    }
 }

return 0;

}




static void disableInts()
{
    if (!SYS_isInInterrupt()) SYS_disableInts();
}

static void enableInts()
{
    if (!SYS_isInInterrupt()) SYS_enableInts();
}


static void introJoyEvent(u16 joy, u16 changed, u16 state)
{
    // START button state changed
    if (changed & BUTTON_START)
    {

    }

    if (changed & state & (BUTTON_A | BUTTON_B | BUTTON_C))
    {
        gamestate = GAME;

    }
}
static void joyEvent(u16 joy, u16 changed, u16 state)
{
    // START button state changed
    if (changed & BUTTON_START)
    {
        gamestate = INTRO;
    }

    if (changed & state & (BUTTON_A | BUTTON_B | BUTTON_C))
    {


    }
}
When i use the commented lines in the sprite structure, i get an "Invalid initializer" error in the build log.
Kuroto
Interested
Posts: 46
Joined: Sat Mar 02, 2013 4:34 pm

Post by Kuroto »

Update:

When i use the following code, it doesn't raise an error anymore, but the sprites aren't drawn :?

Code: Select all

Sprite sprites[MAX_SPRITE];
Sprite *sprflappy = &sprites[0];
Sprite *sprinstr = &sprites[1];

Code: Select all

    // initialize the sprite (Flappy! :D)
    SPR_initSprite(&sprflappy, &flappy_sprite, fix16ToInt(posx), fix16ToInt(posy), TILE_ATTR(PAL1, TRUE, FALSE, FALSE));
    SPR_update(&sprites, 1);
Kuroto
Interested
Posts: 46
Joined: Sat Mar 02, 2013 4:34 pm

Post by Kuroto »

OK, I think I "solved" it.
I noticed that it would be animated when I put it in a different switch case.

So I've created an "instructions" case, and put all the required code there.

Might be a bit of a dirty "hack", but it works I guess...
No idea why though. Maybe I'm missing something...

Code: Select all

#include <genesis.h>
#include "gfx.h"

#define ANIM_STATIC  0
#define ANIM_SCORE   1
#define ANIM_BRONZE  2
#define ANIM_SILVER  3
#define ANIM_GOLD    4
#define ANIM_PLATIN  5
#define ANIM_INSTR   6

#define GRAVITY         FIX16(0.4)
#define MIN_POSX        FIX16(10)
#define MAX_POSX        FIX16(280)
#define MAX_POSY        FIX16(154)
#define SLIDESPEED      FIX16(6)
#define FALSE 0

#define TITLE   0
#define INTRO   1
#define GAME    2
#define INSTRUCTIONS 3

#define MAX_SPRITE 16

// forward
static void disableInts();
static void enableInts();

static void joyEvent(u16 joy, u16 changed, u16 state);

static void introJoyEvent(u16 joy, u16 changed, u16 state);
static void gameJoyEvent(u16 joy, u16 changed, u16 state);
// sprite structure
//Sprite allsprites[MAX_SPRITE];
//Sprite sprflappy = &allsprites[0];
//Sprite sprinstr = &allsprites[1];
Sprite sprites[MAX_SPRITE];
//Sprite sprflappy = &sprites[0];

fix16 camposx;
fix16 camposy;
fix16 posx;
fix16 posy;
fix16 movx;
fix16 movy;
s16 xorder;
s16 yorder;

int gamestate = TITLE;
int music_status;


volatile int waitflag = FALSE;
int score = 0;
int flappyTitlePosition = 250;
char flappyPosString;

int main()

{
//Title screen
    u16 tileIndex = TILE_USERINDEX;
    u16 palette[64];



    //play VGM
    SND_startPlay_VGM(mustitle);

    // set screen width
    VDP_setScreenWidth320();
    VDP_setPaletteColors(0, palette_black, 64);

    //Initialize the joy handler for the title screen
    JOY_init();


    //draw the background
    VDP_drawImageEx(VDP_PLAN_B, &bgcity, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, tileIndex), 0, 0, TRUE, TRUE);
    tileIndex += bgcity.tileset->numTile;

    //draw the logo
    VDP_drawImageEx(VDP_PLAN_A, &gamelogo, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE, tileIndex), 7, 4, TRUE, TRUE);
    tileIndex += gamelogo.tileset->numTile;

    //Initialize sprite engine
    SPR_init(256);

    //Draw the title screen text
    VDP_drawText("* Press START to play! *", 8, 12);
    VDP_drawText("By Steve Gilissen (KUROTO)", 7, 26);
    VDP_drawText("Music: Chippin' by DEMICKXII", 6, 27);

    camposx = FIX16(0);
    camposy = FIX16(0);
    posx = FIX16(250);
    posy = FIX16(42); //was: MAX_POSY;
    movx = FIX16(0);
    movy = FIX16(0);
    xorder = 0;
    yorder = 0;

    // initialize the sprite (Flappy! :D)
    SPR_initSprite(&sprites[0], &flappy_sprite, fix16ToInt(posx), fix16ToInt(posy), TILE_ATTR(PAL0, TRUE, FALSE, FALSE));
    SPR_update(&sprites[0], 1);


    // prepare the palette
    memcpy(&palette[0], bgcity.palette->data, 16*2);
    memcpy(&palette[16], gamelogo.palette->data, 16*2);
    memcpy(&palette[32], flappy_sprite.palette->data, 16*2);
    JOY_setEventHandler(joyEvent);




 while(TRUE)
 {
   switch(gamestate)
   {
      case INTRO:
        //updateIntroAnim();
        //Clear the A Plane to remove the logo and title screen text
        VDP_clearPlan(APLAN, FALSE);


        //move Flappy to the left of the screen to start in the right position
        for (flappyTitlePosition; flappyTitlePosition>0; flappyTitlePosition--)
        {
            SPR_setPosition(&sprites[0], flappyTitlePosition, fix16ToInt(posy));
            SPR_update(&sprites, 2);
            VDP_waitVSync();
        }
        //VDP_waitVSync();
        //stop playing music
        //SND_stopPlay_VGM();

        SPR_initSprite(&sprites[1], &flappy_sprite2, 140, 80, TILE_ATTR(PAL0, TRUE, FALSE, FALSE));
        SPR_setAnim(&sprites[1], ANIM_INSTR);

        SPR_update(&sprites, 2);

        VDP_waitVSync();

        gamestate = INSTRUCTIONS;


        break;

      case GAME:

        //updateGameAnim();
        SPR_setPosition(&sprites[1], -100, -100);
        VDP_drawText("Score:", 0, 0);
        //SND_stopPlay_VGM();

        //update sprites
        //disableInts();
        SPR_update(&sprites, 2);
        //enableInts();
        VDP_waitVSync();
        JOY_setEventHandler(gameJoyEvent);
        break;

      case INSTRUCTIONS:

        //updateGameAnim();
        JOY_setEventHandler(introJoyEvent);


        //update sprites
        //disableInts();
        SPR_update(&sprites, 2);

        //enableInts();
        VDP_waitVSync();
        break;


      case TITLE:

        disableInts();
        SPR_update(&sprites, 1);
        enableInts();
        VDP_waitVSync();
        break;


    }
 }

return 0;

}




static void disableInts()
{
    if (!SYS_isInInterrupt()) SYS_disableInts();
}

static void enableInts()
{
    if (!SYS_isInInterrupt()) SYS_enableInts();
}


static void introJoyEvent(u16 joy, u16 changed, u16 state)
{
    // START button state changed
    if (changed & BUTTON_START)
    {

    }

    if (changed & state & (BUTTON_A | BUTTON_B | BUTTON_C))
    {
        gamestate = GAME;

    }
}

static void gameJoyEvent(u16 joy, u16 changed, u16 state)
{
    // START button state changed
    if (changed & BUTTON_START)
    {

    }

    if (changed & state & (BUTTON_A | BUTTON_B | BUTTON_C))
    {
        SND_playSfx_VGM((u32) sndFlap, sizeof(sndFlap));

    }
}

static void joyEvent(u16 joy, u16 changed, u16 state)
{
    // START button state changed
    if (changed & BUTTON_START)
    {
        gamestate = INTRO;
    }

    if (changed & state & (BUTTON_A | BUTTON_B | BUTTON_C))
    {


    }
}
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

Actually it does not look as a hack, i think you finally found the way to design it :)
Kuroto
Interested
Posts: 46
Joined: Sat Mar 02, 2013 4:34 pm

Post by Kuroto »

I guess the best way to learn things is to break them and repair them again... :lol:
Post Reply