Jump to content
  • Advertisement
Sign in to follow this  
Antonym

Retrieving input from WndProc

This topic is 3529 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

Okey so I have a struct consisting of several booleans that say if a certain key is being currently pressed and well this is determined by window messages. My problem is that how can I pass this structure/an instance of this structure to the windowproc. I don't want to make it global. How can I acomplish this? I included the code involved just in case Right now 'input_data' the instance of the struct is global. The structure
struct input{
	bool move_forward;
	bool move_backward;
	bool turn_right;
	bool turn_left;
	bool shooting;

	input(){
		move_forward = false;
		move_backward = false;
		turn_right = false;
		turn_left = false;
		shooting = false;
	}
};



The main loop(Dunno if including this is necesary but this is where the handleMessages is called)
#include "object.h"
#include <vector>

void mainLoop()
{
	std::vector<object> gameObjects;

	initGame(&gameObjects);

	while(handleMessages())
	{
		playerInput(&gameObjects, &inputData);
		updateObjects(&gameObjects);
		render(&gameObjects);
	}
}


The window/application class where the handleMessages function is.
#include <windows.h>
#include <windowsx.h>
#include "coord.h"

class application
{
private:
	HWND hWnd;
	coord dimensions(640,480);

public:
	HWND getHandle();
	coord getDimensions();
	bool displayWindow(HINSTANCE, int, WNDPROC);
	bool handleMessages();
}

HWND application::getHandle()
{
	return hWnd;
}

coord application::getDimensions()
{
	return dimensions;
}

bool application::displayWindow(HINSTANCE hInstance, int nCmdShow, WNDPROC WindowProc, )
{	
	//Register Window
	const LPCSTR className = "WindowClass1";

	WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.lpszClassName = className;

    if(RegisterClassEx(&wc)==0)
		return FALSE;

	//Create Window
	const LPCSTR windowName = "Dungeon";
	const DWORD windowStyle = WS_OVERLAPPEDWINDOW;

    hWnd = CreateWindowEx(NULL,
                          className,
                          windowName,
						  windowStyle,
                          0, 0,
                          dimensions.x,
						  dimensions.y,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

	if(hWnd == NULL)
		return FALSE;

	//Show Window
    ShowWindow(hWnd, nCmdShow);

	return TRUE;
}

bool application::handleMessages()
{
	static MSG msg;

	if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
		if(msg.message == WM_QUIT)
			return FALSE;

		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return TRUE;
}


The program's entering point and window proc.
#include <windows.h>
#include <windowsx.h>
#include "coord.h"
#include "winmain.h"
#include "direct3d.h"

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

input inputData;

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
	//Display Window
	application app;
	if(!app.displayWindow(nCmdShow, WindowProc))
		return 0;

	//Init D3D
	Direct3D direct3d;
	if(!direct3d.initD3D(app.getHandle(), app.getDimensions))
		return 0;

	sprite ship;
	sprite missile;

	loadGraphics();

	mainLoop();

    closeDX();

    return 0;
}


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
	{
        case WM_DESTROY:
			{
                PostQuitMessage(0);
                return 0;
            } break;

		case WM_KEYDOWN:
			{
				switch(wParam)
				{
				case VK_RIGHT:
					{
						inputData.turn_right = true;
					} break;
					
				case VK_LEFT:
					{
						inputData.turn_left = true;
					} break;

				case VK_UP:
					{
						inputData.move_forward = true;
					} break;

				case VK_DOWN:
					{
						inputData.move_backward = true;
					} break;

				case VK_SPACE:
					{
						inputData.shooting = true;
					} break;

				case VK_ESCAPE:
					{
						DestroyWindow(hWnd);
					} break;

				}

			} break;

		case WM_KEYUP:
			{
				switch(wParam)
				{
				case VK_RIGHT:
					{
						inputData.turn_right = false;
					} break;
					
				case VK_LEFT:
					{
						inputData.turn_left = false;
					} break;

				case VK_UP:
					{
						inputData.move_forward = false;
					} break;

				case VK_DOWN:
					{
						inputData.move_backward = false;
					} break;

				case VK_SPACE:
					{
						inputData.shooting = false;
					} break;
				}

			} break;
	}

    return DefWindowProc (hWnd, message, wParam, lParam);
}



Share this post


Link to post
Share on other sites
Advertisement
I usually make the WndProc function a static member function the application class, and also create a static pointer to 'this', which the WndProc function uses to call non-static members on the same application class.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!