You are on page 1of 5

asddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

dddddd/* A simple example of drawing something random once.


*
* Draws a large rectangle with a random color in a random
* location.
*/

#include "introGlutLib.h"

//include the basic drawing library

#include <time.h>

// for MS Windows time-finding functions

#include <stdlib.h>

// for rand() and srand()

/*
* pickRandomRGB() generates a random color value
*/

unsigned long pickRandomRGB(void);

/* =================================Main:=======================================
* Seed the random number generator with the time before
* starting the graphics loop.
*/
int main()
{
srand(time(NULL));

InitGraphics();

// start GLUT/OpenGL

return 0;
}

/***************************************************************
myDisplay()

When called the first time, draws a randomly colored


rectangle in a random location.
Does nothing when called subsequently.
***************************************************************/

void myDisplay(void)
{
static int first_time = 1;
if (first_time) {
unsigned long color = pickRandomRGB();
int x = rand() % 300;
int y = rand() % 200;
int w = rand() % 400 + 50;
int h = rand() % 200 + 50;
ClearWindow();
SetPenColorHex(color);
DrawFillBox(x, y, x + w, y + h);
first_time = 0;

}
}

/**********************************************************************
myMouse(button, state, x, y)

On mouse click, exit the program.


***********************************************************************/

void myMouse(int button, int state, int x, int y)


{
exit(0);
}

/**********************************************************************
myKeyboard(key, x, y)

On key press, exit the program.


***********************************************************************/

void myKeyboard(unsigned char key, int x, int y)


{
exit(0);
}

/**********************************************************************
mySpecialKey(key, x, y)

On any special key press, exit the program.


***********************************************************************/

void mySpecialKey(int key, int x, int y)


{
exit(0);
}

/**********************************************************************
pickRandomRGB()

Generates the red, green and blue colors separately.


Otherwise most values will be at the low end, with little
or no red. To see the difference, try changing this to just
the line:
return rand() % 0xFFFFFF;

***********************************************************************/

unsigned long pickRandomRGB()


{

int red = rand() % 255;


int green = rand() % 255;
int blue = rand() % 255;
return red * 256 * 256 + green * 256 + blue;
}

You might also like