dpi.c 704 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "dpi.h"
  2. #include <SDL2/SDL.h>
  3. #ifdef _WIN32
  4. #include <Windows.h>
  5. #include <ShellScalingApi.h>
  6. #endif
  7. static float x_factor = 1.f, y_factor = 1.f;
  8. void dpi_init() {
  9. #ifdef _WIN32
  10. SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
  11. HDC dc = GetDC(NULL);
  12. float standard_px_per_logical_inch = 96.f;
  13. x_factor = GetDeviceCaps(dc, LOGPIXELSX) / standard_px_per_logical_inch;
  14. y_factor = GetDeviceCaps(dc, LOGPIXELSY) / standard_px_per_logical_inch;
  15. ReleaseDC(NULL, dc);
  16. #endif
  17. }
  18. float dpi_x(float x) {
  19. return x * x_factor;
  20. }
  21. float dpi_y(float y) {
  22. return y * y_factor;
  23. }
  24. int dpi_xi(int x) {
  25. return (int)(x * x_factor);
  26. }
  27. int dpi_yi(int y) {
  28. return (int)(y * y_factor);
  29. }