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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /** Don't call this directly but instead use `APP->window->loadFont()` */
  25. void loadFile(const std::string& filename, NVGcontext* vg);
  26. ~Font();
  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. /** Don't call this directly but instead use `APP->window->loadImage()` */
  35. void loadFile(const std::string& filename, NVGcontext* vg);
  36. ~Image();
  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. /** The scaling ratio */
  48. float pixelRatio = 1.f;
  49. /* The ratio between the framebuffer size and the window size reported by the OS.
  50. This is not equal to gPixelRatio 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. double getMonitorRefreshRate();
  79. double getFrameTime();
  80. double getLastFrameDuration();
  81. double getFrameDurationRemaining();
  82. std::shared_ptr<Font> loadFont(const std::string& filename);
  83. std::shared_ptr<Image> loadImage(const std::string& filename);
  84. std::shared_ptr<Svg> loadSvg(const std::string& filename) {
  85. return Svg::load(filename);
  86. }
  87. PRIVATE bool& fbDirtyOnSubpixelChange();
  88. };
  89. PRIVATE void init();
  90. PRIVATE void destroy();
  91. } // namespace window
  92. } // namespace rack