win_proc.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifdef _WIN32
  2. #include <windows.h>
  3. #include "internal.h"
  4. #include "pltf/pltf.h"
  5. #define PLTF_KEYS_X(lower, upper) \
  6. case PLTF_KEY_##upper: \
  7. if (pltf_state.key_##lower##_cb != NULL) { \
  8. pltf_state.key_##lower##_cb(action); \
  9. } \
  10. break;
  11. static void handle_key(const WPARAM key, const PltfKeyAction action) {
  12. const PltfKey pltf_key = vk_to_pltf_key(key);
  13. if (pltf_state.key_cb != NULL) {
  14. pltf_state.key_cb(pltf_key, action);
  15. }
  16. }
  17. #undef PLTF_KEYS_X
  18. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  19. WinState* pState = NULL;
  20. if (uMsg == WM_CREATE) {
  21. CREATESTRUCT* pCreate = (CREATESTRUCT*) lParam;
  22. pState = (WinState*) pCreate->lpCreateParams;
  23. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pState);
  24. } else {
  25. LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA);
  26. pState = (WinState*) ptr;
  27. }
  28. switch (uMsg) {
  29. // case WM_LBUTTONDOWN: {
  30. // printf("State: msg: %s, num: %d\n", pState->msg, pState->num);
  31. //
  32. // printf("lParam: %llx\n", lParam);
  33. // int mouse_x = GET_X_LPARAM(lParam);
  34. // printf("mouse_x: %x\n", mouse_x);
  35. // int mouse_y = GET_Y_LPARAM(lParam);
  36. // printf("mouse_y: %x\n", mouse_y);
  37. // printf("x: %d y: %d\n", mouse_x, mouse_y);
  38. //
  39. // return 0;
  40. // }
  41. // case WM_CREATE: {
  42. // return 0;
  43. // }
  44. case WM_KEYDOWN:
  45. handle_key(wParam, PLTF_KEY_DOWN);
  46. return 0;
  47. case WM_KEYUP:
  48. handle_key(wParam, PLTF_KEY_UP);
  49. return 0;
  50. case WM_CLOSE:
  51. // if (MessageBox(hwnd, L"Really quit?", L"My Application", MB_OKCANCEL) == IDOK) {
  52. // }
  53. PostMessage(hwnd, WM_DESTROY, 0, 0);
  54. return 0;
  55. }
  56. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  57. }
  58. #endif // _WIN32