Advertisement
Guest User

Untitled

a guest
Apr 18th, 2013
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.25 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #pragma once
  5. #include <windows.h>
  6. #include <gl/gl.h>
  7. #include <gl/glu.h>
  8.  
  9. #define PI (3.141592653589793)
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13. #include <math.h>
  14. using namespace std;
  15. #include "CG/Cg.h"
  16. #include "CG/CgGL.h"
  17.  
  18.  
  19.  
  20. #pragma comment(lib, "opengl32.lib")
  21. #pragma comment(lib, "glu32.lib")
  22.  
  23. #pragma comment(lib, "C:/Program Files (x86)/NVIDIA Corporation/Cg/lib/cg.lib")
  24. #pragma comment(lib, "C:/Program Files (x86)/NVIDIA Corporation/Cg/lib/cggl.lib")
  25.  
  26.  
  27.  
  28.  
  29.  
  30. #define WIDTH 800
  31. #define HEIGHT 600
  32. #define TOPLEFTX 0
  33. #define TOPLEFTY 0
  34. #define BORDERWIDTH 16
  35. #define BORDERHEIGHT 38
  36.  
  37.  
  38. #define ROTATION_SPEED 0.5f
  39. #define NUM_PLAYERS 2
  40. #define NUM_BULLETS 2
  41. #define NUM_POWERUPS 2
  42.  
  43.  
  44.  
  45.  
  46. //float tankVertexArray[] = {
  47. // -0.8f, -0.8f, 0.0f,
  48. // 0.8f, -0.8f, 0.0f,
  49. // -0.8f, 0.8f, 0.0f,
  50. //
  51. // 0.8f, 0.8f, 0.0f,
  52. // 0.8f, -0.8f, 0.0f,
  53. // -0.8f, 0.8f, 0.0f
  54. //};
  55.  
  56.  
  57.  
  58. float tankVertexArray[] = {
  59. -0.05f, -0.05f, 0.0f,
  60. 0.05f, -0.05f, 0.0f,
  61. -0.05f, 0.05f, 0.0f,
  62.  
  63. 0.05f, 0.05f, 0.0f,
  64. 0.05f, -0.05f, 0.0f,
  65. -0.05f, 0.05f, 0.0f
  66. };
  67.  
  68. float turretVertexArray[] = {
  69.  
  70.  
  71. -0.01f, 0.05f, 0.0f,
  72. -0.01f, 0.00f, 0.0f,
  73. 0.01f, 0.05f, 0.0f,
  74.  
  75. -0.01f, 0.00f, 0.0f,
  76. 0.01f, 0.05f, 0.0f,
  77. 0.01f, 0.00f, 0.0f
  78.  
  79. };
  80.  
  81. float bulletVertexArray[] = {
  82.  
  83.  
  84. -0.005f, -0.00f, 0.0f,
  85. 0.005f, -0.00f, 0.0f,
  86. -0.00f, 0.02f, 0.0f
  87.  
  88. };
  89.  
  90. float powerUpVertexArray[] = {
  91.  
  92.  
  93. -0.005f, -0.001f, 0.0f,
  94. 0.0f, -0.001f, 0.0f,
  95. -0.005f, 0.001f, 0.0f,
  96.  
  97. 0.0f, 0.001f, 0.0f,
  98. 0.0f, -0.001f, 0.0f,
  99. -0.005f, 0.001f, 0.0f
  100.  
  101. };
  102.  
  103.  
  104.  
  105. //All of the information about a Tank
  106. class tankStatus
  107. {
  108.  
  109. private:
  110.  
  111.  
  112.  
  113.  
  114. bool upPressed;
  115. bool downPressed;
  116. bool leftPressed;
  117. bool rightPressed;
  118. bool turretRotateLeftPressed;
  119. bool turretRotateRightPressed;
  120. bool shootPressed;
  121. bool bulletActive;
  122.  
  123. float maxSpeed;
  124. float curSpeed;
  125. float xCord;
  126. float yCord;
  127. float zCord;
  128. float zRotation; //How much tank has been rotated
  129. bool offScreenTopBot;
  130. bool offScreenSide;
  131.  
  132. float turretZRotation; //How much turret has been rotated
  133.  
  134. float color[4];
  135. float turretColor[4];
  136.  
  137. bool poweredUp;
  138. char powerType;
  139.  
  140.  
  141. public:
  142. tankStatus();
  143.  
  144.  
  145. //Getters and setters for the private variables
  146.  
  147. // setting and getting the variables holding whether the button to move the tank up or down has been pressed
  148. bool getUpPressed() { return upPressed; }
  149.  
  150. void setUpPressed(bool pressed) { upPressed= pressed; }
  151.  
  152. bool getDownPressed() { return downPressed; }
  153.  
  154. void setDownPressed(bool pressed) { downPressed= pressed; }
  155.  
  156. bool getLeftPressed() { return leftPressed; }
  157.  
  158. void setLeftPressed(bool pressed) { leftPressed= pressed; }
  159.  
  160. bool getRightPressed() { return rightPressed; }
  161.  
  162. void setRightPressed(bool pressed) { rightPressed= pressed; } //Setting and getting the variables holding whether the button to rotate the tank left or right has been pressed
  163.  
  164. bool getShootPressed() { return shootPressed; }
  165.  
  166. void setShootPressed(bool pressed) { shootPressed= pressed; }
  167.  
  168. bool getTurretLeftPressed() { return turretRotateLeftPressed; }
  169.  
  170. void setTurretLeftPressed(bool pressed) { turretRotateLeftPressed= pressed; }
  171.  
  172. bool getTurretRightPressed() { return turretRotateRightPressed; }
  173.  
  174. void setTurretRightPressed(bool pressed) { turretRotateRightPressed= pressed; }
  175.  
  176. bool getBulletActive() { return bulletActive; }
  177.  
  178. void setBulletActive(bool bullet) { bulletActive= bullet; }
  179.  
  180.  
  181. float getRotation() {return zRotation;}
  182.  
  183. void setRotation(float Rotation) {zRotation = Rotation;}
  184.  
  185. void setTurretRotation(float Rotation) {turretZRotation = Rotation;}
  186.  
  187. float getTurretRotation() {return turretZRotation ;}
  188.  
  189. float getXPosition() {return xCord;}
  190.  
  191. void setXPosition(float x){
  192. if (x<-0.50||x>0.50)
  193. {
  194. offScreenSide = true;
  195. xCord = x;
  196. } else{
  197. xCord = x;
  198. offScreenSide = false;
  199. }
  200.  
  201. }
  202.  
  203. float getYPosition() {return yCord;}
  204.  
  205. void setYPosition(float y){
  206. if (y<-0.8||y>0.8)
  207. {
  208. offScreenTopBot = true;
  209. yCord = y;
  210. } else{
  211. yCord = y;
  212. offScreenTopBot = false;
  213. }
  214. }
  215.  
  216. float getZPosition() {return zCord;}
  217.  
  218. void setZPosition(float z) {zCord = z;}
  219.  
  220. float getSpeed() {return curSpeed;}
  221.  
  222. void setSpeed(float speed){
  223.  
  224. if(speed>0 &&speed<=maxSpeed||speed<0 &&speed>= -maxSpeed){
  225. curSpeed=speed;
  226. }
  227.  
  228. }
  229.  
  230. // float getMaxSpeed(){return maxSpeed;}
  231.  
  232. float getColor(int i) {return color[i];}
  233.  
  234. void setColor(int i,float colors) {color[i]=colors;}
  235.  
  236. float getTurretColor(int i) {return turretColor[i];}
  237.  
  238. bool getOffTopBot() {return offScreenTopBot;}
  239.  
  240. bool getOffSide() {return offScreenSide;}
  241.  
  242.  
  243.  
  244. };
  245.  
  246. //All the Information about a Bullet
  247. class bulletStatus{
  248.  
  249. private:
  250. float bulletSpeed;
  251. float xCord;
  252. float yCord;
  253. float zCord;
  254. float rotation;
  255.  
  256. bool offScreenTopBot;
  257. bool offScreenSide;
  258.  
  259. public:
  260. bulletStatus();
  261.  
  262. float getXPosition() {return xCord;}
  263.  
  264. void setXPosition(float x){
  265. if (x<-0.50||x>0.50)
  266. {
  267. offScreenSide = true;
  268. xCord = x;
  269. } else{
  270. xCord = x;
  271. offScreenSide = false;
  272. }
  273.  
  274. }
  275.  
  276. float getYPosition() {return yCord;}
  277.  
  278. void setYPosition(float y){
  279. if (y<-0.8||y>0.8)
  280. {
  281. offScreenTopBot = true;
  282. yCord = y;
  283. } else{
  284. yCord = y;
  285. offScreenTopBot = false;
  286. }
  287. }
  288.  
  289. float getZPosition() {return zCord;}
  290.  
  291. void setZPosition(float z) {zCord = z;}
  292.  
  293. float getRotation() {return rotation;}
  294.  
  295. void setRotation(float Rotation) {rotation = Rotation;}
  296.  
  297. float getBulletSpeed() {return bulletSpeed;}
  298.  
  299. void setBulletSpeed(float Speed) { bulletSpeed=Speed;}
  300.  
  301.  
  302.  
  303. };
  304.  
  305. //All the Information about a PowerUp
  306. class powerUpStatus{
  307.  
  308. private:
  309. float xCord; //X, Y and z location of the power up
  310. float yCord;
  311. float zCord;
  312. char powerType; //The type of power up s for speed, i for invincibility
  313. public:
  314. powerUpStatus();
  315.  
  316.  
  317. };
  318.  
  319. //All the Information about a Game state
  320. class gameStatus
  321. {
  322. public:
  323. gameStatus(CGprofile vProfile, CGprofile fProfile, CGprogram vProgram, CGprogram fProgram); // the constructor
  324. ~gameStatus();
  325.  
  326.  
  327. tankStatus** tankArray;
  328. bulletStatus** bulletArray;
  329. powerUpStatus** powerUpArray;
  330.  
  331. CGprofile vShaderProfile; // vertex shader profile to use
  332. CGprofile fShaderProfile; // fragment shader profile to use
  333. CGprogram vShaderProgram; // vertex shader program to use
  334. CGprogram fShaderProgram; // fragment program to use
  335.  
  336. CGparameter mvMatrix; // handle to the mv matrix in the vertex shader
  337. CGparameter fragColor; // handle to the color in the fragment shader
  338.  
  339.  
  340. };
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  350. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  351. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  352.  
  353. // Function prototypes.
  354.  
  355. // these are our ones...
  356. void updateGame(HDC deviceContext, gameStatus* currentGame); // update the game
  357. void draw(HDC deviceContext, gameStatus* currentGame); // drawing function containing OpenGL function calls
  358. char* loadShader(string filePath); // compiling shader files
  359. void drawTurret(HDC deviceContext, gameStatus* currentGame);
  360. void drawTurret(HDC deviceContext, gameStatus* currentGame);
  361. bool checkCollisions(int i,HDC deviceContext,gameStatus* currentGame);
  362.  
  363. // these were already here...
  364. LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
  365. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );
  366.  
  367. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  368. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  369. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  370.  
  371.  
  372.  
  373. void updateGame(HDC deviceContext, gameStatus* currentGame)
  374. {
  375.  
  376.  
  377. for (int i = 0; i < NUM_PLAYERS; i++){
  378. if (checkCollisions(i,deviceContext,currentGame)){
  379. currentGame->tankArray[i]->setSpeed(0);
  380. }else {
  381. float rotationRadians = currentGame-> tankArray[i]->getRotation();
  382. float tankrotation = currentGame-> tankArray[i]->getTurretRotation();
  383.  
  384. if(!(currentGame-> tankArray[i]->getRightPressed()&&currentGame-> tankArray[i]->getLeftPressed()||currentGame-> tankArray[i]->getTurretLeftPressed()&&currentGame-> tankArray[i]->getTurretRightPressed())){
  385.  
  386. if (currentGame-> tankArray[i]->getRightPressed()) {currentGame-> tankArray[i]->setRotation(rotationRadians-ROTATION_SPEED) ;}
  387. if (currentGame-> tankArray[i]->getLeftPressed()) {currentGame-> tankArray[i]->setRotation( rotationRadians+ ROTATION_SPEED);}
  388. if (currentGame-> tankArray[i]->getTurretRightPressed()) {currentGame-> tankArray[i]->setTurretRotation(tankrotation + ROTATION_SPEED);}
  389. if (currentGame-> tankArray[i]->getTurretLeftPressed()) {currentGame-> tankArray[i]->setTurretRotation(tankrotation - ROTATION_SPEED) ;}
  390. }
  391.  
  392. float tankSpeed = currentGame->tankArray[i]->getSpeed();
  393.  
  394. if(!(currentGame-> tankArray[i]->getUpPressed()&&currentGame-> tankArray[i]->getDownPressed())){
  395.  
  396. if(currentGame->tankArray[i]->getUpPressed()) {
  397. currentGame->tankArray[i]->setSpeed(tankSpeed+0.00001f);
  398.  
  399. }
  400. if(currentGame->tankArray[i]->getDownPressed()) {
  401. currentGame->tankArray[i]->setSpeed(tankSpeed-0.00001f);
  402. }
  403.  
  404. if(!currentGame->tankArray[i]->getUpPressed()&&!currentGame->tankArray[i]->getDownPressed()&&tankSpeed!=0){
  405.  
  406. if(tankSpeed<0.00001f &&tankSpeed>0){
  407.  
  408. currentGame->tankArray[i]->setSpeed(0);
  409. }else if(tankSpeed>-0.00001f &&tankSpeed<0){
  410.  
  411. currentGame->tankArray[i]->setSpeed(0);
  412. }
  413.  
  414.  
  415. if(currentGame->tankArray[i]->getSpeed()<0){
  416.  
  417. currentGame->tankArray[i]->setSpeed(tankSpeed+0.00001f);
  418.  
  419. }else if(currentGame->tankArray[i]->getSpeed()>0){
  420.  
  421. currentGame->tankArray[i]->setSpeed(tankSpeed-0.00001f);
  422.  
  423. }
  424.  
  425. }
  426.  
  427. }
  428.  
  429.  
  430.  
  431.  
  432. rotationRadians = currentGame-> tankArray[i]->getRotation()* PI / 180 ; //updating rotation in radians
  433. tankSpeed = currentGame->tankArray[i]->getSpeed(); //getting speed in case it was changed
  434.  
  435. float oldx = currentGame->tankArray[i]->getXPosition(); //get current x position
  436. float oldy= currentGame->tankArray[i]->getYPosition(); //get current y position
  437.  
  438. currentGame->tankArray[i]->setXPosition(oldx-(tankSpeed*sin(rotationRadians))); //set new x position
  439. currentGame->tankArray[i]->setYPosition(oldy+(tankSpeed*cos(rotationRadians))); //set new y position
  440.  
  441. if (currentGame->tankArray[i]->getBulletActive())
  442. {
  443. float bulletSpeed = currentGame->bulletArray[i]->getBulletSpeed();
  444. float bulletRotationRadians = currentGame-> tankArray[i]->getRotation()* PI / 180;
  445.  
  446. float bulletOldx = currentGame->bulletArray[i]->getXPosition();
  447. float bulletOldy= currentGame->bulletArray[i]->getYPosition();
  448.  
  449. currentGame->bulletArray[i]->setXPosition( bulletOldx-(bulletSpeed*sin( bulletRotationRadians)));
  450. currentGame->bulletArray[i]->setYPosition( bulletOldy+(bulletSpeed*cos( bulletRotationRadians)));
  451. }
  452.  
  453. }
  454.  
  455. }// end of the for loop updating the game for each tank
  456.  
  457. draw(deviceContext, currentGame);
  458.  
  459. }
  460.  
  461.  
  462. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  463. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  464. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  465.  
  466.  
  467.  
  468.  
  469. void drawPlayer(HDC deviceContext, gameStatus* currentGame)
  470. {
  471.  
  472. for (int i = 0; i < NUM_PLAYERS; i++)
  473. {
  474.  
  475.  
  476. glVertexPointer(3, GL_FLOAT, 0, tankVertexArray);
  477. float xlocation =currentGame->tankArray[0]->getXPosition();
  478. float ylocation =currentGame->tankArray[0]->getYPosition();
  479. float zlocation = -1;
  480. glPushMatrix();
  481.  
  482. glLoadIdentity();
  483. glTranslatef(xlocation,ylocation,zlocation);
  484.  
  485.  
  486. glRotatef(currentGame->tankArray[i]->getRotation(), 0.0, 0.0, 1.0);
  487.  
  488.  
  489. cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
  490. cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getColor(0),currentGame->tankArray[i]->getColor(1),currentGame->tankArray[i]->getColor(2),currentGame->tankArray[i]->getColor(3));
  491.  
  492.  
  493. glDrawArrays(GL_TRIANGLES, 0, 6);
  494.  
  495. glPopMatrix();
  496.  
  497.  
  498. }
  499.  
  500. //for (int i = 0; i < NUM_PLAYERS; i++)
  501. //{
  502. //
  503. //
  504. // glVertexPointer(3, GL_FLOAT, 0, tankVertexArray);
  505. // float xlocation = currentGame->tankArray[i]->getXPosition();
  506. // float ylocation =currentGame->tankArray[i]->getYPosition();
  507. // float zlocation = currentGame->tankArray[i]->getZPosition();
  508. // glPushMatrix();
  509. // glLoadIdentity();
  510. // glTranslatef(xlocation,ylocation,zlocation);
  511.  
  512. //
  513. // glRotatef(currentGame->tankArray[i]->getRotation(), 0.0, 0.0, 1.0);
  514.  
  515.  
  516. // cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
  517. // cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getColor(0),currentGame->tankArray[i]->getColor(1),currentGame->tankArray[i]->getColor(2),currentGame->tankArray[i]->getColor(3));
  518.  
  519. // glDrawArrays(GL_TRIANGLES, 0, 6);
  520.  
  521. // glPopMatrix();
  522.  
  523. // if (currentGame->tankArray[i]->getOffSide())
  524. // {
  525. // glPushMatrix();
  526. // glLoadIdentity();
  527. // glTranslatef(-xlocation,ylocation,zlocation);
  528.  
  529. // glRotatef(currentGame->tankArray[i]->getRotation(), 0.0, 0.0, 1.0);
  530.  
  531.  
  532. // cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
  533. // cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getColor(0),currentGame->tankArray[i]->getColor(1),currentGame->tankArray[i]->getColor(2),currentGame->tankArray[i]->getColor(3));
  534.  
  535. // glDrawArrays(GL_TRIANGLES, 0, 6);
  536.  
  537. // glPopMatrix();
  538.  
  539.  
  540. // }else if (currentGame->tankArray[i]->getOffTopBot())
  541. // {
  542. // glPushMatrix();
  543. // glLoadIdentity();
  544. // glTranslatef(xlocation,-ylocation,zlocation);
  545.  
  546. // // then rotate to the current player's rotation around z axis
  547. // glRotatef(currentGame->tankArray[i]->getRotation(), 0.0, 0.0, 1.0);
  548.  
  549.  
  550. // cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
  551. // cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getColor(0),currentGame->tankArray[i]->getColor(1),currentGame->tankArray[i]->getColor(2),currentGame->tankArray[i]->getColor(3));
  552.  
  553. // glDrawArrays(GL_TRIANGLES, 0, 6);
  554.  
  555. // glPopMatrix();
  556. // }
  557. //
  558. //}
  559. }
  560.  
  561. void drawTurret(HDC deviceContext, gameStatus* currentGame)
  562. {
  563.  
  564. for (int i = 0; i < NUM_PLAYERS; i++)
  565. {
  566.  
  567. glVertexPointer(3, GL_FLOAT, 0, turretVertexArray);
  568. glPushMatrix();
  569.  
  570. glLoadIdentity();
  571. glTranslatef(currentGame->tankArray[i]->getXPosition(),currentGame->tankArray[i]->getYPosition(),currentGame->tankArray[i]->getZPosition());
  572.  
  573.  
  574. glRotatef(currentGame->tankArray[i]->getTurretRotation(), 0.0, 0.0, 1.0);
  575.  
  576.  
  577. cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
  578. cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getTurretColor(0),currentGame->tankArray[i]->getTurretColor(1),currentGame->tankArray[i]->getTurretColor(2),currentGame->tankArray[i]->getTurretColor(3));
  579.  
  580. glDrawArrays(GL_TRIANGLES, 0, 6);
  581.  
  582. glPopMatrix();
  583. }
  584.  
  585. }
  586.  
  587. void drawBullet(HDC deviceContext, gameStatus* currentGame)
  588. {
  589. for (int i = 0; i < NUM_BULLETS; i++)
  590. {
  591. if (currentGame->tankArray[i]->getBulletActive())
  592. {
  593.  
  594. glVertexPointer(3, GL_FLOAT, 0, bulletVertexArray);
  595. glPushMatrix();
  596.  
  597. glLoadIdentity();
  598. glTranslatef(currentGame->bulletArray[i]->getXPosition(),currentGame->bulletArray[i]->getYPosition(),currentGame->bulletArray[i]->getZPosition());
  599. // then rotate to the current player's rotation around z axis
  600. glRotatef(currentGame->bulletArray[i]->getRotation(), 0.0, 0.0, 1.0);
  601.  
  602.  
  603. cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
  604. //cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[0]->getTurretColor(0),currentGame->tankArray[0]->getTurretColor(1),currentGame->tankArray[0]->getTurretColor(2),currentGame->tankArray[0]->getTurretColor(3));
  605.  
  606. glDrawArrays(GL_TRIANGLES, 0, 3);
  607.  
  608. glPopMatrix();
  609.  
  610.  
  611.  
  612. }
  613.  
  614. // restore the old matrix state, leaving things as we found them!
  615. }
  616.  
  617. }
  618.  
  619.  
  620.  
  621. void draw(HDC deviceContext, gameStatus* currentGame)
  622. {
  623.  
  624. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  625.  
  626. glEnableClientState(GL_VERTEX_ARRAY);
  627.  
  628. glMatrixMode(GL_MODELVIEW);
  629.  
  630.  
  631. cgGLEnableProfile(currentGame->vShaderProfile);
  632. cgGLEnableProfile(currentGame->fShaderProfile);
  633. cgGLBindProgram(currentGame->vShaderProgram);
  634. cgGLBindProgram(currentGame->fShaderProgram);
  635.  
  636.  
  637. drawPlayer(deviceContext, currentGame);
  638. //drawTurret(deviceContext, currentGame);
  639. //drawBullet(deviceContext, currentGame);
  640.  
  641.  
  642.  
  643. cgGLUnbindProgram(currentGame->vShaderProfile);
  644. cgGLUnbindProgram(currentGame->fShaderProfile);
  645. cgGLDisableProfile(currentGame->vShaderProfile);
  646. cgGLDisableProfile(currentGame->fShaderProfile);
  647.  
  648.  
  649. glDisableClientState(GL_VERTEX_ARRAY);
  650.  
  651. SwapBuffers(deviceContext);
  652.  
  653. }
  654.  
  655. void createBullet(int tankID, gameStatus* currentGame){
  656.  
  657. if (!currentGame->tankArray[tankID]->getBulletActive() )
  658. {
  659. currentGame->bulletArray[tankID] = new bulletStatus;
  660.  
  661. float tankRotation= currentGame->tankArray[tankID]->getTurretRotation();
  662. float tankX = currentGame->tankArray[tankID]->getXPosition();
  663. float tankY = currentGame->tankArray[tankID]->getYPosition();
  664. float tankZ = currentGame->tankArray[tankID]->getZPosition();
  665. float tankSpeed = currentGame->tankArray[tankID]->getSpeed();
  666.  
  667. currentGame->bulletArray[tankID]->setRotation(tankRotation);
  668.  
  669. currentGame->bulletArray[tankID]->setXPosition(tankX);
  670. currentGame->bulletArray[tankID]->setYPosition(tankY);
  671. currentGame->bulletArray[tankID]->setZPosition(tankZ);
  672. //currentGame->bulletArray[tankID]->setBulletSpeed(tankSpeed);
  673.  
  674. currentGame->tankArray[tankID]->setBulletActive(true);
  675. }
  676.  
  677.  
  678. }
  679.  
  680. bool checkCollisions(int count,HDC deviceContext,gameStatus* currentGame){
  681.  
  682. int holdCount = count;
  683. float xCord1= currentGame->tankArray[count]->getXPosition();
  684. float yCord1= currentGame->tankArray[count]->getYPosition();
  685. float xCord2=0;
  686. float yCord2=0;
  687.  
  688. for(int i=0; i<(NUM_PLAYERS-1);i++){
  689.  
  690. if (!count==i)
  691. {
  692. xCord2= currentGame->tankArray[i]->getXPosition();
  693. yCord2= currentGame->tankArray[i]->getYPosition();
  694. }
  695.  
  696. }
  697.  
  698. if(fabs(xCord1-xCord2 )<= 0.00 &&fabs(yCord1-yCord2)<=0.0)
  699. {
  700. return true;
  701.  
  702. }
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709. return false;
  710. }
  711.  
  712. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  713. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  714. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  715.  
  716.  
  717. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
  718. {
  719.  
  720.  
  721.  
  722. WNDCLASS myWindowClass;
  723. myWindowClass.cbClsExtra = 0;
  724. myWindowClass.cbWndExtra = 0;
  725. myWindowClass.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
  726. myWindowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
  727. myWindowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  728. myWindowClass.hInstance = hInstance;
  729. myWindowClass.lpfnWndProc = WndProc;
  730. myWindowClass.lpszClassName = TEXT("my window class name");
  731. myWindowClass.lpszMenuName = 0;
  732. myWindowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  733.  
  734. RegisterClass(&myWindowClass);
  735.  
  736.  
  737.  
  738. RECT rect;
  739. SetRect( &rect, TOPLEFTX,
  740. TOPLEFTY,
  741. TOPLEFTX + WIDTH,
  742. TOPLEFTY + HEIGHT );
  743.  
  744.  
  745.  
  746.  
  747. AdjustWindowRect( &rect, WS_OVERLAPPEDWINDOW, false );
  748.  
  749.  
  750.  
  751.  
  752. HWND myWindow = CreateWindow(TEXT("my window class name"),
  753. TEXT("OpenGL, No GLUT"),
  754. WS_OVERLAPPEDWINDOW,
  755. TOPLEFTX, TOPLEFTY,
  756. TOPLEFTX + WIDTH+BORDERWIDTH, TOPLEFTY + HEIGHT+BORDERHEIGHT,
  757. NULL, NULL,
  758. hInstance, NULL);
  759.  
  760.  
  761.  
  762. if( myWindow == NULL )
  763. {
  764. FatalAppExit( NULL, TEXT("CreateWindow() failed!") );
  765. }
  766.  
  767.  
  768. ShowWindow( myWindow, iCmdShow );
  769.  
  770.  
  771.  
  772. HDC myDeviceContext = GetDC( myWindow );
  773.  
  774.  
  775. PIXELFORMATDESCRIPTOR myPfd = { 0 };
  776.  
  777.  
  778. myPfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );
  779. myPfd.nVersion = 1;
  780.  
  781. myPfd.dwFlags = PFD_SUPPORT_OPENGL |
  782. PFD_DOUBLEBUFFER |
  783. PFD_DRAW_TO_WINDOW;
  784.  
  785. myPfd.iPixelType = PFD_TYPE_RGBA ;
  786. myPfd.cColorBits = 24;
  787.  
  788.  
  789. myPfd.cDepthBits = 32;
  790.  
  791.  
  792.  
  793.  
  794. int chosenPixelFormat = ChoosePixelFormat( myDeviceContext, &myPfd );
  795.  
  796.  
  797. if( chosenPixelFormat == 0 )
  798. {
  799. FatalAppExit( NULL, TEXT("ChoosePixelFormat() failed!") );
  800. }
  801.  
  802.  
  803. int result = SetPixelFormat( myDeviceContext, chosenPixelFormat, &myPfd );
  804.  
  805.  
  806. if (result == NULL)
  807. {
  808. FatalAppExit( NULL, TEXT("SetPixelFormat() failed!") );
  809. }
  810.  
  811.  
  812.  
  813. HGLRC myRenderContext = wglCreateContext( myDeviceContext );
  814.  
  815.  
  816. wglMakeCurrent( myDeviceContext, myRenderContext );
  817.  
  818.  
  819.  
  820. glMatrixMode(GL_PROJECTION);
  821. glLoadIdentity();
  822. gluPerspective(45.0,(float)WIDTH/(float)HEIGHT, 1, 1000);
  823. glViewport(0, 0, WIDTH, HEIGHT);
  824. glClearColor( 0.0, 0, 0, 0 );
  825.  
  826.  
  827. CGcontext context = cgCreateContext();
  828.  
  829. CGprofile vProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
  830. CGprofile fProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
  831. cgGLSetOptimalOptions(vProfile);
  832. cgGLSetOptimalOptions(fProfile);
  833.  
  834. char* myVertexProgram = loadShader("../shaders/myVertexProgram.cg");
  835. char* myFragmentProgram = loadShader("../shaders/myFragmentProgram.cg");
  836.  
  837. CGprogram vProgram = cgCreateProgram(context, CG_SOURCE, myVertexProgram, vProfile, "main", NULL);
  838. CGprogram fProgram = cgCreateProgram(context, CG_SOURCE, myFragmentProgram, fProfile, "main", NULL);
  839.  
  840.  
  841. cgGLLoadProgram(vProgram);
  842. cgGLLoadProgram(fProgram);
  843.  
  844. MSG msg;
  845. bool needToQuit = false;
  846. gameStatus* currentGame = new gameStatus(vProfile, fProfile, vProgram, fProgram);
  847.  
  848.  
  849. while( !needToQuit )
  850. {
  851. // we need to listen out for OS messages.
  852. // if there is a windows message to process...
  853. if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  854. {
  855.  
  856.  
  857. // if the message was a "quit" message...
  858. if( msg.message == WM_QUIT )
  859. {
  860. needToQuit = true; // we want to quit asap
  861. }
  862.  
  863.  
  864. // if it was a "key pressed" message...
  865. else if (msg.message == WM_KEYDOWN)
  866. {
  867. switch (msg.wParam)
  868. {
  869.  
  870. case 'W':
  871.  
  872. currentGame->tankArray[0]->setUpPressed(true);
  873.  
  874. break;
  875.  
  876.  
  877. case 'A':
  878.  
  879. currentGame->tankArray[0]->setLeftPressed(true);
  880.  
  881. break;
  882.  
  883. case 'S':
  884.  
  885. currentGame->tankArray[0]->setDownPressed(true);
  886.  
  887. break;
  888.  
  889.  
  890. case 'D':
  891.  
  892. currentGame->tankArray[0]->setRightPressed(true);
  893.  
  894. break;
  895.  
  896. case 'Q':
  897.  
  898. currentGame->tankArray[0]->setTurretRightPressed(true);
  899.  
  900. break;
  901. case 'E':
  902.  
  903. currentGame->tankArray[0]->setTurretLeftPressed(true);
  904.  
  905. break;
  906.  
  907. case '2':
  908.  
  909. createBullet(0,currentGame);
  910.  
  911. break;
  912.  
  913. default:
  914.  
  915. break;
  916.  
  917. }
  918. }
  919. // if they let go of a key
  920. else if (msg.message == WM_KEYUP)
  921. {
  922. switch (msg.wParam)
  923. {
  924.  
  925. case 'W':
  926.  
  927. currentGame->tankArray[0]->setUpPressed(false);
  928.  
  929. break;
  930.  
  931.  
  932. case 'A':
  933.  
  934. currentGame->tankArray[0]->setLeftPressed(false);
  935.  
  936. break;
  937.  
  938. case 'S':
  939.  
  940. currentGame->tankArray[0]->setDownPressed(false);
  941.  
  942. break;
  943.  
  944.  
  945. case 'D':
  946.  
  947. currentGame->tankArray[0]->setRightPressed(false);
  948.  
  949. case 'Q':
  950.  
  951. currentGame->tankArray[0]->setTurretRightPressed(false);
  952.  
  953. break;
  954.  
  955. case 'E':
  956.  
  957. currentGame->tankArray[0]->setTurretLeftPressed(false);
  958.  
  959. break;
  960.  
  961.  
  962. default:
  963.  
  964. break;
  965.  
  966. }
  967. }
  968. // if it was any other type of message
  969. else
  970. {
  971.  
  972. TranslateMessage( &msg );
  973. DispatchMessage( &msg );
  974.  
  975. }
  976.  
  977.  
  978.  
  979. }
  980.  
  981.  
  982. updateGame(myDeviceContext, currentGame);
  983.  
  984.  
  985. }
  986.  
  987. delete currentGame;
  988. currentGame = NULL;
  989.  
  990.  
  991. cgDestroyContext(context);
  992.  
  993.  
  994. wglMakeCurrent( NULL, NULL );
  995.  
  996. wglDeleteContext( myRenderContext );
  997.  
  998.  
  999. ReleaseDC( myWindow, myDeviceContext );
  1000.  
  1001.  
  1002. return msg.wParam;
  1003. }
  1004.  
  1005. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1006. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1007. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1008.  
  1009.  
  1010.  
  1011. LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
  1012. {
  1013.  
  1014. switch( message )
  1015. {
  1016. // if they exited the window...
  1017. case WM_DESTROY:
  1018. // post a message "quit" message to the main windows loop
  1019. PostQuitMessage( 0 ) ;
  1020. return 0;
  1021. break;
  1022. }
  1023.  
  1024. return DefWindowProc( hwnd, message, wparam, lparam );
  1025.  
  1026. }
  1027.  
  1028.  
  1029.  
  1030. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1031. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1032. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1033.  
  1034. char* loadShader(string filePath)
  1035. {
  1036.  
  1037.  
  1038. ifstream file(filePath.c_str(), ios::in | ios::binary);
  1039.  
  1040.  
  1041. if(!file)
  1042. {
  1043. cout << "shader text file not found" << endl;
  1044. return 0;
  1045. }
  1046.  
  1047.  
  1048. int size = 0;
  1049.  
  1050. file.seekg(0,ios::end);
  1051. size = file.tellg();
  1052.  
  1053. if(!size)
  1054. {
  1055. cout << "shader file is empty!" << endl;
  1056. }
  1057.  
  1058. char* data = 0;
  1059.  
  1060. data = new char[size+1];
  1061.  
  1062. file.seekg(0,ios::beg);
  1063.  
  1064.  
  1065. file.read(data, size);
  1066.  
  1067.  
  1068. data[size] = '\0';
  1069.  
  1070. return data;
  1071.  
  1072.  
  1073. }
  1074.  
  1075.  
  1076.  
  1077.  
  1078. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1079. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1080. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1081.  
  1082.  
  1083. // this sets up the default values that are common to all game instances
  1084. gameStatus::gameStatus(CGprofile vProfile, CGprofile fProfile, CGprogram vProgram, CGprogram fProgram)
  1085. {
  1086. //An array of pointers to Tanks
  1087. tankArray = new tankStatus* [NUM_PLAYERS];
  1088.  
  1089.  
  1090. //Allocate memory for each tank
  1091. for (int i = 0; i < NUM_PLAYERS; i++)
  1092. {
  1093. tankArray[i] = new tankStatus;
  1094. }
  1095.  
  1096.  
  1097. tankArray[0]->setXPosition(-0.2f);
  1098. tankArray[1]->setXPosition(0.2f);
  1099.  
  1100. // set the colors...
  1101. tankArray[0]->setColor(0, 1.0f);
  1102. tankArray[0]->setColor(1, 0.0f);
  1103. tankArray[0]->setColor(2, 1.0f);
  1104. tankArray[0]->setColor(3, 1.0f);
  1105.  
  1106. tankArray[1]->setColor(0, 0.5f);
  1107. tankArray[1]->setColor(1, 0.0f);
  1108. tankArray[1]->setColor(2, 1.0f);
  1109. tankArray[1]->setColor(3, 1.0f);
  1110.  
  1111.  
  1112. //An array of pointers to Bullets
  1113.  
  1114. bulletArray = new bulletStatus* [NUM_BULLETS];
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121. //An array of pointers to PowerUps
  1122. /*powerUpArray = new powerUpStatus* [NUM_POWERUPS];
  1123.  
  1124.  
  1125.  
  1126. for (int i = 0; i < NUM_POWERUPS; i++)
  1127. {
  1128. powerUpArray[i] = new powerUpStatus;
  1129.  
  1130.  
  1131. }*/
  1132.  
  1133. //playerArray[0]->xPos = -2.0f;
  1134. //playerArray[1]->xPos = 1.0f;
  1135.  
  1136. // set the shaders to be used in the game
  1137. vShaderProfile = vProfile;
  1138. fShaderProfile = fProfile;
  1139. vShaderProgram = vProgram;
  1140. fShaderProgram = fProgram;
  1141.  
  1142.  
  1143. mvMatrix = cgGetNamedParameter(vShaderProgram,"mvMatrix");
  1144. fragColor = cgGetNamedParameter(fShaderProgram, "reqColor");
  1145.  
  1146. }
  1147.  
  1148.  
  1149. gameStatus::~gameStatus()
  1150. {
  1151.  
  1152. //Cleaning up bulletArray
  1153. for (int i = 0; i < NUM_BULLETS; i++)
  1154. {
  1155. if (tankArray[i]->getBulletActive())
  1156. {
  1157. delete bulletArray[i];
  1158. bulletArray[i] = NULL;
  1159. }
  1160.  
  1161.  
  1162.  
  1163. }
  1164.  
  1165. delete[] bulletArray;
  1166. bulletArray = NULL;
  1167.  
  1168. //Cleaning up powerUpArray
  1169.  
  1170. for (int i = 0; i < NUM_POWERUPS; i++)
  1171. {
  1172. delete powerUpArray[i];
  1173. powerUpArray[i] = NULL;
  1174.  
  1175. }
  1176.  
  1177. delete[] powerUpArray;
  1178. powerUpArray = NULL;
  1179.  
  1180.  
  1181. for (int i = 0; i < NUM_PLAYERS; i++)
  1182. {
  1183. delete tankArray[i];
  1184. tankArray[i] = NULL;
  1185. }
  1186. delete[] tankArray;
  1187. tankArray = NULL;
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194. cgDestroyProgram(vShaderProgram);
  1195. cgDestroyProgram(fShaderProgram);
  1196. cgDestroyParameter(mvMatrix);
  1197. cgDestroyParameter(fragColor);
  1198.  
  1199. }
  1200.  
  1201. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1202. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1203. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1204.  
  1205.  
  1206. // this sets up the default values that are common to all players
  1207.  
  1208. tankStatus:: tankStatus()
  1209. {
  1210. upPressed = false;
  1211. downPressed = false;
  1212. leftPressed = false;
  1213. rightPressed = false;
  1214. zRotation = 0.0f;
  1215. maxSpeed = 0.002f;
  1216. curSpeed= 0.0000f;
  1217.  
  1218. xCord = 0.0f;
  1219. yCord = 0.0f;
  1220. zCord = -2.0f;
  1221.  
  1222. turretRotateLeftPressed= false;
  1223. turretRotateRightPressed= false;
  1224.  
  1225. turretZRotation= 0.0f;
  1226. turretColor[0]=1.0f;
  1227. turretColor[1]=0.0f;
  1228. turretColor[2]=0.0f;
  1229. turretColor[3]=1.0f;
  1230.  
  1231. offScreenTopBot = false;
  1232. offScreenSide= false;
  1233.  
  1234. shootPressed=false;
  1235. bulletActive=false;
  1236.  
  1237. poweredUp = false;
  1238. };
  1239.  
  1240. bulletStatus:: bulletStatus()
  1241. {
  1242.  
  1243. rotation = 0.0f;
  1244. bulletSpeed = 0.0025f; //more than the max speed of tanks
  1245.  
  1246. xCord = 0.0f;
  1247. yCord = 0.0f;
  1248. zCord = -2.0f;
  1249. offScreenTopBot = false;
  1250. offScreenSide= false;
  1251. };
  1252.  
  1253.  
  1254. powerUpStatus:: powerUpStatus()
  1255. {
  1256.  
  1257. xCord = 0.0f;
  1258. yCord = 0.0f;
  1259. zCord = -2.0f;
  1260.  
  1261.  
  1262. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement