Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.89 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdint.h>
  3. #include <xinput.h>
  4.  
  5. typedef uint8_t uint8;
  6. typedef uint16_t uint16;
  7. typedef uint32_t uint32;
  8. typedef uint64_t uint64;
  9.  
  10. typedef int8_t int8;
  11. typedef int16_t int16;
  12. typedef int32_t int32;
  13. typedef int64_t int64;
  14.  
  15. struct win32_offscreen_buffer {
  16.     BITMAPINFO bitmapInfo;
  17.     void* memory;
  18.     int width;
  19.     int height;
  20.     int pitch;
  21.     int bytesPerPixel;
  22. };
  23.  
  24. struct win32_window_dimension {
  25.     int width;
  26.     int height;
  27. };
  28.  
  29. static bool g_running;
  30. static win32_offscreen_buffer globalBackBuffer;
  31.  
  32. #define X_INPUT_GET_STATE(name) DWORD WINAPI name(DWORD, XINPUT_STATE*)
  33. typedef X_INPUT_GET_STATE(x_input_get_state);
  34. X_INPUT_GET_STATE(XInputGetStateStub) {
  35.     return 0;
  36. }
  37.  
  38. static x_input_get_state *DXInputGetState = XInputGetStateStub;
  39. #define XInputGetState DXInputGetState
  40.  
  41. #define X_INPUT_SET_STATE(name) DWORD WINAPI name(DWORD, XINPUT_VIBRATION*)
  42. typedef X_INPUT_SET_STATE(x_input_set_state);
  43. X_INPUT_SET_STATE(XInputSetStateStub) {
  44.     return 0;
  45. }
  46.  
  47. static x_input_set_state *DXInputSetState = XInputSetStateStub;
  48. #define XInputSetState DXInputSetState
  49.  
  50. static void
  51. Win32LoadXInput(void) {
  52.     HMODULE XInput = LoadLibrary("xinput1_3.dll");
  53.     if (!XInput)
  54.         return;
  55.  
  56.     XInputGetState = (x_input_get_state*) GetProcAddress(XInput, "XInputGetState");
  57.     XInputSetState = (x_input_set_state*) GetProcAddress(XInput, "XInputSetState");
  58. }
  59.  
  60. static win32_window_dimension
  61. Win32GetWindowDimensions(HWND win) {
  62.     win32_window_dimension dimensions;
  63.     RECT rect;
  64.     GetClientRect(win, &rect);
  65.  
  66.     dimensions.width = rect.right - rect.left;
  67.     dimensions.height = rect.bottom - rect.top;
  68.     return dimensions;
  69. }
  70.  
  71. static void
  72. RenderWeirdGradient(win32_offscreen_buffer buffer, int xOff, int yOff) {
  73.     uint8 *row = (uint8*) buffer.memory;
  74.     for (int y = 0; y < buffer.height; y++) {
  75.         uint32 *pixel = (uint32*) row;
  76.         for (int x = 0; x < buffer.width; x++) {
  77.             uint8 b = x + xOff;
  78.             uint8 g = y + yOff;
  79.  
  80.             *pixel++ = (g << 8) | b;
  81.         }
  82.         row += buffer.pitch;
  83.     }
  84. }
  85.  
  86. static void
  87. Win32ResizeDIBSection(win32_offscreen_buffer *buffer, int w, int h) {
  88.     if (buffer->memory)
  89.         VirtualFree(buffer->memory, 0, MEM_RELEASE);
  90.  
  91.     buffer->width = w;
  92.     buffer->height = h;
  93.  
  94.     buffer->bitmapInfo.bmiHeader.biSize = sizeof(buffer->bitmapInfo.bmiHeader);
  95.     buffer->bitmapInfo.bmiHeader.biWidth = w;
  96.     buffer->bitmapInfo.bmiHeader.biHeight = -h;
  97.     buffer->bitmapInfo.bmiHeader.biPlanes = 1;
  98.     buffer->bitmapInfo.bmiHeader.biBitCount = 32;
  99.     buffer->bitmapInfo.bmiHeader.biCompression = BI_RGB;
  100.  
  101.     buffer->bytesPerPixel = 4;
  102.     buffer->memory = VirtualAlloc(0, w * h * 4, MEM_COMMIT, PAGE_READWRITE);
  103.     buffer->pitch = w * buffer->bytesPerPixel;
  104. }
  105.  
  106. static void
  107. Win32DisplayBuffer(HDC context, int w, int h, win32_offscreen_buffer buffer) {
  108.     StretchDIBits(context, 0, 0, w, h, 0, 0, buffer.width, buffer.height, buffer.memory, &buffer.bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
  109. }
  110.  
  111. LRESULT CALLBACK
  112. Win32WindowCallback(HWND win, UINT msg, WPARAM wParam, LPARAM lParam) {
  113.     LRESULT result = 0;
  114.     switch (msg) {
  115.  
  116.     case WM_SIZE: {
  117.         win32_window_dimension d = Win32GetWindowDimensions(win);
  118.         Win32ResizeDIBSection(&globalBackBuffer, d.width, d.height);
  119.         OutputDebugString("WM_SIZE\n");
  120.     } break;
  121.  
  122.     case WM_DESTROY: {
  123.         g_running = false;
  124.         OutputDebugString("WM_DESTROY\n");
  125.     } break;
  126.  
  127.     case WM_CLOSE: {
  128.         g_running = false;
  129.         OutputDebugString("WM_CLOSE\n");
  130.     } break;
  131.  
  132.     case WM_ACTIVATEAPP: {
  133.         OutputDebugString("WM_ACTIVEAPP\n");
  134.     } break;
  135.  
  136.     case WM_PAINT: {
  137.         PAINTSTRUCT paint;
  138.         HDC context = BeginPaint(win, &paint);
  139.  
  140.         win32_window_dimension d = Win32GetWindowDimensions(win);        
  141.         Win32DisplayBuffer(context, d.width, d.height, globalBackBuffer);
  142.         EndPaint(win, &paint);
  143.     } break;
  144.        
  145.     default: {
  146.         result = DefWindowProc(win, msg, wParam, lParam);
  147.     } break;
  148.     }
  149.     return result;
  150. }
  151.  
  152. int CALLBACK
  153. WinMain(HINSTANCE ins, HINSTANCE prevIns, LPSTR cmdLine, int showCode) {
  154.     Win32LoadXInput();
  155.    
  156.     WNDCLASS windowClass = {};
  157.     windowClass.style = CS_OWNDC | CS_HREDRAW;
  158.     windowClass.lpfnWndProc = Win32WindowCallback;
  159.     windowClass.hInstance = ins;
  160.     //  window.hIcon;
  161.     windowClass.lpszClassName = "HandmadeHeroWindowClass";
  162.  
  163.     Win32ResizeDIBSection(&globalBackBuffer, 1280, 720);
  164.    
  165.     if (RegisterClass(&windowClass) == 0)
  166.         return 0;
  167.    
  168.     HWND window = CreateWindowEx(0, windowClass.lpszClassName, "Handmade Hero", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, ins, 0);
  169.     if (window == 0)
  170.         return 0;
  171.  
  172.     g_running = true;
  173.     int xOff = 0, yOff = 0;
  174.     while (g_running) {
  175.         MSG message;
  176.         while (PeekMessage(&message, 0, 0, 0, PM_REMOVE)) {
  177.             if (message.message == WM_QUIT)
  178.                 g_running = false;
  179.            
  180.             TranslateMessage(&message);
  181.             DispatchMessage(&message);
  182.         }
  183.  
  184.         for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) {
  185.             XINPUT_STATE state;
  186.             if (XInputGetState(i, &state) != ERROR_SUCCESS)
  187.                 break;
  188.  
  189.             XINPUT_GAMEPAD *gp = &state.Gamepad;
  190.            
  191.             bool up = gp->wButtons & XINPUT_GAMEPAD_DPAD_UP;
  192.             bool down = gp->wButtons & XINPUT_GAMEPAD_DPAD_DOWN;
  193.             bool left = gp->wButtons & XINPUT_GAMEPAD_DPAD_LEFT;
  194.             bool right = gp->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT;
  195.             bool start = gp->wButtons & XINPUT_GAMEPAD_START;
  196.             bool back = gp->wButtons & XINPUT_GAMEPAD_BACK;
  197.             bool lshoulder = gp->wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER;
  198.             bool rshoulder = gp->wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER;
  199.             bool a = gp->wButtons & XINPUT_GAMEPAD_A;
  200.             bool b = gp->wButtons & XINPUT_GAMEPAD_B;
  201.             bool x = gp->wButtons & XINPUT_GAMEPAD_X;
  202.             bool y = gp->wButtons & XINPUT_GAMEPAD_Y;
  203.  
  204.             int16 stickX = gp->sThumbLX;
  205.             int16 stickY = gp->sThumbLY;
  206.  
  207.             XINPUT_VIBRATION vib;
  208.             if (a) {
  209.                 yOff++;
  210.                
  211.                 vib.wLeftMotorSpeed = 65535;
  212.                 vib.wRightMotorSpeed = 65535;
  213.             } else {
  214.                 vib.wLeftMotorSpeed = 0;
  215.                 vib.wRightMotorSpeed = 0;
  216.             }
  217.             XInputSetState(0, &vib);
  218.         }
  219.        
  220.         RenderWeirdGradient(globalBackBuffer, xOff, yOff);
  221.    
  222.         win32_window_dimension d = Win32GetWindowDimensions(window);
  223.         HDC context = GetDC(window);
  224.         Win32DisplayBuffer(context, d.width, d.height, globalBackBuffer);
  225.         ReleaseDC(window, context);
  226.     }
  227.  
  228.     return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement