Jump to content
  • Advertisement
Sign in to follow this  
JoshBaker

Class confusion?

This topic is 2570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
#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();
};


#endif




Car 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();





};


#endif



Menu.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

Share this post


Link to post
Share on other sites
Advertisement
You are accessing class instance variables as if they were class static variables. One solution is to declare your global objects as "extern", an outline of which is presented in the article I linked. Another is to not use globals, but instead pass pointers or references to the necessary objects.

Yet another solution is to decouple the classes. The Car class shouldn't need to worry about the Menu. Instead, invert the logic: only update the car when the menu isn't in the way. You could split the renderer into a menu renderer, and an in-game renderer.

Aside: don't declare functions you didn't write. Include the Windows header, do not copy the signature for GetAsyncKeyState(). I would recommend using an enumeration rather than cryptic magic numbers for your Menu states. The player/car position should certainly not be "static". I would recommend such long Sleep() durations in an interactive application. If you want an action to be delayed, use an explicit timer instead.

Share this post


Link to post
Share on other sites

You are accessing class instance variables as if they were class static variables. One solution is to declare your global objects as "extern", an outline of which is presented in the article I linked. Another is to not use globals, but instead pass pointers or references to the necessary objects.

Yet another solution is to decouple the classes. The Car class shouldn't need to worry about the Menu. Instead, invert the logic: only update the car when the menu isn't in the way. You could split the renderer into a menu renderer, and an in-game renderer.

Aside: don't declare functions you didn't write. Include the Windows header, do not copy the signature for GetAsyncKeyState(). I would recommend using an enumeration rather than cryptic magic numbers for your Menu states. The player/car position should certainly not be "static". I would recommend such long Sleep() durations in an interactive application. If you want an action to be delayed, use an explicit timer instead.


Thank you for this , i got it working after including the extern carclass.car :)
Been working on getting rotation to work and camera following for quite sometime.
I didn't want to use sleep() but since at this moment of time my keyinput isnt properly done , im having to use sleep otherwise my changing states number goes way to fast(causing it to go through the menu way to fast).
In c# i used a keypress as opposed to keydown in things such as the menu.

Share this post


Link to post
Share on other sites

In c# i used a keypress as opposed to keydown in things such as the menu.
[/quote]
You can implement key press/release detecting on top of GetASyncState(). You simply have a flag of the previous state, and if the current state is different from the previous state, there has been a transition. You could also detect key presses in your window procedure.

This is why Sleep() is poor, if you implement state flags above, your internal view of the state of the keyboard could get out of sync with the actual state, because you have not been polling fast enough.


I didn't want to use sleep() but since at this moment of time my keyinput isnt properly done , im having to use sleep otherwise my changing states number goes way to fast(causing it to go through the menu way to fast).
[/quote]
You can prevent this by having an internal timer that tells you when to move the cursor. Every time you detect that the key is pressed, move the cursor one space and then set a "reminder" to move it again in +N milliseconds.

E.g:

class Menu
{
enum Button {
NewGame,
Settings,
Exit,
};

static const int TotalButtons = Exit + 1;

static const int CursorTimeoutMs = 2000;

public:

void update(Input &input, Timer &timer)
{
int currentTime = timer.currentMilliseconds();

if(input.keyDown(Input::VK_DOWN))
{
if(direction != 1)
{
direction = 1;
timer = currentTime - CursorTimeoutMs;
}
}
else if(input.keyDown(Input::VK_UP))
{
if(direction != -1)
{
direction = -1;
timer = currentTime - CursorTimeoutMs;
}
}
else
{
direction = 0;
}

if(direction != 0 && currentTime >= timer + CursorTimeoutMs)
{
if(current > 0 && current < TotalButtons)
{
current += direction;
}
timer = currentTime;
}
}

private:
Button current;
int direction;
int timer;
};

I haven't tested that, but something like that should work. In the code above, I actually avoid having two code paths that update the cursor by setting a reminder "in the past".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!