gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > n00b problem

#4392 - Daikath - Sat Mar 29, 2003 6:16 pm

Hi, I am reasonable new to programming, I am making a small demo wherein a couple of balls are on screen and you get to control one ball and the rest move along as though they are connected with a rubberband, this is just practice code to get the hang of it but when I move one ball around on screen the second just stays put. This is the function I use for the second ball to move.
Code:
void Elastiek()
{   
   s16 var1;
   var1 = 0;
   var1 = x - x2;
   /*s16 acceleration;
   s16 speed;*/
   s8 xleft;
   s8 xright;
   s8 possibility;
   u8 trigger;
   /*acceleration = 0;
   speed = 0;*/
   xleft = 0;
   xright = 0;
   possibility = 0;
   trigger = 0;
   if ((var1 < 0) & (trigger == 0))
   {
      possibility = 1;
   }
   if ((var1 > 0) & (trigger == 0))
   {
      possibility = 2;
   }
   switch (possibility)
   {
   case '1':
   {
      trigger = 1;
      xleft = 0 - var1;
      xright = var1 - var1 - var1 - 3;
      while (trigger == 1)
      {
      
         if (x2 > xleft)
         {
            x2--;
         }
         trigger = 2;
      }
      while (trigger == 2)
      {
         if (x2 < xright)
         {
            x2++;
         }
         trigger = 0;
      }
break;
   }
   case '2':
      {
         trigger = 3;
         xright = 0 + var1;
         xleft = var1 - var1 - var1 + 3;
         while (trigger == 3)
         {
            if (x2 < xright)
            {
               x2++;
            }
            trigger = 4;
         }
         while (trigger == 4)
         {
            if (x2 > xleft)
            {
               x2--;
            }
         }
      }
break;
   }

}


I'd be very greatful if someone were to help me :).
_________________
?There are no stupid questions but there are a LOT of inquisitive idiots.?

#4483 - Daikath - Tue Apr 01, 2003 1:03 pm

Code:
// springy.c AGBmain Entry point is contained here (main program loop)

#include "gba.h" //GBA register definitions
#include "gbakeys.h"     //button registers


#include "gbaregs.h"

typedef struct tagOAMEntry
{
u16 Attrib0;
u16 Attrib1;
u16 Attrib2;
u16 Attrib3;
}OAMEntry, *pOAMEntry;
typedef struct tagRotData
{
u16 filler1[3];
u16 pa;
u16 filler2[3];
u16 pb;
u16 filler3[3];
u16 pc;
u16 filler4[3];
u16 pd;
}RotData, *pRotData;


extern const u16 SilohData[];
extern const u16 SilohPalette[];
extern const u16 Siloh2Data[];
//create an OAM variable and make it point to the address of OAM
u16* OAMData = (u16*)0x7000000;

//create the array of sprites (128 is the maximum)
OAMEntry Sprites[128];
/*
u8 counter;
u8 counter2;*/

s16 x;
s16 y;
s16 x2;
s16 y2;
s16 x3;
s16 y3;
u16 busy;

//create the rotation and scaling array (overlaps the OAMEntry array memory)
pRotData rotData = (pRotData)Sprites;




// this is a piece of code to het the sprite move, use it for whatever you want :)
void sprite_move (OAMEntry *s, u16 x, u16 y)
{
  u16 not_y = 0xFF00;
  u16 not_x = 0xFE00;
 
  x = x & 0x1FF;
  y = y & 0xFF;
  Sprites[0].Attrib0 = Sprites[0].Attrib0 & not_y;
  Sprites[0].Attrib0 += y;
 
  Sprites[0].Attrib1 = Sprites[0].Attrib1 & not_x;
  Sprites[0].Attrib1 += x;

  x2 = x2 & 0x1FF;
   y2 = y2 & 0xFF;
   Sprites[1].Attrib0 = Sprites[1].Attrib0 & not_y;
   Sprites[1].Attrib0 += y2;

   Sprites[1].Attrib1 = Sprites[1].Attrib1 & not_x;
   Sprites[1].Attrib1 += x2;


}

//Copy our sprite array to OAM
void CopyOAM()
{
u16 loop;
u16* temp;
temp = (u16*)Sprites;
for(loop = 0; loop < 512; loop++)
{
OAMData[loop] = temp[loop];
}
}

//Set sprites to off screen
void InitializeSprites()
{
u16 loop;
/*for(loop = 0; loop < 128; loop++)
{
Sprites[loop].Attrib0 = 160;  //y to > 159
Sprites[loop].Attrib1 = 240;  //x to > 239
}*/
for (loop = 0; loop < 8; loop++)
{
   Sprites[loop].Attrib0= 1 << 9;
}
}

// CopySpriteData(); // ANTH : added this

void CopySpriteData(void)
{
u16 loop;
volatile u16 *p0=(volatile u16 *)0x6010000;
u16 *p1=(u16 *)SilohData;

for (loop=0;loop<(16*16/*/sizeof(u16)*/);loop++) *p0++=*p1++;
}
/*
void CopySpriteData2(void)
{
u16 loop;
volatile u16 *p0=(volatile u16 *)(0x6010000+(8*8)*(8*8)*1); // ANTH : Fixed this
u16 *p1=(u16*)Siloh2Data;
for (loop=(64*64/sizeof(u16)); loop <
(64*64/sizeof(u16))+(64*64/sizeof(u16)) ; loop++) *p0++=*p1++;
}
*/
//wait for the screen to stop drawing


/*
void WaitforVsync()
{
   while((volatile u16)REG_VCOUNT != 160){}
}*/
void GetInput()
{/*
  u16 joy;

  joy = REG_P1;
  joy = -joy;
*/   
   
   if (KEYS & KEY_LEFT)//32
   {
      x = x + 5;
   }
   /*if (KEYS & KEY_UP)
   {
      y = y + 3;
   }
   if (KEYS & KEY_DOWN)
   {
      y = y - 3;
   }*/
   
   if (KEYS & KEY_RIGHT)//16
   {
    x = x - 5;
   }
  // after we update the x and y position of the sprite for movement we need to update
  // the OAM data as well
  sprite_move (&Sprites[0],x,y);

}

void Elastiek()
{   
   s16 var1;
   var1 = 0;
   var1 = x - x2;
   /*s16 acceleration;
   s16 speed;*/
   s8 xleft;
   s8 xright;
   s8 possibility;
   u8 trigger;
   /*acceleration = 0;
   speed = 0;*/
   xleft = 0;
   xright = 0;
   possibility = 0;
   trigger = 0;
   if (KEYS & KEY_A)
   {
      possibility = 1;
   }
   if (KEYS & KEY_B)
   {
      possibility = 2;
   }
   switch (possibility)
   {
      case '1':
      x2--;
      break;
      case '2':
      x2++;
      break;
   }
}
void WaitforVsync()
{
  while (REG_VCOUNT != 160);
  while (REG_VCOUNT != 161);
  /*counter++;
  if (counter == 40)
  {  counter = 0;
  }*/
  GetInput();
}
/*
void animation(void)
{
   if (counter > 19)
      Sprites[0].Attrib2 = 128;
   else
   Sprites[0].Attrib2 = 0;
      
   if (counter > 19)
      Sprites[1].Attrib2 = 0;
   else
      Sprites[1].Attrib2 = 128;
}*/
int AgbMain()
{
  u16 loop;       //generic loop variable

  x = 80;
  y = 10;
  x2 = 80;
  y2 = 70;
  x3 = 300;
  y3 = 300;
 
  SetVideoMode(MODE0 | OBJ_ENABLE | OBJ_MAP_1D); //set mode 1 and enable
  //sprites and 1d mapping

  for(loop = 0; loop < 256; loop++)
  {
    OBJPaletteMem[loop] = SilohPalette[loop];
  }

  InitializeSprites();                       //set all sprites off screen
  //(stops artifact)


  CopySpriteData(); // ANTH : added this
  /*CopySpriteData2();
     */
 
  Sprites[0].Attrib0 = COLOR_256 | SQUARE | y; //setup sprite info,
  //256 colour, shape and y-coord
  Sprites[0].Attrib1 = SIZE_16 | x;            //size 16x16 and
  //x-coord
  Sprites[0].Attrib2 = 0;                      //pointer to tile where
  //sprite starts



  Sprites[1].Attrib0 = COLOR_256 | SQUARE | y2;
  Sprites[1].Attrib1 = SIZE_16 | x2;
  Sprites[1].Attrib2 = 0;
u16 i;
for (i = 2; i < 128; i++)
{
  Sprites[i].Attrib0 = COLOR_256 | SQUARE | y3;
  Sprites[i].Attrib1 = SIZE_16 | x3;
  Sprites[i].Attrib2 = 0;
}
  while(1)
  {
    //Copies sprite array into OAM.
    CopyOAM();
    /*animation();*/
   GetInput();
   Elastiek();
    WaitforVsync();

  }
}


_________________
?There are no stupid questions but there are a LOT of inquisitive idiots.?

#4484 - Torlus - Tue Apr 01, 2003 1:24 pm

ok first thing :

on your top code you have a variable named "possibility" that can be 1 or 2. but in your switch() instruction, you test on values '1' and '2'.

'1' and '2' there represent characters of values 0x31 and 0x32, and not 1 or 2. You need to replace " case '1': " by " case 1: ". Same thing for the "case 2", of course...

then as i have a quick look at your code i think you'll meet some other problems.
For instance, at the bottom of your code you have :

while (trigger == 4)
{
if (x2 > xleft)
{
x2--;
}
}

which is an endless loop :)