Skip to content

Commit

Permalink
TexturesManager is done.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruofeidu committed Dec 17, 2017
1 parent c7d5172 commit a64dbcc
Show file tree
Hide file tree
Showing 21 changed files with 343 additions and 353 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*.log
*.mp4
*.avi

DuEngine/*.ini
DuEngine/*.glsl
DuEngine/*.cmd
SHSaliency/
Friends/

Expand Down
16 changes: 11 additions & 5 deletions DuEngine/DuConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ using namespace std;

std::string DuConfig::DefaultName = "config.ini";

DuConfig::DuConfig() {
m_bErrorIfNameNotFound = true;
Load(DefaultName);
DuConfig::DuConfig() : DuConfig(DefaultName) {
}

DuConfig::DuConfig(const string &filename) {
DuConfig::DuConfig(string filename) {
m_bErrorIfNameNotFound = true;
Load(filename);
m_name = filename.substr(0, filename.size() - 4);
if (!m_name.compare("config")) {
m_name = "default";
}
}

static string Trim(string str) {
Expand All @@ -23,7 +25,7 @@ static string Trim(string str) {
return str.substr(first, last - first + 1);
}

bool DuConfig::Load(const string &filename) {
bool DuConfig::Load(string filename) {
m_mEntries.clear();
// Open file.
ifstream in(filename.c_str());
Expand Down Expand Up @@ -100,6 +102,10 @@ double DuConfig::GetDouble(const string &name) const {
return atof(GetString(name).c_str());
}

std::string DuConfig::GetName() const {
return m_name;
}

bool DuConfig::HasKey(const string& name) const {
return (m_mEntries.find(name) != m_mEntries.end());
}
Expand Down
6 changes: 4 additions & 2 deletions DuEngine/DuConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ class DuConfig
{
public:
static std::string DefaultName;
std::string m_name;

public:
DuConfig();
DuConfig(const std::string &filename);
bool Load(const std::string &filename);
DuConfig(std::string filename);
bool Load(std::string filename);

void SetErrorIfNameNotFound(bool error) { m_bErrorIfNameNotFound = error; }

Expand All @@ -23,6 +24,7 @@ class DuConfig
int GetInt(const std::string &name) const;
float GetFloat(const std::string &name) const;
double GetDouble(const std::string &name) const;
std::string GetName() const;

public:
std::vector<std::string> GetKeyList() const;
Expand Down
95 changes: 24 additions & 71 deletions DuEngine/DuEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ DuEngine::GC DuEngine::gc;
DuEngine::DuEngine() {
camera = new Camera();
m_window = Window::GetInstance();
m_textureManager = new TexturesManager();
}

DuEngine* DuEngine::GetInstance() {
Expand Down Expand Up @@ -86,51 +87,22 @@ void g_reshape(int width, int height) {

void DuEngine::start(int argc, char* argv[]) {
// setup configuration files and the scene name
if (argc > 1) {
m_configName = std::string(argv[1]);
config = new DuConfig(m_configName);
m_sceneName = m_configName.substr(0, m_configName.size() - 4);
} else {
config = new DuConfig(DuConfig::DefaultName);
m_sceneName = "default";
}

// setup shaders path and presets path
m_shadersPath = std::string(argv[0]);

// automatically search the default shader path from the upper-level folders
for (int i = 3; i >= 0; --i) {
if (i == 0) {
m_shadersPath = "";
} else {
auto keywords = repeatstring("../", i) + "DuEngine/";
if (m_shadersPath.find(keywords) != std::string::npos) {
m_shadersPath = keywords;
break;
}
}
}
m_shadersPath = config->GetStringWithDefault("shaders_path", m_shadersPath);
#if VERBOSE_OUTPUT
if (m_shadersPath.size() > 0) {
info("Relative Path: " + m_shadersPath);
}
#endif
m_presetsPath = config->GetStringWithDefault("presets_path", m_shadersPath + "presets/");
m_resourcesPath = config->GetStringWithDefault("resources_path", m_presetsPath);

m_config = argc > 1 ? new DuConfig(std::string(argv[1])) : new DuConfig();
m_path = new PathManager(std::string(argv[0]), m_config);

// setup the default m_window width and height
m_defaultWidth = config->GetIntWithDefault("window_width", m_defaultWidth);
m_defaultHeight = config->GetIntWithDefault("window_height", m_defaultHeight);
string _windowTitle = config->GetStringWithDefault("window_title", "DuRenderer | " + m_sceneName);
m_defaultWidth = m_config->GetIntWithDefault("window_width", m_defaultWidth);
m_defaultHeight = m_config->GetIntWithDefault("window_height", m_defaultHeight);
string _windowTitle = m_config->GetStringWithDefault("window_title", "DuRenderer | " + m_config->GetName());
m_window->init(argc, argv, m_defaultWidth, m_defaultHeight, _windowTitle);

// setup recording
m_recording = config->GetBoolWithDefault("recording", m_recording);
m_recordPath = config->GetStringWithDefault("record_path", m_sceneName);
m_recordStart = config->GetIntWithDefault("record_start", m_recordStart);
m_recordEnd = config->GetIntWithDefault("record_end", m_recordEnd);
m_recordVideo = config->GetBoolWithDefault("record_video", m_recordVideo);
m_recording = m_config->GetBoolWithDefault("recording", m_recording);
m_recordPath = m_config->GetStringWithDefault("record_path", m_config->GetName());
m_recordStart = m_config->GetIntWithDefault("record_start", m_recordStart);
m_recordEnd = m_config->GetIntWithDefault("record_end", m_recordEnd);
m_recordVideo = m_config->GetBoolWithDefault("record_video", m_recordVideo);


// initialize the scene, shaders, and presets
initScene();
Expand All @@ -157,10 +129,7 @@ void DuEngine::keyboard(unsigned char key, int x, int y, bool up) {
break;
}
}

if (keyboardTexture) {
keyboardTexture->onKeyPress(key, up);
}
m_textureManager->updateKeyboard(key, up);
}

void DuEngine::special(int key, int x, int y, bool up) {
Expand All @@ -174,17 +143,19 @@ void DuEngine::special(int key, int x, int y, bool up) {

case GLUT_KEY_F2:
// Take screenshot
this->takeScreenshot();
m_takeSingleScreenShot = true;
break;

case GLUT_KEY_F5:
m_shadertoy->recompile();
break;
case GLUT_KEY_F6:
// Video Pause
for (const auto& t : videoTextures) {
t->togglePaused();
}
m_textureManager->togglePause();
case GLUT_KEY_F9:
// Debug iFrame
debug(to_string(getFrameNumber()));
break;

case GLUT_KEY_F10:
// Debug Mouse
debug(ShaderToyUniforms::GetMouseString());
Expand All @@ -195,14 +166,7 @@ void DuEngine::special(int key, int x, int y, bool up) {
break;
}
}
if (keyboardTexture) {
keyboardTexture->onKeyPress(key, up);
// hack the four arrows for ShaderToy
if (100 <= key && key <= 103) {
key -= 63;
}
keyboardTexture->onKeyPress(key, up);
}
m_textureManager->updateKeyboard(key, up);
}

void DuEngine::mousePress(int button, int state, int x, int y) {
Expand Down Expand Up @@ -236,23 +200,12 @@ void DuEngine::toggleFullScreen() {
}
}

int DuEngine::getNumFrameFromVideos() {
int ans = 0;
for (const auto &v : videoTextures) {
ans = std::max(ans, v->getNumVideoFrame());
}
return ans;
}

void DuEngine::printHelp() {
info("Help:\n\tF1:\tReset everything.\n\tF2:\tTake Screenshot.\n\tF5:\tReset Time\n\tF6:\tPause\n\tF11:\tFullscreen.\n");
}

void DuEngine::takeScreenshot(string folderName) {
if (m_isPathCreated.find(folderName) == m_isPathCreated.end()) {
CreateDirectory(folderName.c_str(), NULL);
m_isPathCreated.insert(folderName);
}
m_path->createPathIfNotExisted(folderName);

if (m_recordVideo) {
if (m_video == nullptr) {
Expand All @@ -275,7 +228,7 @@ void DuEngine::takeScreenshot(string folderName) {
m_video->release();
}
} else {
cv::imwrite(folderName + "/" + this->m_configName.substr(0, m_configName.size() - 4) + "_" + to_string(getFrameNumber()) + ".png", img);
cv::imwrite(folderName + "/" + m_config->GetName() + "_" + to_string(getFrameNumber()) + ".png", img);
}
}

25 changes: 5 additions & 20 deletions DuEngine/DuEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ShaderToy.h"
#include "DuConfig.h"
#include "TexturesManager.h"
#include "PathManager.h"
#include "Camera.h"
#include "Window.h"
#include "DuUtils.h"
Expand All @@ -19,10 +20,6 @@ void g_reshape(int width, int height);
class DuEngine
{
friend class ShaderToy;
friend class Texture;
friend class TextureVideo;
friend class TextureKeyboard;
friend class TextureSH;

public:
static DuEngine *GetInstance();
Expand All @@ -31,11 +28,7 @@ class DuEngine

protected:
TexturesManager* m_textureManager;

vector<TextureVideo*> videoTextures;
TextureKeyboard* keyboardTexture;
Texture* fontTexture;
vector<Texture*> textures;
PathManager* m_path;

public:
void render();
Expand All @@ -48,7 +41,7 @@ class DuEngine
void reshape(int width, int height);

int getFrameNumber();
string getPresetsPath() { return m_presetsPath; }
string getPresetsPath() { return m_path->getPresetPath(); }

private:
DuEngine();
Expand All @@ -57,31 +50,23 @@ class DuEngine
Camera* camera;
Window* m_window;
ShaderToy* m_shadertoy;
DuConfig* config;
string m_configName;
string m_sceneName;
DuConfig* m_config;
bool m_fullscreen = false;
bool m_recording = false;
bool m_paused = false;

string m_shadersPath = "";
string m_presetsPath = "";
string m_resourcesPath = "";
unordered_set<string> m_isPathCreated;

string m_recordPath = "";
int m_recordStart = 0;
int m_recordEnd = 100;
bool m_recordVideo = false;
cv::VideoWriter* m_video = nullptr;
int m_defaultWidth = 1280;
int m_defaultHeight = 720;
bool m_takeSingleScreenShot = false;

private:
void toggleFullScreen();

int getNumFrameFromVideos();

void printHelp();

class GC
Expand Down
2 changes: 2 additions & 0 deletions DuEngine/DuEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<ClCompile Include="FrameBuffer.cpp" />
<ClCompile Include="Geometry.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="PathManager.cpp" />
<ClCompile Include="ShaderProgram.cpp" />
<ClCompile Include="ShaderToy.cpp" />
<ClCompile Include="ShaderToyFrameBuffer.cpp" />
Expand Down Expand Up @@ -170,6 +171,7 @@
<ClInclude Include="FrameBuffer.h" />
<ClInclude Include="Geometry.h" />
<ClInclude Include="HashVec2b.h" />
<ClInclude Include="PathManager.h" />
<ClInclude Include="ShaderToyFrameBuffer.h" />
<ClInclude Include="ShaderToyScreenBuffer.h" />
<ClInclude Include="ShaderProgram.h" />
Expand Down
6 changes: 6 additions & 0 deletions DuEngine/DuEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@
<ClCompile Include="ShaderProgram.cpp">
<Filter>Source Files\Uniforms</Filter>
</ClCompile>
<ClCompile Include="PathManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Texture.h">
Expand Down Expand Up @@ -263,6 +266,9 @@
<ClInclude Include="ShaderToyFrameBuffer.h">
<Filter>Header Files\FrameBuffer</Filter>
</ClInclude>
<ClInclude Include="PathManager.h">
<Filter>Header Files\Utils</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="Backup.txt" />
Expand Down
Loading

0 comments on commit a64dbcc

Please sign in to comment.