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.

111 lines
3.0KB

  1. #pragma once
  2. #include <memory>
  3. #include <map>
  4. #define GLEW_STATIC
  5. #define GLEW_NO_GLU
  6. #include <GL/glew.h>
  7. #include <GLFW/glfw3.h>
  8. #include <nanovg.h>
  9. #define NANOVG_GL2
  10. #include <nanovg_gl.h>
  11. #include <nanovg_gl_utils.h>
  12. #include <nanosvg.h>
  13. #include <common.hpp>
  14. #include <math.hpp>
  15. namespace rack {
  16. // 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.
  17. struct Font {
  18. NVGcontext* vg;
  19. int handle = -1;
  20. /** Don't call this directly but instead use `APP->window->loadFont()` */
  21. void loadFile(const std::string& filename, NVGcontext* vg);
  22. ~Font();
  23. /** Use `APP->window->loadFont()` instead. */
  24. DEPRECATED static std::shared_ptr<Font> load(const std::string& filename);
  25. };
  26. struct Image {
  27. NVGcontext* vg;
  28. int handle = -1;
  29. /** Don't call this directly but instead use `APP->window->loadImage()` */
  30. void loadFile(const std::string& filename, NVGcontext* vg);
  31. ~Image();
  32. /** Use `APP->window->loadImage()` instead. */
  33. DEPRECATED static std::shared_ptr<Image> load(const std::string& filename);
  34. };
  35. struct Svg {
  36. NSVGimage* handle = NULL;
  37. /** Don't call this directly but instead use `APP->window->loadSvg()` */
  38. void loadFile(const std::string& filename);
  39. ~Svg();
  40. /** Use `APP->window->loadSvg()` instead. */
  41. DEPRECATED static std::shared_ptr<Svg> load(const std::string& filename);
  42. };
  43. DEPRECATED typedef Svg SVG;
  44. struct Window {
  45. struct Internal;
  46. Internal* internal;
  47. GLFWwindow* win = NULL;
  48. NVGcontext* vg = NULL;
  49. /** The scaling ratio */
  50. float pixelRatio = 1.f;
  51. /* The ratio between the framebuffer size and the window size reported by the OS.
  52. This is not equal to gPixelRatio in general.
  53. */
  54. float windowRatio = 1.f;
  55. std::shared_ptr<Font> uiFont;
  56. /** Use load*() instead of modifying these directly. */
  57. std::map<std::string, std::weak_ptr<Font>> fontCache;
  58. std::map<std::string, std::weak_ptr<Image>> imageCache;
  59. std::map<std::string, std::weak_ptr<Svg>> svgCache;
  60. Window();
  61. ~Window();
  62. void run();
  63. /** Takes a screenshot of the screen and saves it to a PNG file. */
  64. void screenshot(const std::string& screenshotPath);
  65. /** Saves a PNG image of all modules to `screenshotsDir/<plugin slug>/<module slug>.png`.
  66. Skips screenshot if the file already exists.
  67. */
  68. void screenshotModules(const std::string& screenshotsDir, float zoom = 1.f);
  69. void close();
  70. void cursorLock();
  71. void cursorUnlock();
  72. bool isCursorLocked();
  73. /** Gets the current keyboard mod state
  74. Don't call this from a Key event. Simply use `e.mods` instead.
  75. */
  76. int getMods();
  77. void setFullScreen(bool fullScreen);
  78. bool isFullScreen();
  79. double getMonitorRefreshRate();
  80. double getLastFrameTime();
  81. double getLastFrameDuration();
  82. double getFrameTimeOverdue();
  83. std::shared_ptr<Font> loadFont(const std::string& filename);
  84. std::shared_ptr<Image> loadImage(const std::string& filename);
  85. std::shared_ptr<Svg> loadSvg(const std::string& filename);
  86. };
  87. void windowInit();
  88. void windowDestroy();
  89. } // namespace rack