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
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. void step();
  64. /** Takes a screenshot of the screen and saves it to a PNG file. */
  65. void screenshot(const std::string& screenshotPath);
  66. /** Saves a PNG image of all modules to `screenshotsDir/<plugin slug>/<module slug>.png`.
  67. Skips screenshot if the file already exists.
  68. */
  69. void screenshotModules(const std::string& screenshotsDir, float zoom = 1.f);
  70. void close();
  71. void cursorLock();
  72. void cursorUnlock();
  73. bool isCursorLocked();
  74. /** Gets the current keyboard mod state
  75. Don't call this from a Key event. Simply use `e.mods` instead.
  76. */
  77. int getMods();
  78. void setFullScreen(bool fullScreen);
  79. bool isFullScreen();
  80. double getMonitorRefreshRate();
  81. double getLastFrameTime();
  82. double getLastFrameDuration();
  83. double getFrameTimeOverdue();
  84. std::shared_ptr<Font> loadFont(const std::string& filename);
  85. std::shared_ptr<Image> loadImage(const std::string& filename);
  86. std::shared_ptr<Svg> loadSvg(const std::string& filename);
  87. };
  88. void windowInit();
  89. void windowDestroy();
  90. } // namespace rack