storing data in static classes

This is a discussion on storing data in static classes within the C++ Programming forums, part of the General Programming Boards category; Hi, i have made a static input class which should store, in the form of an array of bools, which ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    storing data in static classes

    Hi, i have made a static input class which should store, in the form of an array of bools, which keys have been pressed. the problem is it actualy doesnt store them for longer than the scope of the function. Is this because it is a static class.

    i need this functionality so that i can work out key release to stop recurring pressing of keys when keys are pressed. we are using windows input ie GetKeyState, does this have functionality inbuilt to say if the key has been released.

    Thank you

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    code would be more illustrative, but it sounds like the variables in your method need to be static.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    the wierd thing is in a opengl project it works but in an Ogre project it doesnt using the same copy the values are forgotten.

    This is the code from the inputMap

    Code:
    static KeyMap MainKeys[BUTTON_MAX]=
    {
    
    	{ BUTTON_A,	VK_A,                 false},
    	{ BUTTON_D,	VK_D,                 false},
    	{ BUTTON_W,	VK_W,	          false},
    	{ BUTTON_S,	VK_S,	          false},
    	{ BUTTON_LEFT,	VK_LEFT,	          false},
    	{ BUTTON_RIGHT,	VK_RIGHT,          false},
    	{ BUTTON_UP,	VK_UP,	           false},
    	{ BUTTON_DOWN,	VK_DOWN,         false},
    	{ MBUTTON_LEFT,     VK_LBUTTON,    false},
    	{ BUTTON_ESC,       VK_ESCAPE,       false},
    
    };
    and it is accessed though a class as followed

    Code:
    namespace Input
    {
    	static class InputSystem
    	{	
    	private:
    
    		HWND hWnd;
    		int sWidth, sHeight;
    		bool BPressed;
    
    	public:
    		void InitInput(int width, int height,HWND handle)
    		{
    			sWidth = width;
    			sHeight = height;
    			hWnd = handle;
    			BPressed = false;
    		}
    		
    		void Update()
    		{
    			if(BPressed == false)
    				return;
    
    			for(int i = 0; i <  BUTTON_MAX; i++)
    			{
    				if(!(GetKeyState(MainKeys[i].Key)& 0x80))
    					MainKeys[i].keydown = false;
    			}				
    		}
    		bool CheckKeyPressed(int button)
    		{			
    			if(MainKeys[button].keydown)
    				return false;
    
    			if(GetKeyState(MainKeys[button].Key) & 0x80)
    			{
    				BPressed = true;
    				return MainKeys[button].keydown = true;
    			}
    			else
    			{
    				BPressed = false;
    				return MainKeys[button].keydown = false;
    			}
    		}
    	}InputSystem;
    }
    if a press K it is button 10 so CheckKeyPressed(BUTTON_K) would make MainKeys[10].keydown = true; the second it goes out of scope MainKeys[10] is set back to false. if in the Keymap they are set to something different it reverts back to how it was initialised.

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    if InputSystem is static, why not contain MainKeys within it instead of making it a separate global?
    Last edited by m37h0d; 10-22-2009 at 02:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program terminates abruptly
    By roaan in forum C Programming
    Replies: 3
    Last Post: 08-28-2009, 03:53 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  4. Change 2 classes into 1, possible?
    By sugie in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2005, 09:14 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21