1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz
//============================================================
//
// effectmanager.cpp - BGFX shader effect manager
//
// Maintains a string-to-entry lookup of BGFX shader
// effects, defined by effect.h and read by effectreader.h
//
//============================================================
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <bx/readerwriter.h>
#include <bx/crtimpl.h>
#include "emu.h"
#include <bgfx/bgfx.h>
#include "effectmanager.h"
#include "effectreader.h"
#include "effect.h"
using namespace rapidjson;
effect_manager::~effect_manager()
{
for (std::pair<std::string, bgfx_effect*> effect : m_effects)
{
delete effect.second;
}
m_effects.clear();
}
bgfx_effect* effect_manager::effect(std::string name)
{
std::map<std::string, bgfx_effect*>::iterator iter = m_effects.find(name);
if (iter != m_effects.end())
{
return iter->second;
}
return load_effect(name);
}
bgfx_effect* effect_manager::load_effect(std::string name)
{
std::string full_name = name;
if (full_name.length() < 5 || (full_name.compare(full_name.length() - 5, 5, ".json") != 0)) {
full_name = full_name + ".json";
}
std::string path;
osd_subst_env(path, util::string_format("%s" PATH_SEPARATOR "effects" PATH_SEPARATOR, m_options.bgfx_path()));
path += full_name;
bx::CrtFileReader reader;
if (!bx::open(&reader, path.c_str()))
{
printf("Unable to open effect file %s\n", path.c_str());
return nullptr;
}
int32_t size (bx::getSize(&reader));
char* data = new char[size + 1];
bx::read(&reader, reinterpret_cast<void*>(data), size);
bx::close(&reader);
data[size] = 0;
Document document;
document.Parse<kParseCommentsFlag>(data);
delete [] data;
if (document.HasParseError()) {
std::string error(GetParseError_En(document.GetParseError()));
printf("Unable to parse effect %s. Errors returned:\n", path.c_str());
printf("%s\n", error.c_str());
return nullptr;
}
bgfx_effect* effect = effect_reader::read_from_value(document, "Effect '" + name + "': ", m_shaders);
if (effect == nullptr) {
printf("Unable to load effect %s\n", path.c_str());
return nullptr;
}
m_effects[name] = effect;
return effect;
}
|