Guest User

Untitled

a guest
Jan 19th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.24 KB | None | 0 0
  1. /*
  2. * This Code Was Created By Jeff Molofee 2000
  3. * A HUGE Thanks To Fredric Echols For Cleaning Up
  4. * And Optimizing This Code, Making It More Flexible!
  5. * If You've Found This Code Useful, Please Let Me Know.
  6. * Visit My Site At nehe.gamedev.net
  7. */
  8.  
  9. #include <windows.h> // Header File For Windows
  10. #include <gl\gl.h> // Header File For The OpenGL32 Library
  11. #include <gl\glu.h> // Header File For The GLu32 Library
  12. #include <gl\glaux.h> // Header File For The Glaux Library
  13. #include <stdio.h>
  14. #include <string>
  15. #include <math.h>
  16. #include <stdlib.h>
  17.  
  18. #include <time.h>
  19.  
  20. #define FlNum 3
  21.  
  22. #pragma comment(lib, "opengl32.lib")
  23. #pragma comment(lib, "glu32.lib")
  24. #pragma comment(lib, "glaux.lib")
  25.  
  26.  
  27.  
  28. HDC hDC=NULL; // Private GDI Device Context
  29. HGLRC hRC=NULL; // Permanent Rendering Context
  30. HWND hWnd=NULL; // Holds Our Window Handle
  31. HINSTANCE hInstance; // Holds The Instance Of The Application
  32.  
  33. bool keys[256]; // Array Used For The Keyboard Routine
  34. bool active=TRUE; // Window Active Flag Set To TRUE By Default
  35. bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
  36. bool r = TRUE;
  37. bool dbjump = FALSE;
  38. bool down = FALSE;
  39. bool mousecl = FALSE;
  40. bool Bmenu = TRUE;
  41. bool done=FALSE;
  42. bool Be = FALSE;
  43. bool b = FALSE;
  44. bool b2 = FALSE;
  45.  
  46. GLfloat xPlace;
  47. GLfloat yPlace = 3.0f;
  48. GLfloat xPlace2;
  49. GLfloat yPlace2;
  50. GLfloat ySpeed;
  51. GLuint nFace;
  52. GLuint texture[7];
  53. GLuint nChoosen;
  54. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
  55.  
  56.  
  57. GLfloat xPos;
  58.  
  59. GLfloat yPos;
  60.  
  61. //classes
  62. #include "wall.h"
  63.  
  64. void move_to_point(float &x1, float &y1, float &x2, float &y2, float speed)
  65. {
  66. y1+=sin(atan2(y2-y1, x2-x1))*speed;
  67. x1+=cos(atan2(y2-y1, x2-x1))*speed;
  68.  
  69. }
  70. // declaration of a new array
  71. wall f[] = {wall(0.0f, 4.0f, -1.0f, 0.5f), wall(4.0f, 6.0f, -1.0f, 3.5f), wall(-6.0f, 6.0f, -5.0f, -2.0f), wall(-6.0f, -1.0f, 2.0f, 4.0f)}; // access to an element of the array.
  72.  
  73. bool FlCheck(float xP, float xP2, float yP, float yP2)
  74. {
  75. for(int i=0; i<=FlNum; i+=1)
  76. {
  77. if(f[i].Fcheck(xP, xP2, yP, yP2))
  78. return TRUE;
  79. }
  80. return FALSE;
  81. }
  82. clock_t brakeS = -1;
  83. #include "enemy.h"
  84. #include "shoot.h"
  85.  
  86. shoot sh[256];
  87. enemy e[] = {enemy(0.0f, 2.0f)};
  88. void Pmove(float &xP, float &yP, float &yS)
  89. {
  90. if(mousecl)
  91. {
  92. for(int i=0; i<256; i+=1)
  93. {
  94. if(!sh[i].isset() && sh[i].timeCS())
  95. {
  96. sh[i].set((xP+xPlace2)/2, (yP+yPlace2)/2);
  97. break;
  98. }
  99. }
  100. }
  101. mousecl = FALSE;
  102. if(FlCheck(xPlace, xPlace2, yPlace+yS, yPlace2+yS) && yS < 0)
  103. while(!FlCheck(xPlace, xPlace2, yPlace-0.001f, yPlace2-0.001f))
  104. {
  105. yPlace -= 0.001f;
  106. yPlace2 -= 0.001f;
  107. }
  108. else if(FlCheck(xPlace, xPlace2, yPlace+yS, yPlace2+yS) && yS >= 0)
  109. while(!FlCheck(xPlace, xPlace2, yPlace+0.001f, yPlace2+0.001f))
  110. {
  111. yPlace += 0.001f;
  112. yPlace2 += 0.001f;
  113. yS = 0;
  114. }
  115. if(FlCheck(xPlace, xPlace2, yPlace-0.001f, yPlace2-0.001f))
  116. {
  117. yS = 0.0f;
  118. dbjump = TRUE;
  119. if(keys[VK_UP] && r)
  120. {
  121. yS = 0.075f;
  122. r = false;
  123. }
  124. nFace = 0;
  125. }
  126. else
  127. {
  128. nFace = 1;
  129. yS -= 0.0025f;
  130. if(keys[VK_UP] && r && dbjump)
  131. {
  132. yS = 0.1f;
  133. r = false;
  134. dbjump = false;
  135. }
  136. }
  137. if(!keys[VK_UP])
  138. r = TRUE;
  139. if(yS != 0)
  140. if(!FlCheck(xPlace, xPlace2, yPlace+yS, yPlace2+yS))
  141. yPlace += yS;
  142.  
  143. if(keys[VK_RIGHT] )
  144. {
  145. if (!FlCheck(xPlace+0.05f, xPlace2+0.05f, yPlace, yPlace2))
  146. {
  147. xPlace += 0.05f;
  148. }
  149. else
  150. {
  151. while(!FlCheck(xPlace+0.001f, xPlace2+0.001f, yPlace, yPlace2))
  152. {
  153. xPlace += 0.001f;
  154. xPlace2 += 0.001f;
  155. }
  156. }
  157. if(nFace != 1)
  158. nFace = 2;
  159. }
  160. else if(keys[VK_LEFT] )
  161. {
  162. if (!FlCheck(xPlace-0.05f, xPlace2-0.05f, yPlace, yPlace2))
  163. xPlace -= 0.05f;
  164. else
  165. {
  166. while(!FlCheck(xPlace-0.001f, xPlace2-0.001f, yPlace, yPlace2))
  167. {
  168. xPlace -= 0.001f;
  169. xPlace2 -= 0.001f;
  170. }
  171. }
  172. if(nFace != 1)
  173. nFace = 3;
  174. }
  175. if(keys[VK_DOWN])
  176. down = TRUE;
  177. else if(!FlCheck(xPlace, xPlace2, yPlace, yPlace+2.0f))
  178. down = FALSE;
  179. }
  180.  
  181. AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
  182. {
  183. FILE *File=NULL; // File Handle
  184.  
  185. if (!Filename) // Make Sure A Filename Was Given
  186. {
  187. return NULL; // If Not Return NULL
  188. }
  189.  
  190. File=fopen(Filename,"r"); // Check To See If The File Exists
  191.  
  192. if (File) // Does The File Exist?
  193. {
  194. fclose(File); // Close The Handle
  195. return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
  196. }
  197.  
  198. return NULL; // If Load Failed Return NULL
  199. }
  200.  
  201. int LoadGLTextures(std::string fName, int fNum) // Load Bitmaps And Convert To Textures
  202. {
  203. int Status=FALSE; // Status Indicator
  204.  
  205. AUX_RGBImageRec *TextureImage[7]; // Create Storage Space For The Texture
  206.  
  207. memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
  208.  
  209. // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
  210. if (TextureImage[fNum]=LoadBMP((char*)fName.c_str()))
  211. {
  212. Status=TRUE; // Set The Status To TRUE
  213.  
  214. glGenTextures(1, &texture[fNum]); // Create The Texture
  215.  
  216. // Typical Texture Generation Using Data From The Bitmap
  217. glBindTexture(GL_TEXTURE_2D, texture[fNum]);
  218. glTexImage2D(GL_TEXTURE_2D, 0, 4, TextureImage[fNum]->sizeX, TextureImage[fNum]->sizeY, 1, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[fNum]->data);
  219. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  220. }
  221.  
  222. if (TextureImage[fNum]) // If Texture Exists
  223. {
  224. if (TextureImage[fNum]->data) // If Texture Image Exists
  225. {
  226. free(TextureImage[fNum]->data); // Free The Texture Image Memory
  227. }
  228.  
  229. free(TextureImage[fNum]); // Free The Image Structure
  230. }
  231.  
  232. return Status; // Return The Status
  233. }
  234.  
  235. GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
  236. {
  237. if (height==0) // Prevent A Divide By Zero By
  238. {
  239. height=1; // Making Height Equal One
  240. }
  241. glViewport(0,0, width,height); // Reset The Current Viewport
  242.  
  243. glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
  244. glLoadIdentity(); // Reset The Projection Matrix
  245.  
  246. // Calculate The Aspect Ratio Of The Window
  247. gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  248.  
  249. glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
  250. glLoadIdentity();
  251. // Reset The Modelview Matrix
  252. }
  253.  
  254. int InitGL(GLvoid) // All Setup For OpenGL Goes Here
  255. {
  256. if(!LoadGLTextures(std::string("texture/Face0.bmp"), 0))
  257. return FALSE;
  258. if(!LoadGLTextures(std::string("texture/Face1.bmp"), 1))
  259. return FALSE;
  260. if(!LoadGLTextures(std::string("texture/Face2.bmp"), 2))
  261. return FALSE;
  262. if(!LoadGLTextures(std::string("texture/Face3.bmp"), 3))
  263. return FALSE;
  264. if(!LoadGLTextures(std::string("texture/menu1.bmp"), 4))
  265. return FALSE;
  266. if(!LoadGLTextures(std::string("texture/menu2.bmp"), 5))
  267. return FALSE;
  268. if(!LoadGLTextures(std::string("texture/menu3.bmp"), 6))
  269. return FALSE;
  270. glEnable(GL_TEXTURE_2D);
  271. // Enable Smooth Shading
  272. // Black Background
  273.  
  274. glClearDepth(1.0f); // Depth Buffer Setup
  275. // Enables Depth Testing
  276. // The Type Of Depth Testing To Do
  277. // Really Nice Perspective Calculations
  278.  
  279. return TRUE; // Initialization Went OK
  280. }
  281.  
  282. int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
  283. {
  284.  
  285. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
  286. // Reset The Current Modelview Matrix
  287.  
  288. glLoadIdentity();
  289. f[0].Fpaint();
  290. glLoadIdentity();
  291. f[1].Fpaint();
  292. glLoadIdentity();
  293. f[2].Fpaint();
  294. glLoadIdentity();
  295. f[3].Fpaint();
  296. glLoadIdentity();
  297. e[0].Epaint();
  298.  
  299. for(int i=0; i<256; i+=1)
  300. {
  301. if(sh[i].isset())
  302. {
  303. glLoadIdentity();
  304. sh[i].Fpaint();
  305. }
  306. }
  307. glLoadIdentity();
  308. yPlace2 = yPlace+2.0f;
  309. xPlace2 = xPlace+2.0f;
  310. if(down)
  311. yPlace2 = yPlace+0.8f;
  312. glTranslatef(0.0f, 0.0f, -13.0f);
  313. glBindTexture(GL_TEXTURE_2D, texture[nFace]); //use texture 0
  314. glColor3f(1.0f,1.0f,1.0f); // color white
  315. glBegin(GL_POLYGON); //create polygon
  316. glTexCoord2f(0.0f, 0.0f);glVertex3f( xPlace,yPlace, 0.0f); //down left
  317. glTexCoord2f(0.0f, 1.0f);glVertex3f( xPlace, yPlace2, 0.0f); //up left
  318. glTexCoord2f(1.0f, 1.0f);glVertex3f( xPlace2, yPlace2, 0.0f); //up right
  319. glTexCoord2f(1.0f, 0.0f);glVertex3f( xPlace2, yPlace, 0.0f); //down right
  320. glEnd();
  321.  
  322.  
  323. glLoadIdentity();
  324. glTranslatef(0.0f, 0.0f, -13.0f);
  325. glColor3f(1.0f,0.0f,0.0f);
  326. glBegin(GL_POLYGON); //create polygon
  327. glVertex3f( xPos-0.5f, yPos-0.5f, 0.0f); //down left
  328. glVertex3f( xPos-0.5f, yPos+0.5f, 0.0f); //up left
  329. glVertex3f( xPos+0.5f, yPos+0.5f, 0.0f); //up right
  330. glVertex3f( xPos+0.5f, yPos-0.5f, 0.0f); //down right
  331. glEnd();
  332. Pmove(xPlace, yPlace, ySpeed);
  333.  
  334. return TRUE; // Everything Went OK
  335. }
  336. int DrawMenuScene()
  337. {
  338. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  339. glLoadIdentity();
  340. glTranslatef(0.0f, 0.0f, -13.0f);
  341. if(nChoosen == 0)
  342. glColor3f(1.0f,0.0f,0.0f);
  343. else
  344. glColor3f(0.0f,0.0f,1.0f);
  345. glBindTexture(GL_TEXTURE_2D, texture[4]);
  346. glBegin(GL_POLYGON); //create polygon
  347. glTexCoord2f(0.0f, 0.0f);glVertex3f( -3.5, 1.0, 0.0f); //down left
  348. glTexCoord2f(0.0f, 1.0f);glVertex3f( -3.5, +3.0, 0.0f); //up left
  349. glTexCoord2f(1.0f, 1.0f);glVertex3f( +3.5, +3.0, 0.0f); //up right
  350. glTexCoord2f(1.0f, 0.0f);glVertex3f( +3.5, 1.0, 0.0f); //down right
  351. glEnd();
  352. if(nChoosen == 1)
  353. glColor3f(1.0f,0.0f,0.0f);
  354. else
  355. glColor3f(0.0f,0.0f,1.0f);
  356. glBindTexture(GL_TEXTURE_2D, texture[5]);
  357. glBegin(GL_POLYGON); //create polygon
  358. glTexCoord2f(0.0f, 0.0f);glVertex3f( -3.5, -1.5, 0.0f); //down left
  359. glTexCoord2f(0.0f, 1.0f);glVertex3f( -3.5, +0.5, 0.0f); //up left
  360. glTexCoord2f(1.0f, 1.0f);glVertex3f( +3.5, +0.5, 0.0f); //up right
  361. glTexCoord2f(1.0f, 0.0f);glVertex3f( +3.5, -1.5, 0.0f); //down right
  362. glEnd();
  363. if(nChoosen == 2)
  364. glColor3f(1.0f,0.0f,0.0f);
  365. else
  366. glColor3f(0.0f,0.0f,1.0f);
  367. glBindTexture(GL_TEXTURE_2D, texture[6]);
  368. glBegin(GL_POLYGON); //create polygon
  369. glTexCoord2f(0.0f, 0.0f);glVertex3f( -3.5, -4.0, 0.0f); //down left
  370. glTexCoord2f(0.0f, 1.0f);glVertex3f( -3.5, -2.0, 0.0f); //up left
  371. glTexCoord2f(1.0f, 1.0f);glVertex3f( +3.5, -2.0, 0.0f); //up right
  372. glTexCoord2f(1.0f, 0.0f);glVertex3f( +3.5, -4.0, 0.0f); //down right
  373. glEnd();
  374.  
  375. if(keys[VK_DOWN] && nChoosen+1 < 3 && b)
  376. {
  377. nChoosen += 1;
  378. b = FALSE;
  379. }
  380. if(!keys[VK_DOWN])
  381. {
  382. b = TRUE;
  383. }
  384. if(keys[VK_UP] && (nChoosen > 0) && b2)
  385. {
  386. nChoosen -= 1;
  387. b2 = FALSE;
  388. }
  389. if(!keys[VK_UP])
  390. b2 = TRUE;
  391. if(keys[10] || keys[13] && nChoosen==0)
  392. {
  393. Bmenu = FALSE;
  394. glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
  395. }
  396. else if(keys[10] || keys[13] && nChoosen==1)
  397. {
  398. Bmenu = FALSE;
  399. xPlace = 0.0f;
  400. yPlace = 3.0f;
  401. glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
  402. }
  403. else if(keys[10] || keys[13] && nChoosen==2)
  404. {
  405. done=TRUE;
  406. }
  407. return TRUE;
  408. }
  409. GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
  410. {
  411.  
  412. if (fullscreen) // Are We In Fullscreen Mode?
  413. {
  414. ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
  415. ShowCursor(TRUE); // Show Mouse Pointer
  416. }
  417.  
  418. if (hRC) // Do We Have A Rendering Context?
  419. {
  420. if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
  421. {
  422. MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  423. }
  424.  
  425. if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
  426. {
  427. MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  428. }
  429. hRC=NULL; // Set RC To NULL
  430. }
  431.  
  432. if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
  433. {
  434. MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  435. hDC=NULL; // Set DC To NULL
  436. }
  437.  
  438. if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
  439. {
  440. MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  441. hWnd=NULL; // Set hWnd To NULL
  442. }
  443.  
  444. if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
  445. {
  446. MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  447. hInstance=NULL; // Set hInstance To NULL
  448. }
  449. }
  450.  
  451. /* This Code Creates Our OpenGL Window. Parameters Are: *
  452. * title - Title To Appear At The Top Of The Window *
  453. * width - Width Of The GL Window Or Fullscreen Mode *
  454. * height - Height Of The GL Window Or Fullscreen Mode *
  455. * bits - Number Of Bits To Use For Color (8/16/24/32) *
  456. * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
  457.  
  458.  
  459. BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
  460. {
  461.  
  462. GLuint PixelFormat; // Holds The Results After Searching For A Match
  463. WNDCLASS wc; // Windows Class Structure
  464. DWORD dwExStyle; // Window Extended Style
  465. DWORD dwStyle; // Window Style
  466. RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
  467. WindowRect.left=(long)0; // Set Left Value To 0
  468. WindowRect.right=(long)width; // Set Right Value To Requested Width
  469. WindowRect.top=(long)0; // Set Top Value To 0
  470. WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
  471.  
  472.  
  473.  
  474. fullscreen=fullscreenflag; // Set The Global Fullscreen Flag
  475.  
  476. hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
  477. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
  478. wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
  479. wc.cbClsExtra = 0; // No Extra Window Data
  480. wc.cbWndExtra = 0; // No Extra Window Data
  481. wc.hInstance = hInstance; // Set The Instance
  482. wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
  483. wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
  484. wc.hbrBackground = NULL; // No Background Required For GL
  485. wc.lpszMenuName = NULL; // We Don't Want A Menu
  486. wc.lpszClassName = "OpenGL"; // Set The Class Name
  487.  
  488. if (!RegisterClass(&wc)) // Attempt To Register The Window Class
  489. {
  490. MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  491. return FALSE; // Return FALSE
  492. }
  493.  
  494. if (fullscreen) // Attempt Fullscreen Mode?
  495. {
  496. DEVMODE dmScreenSettings; // Device Mode
  497. memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
  498. dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
  499. dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
  500. dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
  501. dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
  502. dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  503.  
  504. // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
  505. if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
  506. {
  507. // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
  508. if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
  509. {
  510. fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
  511. }
  512. else
  513. {
  514. // Pop Up A Message Box Letting User Know The Program Is Closing.
  515. MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
  516. return FALSE; // Return FALSE
  517. }
  518. }
  519. }
  520.  
  521. if (fullscreen) // Are We Still In Fullscreen Mode?
  522. {
  523. dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
  524. dwStyle=WS_POPUP; // Windows Style
  525. ShowCursor(FALSE); // Hide Mouse Pointer
  526. }
  527. else
  528. {
  529. dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
  530. dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
  531. }
  532.  
  533. AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
  534.  
  535. // Create The Window
  536. if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
  537. "OpenGL", // Class Name
  538. title, // Window Title
  539. dwStyle | // Defined Window Style
  540. WS_CLIPSIBLINGS | // Required Window Style
  541. WS_CLIPCHILDREN, // Required Window Style
  542. 0, 0, // Window Position
  543. WindowRect.right-WindowRect.left, // Calculate Window Width
  544. WindowRect.bottom-WindowRect.top, // Calculate Window Height
  545. NULL, // No Parent Window
  546. NULL, // No Menu
  547. hInstance, // Instance
  548. NULL))) // Dont Pass Anything To WM_CREATE
  549. {
  550. KillGLWindow(); // Reset The Display
  551. MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  552. return FALSE; // Return FALSE
  553. }
  554.  
  555. static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
  556. {
  557. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  558. 1, // Version Number
  559. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  560. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  561. PFD_DOUBLEBUFFER, // Must Support Double Buffering
  562. PFD_TYPE_RGBA, // Request An RGBA Format
  563. bits, // Select Our Color Depth
  564. 0, 0, 0, 0, 0, 0, // Color Bits Ignored
  565. 0, // No Alpha Buffer
  566. 0, // Shift Bit Ignored
  567. 0, // No Accumulation Buffer
  568. 0, 0, 0, 0, // Accumulation Bits Ignored
  569. 16, // 16Bit Z-Buffer (Depth Buffer)
  570. 0, // No Stencil Buffer
  571. 0, // No Auxiliary Buffer
  572. PFD_MAIN_PLANE, // Main Drawing Layer
  573. 0, // Reserved
  574. 0, 0, 0 // Layer Masks Ignored
  575. };
  576.  
  577. if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
  578. {
  579. KillGLWindow(); // Reset The Display
  580. MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  581. return FALSE; // Return FALSE
  582. }
  583.  
  584. if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
  585. {
  586. KillGLWindow(); // Reset The Display
  587. MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  588. return FALSE; // Return FALSE
  589. }
  590.  
  591. if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
  592. {
  593. KillGLWindow(); // Reset The Display
  594. MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  595. return FALSE; // Return FALSE
  596. }
  597.  
  598. if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
  599. {
  600. KillGLWindow(); // Reset The Display
  601. MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  602. return FALSE; // Return FALSE
  603. }
  604.  
  605. if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
  606. {
  607. KillGLWindow(); // Reset The Display
  608. MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  609. return FALSE; // Return FALSE
  610. }
  611.  
  612. ShowWindow(hWnd,SW_SHOW); // Show The Window
  613. SetForegroundWindow(hWnd); // Slightly Higher Priority
  614. SetFocus(hWnd); // Sets Keyboard Focus To The Window
  615. ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
  616.  
  617. if (!InitGL()) // Initialize Our Newly Created GL Window
  618. {
  619. KillGLWindow(); // Reset The Display
  620. MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  621. return FALSE; // Return FALSE
  622. }
  623.  
  624. return TRUE; // Success
  625. }
  626.  
  627. LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
  628. UINT uMsg, // Message For This Window
  629. WPARAM wParam, // Additional Message Information
  630. LPARAM lParam) // Additional Message Information
  631. {
  632. switch (uMsg) // Check For Windows Messages
  633. {
  634. case WM_ACTIVATE: // Watch For Window Activate Message
  635. {
  636. if (!HIWORD(wParam)) // Check Minimization State
  637. {
  638. active=TRUE; // Program Is Active
  639. }
  640. else
  641. {
  642. active=FALSE; // Program Is No Longer Active
  643. }
  644.  
  645. return 0; // Return To The Message Loop
  646. }
  647. case WM_SYSCOMMAND: // Intercept System Commands
  648. {
  649. switch (wParam) // Check System Calls
  650. {
  651. case SC_SCREENSAVE: // Screensaver Trying To Start?
  652. case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
  653. return 0; // Prevent From Happening
  654. }
  655. break; // Exit
  656. }
  657. case WM_MOUSEMOVE:
  658. {
  659. xPos = LOWORD(lParam);
  660. yPos = HIWORD(lParam);
  661. xPos = xPos/(640.0f/13.0f)-6.5f;
  662. yPos = -yPos/(480.0f/13.0f) +6.5f;
  663. }
  664. case WM_LBUTTONDOWN:
  665. {
  666. if(wParam == MK_LBUTTON)
  667. mousecl = TRUE;
  668. else
  669. mousecl = FALSE;
  670. break;
  671.  
  672. }
  673.  
  674. case WM_CLOSE: // Did We Receive A Close Message?
  675. {
  676. PostQuitMessage(0); // Send A Quit Message
  677. return 0; // Jump Back
  678. }
  679.  
  680. case WM_KEYDOWN: // Is A Key Being Held Down?
  681. {
  682. keys[wParam] = TRUE; // If So, Mark It As TRUE
  683. return 0; // Jump Back
  684. }
  685.  
  686. case WM_KEYUP: // Has A Key Been Released?
  687. {
  688. keys[wParam] = FALSE; // If So, Mark It As FALSE
  689. return 0; // Jump Back
  690. }
  691.  
  692. case WM_SIZE: // Resize The OpenGL Window
  693. {
  694. ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height
  695. return 0; // Jump Back
  696. }
  697. }
  698.  
  699. // Pass All Unhandled Messages To DefWindowProc
  700. return DefWindowProc(hWnd,uMsg,wParam,lParam);
  701. }
  702.  
  703. int WINAPI WinMain( HINSTANCE hInstance, // Instance
  704. HINSTANCE hPrevInstance, // Previous Instance
  705. LPSTR lpCmdLine, // Command Line Parameters
  706. int nCmdShow) // Window Show State
  707. {
  708.  
  709. MSG msg; // Windows Message Structure
  710. // Bool Variable To Exit Loop
  711.  
  712. // Ask The User Which Screen Mode They Prefer
  713.  
  714. fullscreen=TRUE; // Windowed Mode
  715.  
  716.  
  717. // Create Our OpenGL Window
  718. if (!CreateGLWindow("Game",640,480,16,fullscreen))
  719. {
  720. return 0; // Quit If Window Was Not Created
  721. }
  722.  
  723. while(!done) // Loop That Runs While done=FALSE
  724. {
  725. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
  726. {
  727. if (msg.message==WM_QUIT) // Have We Received A Quit Message?
  728. {
  729. done=TRUE; // If So done=TRUE
  730. }
  731. else // If Not, Deal With Window Messages
  732. {
  733. TranslateMessage(&msg); // Translate The Message
  734. DispatchMessage(&msg); // Dispatch The Message
  735. }
  736. }
  737. else // If There Are No Messages
  738. {
  739. // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
  740. if (active) // Program Active?
  741. {
  742. ShowCursor(FALSE);
  743. if (keys[VK_ESCAPE] && Bmenu == TRUE && Be) // Was ESC Pressed?
  744. {
  745. done=TRUE; // ESC Signalled A Quit
  746. }
  747. else if(keys[VK_ESCAPE] && Bmenu == FALSE && Be)
  748. {
  749. Bmenu = TRUE;
  750. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  751. Be = FALSE;
  752. }
  753. else // Not Time To Quit, Update Screen
  754. {
  755. if(!keys[VK_ESCAPE])
  756. Be = TRUE;
  757. if(Bmenu == FALSE)
  758. DrawGLScene();
  759. else
  760. DrawMenuScene();// Draw The Scene
  761. SwapBuffers(hDC); // Swap Buffers (Double Buffering)
  762. }
  763. }
  764.  
  765. if (keys[VK_F1]) // Is F1 Being Pressed?
  766. {
  767. keys[VK_F1]=FALSE; // If So Make Key FALSE
  768. KillGLWindow(); // Kill Our Current Window
  769. fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
  770. // Recreate Our OpenGL Window
  771. if (!CreateGLWindow("Game",640,480,16,fullscreen))
  772. {
  773. return 0; // Quit If Window Was Not Created
  774. }
  775. }
  776. }
  777. }
  778.  
  779. // Shutdown
  780. KillGLWindow(); // Kill The Window
  781. return (msg.wParam); // Exit The Program
  782. }
Add Comment
Please, Sign In to add comment