Advertisement
Guest User

Diagonal

a guest
Mar 14th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <gl\GL.h>
  3.  
  4. #pragma comment (lib, "opengl32.lib")
  5.  
  6. float xvel, yvel;
  7.  
  8. bool Keys[255];
  9.  
  10. void ProcessInput(unsigned int key, float vel)
  11. {
  12.     switch (key)
  13.     {
  14.         case VK_RIGHT:
  15.         case 'D':
  16.  
  17.             xvel += vel;
  18.  
  19.             break;
  20.  
  21.         case VK_LEFT:
  22.         case 'A':
  23.  
  24.             xvel -= vel;
  25.  
  26.             break;
  27.  
  28.         case VK_DOWN:
  29.         case 'S':
  30.  
  31.             yvel += vel;
  32.  
  33.             break;
  34.  
  35.         case VK_UP:
  36.         case 'W':
  37.  
  38.             yvel -= vel;
  39.  
  40.             break;
  41.     }
  42. }
  43.  
  44. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  45. {
  46.     switch (uMsg)
  47.     {
  48.         /*
  49.         case WM_KEYDOWN:
  50.  
  51.             if ((lParam & (1 << 30)) == 0)
  52.                 ProcessInput(wParam, 4);
  53.  
  54.             break;
  55.  
  56.         case WM_KEYUP:
  57.  
  58.             ProcessInput(wParam, -4);
  59.  
  60.             break;
  61.         */
  62.         case WM_INPUT:
  63.         {
  64.             unsigned int size;
  65.  
  66.             GetRawInputData((HRAWINPUT) lParam, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER));
  67.  
  68.             unsigned char *data = new unsigned char[size];
  69.  
  70.             GetRawInputData((HRAWINPUT) lParam, RID_INPUT, data, &size, sizeof(RAWINPUTHEADER));
  71.  
  72.             RAWINPUT *raw = (RAWINPUT*) data;
  73.  
  74.             if (raw->header.dwType == RIM_TYPEKEYBOARD)
  75.             {
  76.                 if (raw->data.keyboard.Flags & RI_KEY_BREAK)
  77.                 {
  78.                     if (Keys[raw->data.keyboard.MakeCode])
  79.                     {
  80.                         Keys[raw->data.keyboard.MakeCode] = false;
  81.  
  82.                         ProcessInput(MapVirtualKey(raw->data.keyboard.MakeCode, MAPVK_VSC_TO_VK), -4);
  83.                     }
  84.                 }
  85.                 else
  86.                 {
  87.                     if (!Keys[raw->data.keyboard.MakeCode])
  88.                     {
  89.                         Keys[raw->data.keyboard.MakeCode] = true;
  90.  
  91.                         ProcessInput(MapVirtualKey(raw->data.keyboard.MakeCode, MAPVK_VSC_TO_VK), 4);
  92.                     }
  93.                 }
  94.             }
  95.  
  96.             break;
  97.         }
  98.  
  99.         case WM_CLOSE:
  100.        
  101.             PostQuitMessage(EXIT_SUCCESS);
  102.  
  103.             return 0;
  104.     }
  105.  
  106.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  107. }
  108.  
  109. int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  110. {
  111.     TCHAR className[] = TEXT("Window");
  112.  
  113.     WNDCLASS windowClass = {0};
  114.  
  115.     windowClass.lpfnWndProc = WindowProc;
  116.     windowClass.hInstance = hInstance;
  117.     windowClass.lpszClassName = className;
  118.  
  119.     RegisterClass(&windowClass);
  120.  
  121.     unsigned int style = WS_OVERLAPPEDWINDOW;
  122.  
  123.     RECT rectangle = {0, 0, 640, 480};
  124.  
  125.     AdjustWindowRect(&rectangle, style, false);
  126.  
  127.     HWND window = CreateWindow(className, TEXT("Diagonal"), style, CW_USEDEFAULT, CW_USEDEFAULT, rectangle.right - rectangle.left, rectangle.bottom - rectangle.top, 0, 0, hInstance, 0);
  128.  
  129.     RAWINPUTDEVICE rid[1] = {0};
  130.  
  131.     rid[0].usUsagePage = 0x01;
  132.     rid[0].usUsage = 0x06;
  133.     rid[0].dwFlags = RIDEV_NOLEGACY;
  134.    
  135.     RegisterRawInputDevices(rid, 1, sizeof(rid[0]));
  136.  
  137.     HDC device = GetDC(window);
  138.  
  139.     PIXELFORMATDESCRIPTOR descriptor = {0};
  140.  
  141.     descriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  142.     descriptor.nVersion = 1;
  143.     descriptor.dwFlags = PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
  144.     descriptor.iPixelType = PFD_TYPE_RGBA;
  145.     descriptor.cColorBits = 32;
  146.  
  147.     int format = ChoosePixelFormat(device, &descriptor);
  148.  
  149.     SetPixelFormat(device, format, &descriptor);
  150.  
  151.     HGLRC render = wglCreateContext(device);
  152.  
  153.     wglMakeCurrent(device, render);
  154.  
  155.     glMatrixMode(GL_PROJECTION);
  156.  
  157.     glLoadIdentity();
  158.  
  159.     glOrtho(0, 640, 480, 0, 1, -1);
  160.  
  161.     glMatrixMode(GL_MODELVIEW);
  162.  
  163.     glLoadIdentity();
  164.  
  165.     glClearColor(1, 1, 1, 1);
  166.  
  167.     ShowWindow(window, nCmdShow);
  168.  
  169.     MSG message = {0};
  170.  
  171.     float quadWidth = 64, quadHeight = 64;
  172.  
  173.     float x = 0, y = 0;
  174.  
  175.     float degrees = 0;
  176.  
  177.     while (message.message != WM_QUIT)
  178.     {
  179.         while ((PeekMessage(&message, 0, 0, 0, PM_REMOVE)) && (message.message != WM_QUIT))
  180.             DispatchMessage(&message);
  181.        
  182.         x += xvel;
  183.         y += yvel;
  184.  
  185.         if ((xvel != 0) || (yvel != 0))
  186.         {
  187.             if ((xvel != 0) && (yvel != 0))
  188.                 degrees = 45;
  189.             else
  190.                 degrees = 0;
  191.         }
  192.  
  193.         glClear(GL_COLOR_BUFFER_BIT);
  194.  
  195.         glLoadIdentity();
  196.  
  197.         glTranslatef(x + quadWidth / 2.f, y + quadHeight / 2.f, 0);
  198.        
  199.         glRotatef(degrees, 0, 0, 1);
  200.  
  201.         glBegin(GL_QUADS);
  202.        
  203.         glColor3f(1, 0, 0);
  204.  
  205.         glVertex2f( -quadWidth / 2.f, -quadHeight / 2.f);
  206.         glVertex2f(  quadWidth / 2.f, -quadHeight / 2.f);
  207.         glVertex2f(  quadWidth / 2.f,  quadHeight / 2.f);
  208.         glVertex2f( -quadWidth / 2.f,  quadHeight / 2.f);
  209.  
  210.         glEnd();
  211.  
  212.         SwapBuffers(device);
  213.     }
  214.    
  215.     wglMakeCurrent(0, 0);
  216.  
  217.     wglDeleteContext(render);
  218.  
  219.     ReleaseDC(window, device);
  220.  
  221.     DestroyWindow(window);
  222.  
  223.     UnregisterClass(className, hInstance);
  224.  
  225.     return message.wParam;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement