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.

108 lines
2.6KB

  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. #ifdef 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. int handle;
  35. Font(const std::string &filename);
  36. ~Font();
  37. static std::shared_ptr<Font> load(const std::string &filename);
  38. };
  39. struct Image {
  40. int handle;
  41. Image(const std::string &filename);
  42. ~Image();
  43. static std::shared_ptr<Image> load(const std::string &filename);
  44. };
  45. struct SVG {
  46. NSVGimage *handle;
  47. SVG(const std::string &filename);
  48. ~SVG();
  49. static std::shared_ptr<SVG> load(const std::string &filename);
  50. };
  51. struct Window {
  52. GLFWwindow *win = NULL;
  53. NVGcontext *vg = NULL;
  54. /** Secondary nanovg context for drawing to framebuffers */
  55. NVGcontext *fbVg = NULL;
  56. /** The scaling ratio */
  57. float pixelRatio = 1.f;
  58. /* The ratio between the framebuffer size and the window size reported by the OS.
  59. This is not equal to gPixelRatio in general.
  60. */
  61. float windowRatio = 1.f;
  62. bool allowCursorLock = true;
  63. int frame = 0;
  64. /** The last known absolute mouse position in the window */
  65. math::Vec mousePos;
  66. std::shared_ptr<Font> uiFont;
  67. struct Internal;
  68. Internal *internal;
  69. Window();
  70. ~Window();
  71. void run();
  72. void close();
  73. void cursorLock();
  74. void cursorUnlock();
  75. /** Gets the current keyboard mod state
  76. Don't call this from a Key event. Simply use `e.mods` instead.
  77. */
  78. int getMods();
  79. math::Vec getWindowSize();
  80. void setWindowSize(math::Vec size);
  81. math::Vec getWindowPos();
  82. void setWindowPos(math::Vec pos);
  83. bool isMaximized();
  84. void setFullScreen(bool fullScreen);
  85. bool isFullScreen();
  86. };
  87. void windowInit();
  88. void windowDestroy();
  89. } // namespace rack