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.

112 lines
2.9KB

  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. Window();
  55. ~Window();
  56. math::Vec getSize();
  57. void setSize(math::Vec size);
  58. void run();
  59. void step();
  60. /** Takes a screenshot of the screen and saves it to a PNG file. */
  61. 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. 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. double getMonitorRefreshRate();
  78. double getFrameTime();
  79. double getLastFrameDuration();
  80. double getFrameDurationRemaining();
  81. std::shared_ptr<Font> loadFont(const std::string& filename);
  82. std::shared_ptr<Image> loadImage(const std::string& filename);
  83. std::shared_ptr<Svg> loadSvg(const std::string& filename) {
  84. return Svg::load(filename);
  85. }
  86. INTERNAL bool& fbDirtyOnSubpixelChange();
  87. };
  88. void init();
  89. void destroy();
  90. } // namespace window
  91. } // namespace rack