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.

133 lines
3.8KB

  1. #pragma once
  2. #include <memory>
  3. #include <map>
  4. #include <common.hpp>
  5. #include <math.hpp>
  6. #include <window/Svg.hpp>
  7. #define GLEW_STATIC
  8. #define GLEW_NO_GLU
  9. #include <GL/glew.h>
  10. #include <GLFW/glfw3.h>
  11. #include <nanovg.h>
  12. #define NANOVG_GL2
  13. #include <nanovg_gl.h>
  14. #include <nanovg_gl_utils.h>
  15. namespace rack {
  16. /** Handles OS windowing, OpenGL, and NanoVG
  17. */
  18. namespace window {
  19. // 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.
  20. /** Text font loaded in a particular Window context. */
  21. struct Font {
  22. NVGcontext* vg;
  23. int handle = -1;
  24. ~Font();
  25. /** Don't call this directly but instead use `APP->window->loadFont()` */
  26. void loadFile(const std::string& filename, NVGcontext* vg);
  27. /** Use `APP->window->loadFont()` instead. */
  28. DEPRECATED static std::shared_ptr<Font> load(const std::string& filename);
  29. };
  30. /** Bitmap/raster image loaded in a particular Window context. */
  31. struct Image {
  32. NVGcontext* vg;
  33. int handle = -1;
  34. ~Image();
  35. /** Don't call this directly but instead use `APP->window->loadImage()` */
  36. void loadFile(const std::string& filename, NVGcontext* vg);
  37. /** Use `APP->window->loadImage()` instead. */
  38. DEPRECATED static std::shared_ptr<Image> load(const std::string& filename);
  39. };
  40. /** OS window with OpenGL context. */
  41. struct Window {
  42. struct Internal;
  43. Internal* internal;
  44. GLFWwindow* win = NULL;
  45. NVGcontext* vg = NULL;
  46. NVGcontext* fbVg = NULL;
  47. /** UI scaling ratio */
  48. float pixelRatio = 1.f;
  49. /** Ratio between the framebuffer size and the window size reported by the OS.
  50. This is not equal to pixelRatio in general.
  51. */
  52. float windowRatio = 1.f;
  53. std::shared_ptr<Font> uiFont;
  54. PRIVATE Window();
  55. PRIVATE ~Window();
  56. math::Vec getSize();
  57. void setSize(math::Vec size);
  58. PRIVATE void run();
  59. PRIVATE void step();
  60. void activateContext();
  61. /** Takes a screenshot of the screen and saves it to a PNG file. */
  62. PRIVATE void screenshot(const std::string& screenshotPath);
  63. /** Saves a PNG image of all modules to `screenshotsDir/<plugin slug>/<module slug>.png`.
  64. Skips screenshot if the file already exists.
  65. */
  66. PRIVATE void screenshotModules(const std::string& screenshotsDir, float zoom = 1.f);
  67. /** Request Window to be closed after rendering the current frame. */
  68. void close();
  69. void cursorLock();
  70. void cursorUnlock();
  71. bool isCursorLocked();
  72. /** Gets the current keyboard mod state
  73. Don't call this from a Key event. Simply use `e.mods` instead.
  74. */
  75. int getMods();
  76. void setFullScreen(bool fullScreen);
  77. bool isFullScreen();
  78. /** Returns the primary monitor's refresh rate in Hz. */
  79. double getMonitorRefreshRate();
  80. /** Returns the timestamp of the beginning of the current frame render process.
  81. Returns NAN if no frames have begun rendering.
  82. */
  83. double getFrameTime();
  84. /** Returns the total time in seconds spent rendering the last frame.
  85. Returns NAN if no frames have ended rendering.
  86. */
  87. double getLastFrameDuration();
  88. /** Returns the current time remaining in seconds until the frame deadline, according to the frame rate limit. */
  89. double getFrameDurationRemaining();
  90. /** Loads and caches a Font from a file path.
  91. Do not store this reference across screen frames, as the Window may have changed, invalidating the Font.
  92. */
  93. std::shared_ptr<Font> loadFont(const std::string& filename);
  94. /** Loads and caches an Image from a file path.
  95. Do not store this reference across screen frames, as the Window may have changed, invalidating the Image.
  96. */
  97. std::shared_ptr<Image> loadImage(const std::string& filename);
  98. /** Loads and caches an Svg from a file path.
  99. Alias for `Svg::load()`.
  100. */
  101. std::shared_ptr<Svg> loadSvg(const std::string& filename) {
  102. return Svg::load(filename);
  103. }
  104. PRIVATE bool& fbDirtyOnSubpixelChange();
  105. PRIVATE int& fbCount();
  106. };
  107. PRIVATE void init();
  108. PRIVATE void destroy();
  109. } // namespace window
  110. } // namespace rack