Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <windows.h>
- #include <gl/gl.h>
- #include <gl/glu.h>
- #define PI (3.141592653589793)
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <math.h>
- using namespace std;
- #include "CG/Cg.h"
- #include "CG/CgGL.h"
- #pragma comment(lib, "opengl32.lib")
- #pragma comment(lib, "glu32.lib")
- #pragma comment(lib, "C:/Program Files (x86)/NVIDIA Corporation/Cg/lib/cg.lib")
- #pragma comment(lib, "C:/Program Files (x86)/NVIDIA Corporation/Cg/lib/cggl.lib")
- #define WIDTH 800
- #define HEIGHT 600
- #define TOPLEFTX 0
- #define TOPLEFTY 0
- #define BORDERWIDTH 16
- #define BORDERHEIGHT 38
- #define ROTATION_SPEED 0.5f
- #define NUM_PLAYERS 2
- #define NUM_BULLETS 2
- #define NUM_POWERUPS 2
- //float tankVertexArray[] = {
- // -0.8f, -0.8f, 0.0f,
- // 0.8f, -0.8f, 0.0f,
- // -0.8f, 0.8f, 0.0f,
- //
- // 0.8f, 0.8f, 0.0f,
- // 0.8f, -0.8f, 0.0f,
- // -0.8f, 0.8f, 0.0f
- //};
- float tankVertexArray[] = {
- -0.05f, -0.05f, 0.0f,
- 0.05f, -0.05f, 0.0f,
- -0.05f, 0.05f, 0.0f,
- 0.05f, 0.05f, 0.0f,
- 0.05f, -0.05f, 0.0f,
- -0.05f, 0.05f, 0.0f
- };
- float turretVertexArray[] = {
- -0.01f, 0.05f, 0.0f,
- -0.01f, 0.00f, 0.0f,
- 0.01f, 0.05f, 0.0f,
- -0.01f, 0.00f, 0.0f,
- 0.01f, 0.05f, 0.0f,
- 0.01f, 0.00f, 0.0f
- };
- float bulletVertexArray[] = {
- -0.005f, -0.00f, 0.0f,
- 0.005f, -0.00f, 0.0f,
- -0.00f, 0.02f, 0.0f
- };
- float powerUpVertexArray[] = {
- -0.005f, -0.001f, 0.0f,
- 0.0f, -0.001f, 0.0f,
- -0.005f, 0.001f, 0.0f,
- 0.0f, 0.001f, 0.0f,
- 0.0f, -0.001f, 0.0f,
- -0.005f, 0.001f, 0.0f
- };
- //All of the information about a Tank
- class tankStatus
- {
- private:
- bool upPressed;
- bool downPressed;
- bool leftPressed;
- bool rightPressed;
- bool turretRotateLeftPressed;
- bool turretRotateRightPressed;
- bool shootPressed;
- bool bulletActive;
- float maxSpeed;
- float curSpeed;
- float xCord;
- float yCord;
- float zCord;
- float zRotation; //How much tank has been rotated
- bool offScreenTopBot;
- bool offScreenSide;
- float turretZRotation; //How much turret has been rotated
- float color[4];
- float turretColor[4];
- bool poweredUp;
- char powerType;
- public:
- tankStatus();
- //Getters and setters for the private variables
- // setting and getting the variables holding whether the button to move the tank up or down has been pressed
- bool getUpPressed() { return upPressed; }
- void setUpPressed(bool pressed) { upPressed= pressed; }
- bool getDownPressed() { return downPressed; }
- void setDownPressed(bool pressed) { downPressed= pressed; }
- bool getLeftPressed() { return leftPressed; }
- void setLeftPressed(bool pressed) { leftPressed= pressed; }
- bool getRightPressed() { return rightPressed; }
- 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
- bool getShootPressed() { return shootPressed; }
- void setShootPressed(bool pressed) { shootPressed= pressed; }
- bool getTurretLeftPressed() { return turretRotateLeftPressed; }
- void setTurretLeftPressed(bool pressed) { turretRotateLeftPressed= pressed; }
- bool getTurretRightPressed() { return turretRotateRightPressed; }
- void setTurretRightPressed(bool pressed) { turretRotateRightPressed= pressed; }
- bool getBulletActive() { return bulletActive; }
- void setBulletActive(bool bullet) { bulletActive= bullet; }
- float getRotation() {return zRotation;}
- void setRotation(float Rotation) {zRotation = Rotation;}
- void setTurretRotation(float Rotation) {turretZRotation = Rotation;}
- float getTurretRotation() {return turretZRotation ;}
- float getXPosition() {return xCord;}
- void setXPosition(float x){
- if (x<-0.50||x>0.50)
- {
- offScreenSide = true;
- xCord = x;
- } else{
- xCord = x;
- offScreenSide = false;
- }
- }
- float getYPosition() {return yCord;}
- void setYPosition(float y){
- if (y<-0.8||y>0.8)
- {
- offScreenTopBot = true;
- yCord = y;
- } else{
- yCord = y;
- offScreenTopBot = false;
- }
- }
- float getZPosition() {return zCord;}
- void setZPosition(float z) {zCord = z;}
- float getSpeed() {return curSpeed;}
- void setSpeed(float speed){
- if(speed>0 &&speed<=maxSpeed||speed<0 &&speed>= -maxSpeed){
- curSpeed=speed;
- }
- }
- // float getMaxSpeed(){return maxSpeed;}
- float getColor(int i) {return color[i];}
- void setColor(int i,float colors) {color[i]=colors;}
- float getTurretColor(int i) {return turretColor[i];}
- bool getOffTopBot() {return offScreenTopBot;}
- bool getOffSide() {return offScreenSide;}
- };
- //All the Information about a Bullet
- class bulletStatus{
- private:
- float bulletSpeed;
- float xCord;
- float yCord;
- float zCord;
- float rotation;
- bool offScreenTopBot;
- bool offScreenSide;
- public:
- bulletStatus();
- float getXPosition() {return xCord;}
- void setXPosition(float x){
- if (x<-0.50||x>0.50)
- {
- offScreenSide = true;
- xCord = x;
- } else{
- xCord = x;
- offScreenSide = false;
- }
- }
- float getYPosition() {return yCord;}
- void setYPosition(float y){
- if (y<-0.8||y>0.8)
- {
- offScreenTopBot = true;
- yCord = y;
- } else{
- yCord = y;
- offScreenTopBot = false;
- }
- }
- float getZPosition() {return zCord;}
- void setZPosition(float z) {zCord = z;}
- float getRotation() {return rotation;}
- void setRotation(float Rotation) {rotation = Rotation;}
- float getBulletSpeed() {return bulletSpeed;}
- void setBulletSpeed(float Speed) { bulletSpeed=Speed;}
- };
- //All the Information about a PowerUp
- class powerUpStatus{
- private:
- float xCord; //X, Y and z location of the power up
- float yCord;
- float zCord;
- char powerType; //The type of power up s for speed, i for invincibility
- public:
- powerUpStatus();
- };
- //All the Information about a Game state
- class gameStatus
- {
- public:
- gameStatus(CGprofile vProfile, CGprofile fProfile, CGprogram vProgram, CGprogram fProgram); // the constructor
- ~gameStatus();
- tankStatus** tankArray;
- bulletStatus** bulletArray;
- powerUpStatus** powerUpArray;
- CGprofile vShaderProfile; // vertex shader profile to use
- CGprofile fShaderProfile; // fragment shader profile to use
- CGprogram vShaderProgram; // vertex shader program to use
- CGprogram fShaderProgram; // fragment program to use
- CGparameter mvMatrix; // handle to the mv matrix in the vertex shader
- CGparameter fragColor; // handle to the color in the fragment shader
- };
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // Function prototypes.
- // these are our ones...
- void updateGame(HDC deviceContext, gameStatus* currentGame); // update the game
- void draw(HDC deviceContext, gameStatus* currentGame); // drawing function containing OpenGL function calls
- char* loadShader(string filePath); // compiling shader files
- void drawTurret(HDC deviceContext, gameStatus* currentGame);
- void drawTurret(HDC deviceContext, gameStatus* currentGame);
- bool checkCollisions(int i,HDC deviceContext,gameStatus* currentGame);
- // these were already here...
- LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void updateGame(HDC deviceContext, gameStatus* currentGame)
- {
- for (int i = 0; i < NUM_PLAYERS; i++){
- if (checkCollisions(i,deviceContext,currentGame)){
- currentGame->tankArray[i]->setSpeed(0);
- }else {
- float rotationRadians = currentGame-> tankArray[i]->getRotation();
- float tankrotation = currentGame-> tankArray[i]->getTurretRotation();
- if(!(currentGame-> tankArray[i]->getRightPressed()&¤tGame-> tankArray[i]->getLeftPressed()||currentGame-> tankArray[i]->getTurretLeftPressed()&¤tGame-> tankArray[i]->getTurretRightPressed())){
- if (currentGame-> tankArray[i]->getRightPressed()) {currentGame-> tankArray[i]->setRotation(rotationRadians-ROTATION_SPEED) ;}
- if (currentGame-> tankArray[i]->getLeftPressed()) {currentGame-> tankArray[i]->setRotation( rotationRadians+ ROTATION_SPEED);}
- if (currentGame-> tankArray[i]->getTurretRightPressed()) {currentGame-> tankArray[i]->setTurretRotation(tankrotation + ROTATION_SPEED);}
- if (currentGame-> tankArray[i]->getTurretLeftPressed()) {currentGame-> tankArray[i]->setTurretRotation(tankrotation - ROTATION_SPEED) ;}
- }
- float tankSpeed = currentGame->tankArray[i]->getSpeed();
- if(!(currentGame-> tankArray[i]->getUpPressed()&¤tGame-> tankArray[i]->getDownPressed())){
- if(currentGame->tankArray[i]->getUpPressed()) {
- currentGame->tankArray[i]->setSpeed(tankSpeed+0.00001f);
- }
- if(currentGame->tankArray[i]->getDownPressed()) {
- currentGame->tankArray[i]->setSpeed(tankSpeed-0.00001f);
- }
- if(!currentGame->tankArray[i]->getUpPressed()&&!currentGame->tankArray[i]->getDownPressed()&&tankSpeed!=0){
- if(tankSpeed<0.00001f &&tankSpeed>0){
- currentGame->tankArray[i]->setSpeed(0);
- }else if(tankSpeed>-0.00001f &&tankSpeed<0){
- currentGame->tankArray[i]->setSpeed(0);
- }
- if(currentGame->tankArray[i]->getSpeed()<0){
- currentGame->tankArray[i]->setSpeed(tankSpeed+0.00001f);
- }else if(currentGame->tankArray[i]->getSpeed()>0){
- currentGame->tankArray[i]->setSpeed(tankSpeed-0.00001f);
- }
- }
- }
- rotationRadians = currentGame-> tankArray[i]->getRotation()* PI / 180 ; //updating rotation in radians
- tankSpeed = currentGame->tankArray[i]->getSpeed(); //getting speed in case it was changed
- float oldx = currentGame->tankArray[i]->getXPosition(); //get current x position
- float oldy= currentGame->tankArray[i]->getYPosition(); //get current y position
- currentGame->tankArray[i]->setXPosition(oldx-(tankSpeed*sin(rotationRadians))); //set new x position
- currentGame->tankArray[i]->setYPosition(oldy+(tankSpeed*cos(rotationRadians))); //set new y position
- if (currentGame->tankArray[i]->getBulletActive())
- {
- float bulletSpeed = currentGame->bulletArray[i]->getBulletSpeed();
- float bulletRotationRadians = currentGame-> tankArray[i]->getRotation()* PI / 180;
- float bulletOldx = currentGame->bulletArray[i]->getXPosition();
- float bulletOldy= currentGame->bulletArray[i]->getYPosition();
- currentGame->bulletArray[i]->setXPosition( bulletOldx-(bulletSpeed*sin( bulletRotationRadians)));
- currentGame->bulletArray[i]->setYPosition( bulletOldy+(bulletSpeed*cos( bulletRotationRadians)));
- }
- }
- }// end of the for loop updating the game for each tank
- draw(deviceContext, currentGame);
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void drawPlayer(HDC deviceContext, gameStatus* currentGame)
- {
- for (int i = 0; i < NUM_PLAYERS; i++)
- {
- glVertexPointer(3, GL_FLOAT, 0, tankVertexArray);
- float xlocation =currentGame->tankArray[0]->getXPosition();
- float ylocation =currentGame->tankArray[0]->getYPosition();
- float zlocation = -1;
- glPushMatrix();
- glLoadIdentity();
- glTranslatef(xlocation,ylocation,zlocation);
- glRotatef(currentGame->tankArray[i]->getRotation(), 0.0, 0.0, 1.0);
- cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
- cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getColor(0),currentGame->tankArray[i]->getColor(1),currentGame->tankArray[i]->getColor(2),currentGame->tankArray[i]->getColor(3));
- glDrawArrays(GL_TRIANGLES, 0, 6);
- glPopMatrix();
- }
- //for (int i = 0; i < NUM_PLAYERS; i++)
- //{
- //
- //
- // glVertexPointer(3, GL_FLOAT, 0, tankVertexArray);
- // float xlocation = currentGame->tankArray[i]->getXPosition();
- // float ylocation =currentGame->tankArray[i]->getYPosition();
- // float zlocation = currentGame->tankArray[i]->getZPosition();
- // glPushMatrix();
- // glLoadIdentity();
- // glTranslatef(xlocation,ylocation,zlocation);
- //
- // glRotatef(currentGame->tankArray[i]->getRotation(), 0.0, 0.0, 1.0);
- // cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
- // cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getColor(0),currentGame->tankArray[i]->getColor(1),currentGame->tankArray[i]->getColor(2),currentGame->tankArray[i]->getColor(3));
- // glDrawArrays(GL_TRIANGLES, 0, 6);
- // glPopMatrix();
- // if (currentGame->tankArray[i]->getOffSide())
- // {
- // glPushMatrix();
- // glLoadIdentity();
- // glTranslatef(-xlocation,ylocation,zlocation);
- // glRotatef(currentGame->tankArray[i]->getRotation(), 0.0, 0.0, 1.0);
- // cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
- // cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getColor(0),currentGame->tankArray[i]->getColor(1),currentGame->tankArray[i]->getColor(2),currentGame->tankArray[i]->getColor(3));
- // glDrawArrays(GL_TRIANGLES, 0, 6);
- // glPopMatrix();
- // }else if (currentGame->tankArray[i]->getOffTopBot())
- // {
- // glPushMatrix();
- // glLoadIdentity();
- // glTranslatef(xlocation,-ylocation,zlocation);
- // // then rotate to the current player's rotation around z axis
- // glRotatef(currentGame->tankArray[i]->getRotation(), 0.0, 0.0, 1.0);
- // cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
- // cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getColor(0),currentGame->tankArray[i]->getColor(1),currentGame->tankArray[i]->getColor(2),currentGame->tankArray[i]->getColor(3));
- // glDrawArrays(GL_TRIANGLES, 0, 6);
- // glPopMatrix();
- // }
- //
- //}
- }
- void drawTurret(HDC deviceContext, gameStatus* currentGame)
- {
- for (int i = 0; i < NUM_PLAYERS; i++)
- {
- glVertexPointer(3, GL_FLOAT, 0, turretVertexArray);
- glPushMatrix();
- glLoadIdentity();
- glTranslatef(currentGame->tankArray[i]->getXPosition(),currentGame->tankArray[i]->getYPosition(),currentGame->tankArray[i]->getZPosition());
- glRotatef(currentGame->tankArray[i]->getTurretRotation(), 0.0, 0.0, 1.0);
- cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
- cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[i]->getTurretColor(0),currentGame->tankArray[i]->getTurretColor(1),currentGame->tankArray[i]->getTurretColor(2),currentGame->tankArray[i]->getTurretColor(3));
- glDrawArrays(GL_TRIANGLES, 0, 6);
- glPopMatrix();
- }
- }
- void drawBullet(HDC deviceContext, gameStatus* currentGame)
- {
- for (int i = 0; i < NUM_BULLETS; i++)
- {
- if (currentGame->tankArray[i]->getBulletActive())
- {
- glVertexPointer(3, GL_FLOAT, 0, bulletVertexArray);
- glPushMatrix();
- glLoadIdentity();
- glTranslatef(currentGame->bulletArray[i]->getXPosition(),currentGame->bulletArray[i]->getYPosition(),currentGame->bulletArray[i]->getZPosition());
- // then rotate to the current player's rotation around z axis
- glRotatef(currentGame->bulletArray[i]->getRotation(), 0.0, 0.0, 1.0);
- cgGLSetStateMatrixParameter(currentGame->mvMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
- //cgSetParameter4f(currentGame->fragColor, currentGame->tankArray[0]->getTurretColor(0),currentGame->tankArray[0]->getTurretColor(1),currentGame->tankArray[0]->getTurretColor(2),currentGame->tankArray[0]->getTurretColor(3));
- glDrawArrays(GL_TRIANGLES, 0, 3);
- glPopMatrix();
- }
- // restore the old matrix state, leaving things as we found them!
- }
- }
- void draw(HDC deviceContext, gameStatus* currentGame)
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glEnableClientState(GL_VERTEX_ARRAY);
- glMatrixMode(GL_MODELVIEW);
- cgGLEnableProfile(currentGame->vShaderProfile);
- cgGLEnableProfile(currentGame->fShaderProfile);
- cgGLBindProgram(currentGame->vShaderProgram);
- cgGLBindProgram(currentGame->fShaderProgram);
- drawPlayer(deviceContext, currentGame);
- //drawTurret(deviceContext, currentGame);
- //drawBullet(deviceContext, currentGame);
- cgGLUnbindProgram(currentGame->vShaderProfile);
- cgGLUnbindProgram(currentGame->fShaderProfile);
- cgGLDisableProfile(currentGame->vShaderProfile);
- cgGLDisableProfile(currentGame->fShaderProfile);
- glDisableClientState(GL_VERTEX_ARRAY);
- SwapBuffers(deviceContext);
- }
- void createBullet(int tankID, gameStatus* currentGame){
- if (!currentGame->tankArray[tankID]->getBulletActive() )
- {
- currentGame->bulletArray[tankID] = new bulletStatus;
- float tankRotation= currentGame->tankArray[tankID]->getTurretRotation();
- float tankX = currentGame->tankArray[tankID]->getXPosition();
- float tankY = currentGame->tankArray[tankID]->getYPosition();
- float tankZ = currentGame->tankArray[tankID]->getZPosition();
- float tankSpeed = currentGame->tankArray[tankID]->getSpeed();
- currentGame->bulletArray[tankID]->setRotation(tankRotation);
- currentGame->bulletArray[tankID]->setXPosition(tankX);
- currentGame->bulletArray[tankID]->setYPosition(tankY);
- currentGame->bulletArray[tankID]->setZPosition(tankZ);
- //currentGame->bulletArray[tankID]->setBulletSpeed(tankSpeed);
- currentGame->tankArray[tankID]->setBulletActive(true);
- }
- }
- bool checkCollisions(int count,HDC deviceContext,gameStatus* currentGame){
- int holdCount = count;
- float xCord1= currentGame->tankArray[count]->getXPosition();
- float yCord1= currentGame->tankArray[count]->getYPosition();
- float xCord2=0;
- float yCord2=0;
- for(int i=0; i<(NUM_PLAYERS-1);i++){
- if (!count==i)
- {
- xCord2= currentGame->tankArray[i]->getXPosition();
- yCord2= currentGame->tankArray[i]->getYPosition();
- }
- }
- if(fabs(xCord1-xCord2 )<= 0.00 &&fabs(yCord1-yCord2)<=0.0)
- {
- return true;
- }
- return false;
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
- {
- WNDCLASS myWindowClass;
- myWindowClass.cbClsExtra = 0;
- myWindowClass.cbWndExtra = 0;
- myWindowClass.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
- myWindowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
- myWindowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
- myWindowClass.hInstance = hInstance;
- myWindowClass.lpfnWndProc = WndProc;
- myWindowClass.lpszClassName = TEXT("my window class name");
- myWindowClass.lpszMenuName = 0;
- myWindowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
- RegisterClass(&myWindowClass);
- RECT rect;
- SetRect( &rect, TOPLEFTX,
- TOPLEFTY,
- TOPLEFTX + WIDTH,
- TOPLEFTY + HEIGHT );
- AdjustWindowRect( &rect, WS_OVERLAPPEDWINDOW, false );
- HWND myWindow = CreateWindow(TEXT("my window class name"),
- TEXT("OpenGL, No GLUT"),
- WS_OVERLAPPEDWINDOW,
- TOPLEFTX, TOPLEFTY,
- TOPLEFTX + WIDTH+BORDERWIDTH, TOPLEFTY + HEIGHT+BORDERHEIGHT,
- NULL, NULL,
- hInstance, NULL);
- if( myWindow == NULL )
- {
- FatalAppExit( NULL, TEXT("CreateWindow() failed!") );
- }
- ShowWindow( myWindow, iCmdShow );
- HDC myDeviceContext = GetDC( myWindow );
- PIXELFORMATDESCRIPTOR myPfd = { 0 };
- myPfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );
- myPfd.nVersion = 1;
- myPfd.dwFlags = PFD_SUPPORT_OPENGL |
- PFD_DOUBLEBUFFER |
- PFD_DRAW_TO_WINDOW;
- myPfd.iPixelType = PFD_TYPE_RGBA ;
- myPfd.cColorBits = 24;
- myPfd.cDepthBits = 32;
- int chosenPixelFormat = ChoosePixelFormat( myDeviceContext, &myPfd );
- if( chosenPixelFormat == 0 )
- {
- FatalAppExit( NULL, TEXT("ChoosePixelFormat() failed!") );
- }
- int result = SetPixelFormat( myDeviceContext, chosenPixelFormat, &myPfd );
- if (result == NULL)
- {
- FatalAppExit( NULL, TEXT("SetPixelFormat() failed!") );
- }
- HGLRC myRenderContext = wglCreateContext( myDeviceContext );
- wglMakeCurrent( myDeviceContext, myRenderContext );
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0,(float)WIDTH/(float)HEIGHT, 1, 1000);
- glViewport(0, 0, WIDTH, HEIGHT);
- glClearColor( 0.0, 0, 0, 0 );
- CGcontext context = cgCreateContext();
- CGprofile vProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
- CGprofile fProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
- cgGLSetOptimalOptions(vProfile);
- cgGLSetOptimalOptions(fProfile);
- char* myVertexProgram = loadShader("../shaders/myVertexProgram.cg");
- char* myFragmentProgram = loadShader("../shaders/myFragmentProgram.cg");
- CGprogram vProgram = cgCreateProgram(context, CG_SOURCE, myVertexProgram, vProfile, "main", NULL);
- CGprogram fProgram = cgCreateProgram(context, CG_SOURCE, myFragmentProgram, fProfile, "main", NULL);
- cgGLLoadProgram(vProgram);
- cgGLLoadProgram(fProgram);
- MSG msg;
- bool needToQuit = false;
- gameStatus* currentGame = new gameStatus(vProfile, fProfile, vProgram, fProgram);
- while( !needToQuit )
- {
- // we need to listen out for OS messages.
- // if there is a windows message to process...
- if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
- {
- // if the message was a "quit" message...
- if( msg.message == WM_QUIT )
- {
- needToQuit = true; // we want to quit asap
- }
- // if it was a "key pressed" message...
- else if (msg.message == WM_KEYDOWN)
- {
- switch (msg.wParam)
- {
- case 'W':
- currentGame->tankArray[0]->setUpPressed(true);
- break;
- case 'A':
- currentGame->tankArray[0]->setLeftPressed(true);
- break;
- case 'S':
- currentGame->tankArray[0]->setDownPressed(true);
- break;
- case 'D':
- currentGame->tankArray[0]->setRightPressed(true);
- break;
- case 'Q':
- currentGame->tankArray[0]->setTurretRightPressed(true);
- break;
- case 'E':
- currentGame->tankArray[0]->setTurretLeftPressed(true);
- break;
- case '2':
- createBullet(0,currentGame);
- break;
- default:
- break;
- }
- }
- // if they let go of a key
- else if (msg.message == WM_KEYUP)
- {
- switch (msg.wParam)
- {
- case 'W':
- currentGame->tankArray[0]->setUpPressed(false);
- break;
- case 'A':
- currentGame->tankArray[0]->setLeftPressed(false);
- break;
- case 'S':
- currentGame->tankArray[0]->setDownPressed(false);
- break;
- case 'D':
- currentGame->tankArray[0]->setRightPressed(false);
- case 'Q':
- currentGame->tankArray[0]->setTurretRightPressed(false);
- break;
- case 'E':
- currentGame->tankArray[0]->setTurretLeftPressed(false);
- break;
- default:
- break;
- }
- }
- // if it was any other type of message
- else
- {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- }
- updateGame(myDeviceContext, currentGame);
- }
- delete currentGame;
- currentGame = NULL;
- cgDestroyContext(context);
- wglMakeCurrent( NULL, NULL );
- wglDeleteContext( myRenderContext );
- ReleaseDC( myWindow, myDeviceContext );
- return msg.wParam;
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
- {
- switch( message )
- {
- // if they exited the window...
- case WM_DESTROY:
- // post a message "quit" message to the main windows loop
- PostQuitMessage( 0 ) ;
- return 0;
- break;
- }
- return DefWindowProc( hwnd, message, wparam, lparam );
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- char* loadShader(string filePath)
- {
- ifstream file(filePath.c_str(), ios::in | ios::binary);
- if(!file)
- {
- cout << "shader text file not found" << endl;
- return 0;
- }
- int size = 0;
- file.seekg(0,ios::end);
- size = file.tellg();
- if(!size)
- {
- cout << "shader file is empty!" << endl;
- }
- char* data = 0;
- data = new char[size+1];
- file.seekg(0,ios::beg);
- file.read(data, size);
- data[size] = '\0';
- return data;
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // this sets up the default values that are common to all game instances
- gameStatus::gameStatus(CGprofile vProfile, CGprofile fProfile, CGprogram vProgram, CGprogram fProgram)
- {
- //An array of pointers to Tanks
- tankArray = new tankStatus* [NUM_PLAYERS];
- //Allocate memory for each tank
- for (int i = 0; i < NUM_PLAYERS; i++)
- {
- tankArray[i] = new tankStatus;
- }
- tankArray[0]->setXPosition(-0.2f);
- tankArray[1]->setXPosition(0.2f);
- // set the colors...
- tankArray[0]->setColor(0, 1.0f);
- tankArray[0]->setColor(1, 0.0f);
- tankArray[0]->setColor(2, 1.0f);
- tankArray[0]->setColor(3, 1.0f);
- tankArray[1]->setColor(0, 0.5f);
- tankArray[1]->setColor(1, 0.0f);
- tankArray[1]->setColor(2, 1.0f);
- tankArray[1]->setColor(3, 1.0f);
- //An array of pointers to Bullets
- bulletArray = new bulletStatus* [NUM_BULLETS];
- //An array of pointers to PowerUps
- /*powerUpArray = new powerUpStatus* [NUM_POWERUPS];
- for (int i = 0; i < NUM_POWERUPS; i++)
- {
- powerUpArray[i] = new powerUpStatus;
- }*/
- //playerArray[0]->xPos = -2.0f;
- //playerArray[1]->xPos = 1.0f;
- // set the shaders to be used in the game
- vShaderProfile = vProfile;
- fShaderProfile = fProfile;
- vShaderProgram = vProgram;
- fShaderProgram = fProgram;
- mvMatrix = cgGetNamedParameter(vShaderProgram,"mvMatrix");
- fragColor = cgGetNamedParameter(fShaderProgram, "reqColor");
- }
- gameStatus::~gameStatus()
- {
- //Cleaning up bulletArray
- for (int i = 0; i < NUM_BULLETS; i++)
- {
- if (tankArray[i]->getBulletActive())
- {
- delete bulletArray[i];
- bulletArray[i] = NULL;
- }
- }
- delete[] bulletArray;
- bulletArray = NULL;
- //Cleaning up powerUpArray
- for (int i = 0; i < NUM_POWERUPS; i++)
- {
- delete powerUpArray[i];
- powerUpArray[i] = NULL;
- }
- delete[] powerUpArray;
- powerUpArray = NULL;
- for (int i = 0; i < NUM_PLAYERS; i++)
- {
- delete tankArray[i];
- tankArray[i] = NULL;
- }
- delete[] tankArray;
- tankArray = NULL;
- cgDestroyProgram(vShaderProgram);
- cgDestroyProgram(fShaderProgram);
- cgDestroyParameter(mvMatrix);
- cgDestroyParameter(fragColor);
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // this sets up the default values that are common to all players
- tankStatus:: tankStatus()
- {
- upPressed = false;
- downPressed = false;
- leftPressed = false;
- rightPressed = false;
- zRotation = 0.0f;
- maxSpeed = 0.002f;
- curSpeed= 0.0000f;
- xCord = 0.0f;
- yCord = 0.0f;
- zCord = -2.0f;
- turretRotateLeftPressed= false;
- turretRotateRightPressed= false;
- turretZRotation= 0.0f;
- turretColor[0]=1.0f;
- turretColor[1]=0.0f;
- turretColor[2]=0.0f;
- turretColor[3]=1.0f;
- offScreenTopBot = false;
- offScreenSide= false;
- shootPressed=false;
- bulletActive=false;
- poweredUp = false;
- };
- bulletStatus:: bulletStatus()
- {
- rotation = 0.0f;
- bulletSpeed = 0.0025f; //more than the max speed of tanks
- xCord = 0.0f;
- yCord = 0.0f;
- zCord = -2.0f;
- offScreenTopBot = false;
- offScreenSide= false;
- };
- powerUpStatus:: powerUpStatus()
- {
- xCord = 0.0f;
- yCord = 0.0f;
- zCord = -2.0f;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement