IonicWind Software

Aurora Compiler => General Discussion => Topic started by: lviklund on February 06, 2006, 03:21:00 PM

Title: SillyBalls???
Post by: lviklund on February 06, 2006, 03:21:00 PM
I need your help :'(

Here is a simple program that I use to try. I have done it in at least 10 different languages but I am stuck here.
I thought I could do this one and add it to the example programs that I am putting together.

In the DrawBalls method Aurora crashes out in the if clauses. I have tried all I can think of. I need your eyes and brains.

/Lasse

#autodefine "off"

#typedef uint unsigned int

#define WINDOW_WIDTH  400
#define WINDOW_HEIGHT 400

#define RADIUS        10
#define NUM_BALLS      2
#define MAX_VELOCITY  3
#define WHITE 0xFFFFFF
#define RED 0x0000FF

declare import, GetTickCount(),uint;

struct SBall
{
  int posX;  //to hold the balls coordinates
  int posY;
  int velX;  //and velocity
  int velY;
}

class mywindow : window
{
//overridden functions.
declare OnClose(),int;
declare OnCreate(),int;
declare OnPaint(),int;
declare DrawBalls(),int;
declare SetUpBalls(),int;

RECT rect;
int cxClient, cyClient, cwClient, chClient;
SBall balls[NUM_BALLS];
//pointer pBalls[NUM_BALLS];
}

global sub main()
{
mywindow w;

w.Create(0,0,WINDOW_WIDTH,WINDOW_HEIGHT,AWS_CAPTION|AWS_VISIBLE|AWS_SYSMENU,0,"Silly balls!!",0);

do
{
wait();
}until w.m_hwnd = 0;
return;
}

mywindow::OnClose(),int
{
Destroy();
return TRUE;
}

mywindow::OnCreate(),int
{
//messagebox(null,"OnCreate","");
CenterWindow();
if SetUpBalls()
return true;
}

mywindow::OnPaint(),int
{
if DrawBalls()
return true;
}

mywindow::SetUpBalls(),int
{
rect = GetClientRect();
cxClient = rect.left;
cyClient = rect.top;

cwClient = rect.right-rect.left;
chClient = rect.bottom-rect.top;

//DrawRect(cxClient, cyClient, cwClient, chClient, WHITE, WHITE);
//messagebox(null,str$(cxClient) +  " " + str$(cyClient) +  " " + str$(cwClient) +  " " + str$(chClient),"rect");

//seed random number generator
seedrnd(GetTickCount());

//set up the balls with random positions and velocities
for (int i=0; i<NUM_BALLS; i++)
{
balls[i].posX = Rand((RADIUS/2), cwClient - (RADIUS/2));
balls[i].posY = Rand((RADIUS/2), chClient - (RADIUS/2));
balls[i].velX = Rand(1,MAX_VELOCITY);
balls[i].velY = Rand(1,MAX_VELOCITY);
//messagebox(null,"Balls ready!\nHere is velX and velY:\n" + str$(balls[i].velX) + " " + str$(balls[i].velY) +
// "\nand posX and posY:\n" + str$(balls[i].posX) + " " + str$(balls[i].posY)," ");
}

//DrawBalls();
return true;
}

mywindow::DrawBalls(),int
{
//uint cacheDC;
//cacheDC = GetHDC();
//DrawMode(TRANSPARENT);

for (int j=0; j<NUM_BALLS; j++)
{
//check to see if they have hit any walls and reverse velocity
//accordingly
if ((balls[j].posX + (RADIUS/2)) >= cwClient or (balls[j].posX - (RADIUS/2)) < 0)
{
balls[j].velX = -balls[j].velX;
}
if ((balls[j].posY + (RADIUS/2)) >= chClient or (balls[j].posY - (RADIUS/2)) < 0)
{
balls[j].velY = -balls[j].velY;
}

// update their position
balls[j].posX += balls[j].velX;
balls[j].posY += balls[j].velY;

//render to display
Circle(balls[j].posX, balls[j].posY, RADIUS, RED,RED);
//messagebox(null,"Boll " + str$(i) + " klar.",str$(balls[i].posX) + " " + str$(balls[i].posY) + " " + str$(RADIUS));
}
//DrawMode(OPAQUE);
//WriteText(rect.left+20,rect.bottom-20, "Ball number: " + str$(i) + " ready. " + str$(balls[i].velX) + " " + str$(balls[i].velY) + " " + str$(RADIUS));

//messagebox(null,"Alla bollar ritade.","");
//ReleaseHDC(cacheDC);
return true;
}

Title: Re: SillyBalls???
Post by: Bruce Peaslee on February 06, 2006, 05:02:33 PM
You are declaring the array in the wrong place. I don't believe the Constructor is right.

I made it a global (outside of all class definitions and subroutines) and it works.
Title: Re: SillyBalls???
Post by: lviklund on February 06, 2006, 05:58:36 PM
Thank you
I thought I had tried that  ???  :-[.
Fresh eyes are worth a lot.
I just got frustrated after a while.
Anyway, it works so now I can go on with the fun parts :).

Thank's
Lasse
Title: Re: SillyBalls???
Post by: Bruce Peaslee on February 06, 2006, 10:16:30 PM
De nada.

This is all new to me. I was able to find your error because I made it myself - more than once.
Title: Re: SillyBalls???
Post by: Rock Ridge Farm (Larry) on February 07, 2006, 05:18:25 AM
That is called experience. Once you have made lots of errors you become
an expert. At my current error rate I will be well advanced soon, either that
or just stupid.  :)
Title: Re: SillyBalls???
Post by: lviklund on February 07, 2006, 09:15:30 AM
I do not understand why I am not able to declare an array in the class? Is it a problem with the struct thing? With the use of #define??
In the Keno example Pauil is doing this in the class: int balls[80]. ???
Uh!?
Were is the catch?
I am confused (default)

/Lasse
Title: Re: SillyBalls???
Post by: Ionic Wind Support Team on February 07, 2006, 09:33:23 AM
Should work.  I look at your code when I get time.  Still deep in IDE code right now.
Title: Re: SillyBalls???
Post by: Ionic Wind Support Team on February 07, 2006, 09:48:06 AM
One thing...don't do this:

mywindow::OnPaint(),int
{
if DrawBalls()
return true;
}

Your ounly returning TRUE if DrawBalls returns true.  Otherwise your not returning.  Should be:

mywindow::OnPaint(),int
{
if DrawBalls()
   return true;
return false;
}

Title: Re: SillyBalls???
Post by: Mike Stefanik on February 07, 2006, 09:53:32 AM
A test program I have uses a class member variable that's an array of pointers (see one of my earlier posts) without a problem. I'm using the latest build of the Alpha 2 compiler.

Good point about the return. I hope that down the road, you have the compiler display a warning if the code can "fall through" without an explicit return; it's kind of a big "gotcha" there. Is there any reason you can't emit a return (even if it duplicates an explicit return) at the end of a function? It would probably save some hair pulling.
Title: Re: SillyBalls???
Post by: Ionic Wind Support Team on February 07, 2006, 09:55:57 AM
Well there is a bug in the compiler somewhere.  It works if I change the second IF statement in DrawBalls to use the 'this' pointer when accessing the member variable.

if ((this->balls[j].posY + (RADIUS/2)) >= chClient) or ((this->balls[j].posY - (RADIUS/2)) < 0)
{
balls[j].velY = -balls[j].velY;
}

Somewhere the compiler is losing track of the class reference during that statement.  Still digging ;)
Title: Re: SillyBalls???
Post by: Ionic Wind Support Team on February 07, 2006, 10:15:49 AM
OK found and fixed.  I'll update later today.
Title: Re: SillyBalls???
Post by: lviklund on February 07, 2006, 11:53:03 AM
Thank you Paul.
Good to know that all bugs is not in my own head.

QuoteYour ounly returning TRUE if DrawBalls returns true.  Otherwise your not returning.

I know. This was a stripped version in frustration. I'll post the complete code together with some other examples for this boards to check.
I hope they can end up among our examples.

I will try to comment the code not to much and not to sparse.