May 19, 2024, 05:58:09 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Sparklies ..

Started by GWS, February 14, 2006, 01:27:30 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

Hi folks,

Here's a little graphics example, originally done for IBPro, and translated with some help from Paul 'cos I'm not sure what I'm doing with this OOP stuff yet ..ÂÃ,  :)

If you spot anything that can be improved, let me know ..


// Sparklies -ÂÃ,  GWS Feb 2006

#autodefine "off"

declare import, GetSystemMetrics(int nIndex),unsigned int;

class win : window
{

//class constructor
declare win();

//virtual overrides ..
declare OnCreate(),int;
declare OnClose(),int;
declare OnControl(int nID, int nNotifyCode, unsigned int hControl),int;
declare OnTimer(int nIDEvent),int;

//member functions
declare Draw();

// member variables ..
CButton b1;
CButton b2;
int run,redraw,pause;
float pi;
}

// whenever you have member variables you should have a constructor
// to set them to an initial value.ÂÃ,  Treat member variables like local ones
// (their contents might be random if you're defining the class as a local
//ÂÃ,  variable in Main).

win::win()
{
// these are the member variables.ÂÃ,  Since you are in a method of the class,
// you access them directly ..
pause = 0;
run = 0;
redraw = 0;
pi = 4 * atan(1); // initialise pi
}


win::OnCreate(),int
{
CenterWindow();
return true;
}

win::OnClose(),int
{
StopTimer();
run = 0;
return true;
}

win::OnControl(int nID, int nNotifyCode, unsigned int hControl),int
{
select(nID) //same as @CONTROLID
ÂÃ,  {
case 1:
if nNotifyCode = 0 // exit button clicked
run = 0;
ÂÃ,  ÂÃ,  case 2:
if nNotifyCode = 0 // pause button clicked
{
if pause = 0
{
pause = 1;
b2.SetText("Continue");
}
else
{
pause = 0;
b2.SetText("Pause");
}
}
}
return true;
}

win::OnTimer(int nIDEvent),int
{
if(pause = 0)
{
stoptimer();
Draw();
}
return true;
}

global sub main()
{
def w as win;
int style,ScreenWidth,ScreenHeight;
int xpos,ypos,bWidth,bHeight;
//run is a member variable of the win class ..
w.run = 1;
ScreenWidth = GetSystemMetrics(0);
ScreenHeight = GetSystemMetrics(1);

style = AWS_CAPTION|AWS_VISIBLE|AWS_BORDER|AWS_SYSMENU|AWS_AUTODRAW|AWS_SIZE;
w.Create(0,0,ScreenWidth,ScreenHeight,style,0,"Sparklies",NULL);
w.SetWindowColor(0x300000) ;

bWidth = 70 * ScreenWidth/800;
bHeight = 30 * ScreenHeight/600;

xpos = (0.75 * ScreenWidth - bWidth/2);
ypos = (0.85 * ScreenHeight - 35 * ScreenHeight/600);

w.b1.Create(xpos,ypos,bWidth,bHeight,AWS_VISIBLE,1,"Exit",w);
xpos = (0.25 * ScreenWidth - bWidth/2);
w.b2.Create(xpos,ypos,bWidth,bHeight,AWS_VISIBLE,2,"Pause",w);

// draw first pattern ..
w.draw();

//
// four-cusped hypocycloid defined by:
// x = a * cos(t) ^ 3
// y = a * sin(t) ^ 3
// (angles in radians) ..
//
do
{
wait();
}until w.run = 0;

return;
}

win::draw()
{
int i,t,x,y,x2,y2,av1,av2;
int ScreenWidth,ScreenHeight;
float angle;

SetWindowColor(0x300000);
ScreenWidth = GetSystemMetrics(0);
ScreenHeight = GetSystemMetrics(1);

select ScreenWidth
{
case 800:
av1 = 100; av2 = 200;
case 1000:
av1 = 200; av2 = 300;
default:
av1 = 200; av2 = 300;
}

// draw the pattern ...

for(int a = av1; a <= av2; a += 100 )
{
ÂÃ,  ÂÃ, 
// the inner pattern ...
if a = av1
{
// try different rastermodes ...
// redraw is a member variable of the class
select redraw
{
case 0:
SetRasterOp(RMNOTMASKPEN);
case 1:
SetRasterOp(RMNOT);
case 2:
SetRasterOp(RMCOPYPEN);
case 3:
SetRasterOp(RMNOTCOPYPEN);
case 4:
SetRasterOp(RMMERGEPENNOT);
case 5:
SetRasterOp(RMMASKPENNOT);
case 6:
SetRasterOp(RMMERGENOTPEN);
case 7:
SetRasterOp(RMMERGENOTPEN);ÂÃ, 
case 8:
SetRasterOp(RMMERGEPEN);ÂÃ, 
}
redraw++;
if redraw > 8
redraw = 0;
ÂÃ,  }
ÂÃ,  else
ÂÃ,  {
// normal drawing mode ...
SetRasterOp(RMCOPYPEN);
ÂÃ,  }

angle = rnd(2,8);
ÂÃ,  ÂÃ, 
ÂÃ,  for t = 0; t <= 360; t += angle
ÂÃ,  {
x = a * cosd(t) ^ 3 + ScreenWidth/2;
y = a * sind(t) ^ 3 + ScreenHeight/2 - 50;

for i = 1; i <= rnd(5,15); i++
{
x2 = x + rnd(50) - rnd(50);
y2 = y + rnd(50) - rnd(50);
line(x, y, x2, y2, rgb(rnd(40,255),rnd(40,255),rnd(40,255)));
}
}
}
StartTimer(5000);
return;
}



The indentation's probably all gone to pot - sorry about that - it was OK this end .. :)

all the best, :)

Graham
Tomorrow may be too late ..