Browse Source

Make Window::*Cache public. Move Font, Image, and Svg constructor to loadFile() methods.

tags/v1.0.0
Andrew Belt 6 years ago
parent
commit
257a357187
2 changed files with 34 additions and 26 deletions
  1. +12
    -6
      include/window.hpp
  2. +22
    -20
      src/window.cpp

+ 12
- 6
include/window.hpp View File

@@ -3,6 +3,7 @@
#include "math.hpp" #include "math.hpp"


#include <memory> #include <memory>
#include <map>
#define GLEW_STATIC #define GLEW_STATIC
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@@ -41,9 +42,9 @@ namespace rack {


struct Font { struct Font {
NVGcontext *vg; NVGcontext *vg;
int handle;
int handle = -1;
/** Don't call this directly but instead use `APP->window->loadFont()` */ /** Don't call this directly but instead use `APP->window->loadFont()` */
Font(NVGcontext *vg, const std::string &filename);
void loadFile(const std::string &filename, NVGcontext *vg);
~Font(); ~Font();
/** Use `APP->window->loadFont()` instead. */ /** Use `APP->window->loadFont()` instead. */
DEPRECATED static std::shared_ptr<Font> load(const std::string &filename); DEPRECATED static std::shared_ptr<Font> load(const std::string &filename);
@@ -51,18 +52,18 @@ struct Font {


struct Image { struct Image {
NVGcontext *vg; NVGcontext *vg;
int handle;
int handle = -1;
/** Don't call this directly but instead use `APP->window->loadImage()` */ /** Don't call this directly but instead use `APP->window->loadImage()` */
Image(NVGcontext *vg, const std::string &filename);
void loadFile(const std::string &filename, NVGcontext *vg);
~Image(); ~Image();
/** Use `APP->window->loadImage()` instead. */ /** Use `APP->window->loadImage()` instead. */
DEPRECATED static std::shared_ptr<Image> load(const std::string &filename); DEPRECATED static std::shared_ptr<Image> load(const std::string &filename);
}; };


struct Svg { struct Svg {
NSVGimage *handle;
NSVGimage *handle = NULL;
/** Don't call this directly but instead use `APP->window->loadSvg()` */ /** Don't call this directly but instead use `APP->window->loadSvg()` */
Svg(const std::string &filename);
void loadFile(const std::string &filename);
~Svg(); ~Svg();
/** Use `APP->window->loadSvg()` instead. */ /** Use `APP->window->loadSvg()` instead. */
DEPRECATED static std::shared_ptr<Svg> load(const std::string &filename); DEPRECATED static std::shared_ptr<Svg> load(const std::string &filename);
@@ -86,6 +87,11 @@ struct Window {
std::shared_ptr<Font> uiFont; std::shared_ptr<Font> uiFont;
double frameTimeStart = 0.f; double frameTimeStart = 0.f;


/** Use load*() instead of modifying these directly. */
std::map<std::string, std::weak_ptr<Font>> fontCache;
std::map<std::string, std::weak_ptr<Image>> imageCache;
std::map<std::string, std::weak_ptr<Svg>> svgCache;

struct Internal; struct Internal;
Internal *internal; Internal *internal;




+ 22
- 20
src/window.cpp View File

@@ -23,7 +23,7 @@
namespace rack { namespace rack {




Font::Font(NVGcontext *vg, const std::string &filename) {
void Font::loadFile(const std::string &filename, NVGcontext *vg) {
handle = nvgCreateFont(vg, filename.c_str(), filename.c_str()); handle = nvgCreateFont(vg, filename.c_str(), filename.c_str());
if (handle >= 0) { if (handle >= 0) {
INFO("Loaded font %s", filename.c_str()); INFO("Loaded font %s", filename.c_str());
@@ -41,7 +41,7 @@ std::shared_ptr<Font> Font::load(const std::string &filename) {
return APP->window->loadFont(filename); return APP->window->loadFont(filename);
} }


Image::Image(NVGcontext *vg, const std::string &filename) {
void Image::loadFile(const std::string &filename, NVGcontext *vg) {
handle = nvgCreateImage(vg, filename.c_str(), NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY); handle = nvgCreateImage(vg, filename.c_str(), NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
if (handle > 0) { if (handle > 0) {
INFO("Loaded image %s", filename.c_str()); INFO("Loaded image %s", filename.c_str());
@@ -53,14 +53,15 @@ Image::Image(NVGcontext *vg, const std::string &filename) {


Image::~Image() { Image::~Image() {
// TODO What if handle is invalid? // TODO What if handle is invalid?
nvgDeleteImage(vg, handle);
if (handle >= 0)
nvgDeleteImage(vg, handle);
} }


std::shared_ptr<Image> Image::load(const std::string &filename) { std::shared_ptr<Image> Image::load(const std::string &filename) {
return APP->window->loadImage(filename); return APP->window->loadImage(filename);
} }


Svg::Svg(const std::string &filename) {
void Svg::loadFile(const std::string &filename) {
handle = nsvgParseFromFile(filename.c_str(), "px", app::SVG_DPI); handle = nsvgParseFromFile(filename.c_str(), "px", app::SVG_DPI);
if (handle) { if (handle) {
INFO("Loaded SVG %s", filename.c_str()); INFO("Loaded SVG %s", filename.c_str());
@@ -71,7 +72,8 @@ Svg::Svg(const std::string &filename) {
} }


Svg::~Svg() { Svg::~Svg() {
nsvgDelete(handle);
if (handle)
nsvgDelete(handle);
} }


std::shared_ptr<Svg> Svg::load(const std::string &filename) { std::shared_ptr<Svg> Svg::load(const std::string &filename) {
@@ -86,10 +88,6 @@ struct Window::Internal {
int lastWindowY = 0; int lastWindowY = 0;
int lastWindowWidth = 0; int lastWindowWidth = 0;
int lastWindowHeight = 0; int lastWindowHeight = 0;

std::map<std::string, std::weak_ptr<Font>> fontCache;
std::map<std::string, std::weak_ptr<Image>> imageCache;
std::map<std::string, std::weak_ptr<Svg>> svgCache;
}; };




@@ -457,29 +455,33 @@ bool Window::isFrameOverdue() {
} }


std::shared_ptr<Font> Window::loadFont(const std::string &filename) { std::shared_ptr<Font> Window::loadFont(const std::string &filename) {
auto sp = internal->fontCache[filename].lock();
if (!sp)
internal->fontCache[filename] = sp = std::make_shared<Font>(vg, filename);
auto sp = fontCache[filename].lock();
if (!sp) {
fontCache[filename] = sp = std::make_shared<Font>();
sp->loadFile(filename, vg);
}
return sp; return sp;
} }


std::shared_ptr<Image> Window::loadImage(const std::string &filename) { std::shared_ptr<Image> Window::loadImage(const std::string &filename) {
auto sp = internal->imageCache[filename].lock();
if (!sp)
internal->imageCache[filename] = sp = std::make_shared<Image>(vg, filename);
auto sp = imageCache[filename].lock();
if (!sp) {
imageCache[filename] = sp = std::make_shared<Image>();
sp->loadFile(filename, vg);
}
return sp; return sp;
} }


std::shared_ptr<Svg> Window::loadSvg(const std::string &filename) { std::shared_ptr<Svg> Window::loadSvg(const std::string &filename) {
auto sp = internal->svgCache[filename].lock();
if (!sp)
internal->svgCache[filename] = sp = std::make_shared<Svg>(filename);
auto sp = svgCache[filename].lock();
if (!sp) {
svgCache[filename] = sp = std::make_shared<Svg>();
sp->loadFile(filename);
}
return sp; return sp;
} }






void windowInit() { void windowInit() {
int err; int err;




Loading…
Cancel
Save