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.

Beginners > Moving lines and circles about and swithcing between both.

#146267 - speedemon35 - Sat Dec 01, 2007 7:04 pm

hy to all of you again. I got some quite helpful feedback here last time i think i'll give gba programming a second shot.

Anyway. My new task consists of a few things: drawing a line(segment to be more precise since it can't take up all the width of height of the screen), drawing a circle, making them move about and have the option of the user pressing 1 key so he can alter between circle and line.

I've managed to fix up the code for drawing 1 single line. I've also managed in a separate code to draw the circle. But for now my biggest problem is getting them to move around. I figured if i can get the line to move first then it will be almost the same thing to make the circle move so i'll focus only on the line for now. I added in the check buttons thing and all that with the while{1} loop but for some reason its not working properly. Either i'm using the wrong variables inside the loop, or i need to place more variables inside it or my while loop is simply in the wrong place and its doing nothing what so ever:(

the code is here:

Code:

#define MULTIBOOT int __gba_multiboot;
MULTIBOOT
#include <stdlib.h>
//declare the function prototype
void DrawPixel3(int, int, unsigned short);
void DrawLine3(int, int, int, int, unsigned short);
//declare some defines for the video mode
#define ScreenWidth 240
#define ScreenHeight 160
#define REG_DISPCNT *(unsigned long*)0x4000000
#define MODE_3 0x3
#define BG2_ENABLE 0x400
//changes the video mode
#define SetMode(mode) REG_DISPCNT = (mode)
//packs three values into a 15-bit color
#define RGB(r,g,b) ((r)+(g<<5)+(b<<10))
//Key Definitions from Nokturn's key demo
#define KEY_A 1
#define KEY_B 2
#define KEY_SELECT 4
#define KEY_START 8
#define KEY_RIGHT 16
#define KEY_LEFT 32
#define KEY_UP 64
#define KEY_DOWN 128
#define KEY_R 256
#define KEY_L 512
//create a pointer to the video buffer
unsigned short* videoBuffer = (unsigned short*)0x6000000;
volatile int* KEYS = (volatile int*)0x04000130;
short col[14];
bool buttons[10];
// Function: main()
int main(void)
{
int x1,y1,x2,y2;
unsigned short color;
SetMode(MODE_3 | BG2_ENABLE);
while(1)
{
x1 = 100;
y1 = 80;
x2 = 140;
y2 = 80;
color = RGB(31,31,0);
DrawLine3(x1,y1,x2,y2,color);
}
return 0;
}
void clearscreen()
{
   int x, y;
   unsigned short black= RGB(0,0,0);
   
   for ( x = 0; x < (ScreenWidth); x+=1)
   {
      for ( y = 0; y < (ScreenHeight); y+=1)
      {DrawPixel3(x,y,black);}
   }
}
void checkbuttons()
{
   buttons[0] = !((*KEYS) & KEY_A);
   buttons[1] = !((*KEYS) & KEY_B);
   buttons[2] = !((*KEYS) & KEY_UP);
   buttons[3] = !((*KEYS) & KEY_DOWN);
   buttons[4] = !((*KEYS) & KEY_LEFT);
   buttons[5] = !((*KEYS) & KEY_RIGHT);
   buttons[6] = !((*KEYS) & KEY_START);
   buttons[7] = !((*KEYS) & KEY_SELECT);
   buttons[8] = !((*KEYS) & KEY_L);
   buttons[9] = !((*KEYS) & KEY_R);
}
// Function: DrawPixel3
void DrawPixel3(int x, int y, unsigned short color)
{
videoBuffer[y * 240 + x] = color;
}
// Function: DrawLine3
void DrawLine3(int x1, int y1, int x2, int y2, unsigned short color)
{
int i, deltax, deltay, numpixels;
int d, dinc1, dinc2;
int x, xinc1, xinc2;
int y, yinc1, yinc2;
//calculate deltaX and deltaY
deltax = abs(x2 - x1);
deltay = abs(y2 - y1);
//initialize
if(deltax >= deltay)
{
//If x is independent variable
numpixels = deltax + 1;
d = (2 * deltay) - deltax;
dinc1 = deltay << 1;
dinc2 = (deltay - deltax) << 1;
xinc1 = 1;
xinc2 = 1;
yinc1 = 0;
yinc2 = 1;
}
else
{
//if y is independent variable
numpixels = deltay + 1;
d = (2 * deltax) - deltay;
dinc1 = deltax << 1;
dinc2 = (deltax - deltay) << 1;
xinc1 = 0;
xinc2 = 1;
yinc1 = 1;
yinc2 = 1;
}
//move the right direction
if(x1 > x2)
{
xinc1 = -xinc1;
xinc2 = -xinc2;
}
if(y1 > y2)
{
yinc1 = -yinc1;
yinc2 = -yinc2;
}
x = x1;
y = y1;
//draw the pixels
for(i = 1; i < numpixels; i++)
{
DrawPixel3(x, y, color);
if(d < 0)
{
d = d + dinc1;
x = x + xinc1;
y = y + yinc1;
}
else
{
d = d + dinc2;
x = x + xinc2;
y = y + yinc2;

while(1)
   {
      checkbuttons();
      
      if (buttons[2])
      {
         if (y > 0)//up
         {clearscreen(); y = deltay - 1; DrawLine3(x1,y1,x2,y2,color);}
      }
      if (buttons[3])//down
      {
         if (y < (ScreenHeight - 1))
         {clearscreen(); y = deltay + 1; DrawLine3(x1,y1,x2,y2,color);}
      }
      if (buttons[4])
      {
         if ( x > 0 )//left
         {clearscreen(); x = deltax - 1; DrawLine3(x1,y1,x2,y2,color);}
      }
      if (buttons[5])//right
      {
         if ( x < (ScreenWidth - 1))
         {clearscreen(); x = deltax + 1; DrawLine3(x1,y1,x2,y2,color);}
      }
    }
}
}
}


i've tried changing the variables inside the while loop a couple of times already but none of them worked correctly so i just left them at my last attempt to make them work.

I have a pretty good idea on how i need to go about letting the user change between circle and line since it should simply follow the same logic as changing a pixels color each time 1 key was pressed. Right now my concern is getting that line to move around.

Again thank you for your time.

Sincerely,

Daniel Mendes.

#146695 - speedemon35 - Fri Dec 07, 2007 8:00 pm

Never mind this thread. i ended up figuring it out and i already submitted my code