Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdint.h>
- #include <xinput.h>
- typedef uint8_t uint8;
- typedef uint16_t uint16;
- typedef uint32_t uint32;
- typedef uint64_t uint64;
- typedef int8_t int8;
- typedef int16_t int16;
- typedef int32_t int32;
- typedef int64_t int64;
- struct win32_offscreen_buffer {
- BITMAPINFO bitmapInfo;
- void* memory;
- int width;
- int height;
- int pitch;
- int bytesPerPixel;
- };
- struct win32_window_dimension {
- int width;
- int height;
- };
- static bool g_running;
- static win32_offscreen_buffer globalBackBuffer;
- #define X_INPUT_GET_STATE(name) DWORD WINAPI name(DWORD, XINPUT_STATE*)
- typedef X_INPUT_GET_STATE(x_input_get_state);
- X_INPUT_GET_STATE(XInputGetStateStub) {
- return 0;
- }
- static x_input_get_state *DXInputGetState = XInputGetStateStub;
- #define XInputGetState DXInputGetState
- #define X_INPUT_SET_STATE(name) DWORD WINAPI name(DWORD, XINPUT_VIBRATION*)
- typedef X_INPUT_SET_STATE(x_input_set_state);
- X_INPUT_SET_STATE(XInputSetStateStub) {
- return 0;
- }
- static x_input_set_state *DXInputSetState = XInputSetStateStub;
- #define XInputSetState DXInputSetState
- static void
- Win32LoadXInput(void) {
- HMODULE XInput = LoadLibrary("xinput1_3.dll");
- if (!XInput)
- return;
- XInputGetState = (x_input_get_state*) GetProcAddress(XInput, "XInputGetState");
- XInputSetState = (x_input_set_state*) GetProcAddress(XInput, "XInputSetState");
- }
- static win32_window_dimension
- Win32GetWindowDimensions(HWND win) {
- win32_window_dimension dimensions;
- RECT rect;
- GetClientRect(win, &rect);
- dimensions.width = rect.right - rect.left;
- dimensions.height = rect.bottom - rect.top;
- return dimensions;
- }
- static void
- RenderWeirdGradient(win32_offscreen_buffer buffer, int xOff, int yOff) {
- uint8 *row = (uint8*) buffer.memory;
- for (int y = 0; y < buffer.height; y++) {
- uint32 *pixel = (uint32*) row;
- for (int x = 0; x < buffer.width; x++) {
- uint8 b = x + xOff;
- uint8 g = y + yOff;
- *pixel++ = (g << 8) | b;
- }
- row += buffer.pitch;
- }
- }
- static void
- Win32ResizeDIBSection(win32_offscreen_buffer *buffer, int w, int h) {
- if (buffer->memory)
- VirtualFree(buffer->memory, 0, MEM_RELEASE);
- buffer->width = w;
- buffer->height = h;
- buffer->bitmapInfo.bmiHeader.biSize = sizeof(buffer->bitmapInfo.bmiHeader);
- buffer->bitmapInfo.bmiHeader.biWidth = w;
- buffer->bitmapInfo.bmiHeader.biHeight = -h;
- buffer->bitmapInfo.bmiHeader.biPlanes = 1;
- buffer->bitmapInfo.bmiHeader.biBitCount = 32;
- buffer->bitmapInfo.bmiHeader.biCompression = BI_RGB;
- buffer->bytesPerPixel = 4;
- buffer->memory = VirtualAlloc(0, w * h * 4, MEM_COMMIT, PAGE_READWRITE);
- buffer->pitch = w * buffer->bytesPerPixel;
- }
- static void
- Win32DisplayBuffer(HDC context, int w, int h, win32_offscreen_buffer buffer) {
- StretchDIBits(context, 0, 0, w, h, 0, 0, buffer.width, buffer.height, buffer.memory, &buffer.bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
- }
- LRESULT CALLBACK
- Win32WindowCallback(HWND win, UINT msg, WPARAM wParam, LPARAM lParam) {
- LRESULT result = 0;
- switch (msg) {
- case WM_SIZE: {
- win32_window_dimension d = Win32GetWindowDimensions(win);
- Win32ResizeDIBSection(&globalBackBuffer, d.width, d.height);
- OutputDebugString("WM_SIZE\n");
- } break;
- case WM_DESTROY: {
- g_running = false;
- OutputDebugString("WM_DESTROY\n");
- } break;
- case WM_CLOSE: {
- g_running = false;
- OutputDebugString("WM_CLOSE\n");
- } break;
- case WM_ACTIVATEAPP: {
- OutputDebugString("WM_ACTIVEAPP\n");
- } break;
- case WM_PAINT: {
- PAINTSTRUCT paint;
- HDC context = BeginPaint(win, &paint);
- win32_window_dimension d = Win32GetWindowDimensions(win);
- Win32DisplayBuffer(context, d.width, d.height, globalBackBuffer);
- EndPaint(win, &paint);
- } break;
- default: {
- result = DefWindowProc(win, msg, wParam, lParam);
- } break;
- }
- return result;
- }
- int CALLBACK
- WinMain(HINSTANCE ins, HINSTANCE prevIns, LPSTR cmdLine, int showCode) {
- Win32LoadXInput();
- WNDCLASS windowClass = {};
- windowClass.style = CS_OWNDC | CS_HREDRAW;
- windowClass.lpfnWndProc = Win32WindowCallback;
- windowClass.hInstance = ins;
- // window.hIcon;
- windowClass.lpszClassName = "HandmadeHeroWindowClass";
- Win32ResizeDIBSection(&globalBackBuffer, 1280, 720);
- if (RegisterClass(&windowClass) == 0)
- return 0;
- HWND window = CreateWindowEx(0, windowClass.lpszClassName, "Handmade Hero", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, ins, 0);
- if (window == 0)
- return 0;
- g_running = true;
- int xOff = 0, yOff = 0;
- while (g_running) {
- MSG message;
- while (PeekMessage(&message, 0, 0, 0, PM_REMOVE)) {
- if (message.message == WM_QUIT)
- g_running = false;
- TranslateMessage(&message);
- DispatchMessage(&message);
- }
- for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) {
- XINPUT_STATE state;
- if (XInputGetState(i, &state) != ERROR_SUCCESS)
- break;
- XINPUT_GAMEPAD *gp = &state.Gamepad;
- bool up = gp->wButtons & XINPUT_GAMEPAD_DPAD_UP;
- bool down = gp->wButtons & XINPUT_GAMEPAD_DPAD_DOWN;
- bool left = gp->wButtons & XINPUT_GAMEPAD_DPAD_LEFT;
- bool right = gp->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT;
- bool start = gp->wButtons & XINPUT_GAMEPAD_START;
- bool back = gp->wButtons & XINPUT_GAMEPAD_BACK;
- bool lshoulder = gp->wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER;
- bool rshoulder = gp->wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER;
- bool a = gp->wButtons & XINPUT_GAMEPAD_A;
- bool b = gp->wButtons & XINPUT_GAMEPAD_B;
- bool x = gp->wButtons & XINPUT_GAMEPAD_X;
- bool y = gp->wButtons & XINPUT_GAMEPAD_Y;
- int16 stickX = gp->sThumbLX;
- int16 stickY = gp->sThumbLY;
- XINPUT_VIBRATION vib;
- if (a) {
- yOff++;
- vib.wLeftMotorSpeed = 65535;
- vib.wRightMotorSpeed = 65535;
- } else {
- vib.wLeftMotorSpeed = 0;
- vib.wRightMotorSpeed = 0;
- }
- XInputSetState(0, &vib);
- }
- RenderWeirdGradient(globalBackBuffer, xOff, yOff);
- win32_window_dimension d = Win32GetWindowDimensions(window);
- HDC context = GetDC(window);
- Win32DisplayBuffer(context, d.width, d.height, globalBackBuffer);
- ReleaseDC(window, context);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement