Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <gl\GL.h>
- #pragma comment (lib, "opengl32.lib")
- float xvel, yvel;
- bool Keys[255];
- void ProcessInput(unsigned int key, float vel)
- {
- switch (key)
- {
- case VK_RIGHT:
- case 'D':
- xvel += vel;
- break;
- case VK_LEFT:
- case 'A':
- xvel -= vel;
- break;
- case VK_DOWN:
- case 'S':
- yvel += vel;
- break;
- case VK_UP:
- case 'W':
- yvel -= vel;
- break;
- }
- }
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- /*
- case WM_KEYDOWN:
- if ((lParam & (1 << 30)) == 0)
- ProcessInput(wParam, 4);
- break;
- case WM_KEYUP:
- ProcessInput(wParam, -4);
- break;
- */
- case WM_INPUT:
- {
- unsigned int size;
- GetRawInputData((HRAWINPUT) lParam, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER));
- unsigned char *data = new unsigned char[size];
- GetRawInputData((HRAWINPUT) lParam, RID_INPUT, data, &size, sizeof(RAWINPUTHEADER));
- RAWINPUT *raw = (RAWINPUT*) data;
- if (raw->header.dwType == RIM_TYPEKEYBOARD)
- {
- if (raw->data.keyboard.Flags & RI_KEY_BREAK)
- {
- if (Keys[raw->data.keyboard.MakeCode])
- {
- Keys[raw->data.keyboard.MakeCode] = false;
- ProcessInput(MapVirtualKey(raw->data.keyboard.MakeCode, MAPVK_VSC_TO_VK), -4);
- }
- }
- else
- {
- if (!Keys[raw->data.keyboard.MakeCode])
- {
- Keys[raw->data.keyboard.MakeCode] = true;
- ProcessInput(MapVirtualKey(raw->data.keyboard.MakeCode, MAPVK_VSC_TO_VK), 4);
- }
- }
- }
- break;
- }
- case WM_CLOSE:
- PostQuitMessage(EXIT_SUCCESS);
- return 0;
- }
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- TCHAR className[] = TEXT("Window");
- WNDCLASS windowClass = {0};
- windowClass.lpfnWndProc = WindowProc;
- windowClass.hInstance = hInstance;
- windowClass.lpszClassName = className;
- RegisterClass(&windowClass);
- unsigned int style = WS_OVERLAPPEDWINDOW;
- RECT rectangle = {0, 0, 640, 480};
- AdjustWindowRect(&rectangle, style, false);
- HWND window = CreateWindow(className, TEXT("Diagonal"), style, CW_USEDEFAULT, CW_USEDEFAULT, rectangle.right - rectangle.left, rectangle.bottom - rectangle.top, 0, 0, hInstance, 0);
- RAWINPUTDEVICE rid[1] = {0};
- rid[0].usUsagePage = 0x01;
- rid[0].usUsage = 0x06;
- rid[0].dwFlags = RIDEV_NOLEGACY;
- RegisterRawInputDevices(rid, 1, sizeof(rid[0]));
- HDC device = GetDC(window);
- PIXELFORMATDESCRIPTOR descriptor = {0};
- descriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
- descriptor.nVersion = 1;
- descriptor.dwFlags = PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
- descriptor.iPixelType = PFD_TYPE_RGBA;
- descriptor.cColorBits = 32;
- int format = ChoosePixelFormat(device, &descriptor);
- SetPixelFormat(device, format, &descriptor);
- HGLRC render = wglCreateContext(device);
- wglMakeCurrent(device, render);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 640, 480, 0, 1, -1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glClearColor(1, 1, 1, 1);
- ShowWindow(window, nCmdShow);
- MSG message = {0};
- float quadWidth = 64, quadHeight = 64;
- float x = 0, y = 0;
- float degrees = 0;
- while (message.message != WM_QUIT)
- {
- while ((PeekMessage(&message, 0, 0, 0, PM_REMOVE)) && (message.message != WM_QUIT))
- DispatchMessage(&message);
- x += xvel;
- y += yvel;
- if ((xvel != 0) || (yvel != 0))
- {
- if ((xvel != 0) && (yvel != 0))
- degrees = 45;
- else
- degrees = 0;
- }
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(x + quadWidth / 2.f, y + quadHeight / 2.f, 0);
- glRotatef(degrees, 0, 0, 1);
- glBegin(GL_QUADS);
- glColor3f(1, 0, 0);
- glVertex2f( -quadWidth / 2.f, -quadHeight / 2.f);
- glVertex2f( quadWidth / 2.f, -quadHeight / 2.f);
- glVertex2f( quadWidth / 2.f, quadHeight / 2.f);
- glVertex2f( -quadWidth / 2.f, quadHeight / 2.f);
- glEnd();
- SwapBuffers(device);
- }
- wglMakeCurrent(0, 0);
- wglDeleteContext(render);
- ReleaseDC(window, device);
- DestroyWindow(window);
- UnregisterClass(className, hInstance);
- return message.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement