#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
#define WINDOW_WIDTH 500
#define WINDOW_HEIGHT 500
#define WINDOW_X 200
#define WINDOW_Y 200
#define FRAMES_PER_SECOND 10
#define KEY_EXIT 27
#define STAR_LEN 80
#define STAR_X 0
#define STAR_Y 1
#define STAR_V 2
#define PI 3.1415927
#define SIDE 10
#define drawSquare(x,y,side)
glBegin(GL_QUADS);\
glVertex2f(x-side/2, y-side/2); glVertex2f(x+side/2, y-side/2);\
glVertex2f(x+side/2, y+side/2); glVertex2f(x-side/2, y+side/2);\
glEnd();
int star[STAR_LEN][3];
int timer = 0;
int speed = 10;
void handleKeyEvent(unsigned char key, int x, int y);
void handleMouseEvent(int button, int state, int x, int y);
void handleReshape(int width, int height);
void run(int null);
void repaint(void);
void update(void);
void initScene(void);
void doScene(void);
void drawScene(void);
void exitScene(void);
void sleepMilliSeconds(long milliSeconds);
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(WINDOW_X, WINDOW_Y);
glutCreateWindow("demo");
glutKeyboardFunc(handleKeyEvent);
glutMouseFunc(handleMouseEvent);
glutReshapeFunc(handleReshape);
initScene();
glutDisplayFunc(repaint);
glutTimerFunc(FRAMES_PER_SECOND, run, 0);
glutMainLoop();
return 0;
}
void run(int null)
{
clock_t startTime, endTime;
startTime = clock();
update();
repaint();
endTime = clock();
if (endTime - startTime < FRAMES_PER_SECOND)
{
sleepMilliSeconds(FRAMES_PER_SECOND - (endTime - startTime));
}
glutPostRedisplay();
glutTimerFunc(FRAMES_PER_SECOND, run, 0);
}
void update(void)
{
doScene();
}
void repaint(void)
{
drawScene();
glutSwapBuffers();
}
void initScene(void)
{
int i;
srand(time(0));
for (i = 0; i < STAR_LEN; i++)
{
star[i][STAR_X] = rand() % WINDOW_WIDTH;
star[i][STAR_Y] = rand() % WINDOW_HEIGHT;
star[i][STAR_V] =
rand()%100 + 200;
}
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D(0, (GLdouble)WINDOW_WIDTH, 0, (GLdouble)WINDOW_HEIGHT);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_COLOR_MATERIAL);
}
void doScene(void)
{
int i;
timer++;
if (timer == 200)
{
timer = 1;
}
if (timer % 20 == 0)
{
for (i = 0; i < STAR_LEN; i++)
{
star[i][STAR_X] = rand() % WINDOW_WIDTH;
star[i][STAR_Y] = rand() % WINDOW_HEIGHT;
star[i][STAR_V] = rand()%100 + 100;
}
}
for (i = 0; i < STAR_LEN; i++)
{
star[i][STAR_V] -= speed;
}
}
void drawScene(void)
{
unsigned char r, g, b;
int i, angle;
glClear (GL_COLOR_BUFFER_BIT);
r = rand()%200 + 55;
g = rand()%200 + 55;
b = rand()%200 + 55;
glColor3ub(r, g, b);
for (i = 0; i < STAR_LEN; i++)
{
drawSquare(star[i][STAR_X], star[i][STAR_Y]+star[i][STAR_V], 2);
}
for (i = 0; i <= timer; i++)
{
angle = 2*PI*(72*i-timer*20)/360;
drawSquare((WINDOW_WIDTH>>1) + timer*cos(angle), (WINDOW_HEIGHT>>1) + timer*sin(angle), SIDE);
}
}
void exitScene(void)
{
exit(0);
}
void handleKeyEvent(unsigned char key, int x, int y)
{
switch (key)
{
case KEY_EXIT:
exitScene();
break;
default:
break;
}
}
void handleMouseEvent(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(repaint);
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
void handleReshape(int width, int height)
{
}
void sleepMilliSeconds(long milliSeconds)
{
clock_t start, end;
start = clock();
end
= start + milliSeconds;
while (start < end)
{
start = clock();
}
}