chore: some readability improvements on code

This commit is contained in:
Almamu 2025-04-11 01:40:13 +02:00
parent 33340f3cbc
commit ae66deffb6
7 changed files with 174 additions and 120 deletions

View File

@ -290,7 +290,7 @@ void CWallpaperApplication::takeScreenshot (const std::filesystem::path& filenam
delete [] bitmap; delete [] bitmap;
} }
void CWallpaperApplication::show () { void CWallpaperApplication::setupOutput () {
const char* XDG_SESSION_TYPE = getenv ("XDG_SESSION_TYPE"); const char* XDG_SESSION_TYPE = getenv ("XDG_SESSION_TYPE");
if (!XDG_SESSION_TYPE) { if (!XDG_SESSION_TYPE) {
@ -381,7 +381,11 @@ void CWallpaperApplication::show () {
} else { } else {
this->m_audioRecorder = new WallpaperEngine::Audio::Drivers::Recorders::CPlaybackRecorder (); this->m_audioRecorder = new WallpaperEngine::Audio::Drivers::Recorders::CPlaybackRecorder ();
} }
// initialize render context
m_renderContext = new WallpaperEngine::Render::CRenderContext (*m_videoDriver, *m_inputContext, *this);
}
void CWallpaperApplication::setupAudio () {
// audio playing detector // audio playing detector
WallpaperEngine::Audio::Drivers::Detectors::CPulseAudioPlayingDetector audioDetector (this->m_context, WallpaperEngine::Audio::Drivers::Detectors::CPulseAudioPlayingDetector audioDetector (this->m_context,
*this->m_fullScreenDetector); *this->m_fullScreenDetector);
@ -390,9 +394,9 @@ void CWallpaperApplication::show () {
new WallpaperEngine::Audio::Drivers::CSDLAudioDriver (this->m_context, audioDetector, *this->m_audioRecorder); new WallpaperEngine::Audio::Drivers::CSDLAudioDriver (this->m_context, audioDetector, *this->m_audioRecorder);
// initialize audio context // initialize audio context
m_audioContext = new WallpaperEngine::Audio::CAudioContext (*m_audioDriver); m_audioContext = new WallpaperEngine::Audio::CAudioContext (*m_audioDriver);
// initialize render context }
m_renderContext = new WallpaperEngine::Render::CRenderContext (*m_videoDriver, *m_inputContext, *this);
void CWallpaperApplication::prepareOutputs () {
// create a new background for each screen // create a new background for each screen
// set all the specific wallpapers required // set all the specific wallpapers required
@ -402,6 +406,12 @@ void CWallpaperApplication::show () {
info->getWallpaper (), *m_renderContext, *m_audioContext, *m_browserContext, info->getWallpaper (), *m_renderContext, *m_audioContext, *m_browserContext,
this->m_context.settings.general.screenScalings [background])); this->m_context.settings.general.screenScalings [background]));
} }
}
void CWallpaperApplication::show () {
this->setupOutput ();
this->setupAudio ();
this->prepareOutputs ();
static time_t seconds; static time_t seconds;
static struct tm* timeinfo; static struct tm* timeinfo;

View File

@ -105,6 +105,18 @@ class CWallpaperApplication {
* Prepares CEF browser to be used * Prepares CEF browser to be used
*/ */
void setupBrowser (); void setupBrowser ();
/**
* Prepares desktop environment-related things (like render, window, fullscreen detector, etc)
*/
void setupOutput ();
/**
* Prepares all audio-related things (like detector, output, etc)
*/
void setupAudio ();
/**
* Prepares the render-context of all the backgrounds so they can be displayed on the screen
*/
void prepareOutputs ();
/** /**
* Takes an screenshot of the background and saves it to the specified path * Takes an screenshot of the background and saves it to the specified path
* *

View File

@ -287,7 +287,6 @@ void CAudioStream::dequeuePacket (AVPacket* output) {
SDL_LockMutex (this->m_queue->mutex); SDL_LockMutex (this->m_queue->mutex);
while (this->m_audioContext.getApplicationContext ().state.general.keepRunning) { while (this->m_audioContext.getApplicationContext ().state.general.keepRunning) {
#if FF_API_FIFO_OLD_API #if FF_API_FIFO_OLD_API
int ret = -1; int ret = -1;

View File

@ -60,7 +60,7 @@ const ITexture* CPass::resolveTexture (const ITexture* expected, int index, cons
return fbo; return fbo;
} }
void CPass::render () { void CPass::setupRenderFramebuffer () {
// set the framebuffer we're drawing to // set the framebuffer we're drawing to
glBindFramebuffer (GL_FRAMEBUFFER, this->m_drawTo->getFramebuffer ()); glBindFramebuffer (GL_FRAMEBUFFER, this->m_drawTo->getFramebuffer ());
@ -99,7 +99,9 @@ void CPass::render () {
} else { } else {
glDepthMask (true); glDepthMask (true);
} }
}
void CPass::setupRenderTexture () {
// use the shader we have registered // use the shader we have registered
glUseProgram (this->m_programID); glUseProgram (this->m_programID);
@ -148,6 +150,44 @@ void CPass::render () {
} }
} }
// used in animations when one of the frames is vertical instead of horizontal
// rotation with translation = origin and end of the image to display
if (this->g_Texture0Rotation != -1)
glUniform4f (this->g_Texture0Rotation, rotation.x, rotation.y, rotation.z, rotation.w);
// this actually picks the origin point of the image from the atlast
if (this->g_Texture0Translation != -1)
glUniform2f (this->g_Texture0Translation, translation.x, translation.y);
}
void CPass::setupRenderReferenceUniforms () {
// add reference uniforms
for (const auto& [name, value] : this->m_referenceUniforms) {
switch (value->type) {
case Double: glUniform1d (value->id, *static_cast<const double*> (*value->value)); break;
case Float: glUniform1f (value->id, *static_cast<const float*> (*value->value)); break;
case Integer: glUniform1i (value->id, *static_cast<const int*> (*value->value)); break;
case Vector4:
glUniform4fv (value->id, 1, glm::value_ptr (*static_cast<const glm::vec4*> (*value->value)));
break;
case Vector3:
glUniform3fv (value->id, 1, glm::value_ptr (*static_cast<const glm::vec3*> (*value->value)));
break;
case Vector2:
glUniform2fv (value->id, 1, glm::value_ptr (*static_cast<const glm::vec2*> (*value->value)));
break;
case Matrix4:
glUniformMatrix4fv (value->id, 1, GL_FALSE,
glm::value_ptr (*static_cast<const glm::mat4*> (*value->value)));
break;
case Matrix3:
glUniformMatrix3fv (value->id, 1, GL_FALSE,
glm::value_ptr (*static_cast<const glm::mat3*> (*value->value)));
break;
}
}
}
void CPass::setupRenderUniforms () {
// add uniforms // add uniforms
for (const auto& [name, value] : this->m_uniforms) { for (const auto& [name, value] : this->m_uniforms) {
switch (value->type) { switch (value->type) {
@ -174,40 +214,9 @@ void CPass::render () {
break; break;
} }
} }
// add reference uniforms }
for (const auto& [name, value] : this->m_referenceUniforms) {
switch (value->type) {
case Double: glUniform1d (value->id, *static_cast<const double*> (*value->value)); break;
case Float: glUniform1f (value->id, *static_cast<const float*> (*value->value)); break;
case Integer: glUniform1i (value->id, *static_cast<const int*> (*value->value)); break;
case Vector4:
glUniform4fv (value->id, 1, glm::value_ptr (*static_cast<const glm::vec4*> (*value->value)));
break;
case Vector3:
glUniform3fv (value->id, 1, glm::value_ptr (*static_cast<const glm::vec3*> (*value->value)));
break;
case Vector2:
glUniform2fv (value->id, 1, glm::value_ptr (*static_cast<const glm::vec2*> (*value->value)));
break;
case Matrix4:
glUniformMatrix4fv (value->id, 1, GL_FALSE,
glm::value_ptr (*static_cast<const glm::mat4*> (*value->value)));
break;
case Matrix3:
glUniformMatrix3fv (value->id, 1, GL_FALSE,
glm::value_ptr (*static_cast<const glm::mat3*> (*value->value)));
break;
}
}
// used in animations when one of the frames is vertical instead of horizontal
// rotation with translation = origin and end of the image to display
if (this->g_Texture0Rotation != -1)
glUniform4f (this->g_Texture0Rotation, rotation.x, rotation.y, rotation.z, rotation.w);
// this actually picks the origin point of the image from the atlast
if (this->g_Texture0Translation != -1)
glUniform2f (this->g_Texture0Translation, translation.x, translation.y);
void CPass::setupRenderAttributes () {
for (const auto& cur : this->m_attribs) { for (const auto& cur : this->m_attribs) {
glEnableVertexAttribArray (cur->id); glEnableVertexAttribArray (cur->id);
glBindBuffer (GL_ARRAY_BUFFER, *cur->value); glBindBuffer (GL_ARRAY_BUFFER, *cur->value);
@ -220,11 +229,15 @@ void CPass::render () {
.c_str ()); .c_str ());
#endif /* DEBUG */ #endif /* DEBUG */
} }
}
void CPass::renderGeometry () const {
// start actual rendering now // start actual rendering now
glBindBuffer (GL_ARRAY_BUFFER, this->a_Position); glBindBuffer (GL_ARRAY_BUFFER, this->a_Position);
glDrawArrays (GL_TRIANGLES, 0, 6); glDrawArrays (GL_TRIANGLES, 0, 6);
}
void CPass::cleanupRenderSetup () {
// disable vertex attribs array and textures // disable vertex attribs array and textures
for (const auto& cur : this->m_attribs) for (const auto& cur : this->m_attribs)
glDisableVertexAttribArray (cur->id); glDisableVertexAttribArray (cur->id);
@ -242,6 +255,16 @@ void CPass::render () {
} }
} }
void CPass::render () {
this->setupRenderFramebuffer ();
this->setupRenderTexture ();
this->setupRenderUniforms ();
this->setupRenderReferenceUniforms ();
this->setupRenderAttributes ();
this->renderGeometry ();
this->cleanupRenderSetup ();
}
const CMaterial* CPass::getMaterial () const { const CMaterial* CPass::getMaterial () const {
return this->m_material; return this->m_material;
} }
@ -405,7 +428,7 @@ void CPass::setupAttributes () {
this->addAttribute ("a_Position", GL_FLOAT, 3, &this->a_Position); this->addAttribute ("a_Position", GL_FLOAT, 3, &this->a_Position);
} }
void CPass::setupUniforms () { void CPass::setupTextureUniforms () {
// resolve the main texture // resolve the main texture
const ITexture* texture = this->resolveTexture (this->m_material->getImage ()->getTexture (), 0); const ITexture* texture = this->resolveTexture (this->m_material->getImage ()->getTexture (), 0);
// register all the texture uniforms with correct values // register all the texture uniforms with correct values
@ -420,96 +443,98 @@ void CPass::setupUniforms () {
this->addUniform ("g_Texture0Resolution", texture->getResolution ()); this->addUniform ("g_Texture0Resolution", texture->getResolution ());
// do the real, final texture setup for the whole process // do the real, final texture setup for the whole process
{ auto cur = this->m_textures.begin ();
auto cur = this->m_textures.begin (); const auto end = this->m_textures.end ();
const auto end = this->m_textures.end (); auto fragCur = this->m_fragShader->getTextures ().begin ();
auto fragCur = this->m_fragShader->getTextures ().begin (); const auto fragEnd = this->m_fragShader->getTextures ().end ();
const auto fragEnd = this->m_fragShader->getTextures ().end (); auto vertCur = this->m_vertShader->getTextures ().begin ();
auto vertCur = this->m_vertShader->getTextures ().begin (); const auto vertEnd = this->m_vertShader->getTextures ().end ();
const auto vertEnd = this->m_vertShader->getTextures ().end (); auto bindCur = this->m_material->getMaterial ()->getTextureBinds ().begin ();
auto bindCur = this->m_material->getMaterial ()->getTextureBinds ().begin (); const auto bindEnd = this->m_material->getMaterial ()->getTextureBinds ().end ();
const auto bindEnd = this->m_material->getMaterial ()->getTextureBinds ().end ();
int index = 1; int index = 1;
// technically m_textures should have the right amount of textures // technically m_textures should have the right amount of textures
// but better be safe than sorry // but better be safe than sorry
while (bindCur != bindEnd || cur != end || fragCur != fragEnd || vertCur != vertEnd) { while (bindCur != bindEnd || cur != end || fragCur != fragEnd || vertCur != vertEnd) {
if (bindCur != bindEnd) { if (bindCur != bindEnd) {
this->m_finalTextures [bindCur->first] = nullptr; this->m_finalTextures [bindCur->first] = nullptr;
++bindCur; ++bindCur;
}
if (cur != end) {
if ((*cur) != nullptr)
this->m_finalTextures [index] = *cur;
index++;
++cur;
}
if (fragCur != fragEnd) {
std::string textureName = fragCur->second;
try {
// resolve the texture first
const ITexture* textureRef;
if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) {
textureRef = this->getMaterial ()->getEffect ()->findFBO (textureName);
if (textureRef == nullptr)
textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName);
} else
textureRef = this->getContext ().resolveTexture (textureName);
// ensure there's no texture in that slot already, shader textures are defaults in case nothing is
// there
if (this->m_finalTextures.find (fragCur->first) == this->m_finalTextures.end ())
this->m_finalTextures [fragCur->first] = textureRef;
} catch (std::runtime_error& ex) {
sLog.error ("Cannot resolve texture ", textureName, " for fragment shader ", ex.what ());
} }
if (cur != end) { ++fragCur;
if ((*cur) != nullptr) }
this->m_finalTextures [index] = *cur;
index++; if (vertCur != vertEnd) {
++cur; std::string textureName = vertCur->second;
try {
// resolve the texture first
const ITexture* textureRef;
if (textureName.find ("_rt_") == 0) {
textureRef = this->getMaterial ()->getEffect ()->findFBO (textureName);
if (textureRef == nullptr)
textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName);
} else
textureRef = this->getContext ().resolveTexture (textureName);
// ensure there's no texture in that slot already, shader textures are defaults in case nothing is
// there
if (this->m_finalTextures.find (vertCur->first) == this->m_finalTextures.end ())
this->m_finalTextures [vertCur->first] = textureRef;
} catch (std::runtime_error& ex) {
sLog.error ("Cannot resolve texture ", textureName, " for vertex shader ", ex.what ());
} }
if (fragCur != fragEnd) { ++vertCur;
std::string textureName = fragCur->second;
try {
// resolve the texture first
const ITexture* textureRef;
if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) {
textureRef = this->getMaterial ()->getEffect ()->findFBO (textureName);
if (textureRef == nullptr)
textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName);
} else
textureRef = this->getContext ().resolveTexture (textureName);
// ensure there's no texture in that slot already, shader textures are defaults in case nothing is
// there
if (this->m_finalTextures.find (fragCur->first) == this->m_finalTextures.end ())
this->m_finalTextures [fragCur->first] = textureRef;
} catch (std::runtime_error& ex) {
sLog.error ("Cannot resolve texture ", textureName, " for fragment shader ", ex.what ());
}
++fragCur;
}
if (vertCur != vertEnd) {
std::string textureName = vertCur->second;
try {
// resolve the texture first
const ITexture* textureRef;
if (textureName.find ("_rt_") == 0) {
textureRef = this->getMaterial ()->getEffect ()->findFBO (textureName);
if (textureRef == nullptr)
textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName);
} else
textureRef = this->getContext ().resolveTexture (textureName);
// ensure there's no texture in that slot already, shader textures are defaults in case nothing is
// there
if (this->m_finalTextures.find (vertCur->first) == this->m_finalTextures.end ())
this->m_finalTextures [vertCur->first] = textureRef;
} catch (std::runtime_error& ex) {
sLog.error ("Cannot resolve texture ", textureName, " for vertex shader ", ex.what ());
}
++vertCur;
}
} }
} }
for (const auto& [index, expectedTexture] : this->m_finalTextures) { for (const auto& [textureIndex, expectedTexture] : this->m_finalTextures) {
std::ostringstream namestream; std::ostringstream namestream;
namestream << "g_Texture" << index << "Resolution"; namestream << "g_Texture" << textureIndex << "Resolution";
texture = this->resolveTexture (expectedTexture, index, texture); texture = this->resolveTexture (expectedTexture, textureIndex, texture);
this->addUniform (namestream.str (), texture->getResolution ()); this->addUniform (namestream.str (), texture->getResolution ());
} }
}
void CPass::setupUniforms () {
this->setupTextureUniforms ();
const auto projection = this->getMaterial ()->getImage ()->getScene ()->getScene ()->getOrthogonalProjection (); const auto projection = this->getMaterial ()->getImage ()->getScene ()->getScene ()->getOrthogonalProjection ();

View File

@ -100,6 +100,7 @@ class CPass final : public Helpers::CContextAware {
void setupShaders (); void setupShaders ();
void setupShaderVariables (); void setupShaderVariables ();
void setupUniforms (); void setupUniforms ();
void setupTextureUniforms ();
void setupAttributes (); void setupAttributes ();
void addAttribute (const std::string& name, GLint type, GLint elements, const GLuint* value); void addAttribute (const std::string& name, GLint type, GLint elements, const GLuint* value);
void addUniform (CShaderVariable* value); void addUniform (CShaderVariable* value);
@ -132,6 +133,14 @@ class CPass final : public Helpers::CContextAware {
template <typename T> void addUniform (const std::string& name, UniformType type, T* value, int count = 1); template <typename T> void addUniform (const std::string& name, UniformType type, T* value, int count = 1);
template <typename T> void addUniform (const std::string& name, UniformType type, T** value); template <typename T> void addUniform (const std::string& name, UniformType type, T** value);
void setupRenderFramebuffer ();
void setupRenderTexture ();
void setupRenderUniforms ();
void setupRenderReferenceUniforms ();
void setupRenderAttributes ();
void renderGeometry () const;
void cleanupRenderSetup ();
const ITexture* resolveTexture (const ITexture* expected, int index, const ITexture* previous = nullptr); const ITexture* resolveTexture (const ITexture* expected, int index, const ITexture* previous = nullptr);
CMaterial* m_material; CMaterial* m_material;

View File

@ -260,7 +260,7 @@ void CCompiler::parseComboConfiguration (const std::string& content, int default
const auto entry = this->m_combos->find (combo->get<std::string> ()); const auto entry = this->m_combos->find (combo->get<std::string> ());
// add the combo to the found list // add the combo to the found list
this->m_foundCombos->insert (std::make_pair<std::string, int> (*combo, true)); this->m_foundCombos->insert (std::make_pair (*combo, true));
// if the combo was not found in the predefined values this means that the default value in the JSON data can be // if the combo was not found in the predefined values this means that the default value in the JSON data can be
// used so only define the ones that are not already defined // used so only define the ones that are not already defined
@ -271,11 +271,11 @@ void CCompiler::parseComboConfiguration (const std::string& content, int default
// if no combo is defined just load the default settings // if no combo is defined just load the default settings
if (defvalue == data.end ()) { if (defvalue == data.end ()) {
// TODO: PROPERLY SUPPORT EMPTY COMBOS // TODO: PROPERLY SUPPORT EMPTY COMBOS
this->m_combos->insert (std::make_pair<std::string, int> (*combo, (int) defaultValue)); this->m_combos->insert (std::make_pair (*combo, (int) defaultValue));
} else if (defvalue->is_number_float ()) { } else if (defvalue->is_number_float ()) {
sLog.exception ("float combos are not supported in shader ", this->m_file, ". ", *combo); sLog.exception ("float combos are not supported in shader ", this->m_file, ". ", *combo);
} else if (defvalue->is_number_integer ()) { } else if (defvalue->is_number_integer ()) {
this->m_combos->insert (std::make_pair<std::string, int> (*combo, defvalue->get<int> ())); this->m_combos->insert (std::make_pair (*combo, defvalue->get<int> ()));
} else if (defvalue->is_string ()) { } else if (defvalue->is_string ()) {
sLog.exception ("string combos are not supported in shader ", this->m_file, ". ", *combo); sLog.exception ("string combos are not supported in shader ", this->m_file, ". ", *combo);
} else { } else {
@ -352,11 +352,11 @@ void CCompiler::parseParameterConfiguration (const std::string& type, const std:
if (this->m_passTextures.size () > index && if (this->m_passTextures.size () > index &&
(!this->m_passTextures.at (index).empty () || textureName != data.end ())) { (!this->m_passTextures.at (index).empty () || textureName != data.end ())) {
// add the new combo to the list // add the new combo to the list
this->m_combos->insert (std::make_pair<std::string, int> (*combo, 1)); this->m_combos->insert (std::make_pair (*combo, 1));
// textures linked to combos need to be tracked too // textures linked to combos need to be tracked too
if (this->m_foundCombos->find (*combo) == this->m_foundCombos->end ()) if (this->m_foundCombos->find (*combo) == this->m_foundCombos->end ())
this->m_foundCombos->insert (std::make_pair<std::string, bool> (*combo, true)); this->m_foundCombos->insert (std::make_pair (*combo, true));
} }
} }

View File

@ -45,7 +45,6 @@ namespace WallpaperEngine::Render::Wallpapers {
static const std::string Type; static const std::string Type;
private: private:
WallpaperEngine::WebBrowser::CWebBrowserContext& m_browserContext; WallpaperEngine::WebBrowser::CWebBrowserContext& m_browserContext;
CefRefPtr<CefBrowser> m_browser; CefRefPtr<CefBrowser> m_browser;
CefRefPtr<WallpaperEngine::WebBrowser::CEF::CBrowserClient> m_client; CefRefPtr<WallpaperEngine::WebBrowser::CEF::CBrowserClient> m_client;