/* Space Exploration 2007.12.16 HSGames.,2007 http://hsgames.com/spacex */ //ܼ â شٰ ؼ Ͽϴ. #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) #include #include #include //GetTickCount() include #include #include #define LISTS 256//Ʈ ִ밪 #define PLANETNUM 70// #define STARNUM 200// #define TICK 10// Tick const int MAXBULLETTIME = 1000;//Ʈ ʱⰪ void InitFunc();//ʱȭ void InitListsFunc();//ʱȭ Ʈ Ʈ void Moving();// ̵ void SetVertex(int,float,float);// ؽ void DisplayStar();// void DisplayShip();// void MyPolygon(int a, int b, int c, int d);//ڷ 4 ؽ ׸ colorcube Լ Ȱ void Colorcube(int c);//ü ׸ colorcube Լ Ȱ void Display();//display ݹԼ void GameIdle();//Idle void Mouse(int button, int state, int x, int y);//콺 ݹԼ void MouseMoveCallbackFunc(int x, int y);// ݹԼ void Key(unsigned char k, int xx, int yy);//Ű ݹԼ void MyReshape(int w, int h);// ݹԼ void DisplayTimeBar();//Ʈ void DisplayPoint();// void DisplayGameOver();//ӿ void DisplayLogo();//ù ȭ void glPrint(GLfloat, GLfloat, const char*,int);//¥ Լ void CollisionCheck(int);//浹üũ void Restart();// void Ending();// float speed;// ӵ (ӳ̵) float before_speed = 0.0;//Ʈ 带 ϼӵ int bullet_time = 0;//Ʈ float movedx = 0.0;// ۽ÿ ŭ ̵߳? float movedy = 0.0; int before_x = 300;// ݹ ̵ ݹ 콺xyǥ int before_y = 300; unsigned int point = 0;// int started = 0;//ӽ߳? int gameover = 0;//ӳ? int rocket_mode;// ׸? GLfloat vertices[][3] = {{-1.0,-1.0,-1.0}, {+1.0,-1.0,-1.0}, {+1.0,+1.0,-1.0}, {-1.0,+1.0,-1.0}, {-1.0,-1.0,+1.0}, {+1.0,-1.0,+1.0}, {+1.0,+1.0,+1.0}, {-1.0,+1.0,+1.0}}; //colorcube ׸ ť ؽ GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.5,0.0}, {0.8,0.4,0.0}, {0.3,0.3,0.0}, {0.7,0.0,0.0}, {0.4,0.4,0.0}, {0.8,0.7,0.0}, {0.9,1.0,0.3}}; //ť ؽ ( ) int colormode = 0;// ұ? struct planet//ü { float tx;//̵ ǥ x,y,z float ty; float tz; GLfloat vertices[8][3];// vertices 8 ؽ,6ü GLfloat min[3];// ǥ GLfloat max[3];// ū ǥ(浹üũ ) }planets[LISTS]; struct star// ü { float x;// ġ float y; }stars[STARNUM]; static GLdouble viewer[]= {0.0, 0.0, 5.0}; /* initial viewer location */ int baseLists, countLists;//Ʈ void glPrint(GLfloat x, GLfloat y, const char* str,int glfont)//Ʈ ޾ { int i; glRasterPos2f(x, y); //ġ for(i=0;i -40.0f-planets[p].max[0]) && (planets[p].ty < 40.0f-planets[p].min[1] && planets[p].ty > -40.0f-planets[p].max[1]) && (planets[p].tz < 40.0f-planets[p].min[2] && planets[p].tz > -120.0f-planets[p].max[2]) ) { printf("%.2f,%.2f,%.2f\n",planets[p].tx,planets[p].ty,planets[p].tz); printf("%.2f,%.2f,%.2f,%.2f\n",planets[p].min[0],planets[p].max[0],planets[p].min[1],planets[p].max[1]); glutIdleFunc(NULL);// μ gameover = 1;//ӿ } } void Moving()// ٰ´ { int j; static int stime = 0, etime =0;//ƽ Tickð stime = GetTickCount();// Tickҷ //Game Tick: //ǻ͸ Ʋ⶧ // ӷ if(stime - etime >= TICK)// Tick Ǿ Tick TICK̻ ٸ { for(j=0;j= 10.0)// ƴٸ { planets[j].tz = -10000.0;// ָ ġ planets[j].tx = ((float)(rand()%8000))-4000.0;//x,y planets[j].ty = ((float)(rand()%8000))-4000.0; } SetVertex(j,planets[j].tx,planets[j].ty,planets[j].tz); // ؽ Colorcube(j);//Ʈ } } if(bullet_time > -MAXBULLETTIME)//Ʈ 尡 ƴ϶ bullet_time--;//ְѰġ ä //Ʈ (϶ ä ,϶ ) if(bullet_time > 6)//ϴ ͺ ä ӵ bullet_time-=6; if(bullet_time == 0)// Ʈ { speed = before_speed; } etime = stime;//Tick time if(point < UINT_MAX)//Ʈ Ѱġ ̶ { if(bullet_time < 0)//Ʈ尡 ƴϸ point++;// } else// ׷ Ѱġ ٴ޾ Ending();// } } //Ʈ void InitFunc()//ʱȭ { srand(time(NULL));// bullet_time = -MAXBULLETTIME;//Ʈ started = 0; gameover = 0; speed = 150.0;//ó ӵ rocket_mode = 1; } void InitListsFunc()//Ʈ ʱȭ { int i,j,k; float pivotx = 0.0,pivoty = 0.0; baseLists = glGenLists(LISTS);//Ʈ genlists glListBase(baseLists);//Ʈ ̽ countLists = 0;//Ʈ for(i=0;i planets[i].vertices[j][k]) planets[i].min[k] = planets[i].vertices[j][k]; } countLists++;//Ʈ ߰ } } void DisplayStar()// { int i; GLfloat x,y; glColor3f(1.0 , 1.0 , 1.0);// glBegin(GL_POINTS); //srand(100); for(i=0;i70.0) x-=140.0; else if(x<-70.0) x+=140.0; if(y>70.0) y-=140.0; else if(y<-70.0) y+=140.0; glVertex3f(x , y , -50.0);// } glEnd(); } void DisplayShip()// { float x,y,z; int i; glLoadIdentity(); gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // 콺ݹԼ ̵ ġ ŭ Lookat Ͽ // ִϸ̼ ȿ if(rocket_mode == 0)//ϸ忡 { //簢 glColor4f(0.5 , 0.5 , 0.5, 0.5); glBegin(GL_POLYGON); glVertex3f(-0.5 , 0.5, 1.0); glVertex3f(0.5 , 0.5, 1.0); glVertex3f(0.5 , -0.5, 1.0); glVertex3f(-0.5 , -0.5, 1.0); glEnd(); return; } // glColor4f(1.0 , 1.0 , 1.0, 0.5); glBegin(GL_POLYGON); glVertex3f(-0.5 , 0.2, 1.0); glVertex3f(-0.2 , 0.5, 1.0); glVertex3f(0.2 , 0.5, 1.0); glVertex3f(0.5 , 0.2, 1.0); glColor4f(0.5 , 0.5 , 0.5, 0.5); glVertex3f(0.5 , -0.2, 1.0); glVertex3f(0.2 , -0.5, 1.0); glVertex3f(-0.2 ,-0.5, 1.0); glVertex3f(-0.5 , -0.2, 1.0); glEnd(); // glColor4f(0.5 , 0.5 , 0.5, 0.5); glBegin(GL_QUAD_STRIP); glVertex3f(-0.5 , 0.2, 1.0); glVertex3f(-0.5 , 0.2, 0.0); glVertex3f(-0.2 , 0.5, 1.0); glVertex3f(-0.2 , 0.5, 0.0); glVertex3f(0.2 , 0.5, 1.0); glVertex3f(0.2 , 0.5, 0.0); glColor4f(0.8 , 0.8 , 0.8, 0.5); glVertex3f(0.5 , 0.2, 1.0); glVertex3f(0.5 , 0.2, 0.0); glVertex3f(0.5 , -0.2, 1.0); glVertex3f(0.5 , -0.2, 0.0); glVertex3f(0.2 , -0.5, 1.0); glVertex3f(0.2 , -0.5, 0.0); glColor4f(0.5 , 0.5 , 0.5, 0.5); glVertex3f(-0.2 ,-0.5, 1.0); glVertex3f(-0.2 ,-0.5, 0.0); glVertex3f(-0.5 , -0.2, 1.0); glVertex3f(-0.5 , -0.2, 0.0); glVertex3f(-0.5 , 0.2, 1.0); glVertex3f(-0.5 , 0.2, 0.0); glEnd(); //ٸ glColor4f(0.3 , 0.3 , 0.3, 0.5); glBegin(GL_QUAD_STRIP); glVertex3f(-0.5 , 0.2, 0.0); glVertex3f(-0.05 , 0.02, -2.0); glVertex3f(-0.2 , 0.5, 0.0); glVertex3f(-0.02 , 0.05, -2.0); glVertex3f(0.2 , 0.5, 0.0); glVertex3f(0.02 , 0.05, -2.0); glColor4f(0.7 , 0.7 , 0.7, 0.5); glVertex3f(0.5 , 0.2, 0.0); glVertex3f(0.05 , 0.02, -2.0); glVertex3f(0.5 , -0.2, 0.0); glVertex3f(0.05 , -0.02, -2.0); glVertex3f(0.2 , -0.5, 0.0); glColor4f(0.3 , 0.3 , 0.3, 0.5); glVertex3f(0.02 , -0.05, -2.0); glVertex3f(-0.2 ,-0.5, 0.0); glVertex3f(-0.02 ,-0.05, -2.0); glVertex3f(-0.5 , -0.2, 0.0); glVertex3f(-0.05 , -0.02, -2.0); glVertex3f(-0.5 , 0.2, 0.0); glVertex3f(-0.05 , 0.02, -2.0); glEnd(); // glColor4f(0.0 , 0.0 , 0.4, 0.3); glBegin(GL_TRIANGLE_FAN); glVertex3f(0.0 , 0.9, 0.8); glVertex3f(-0.4 , 0.2, 1.0); glVertex3f(0.4 , 0.2, 1.0); glVertex3f(0.0 , 0.2, -1.0); glVertex3f(-0.4 , 0.2, 1.1); glEnd(); glBegin(GL_TRIANGLE_FAN); glVertex3f(0.0 , -0.9, 0.5); glVertex3f(-0.4 , -0.2, 1.0); glVertex3f(0.4 , -0.2, 1.0); glVertex3f(0.0 , -0.2, -1.0); glVertex3f(-0.4 , -0.2, 1.1); glEnd(); glBegin(GL_TRIANGLE_FAN); glVertex3f(-0.9, 0.0, 0.5); glVertex3f(-0.2, -0.4, 1.0); glVertex3f(-0.2, 0.4, 1.0); glVertex3f(-0.2, 0.0, -1.0); glVertex3f(-0.2, -0.4, 1.1); glEnd(); glBegin(GL_TRIANGLE_FAN); glVertex3f(0.9, 0.0, 0.5); glVertex3f(0.2, -0.4, 1.0); glVertex3f(0.2, 0.4, 1.0); glVertex3f(0.2, 0.0, -1.0); glVertex3f(0.2, -0.4, 1.1); glEnd(); //Ҳ glColor4f(1.0 , 0.0 , 0.0, 0.3); glBegin(GL_QUAD_STRIP); for(i=0;i<10;i++)//Ź QUAD STRIP Ͽ Ҳȿ { x = ((float)(rand()%200)-100.0)/150.0; y = ((float)(rand()%200)-100.0)/150.0; z = ((float)(rand()%200)-100.0)/150.0; glVertex3f(x,y,1.4+z); } glEnd(); } void DisplayLogo()//ӽ ΰ { glColor3f(8.0 ,8.0 ,0.5); glPrint(-2.2, 2.5, "S p a c e E x p l o r a t i o n",1); glColor3f(0.0 ,0.0 ,0.9); glPrint(-1.3, -2.6, "(@@^)HSGames.,2007",0); glPrint(-1.3, -3.7, "http://hsgames.com/spacex",0); glColor3f(1.0 ,1.0 ,1.0); glPrint(-2.2, -1.5, "To start game, click mouse button",2); glColor3f(1.0 ,0.0 ,0.0); glPrint(1.5, -4.2, "L Button & Drag : Move",0); glPrint(1.5, -4.5, "R Button : Matrix mode",0); glColor3f(1.0 ,0.5 ,0.5); glPrint(-4.9, -4.2, "Key 'C' : Planet's Color",0); glPrint(-4.9, -4.5, "Key 'R' : Rocket View",0); // viewer[0] = (float)(rand()%10) - 5.0; viewer[1] = (float)(rand()%10) - 5.0; DisplayShip(); } void DisplayTimeBar()//Ʈ { glColor4f(1.0 , 1.0 , 1.0, 0.5); glBegin(GL_POLYGON); glVertex3f(1.5 , -2.3, 2.0); glVertex3f(1.5 , -2.4, 2.0); if(bullet_time < 0)// ƴ϶ Ķ { glColor4f(0.0 , 0.0 , 1.0, 0.5); glVertex3f(1.5-(float)bullet_time/(float)MAXBULLETTIME , -2.5, 2.0); glVertex3f(1.5-(float)bullet_time/(float)MAXBULLETTIME , -2.2, 2.0); }else//̶ { glColor4f(1.0 , 0.0 , 0.0, 0.5); glVertex3f(1.5+(float)bullet_time/(float)MAXBULLETTIME , -2.5, 2.0); glVertex3f(1.5+(float)bullet_time/(float)MAXBULLETTIME , -2.2, 2.0); } glEnd(); } void DisplayPoint()// { char str[256]; sprintf(str,"Point : %d",point); glColor3f(1.0, 1.0, 1.0);//RGB glPrint(2.0, 4.5, str,0); } void DisplayGameOver()//ӿ { char str[256]; glColor3f(1.0, 1.0, 1.0);//RGB sprintf(str,"Point : %d",point);// glPrint(-0.8, 3.0, str,0); sprintf(str,"Game Over"); glPrint(-0.7, 2.0, str,0); sprintf(str,"If you want to submit your score to server,"); glPrint(-3.0, 0.4, str,0); sprintf(str,"Press 'Enter' key."); glPrint(-3.0, 0.0, str,0); sprintf(str,"To be continue, Click Mouse button."); glPrint(-3.0, -0.8, str,0); sprintf(str,"http://www.hsgames.com/spacex"); glPrint(-3.0, -2.8, str,0); //޽ //⵿ ǥ glColor4f(0.7 , 0.0 , 0.0, 0.6); glBegin(GL_POLYGON); glVertex3f(-100.0 , 100.0, 2.0); glVertex3f(100.0 , 100.0, 2.0); glVertex3f(100.0 , -100.0, 2.0); glVertex3f(-100.0 , -100.0, 2.0); glEnd(); } void Display()//display ݹԼ { /* display callback, clear frame buffer and z buffer, rotate cube and draw, swap buffers */ int i; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// glLoadIdentity(); gluLookAt(0.0,0.0,viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);// ϸ Ǿ Lookatʱȭ DisplayStar();// ׸ if(gameover)//ӿ Ǿٸ { DisplayGameOver();//ӿ } else if(started)//ӿ ƴϰ ۵Ǿٸ { if (countLists) { for (i=1; i<=50+(point/500)&&i<=countLists; i++) //clear ĺ Ʈ ȣϿ ׷ glCallList(i); }// ŭ  DisplayTimeBar(); DisplayPoint(); DisplayShip();//̽, }else// { DisplayLogo();//ΰ } glFlush();// glutSwapBuffers();//,ӹ } void GameIdle()// idle μ { //GameProcess Moving();//̵ϰ Display();// } void Restart()//ӿ { int i,j,k; float pivotx = 0.0,pivoty = 0.0; gameover = 0; started = 0; speed = 150.0; point = 0;// ʱȭ bullet_time = -MAXBULLETTIME; for(i=0;i=50) planets[i].tz = -10000.0; for(j=0;j<8;j++) for(k=0;k<3;k++) { planets[i].vertices[j][k] = (float)((rand()%2000)-1000)/5.0; } SetVertex(i,planets[i].tx,planets[i].ty,planets[i].tz); } Display(); } void Ending()// { char str[512]; STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); sprintf(str,"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE http://hsgames.com/spacex/ending.php"); CreateProcessA( NULL, // No module name (use command line) str, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi ); } void Mouse(int button, int state, int x, int y)//콺 ݹ { if (button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)// ư { if(started == 0) return; if(bullet_time > 0)//Ʈ { speed = before_speed; bullet_time *= -1; }else if(bullet_time < -100)// Ʈ { bullet_time *= -1; before_speed = speed; speed = 5.0;//5.0 } } if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)//콺 { if(gameover)//ӿ { Restart(); return; } if(started == 0)//ӽ۾ { started = 1;// viewer[2] = 5.0; glutIdleFunc(GameIdle); } before_x = x;//ݹ鿡 ʱ 콺 ġ before_y = y; }else if(button==GLUT_LEFT_BUTTON && state==GLUT_UP) { //콺 ̸ ٽ viewer[0] = 0.0; viewer[1] = 0.0; } if (button==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) { } } void MouseMoveCallbackFunc(int x, int y)// ݹ { int i; float x2 = (x-before_x)*3.0;//콺 ŭ ̵ߴ float y2 = (y-before_y)*3.0; for(i=0;i 5.0) viewer[0] = 5.0; else if(viewer[0] < -5.0) viewer[0] = -5.0; if(viewer[1] > 5.0) viewer[1] = 5.0; else if(viewer[1] < -5.0) viewer[1] = -5.0; before_x = x;// ǥ before_y = y; } void Key(unsigned char k, int xx, int yy) //Ű ݹ { int i,j; char str[512]; STARTUPINFO si; PROCESS_INFORMATION pi; struct tm *nowtime;//¥ ð ϴ ü time_t temp;//time_t long long temp;ٲ㵵 unsigned int dkey; switch(k) { case 13://ӿ ʹ if(!gameover) return; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); time(&temp);// ð temp nowtime=localtime(&temp);//temp ¥ ð nowtime sprintf(str,"%02d%02d",nowtime->tm_mday,nowtime->tm_hour); dkey = atoi(str); // ؽ Ű Ͽ CreateProcessA( NULL, // No module name (use command line) str, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi ); Restart(); break; case 'c':case 'C'://C  if(colormode) { GLfloat colors2[][3] = {{0.0,0.0,0.0},{1.0,0.5,0.0}, {0.8,0.4,0.0}, {0.3,0.3,0.0}, {0.7,0.0,0.0}, {0.4,0.4,0.0}, {0.8,0.7,0.0}, {0.9,1.0,0.3}}; colormode = 0; for(i=0;i<8;i++) for(j=0;j<3;j++) colors[i][j] = colors2[i][j]; }else { GLfloat colors2[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}}; colormode = 1; for(i=0;i<8;i++) for(j=0;j<3;j++) colors[i][j] = colors2[i][j]; } break; case 'x': viewer[0] += 1.0; break; case 'X': viewer[0] -= 1.0; break; case 'y': viewer[1] += 1.0; break; case 'Y': viewer[1] -= 1.0; break; case 'z': viewer[2] += 1.0; break; case 'Z': viewer[2] -= 1.0; break; case 'r':case 'R'://ϱ׸ rocket_mode = (rocket_mode?0:1); break; } glFlush(); } void MyReshape(int w, int h)// { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //ǰ COP ü if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h / (GLfloat) w, 2.0, 10000.0); else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h, 2.0* (GLfloat) w / (GLfloat) h, 2.0, 10000.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char **argv)//Լ { glutInit(&argc, argv); /* need both double buffering and z buffer */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(600, 600); glutCreateWindow("Space Exploration_ComputerGraphicsProject_2002200735ѽ"); //glutCreateWindow("Space Exploration_HSGames"); InitFunc(); InitListsFunc(); glutReshapeFunc(MyReshape); glutDisplayFunc(Display); glutMouseFunc(Mouse); glutMotionFunc(MouseMoveCallbackFunc); glutKeyboardFunc(Key); glEnable(GL_DEPTH_TEST); /* Enable hidden-surface removal */ glEnable(GL_BLEND); // տִ  ʵ ϵ ϴ //Alpha glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.0); glutMainLoop(); }