You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.0KB

  1. #pragma once
  2. #include "common.hpp"
  3. #include "math.hpp"
  4. #include <memory>
  5. #define GLEW_STATIC
  6. #include <GL/glew.h>
  7. #include <GLFW/glfw3.h>
  8. #include <nanovg.h>
  9. #define NANOVG_GL2
  10. #include <nanovg_gl.h>
  11. #include <nanovg_gl_utils.h>
  12. #include <nanosvg.h>
  13. /** Remaps Ctrl to Cmd on Mac
  14. Use this instead of GLFW_MOD_CONTROL, since Cmd should be used on Mac in place of Ctrl on Linux/Windows.
  15. */
  16. #if defined ARCH_MAC
  17. #define WINDOW_MOD_CTRL GLFW_MOD_SUPER
  18. #define WINDOW_MOD_CTRL_NAME "Cmd"
  19. #else
  20. #define WINDOW_MOD_CTRL GLFW_MOD_CONTROL
  21. #define WINDOW_MOD_CTRL_NAME "Ctrl"
  22. #endif
  23. #define WINDOW_MOD_SHIFT_NAME "Shift"
  24. #define WINDOW_MOD_ALT_NAME "Alt"
  25. /** Filters actual mod keys from the mod flags.
  26. Use this if you don't care about GLFW_MOD_CAPS_LOCK and GLFW_MOD_NUM_LOCK.
  27. Example usage:
  28. if ((e.mod & WINDOW_MOD_MASK) == (WINDOW_MOD | GLFW_MOD_SHIFT)) ...
  29. */
  30. #define WINDOW_MOD_MASK (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL | GLFW_MOD_ALT | GLFW_MOD_SUPER)
  31. namespace rack {
  32. // Constructing these directly will load from the disk each time. Use the load() functions to load from disk and cache them as long as the shared_ptr is held.
  33. struct Font {
  34. NVGcontext *vg;
  35. int handle;
  36. /** Don't call this directly but instead use `APP->window->loadFont()` */
  37. Font(NVGcontext *vg, const std::string &filename);
  38. ~Font();
  39. /** Use `APP->window->loadFont()` instead. */
  40. DEPRECATED static std::shared_ptr<Font> load(const std::string &filename);
  41. };
  42. struct Image {
  43. NVGcontext *vg;
  44. int handle;
  45. /** Don't call this directly but instead use `APP->window->loadImage()` */
  46. Image(NVGcontext *vg, const std::string &filename);
  47. ~Image();
  48. /** Use `APP->window->loadImage()` instead. */
  49. DEPRECATED static std::shared_ptr<Image> load(const std::string &filename);
  50. };
  51. struct Svg {
  52. NSVGimage *handle;
  53. /** Don't call this directly but instead use `APP->window->loadSvg()` */
  54. Svg(const std::string &filename);
  55. ~Svg();
  56. /** Use `APP->window->loadSvg()` instead. */
  57. DEPRECATED static std::shared_ptr<Svg> load(const std::string &filename);
  58. };
  59. DEPRECATED typedef Svg SVG;
  60. struct Window {
  61. GLFWwindow *win = NULL;
  62. NVGcontext *vg = NULL;
  63. /** The scaling ratio */
  64. float pixelRatio = 1.f;
  65. /* The ratio between the framebuffer size and the window size reported by the OS.
  66. This is not equal to gPixelRatio in general.
  67. */
  68. float windowRatio = 1.f;
  69. int frame = 0;
  70. /** The last known absolute mouse position in the window */
  71. math::Vec mousePos;
  72. std::shared_ptr<Font> uiFont;
  73. struct Internal;
  74. Internal *internal;
  75. Window();
  76. ~Window();
  77. void run();
  78. void close();
  79. void cursorLock();
  80. void cursorUnlock();
  81. /** Gets the current keyboard mod state
  82. Don't call this from a Key event. Simply use `e.mods` instead.
  83. */
  84. int getMods();
  85. void setFullScreen(bool fullScreen);
  86. bool isFullScreen();
  87. std::shared_ptr<Font> loadFont(const std::string &filename);
  88. std::shared_ptr<Image> loadImage(const std::string &filename);
  89. std::shared_ptr<Svg> loadSvg(const std::string &filename);
  90. };
  91. void windowInit();
  92. void windowDestroy();
  93. } // namespace rack