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.

135 lines
4.0KB

  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. /** Takes a screenshot of the screen and saves it to a PNG file. */
  61. PRIVATE void screenshot(const std::string& screenshotPath);
  62. /** Saves a PNG image of all modules to `screenshotsDir/<plugin slug>/<module slug>.png`.
  63. Skips screenshot if the file already exists.
  64. */
  65. PRIVATE void screenshotModules(const std::string& screenshotsDir, float zoom = 1.f);
  66. /** Request Window to be closed after rendering the current frame. */
  67. void close();
  68. void cursorLock();
  69. void cursorUnlock();
  70. bool isCursorLocked();
  71. /** Gets the current keyboard mod state
  72. Don't call this from a Key event. Simply use `e.mods` instead.
  73. */
  74. int getMods();
  75. void setFullScreen(bool fullScreen);
  76. bool isFullScreen();
  77. /** Returns the primary monitor's refresh rate in Hz. */
  78. double getMonitorRefreshRate();
  79. /** Returns the timestamp of the beginning of the current frame render process.
  80. Returns NAN if no frames have begun rendering.
  81. */
  82. double getFrameTime();
  83. /** Returns the total time in seconds spent rendering the last frame.
  84. Returns NAN if no frames have ended rendering.
  85. */
  86. double getLastFrameDuration();
  87. /** Returns the current time remaining in seconds until the frame deadline, according to the frame rate limit. */
  88. double getFrameDurationRemaining();
  89. /** Loads and caches a Font from a file path.
  90. Do not store this reference across screen frames, as the Window may have changed, invalidating the Font.
  91. Automatically adds fallback fonts such as Japanese, Chinese, and emoji.
  92. */
  93. std::shared_ptr<Font> loadFont(const std::string& filename);
  94. /** Loads and caches a Font without adding fallback fonts. */
  95. std::shared_ptr<Font> loadFontWithoutFallbacks(const std::string& filename);
  96. /** Loads and caches an Image from a file path.
  97. Do not store this reference across screen frames, as the Window may have changed, invalidating the Image.
  98. */
  99. std::shared_ptr<Image> loadImage(const std::string& filename);
  100. /** Loads and caches an Svg from a file path.
  101. Alias for `Svg::load()`.
  102. */
  103. std::shared_ptr<Svg> loadSvg(const std::string& filename) {
  104. return Svg::load(filename);
  105. }
  106. PRIVATE bool& fbDirtyOnSubpixelChange();
  107. PRIVATE int& fbCount();
  108. };
  109. PRIVATE void init();
  110. PRIVATE void destroy();
  111. } // namespace window
  112. } // namespace rack