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.

Window.hpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. */
  92. std::shared_ptr<Font> loadFont(const std::string& filename);
  93. /** Loads and caches an Image from a file path.
  94. Do not store this reference across screen frames, as the Window may have changed, invalidating the Image.
  95. */
  96. std::shared_ptr<Image> loadImage(const std::string& filename);
  97. /** Loads and caches an Svg from a file path.
  98. Alias for `Svg::load()`.
  99. */
  100. std::shared_ptr<Svg> loadSvg(const std::string& filename) {
  101. return Svg::load(filename);
  102. }
  103. PRIVATE bool& fbDirtyOnSubpixelChange();
  104. PRIVATE int& fbCount();
  105. };
  106. PRIVATE void init();
  107. PRIVATE void destroy();
  108. } // namespace window
  109. } // namespace rack