Original Post
Hello please take a look at my code.
I want to access variables in certain classes and use them in other situations?
Please note most of these class's have been shortened .
Main
Car Header
Car Class
Menu.H
Menu.cpp
Draw Function
Hope you can help thanks
I want to access variables in certain classes and use them in other situations?
Please note most of these class's have been shortened .
Main
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <vector>
using namespace std;
#include "dxManager.h"
#include "Car.h"
#include "MenuSystem.h"
/*******************************************************************
* Global Variables
*******************************************************************/
HWND hWnd; //window handle
int windowWidth = 1024;
int windowHeight = 768;
//directX manager
dxManager dx;
// Game Menu
MenuSystem gamemenu;
// Car Class
Car carclass;
/*******************************************************************
* WinMain
*******************************************************************/
int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
// Set up the application window
if ( !initWindow(hWnd, hInstance, windowWidth, windowHeight)) return 0;
//set up directx manager
if ( !dx.initialize(&hWnd) ) return 0;
// Main message loop
MSG msg = {0};
while (WM_QUIT != msg.message)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
dx.renderScene();
gamemenu.Menu();
carclass.PlayerMovement();
carclass.Rotation();
}
return (int) msg.wParam;
}Car Header
// C++ header created by Josh Baker for player.
#ifndef CAR
#define CAR
#include <windows.h>
#include <d3d10.h>
#include <d3dx10.h>
#pragma comment(lib, "d3dx10d.lib")
class Car
{
/*******************************************************************
* Methods
********************************************************************/
public:
//constructor and destructor
Car();
~Car();
static float playerX;
static float playerY ;
float playerSpeed ;
float playerMaxSpeed ;
float playerRotation;
void PlayerMovement();
void Rotation();
};
#endifCar Class
#include <math.h>
#include "Car.h"
#include "MenuSystem.h"
#pragma comment(lib, "d3dx10d.lib")
// variables defined
// constructor
Car::Car()
{
playerX = 0.0f; // player x pos
playerY = 0.0f; // player y pos
playerSpeed = 0.0f; // players speed
playerMaxSpeed = 0.5f; // players maximum speed
playerRotation = 0.0f; // player rotation angle
}
void Car::PlayerMovement()
{
if (MenuSystem::startOn == true )
{
if (playerSpeed > playerMaxSpeed)
{
playerSpeed = 0.2f;
}
if (GetAsyncKeyState(VK_UP ) )
{
playerSpeed += 0.001f * 0.0001f;
playerY += playerSpeed;
}
if (GetAsyncKeyState(VK_DOWN ) )
{
playerY -= 0.001f;
}
if (GetAsyncKeyState(VK_RIGHT ) )
{
playerX -= 0.001f;
}
if (GetAsyncKeyState(VK_RIGHT ) )
{
playerY -= 0.001f;
}
}
}
void Car::Rotation()
{
playerX = playerX + cos(playerRotation);
playerY = playerY - sin(playerRotation);
}
Menu.H
// C++ header created by Josh Baker for player.
#ifndef MENUSYSTEM
#define MENUSYSTEM
#include <windows.h>
#include <d3d10.h>
#include <d3dx10.h>
#pragma comment(lib, "d3dx10d.lib")
class MenuSystem
{
/*******************************************************************
* Methods
********************************************************************/
public:
//constructor and destructor
MenuSystem();
~MenuSystem();
int statenumber;
bool creditsOn;
bool startOn;
bool settingsOn;
bool menuOn;
void Menu();
void MenuState();
};
#endifMenu.cpp
#include <math.h>
#include "MenuSystem.h"
#pragma comment(lib, "d3dx10d.lib")
SHORT WINAPI GetAsyncKeyState // Gets Keyinput
(
__in int vKey
);
void MenuSystem::Menu()
{
if (menuOn == true)
{
if (GetAsyncKeyState(VK_DOWN))
{
statenumber += 1;
Sleep(200);
}
if (GetAsyncKeyState(VK_UP ))
{
MenuSystem::statenumber -= 1;
Sleep(200);
}
if(statenumber > 3) // fixes off menu
{
statenumber = 0;
}
if(statenumber < 0) // fixes off menu
{
statenumber = 3;
}
if (GetAsyncKeyState(VK_RETURN) && creditsOn == false && statenumber == 2)
{
creditsOn = true;
}
if (GetAsyncKeyState(VK_RETURN) && startOn == false && statenumber == 0)
{
startOn = true;
}
if (startOn == true)
{
menuOn = false;
}
if (GetAsyncKeyState(VK_RETURN) && statenumber == 3)
{
PostQuitMessage(0);
}
if (GetAsyncKeyState(VK_RETURN) && settingsOn == false && statenumber == 1)
{
settingsOn = true;
}
if (GetAsyncKeyState(VK_ESCAPE) && creditsOn == true)
{
creditsOn = false;
}
if (startOn == true && GetAsyncKeyState(VK_ESCAPE))
{
startOn = false;
}
}
}
Draw Function
void dxManager::renderScene()
{
pD3DDevice->ClearRenderTargetView( pRenderTargetView, D3DXCOLOR(0.5,0.5,0.5,1) );
pD3DDevice->ClearDepthStencilView( pDepthStencilView, D3D10_CLEAR_DEPTH, 1.0f, 0 );
D3DXMATRIX scaling;
D3DXMatrixScaling(&scaling, 2.7f, 2.0f, 2.0f);
D3DXMATRIX scalingPlayer;
D3DXMatrixScaling(&scalingPlayer, 0.1f, 0.2f, 0.1f);
//draw
for( UINT p = 0; p < techDesc.Passes; p++ )
{
if (MenuSystem::startOn == false)
{
//set resources for plane 1 (MENU)
D3DXMatrixTranslation( &worldMatrix, 0,0,2 );
worldMatrix = scaling * worldMatrix ;
pWorldMatrixEffectVariable->SetMatrix(worldMatrix);
pTextureSR->SetResource( pMenuTex );
//apply technique
pBasicTechnique->GetPassByIndex( p )->Apply( 0 );
pCubeMesh->DrawSubset(0);
}
if (MenuSystem::startOn == true)
{
//set resources for plane 2 (PLAYER)
D3DXMatrixTranslation( &worldMatrix, Car::playerX,Car::playerY,2.0 );
worldMatrix = scalingPlayer * worldMatrix ;
pWorldMatrixEffectVariable->SetMatrix(worldMatrix);
pTextureSR->SetResource( pPlayerTex );
//apply technique
pBasicTechnique->GetPassByIndex( p )->Apply( 0 );
pCubeMesh->DrawSubset(0);
//set resources for plane 3 (HUD)
D3DXMatrixTranslation( &worldMatrix, 0,0,2 );
worldMatrix = scaling * worldMatrix ;
pWorldMatrixEffectVariable->SetMatrix(worldMatrix);
pTextureSR->SetResource( pGameHUD );
//apply technique
pBasicTechnique->GetPassByIndex( p )->Apply( 0 );
pCubeMesh->DrawSubset(0);
}
RECT rc = { 100, 100, 400, 400};Hope you can help thanks