From e5e8e4bc2313851c78ebca5c79e8c9e07aa89e69 Mon Sep 17 00:00:00 2001 From: Explodingbill Date: Tue, 20 Aug 2024 16:00:20 +1000 Subject: [PATCH] f --- src/Utils/CCBlurLayer.cpp | 28 +++++++++- src/Utils/CCBlurLayer.cpp.bak | 101 ++++++++++++++++++++++++++++++++++ src/Utils/CCBlurLayer.hpp | 1 + src/Utils/CCBlurLayer.hpp.bak | 24 ++++++++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 src/Utils/CCBlurLayer.cpp.bak create mode 100644 src/Utils/CCBlurLayer.hpp.bak diff --git a/src/Utils/CCBlurLayer.cpp b/src/Utils/CCBlurLayer.cpp index 5ff3b55..69a759f 100644 --- a/src/Utils/CCBlurLayer.cpp +++ b/src/Utils/CCBlurLayer.cpp @@ -38,7 +38,8 @@ using namespace Blur; CCBlurLayer::~CCBlurLayer() { - + if (rtex) + rtex->release(); } bool CCBlurLayer::init() @@ -74,6 +75,14 @@ CCBlurLayer* CCBlurLayer::create() void CCBlurLayer::visit() { + if (rtex) + { + if (rtex->getSprite() && getOpacity()) + rtex->getSprite()->setOpacity(getOpacity()); + + return rtex->visit(); + } + if (this->getOpacity()) { float v = this->getOpacity() / 255.0f; @@ -95,6 +104,20 @@ void CCBlurLayer::draw() if (blurStrength == 0) return CCLayerColor::draw(); + bool once = false; + + if (once) + { + blurStrength = 1; + + rtex = CCRenderTexture::create(as(CCDirector::get()->getWinSize().width), as(CCDirector::get()->getWinSize().height)); + rtex->beginWithClear(0, 0, 0, 0); + rtex->setAnchorPoint(ccp(0, 0)); + rtex->retain(); + + this->addChild(rtex); + } + GLint drawFbo = 0; GLint readFbo = 0; glGetIntegerv(0x8CA6, &drawFbo); @@ -161,6 +184,9 @@ void CCBlurLayer::draw() glBindVertexArray(0); + if (rtex) + rtex->end(); + #endif } diff --git a/src/Utils/CCBlurLayer.cpp.bak b/src/Utils/CCBlurLayer.cpp.bak new file mode 100644 index 0000000..ab2306b --- /dev/null +++ b/src/Utils/CCBlurLayer.cpp.bak @@ -0,0 +1,101 @@ +#include "CCBlurLayer.hpp" + +std::vector layers; + +#define APP_SHADER_SOURCE(...) #__VA_ARGS__ + +const GLchar* vertShaderSource = APP_SHADER_SOURCE( + attribute vec4 a_position; + attribute vec2 a_texCoord; + varying vec2 v_texCoord; + + void main() { + gl_Position = CC_PMatrix * CC_MVMatrix * a_position; + v_texCoord = a_texCoord; + } +); + +const GLchar* fragShaderSource = + "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec2 v_texCoord;\n" + "uniform sampler2D CC_Texture0;\n" + "void main() {\n" + " vec4 color = texture2D(CC_Texture0, v_texCoord);\n" + " gl_FragColor = vec4(color.rgb, 1.0);\n" + "}"; + +bool CCBlurLayer::init() +{ + if (!CCLayerColor::init()) + return false; + + layers.push_back(this); + + render = CCRenderTexture::create(getContentWidth(), getContentHeight()); + render->retain(); + + program = new CCGLProgram(); + program->initWithVertexShaderByteArray(vertShaderSource, fragShaderSource); + + program->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position); + program->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords); + + program->link(); + program->updateUniforms(); + program->retain(); + + render->getSprite()->setShaderProgram(program); + + this->addChild(render, 69); + return true; +} + +CCBlurLayer* CCBlurLayer::create() +{ + auto pRet = new CCBlurLayer(); + + if (pRet && pRet->init()) + { + pRet->autorelease(); + return pRet; + } + + CC_SAFE_DELETE(pRet); + return nullptr; +} + +CCBlurLayer::~CCBlurLayer() +{ + log::info("removing this: {}", this); + + program->release(); + render->release(); + + layers.erase(std::remove(layers.begin(), layers.end(), this), layers.end()); +} + +void CCBlurLayer::visit() +{ + if (this != layers[layers.size() - 1]) + return; + + if (auto scene = CCScene::get()) + { + if (auto parent = this->getParent()) + { + parent->setVisible(false); + + render->beginWithClear(0, 0, 0, 0); + scene->visit(); + render->end(); + + parent->setVisible(true); + + render->setPosition(CCDirector::get()->getWinSize() / 2); + + return CCNode::visit(); + } + } +} \ No newline at end of file diff --git a/src/Utils/CCBlurLayer.hpp b/src/Utils/CCBlurLayer.hpp index c71ea90..115a182 100644 --- a/src/Utils/CCBlurLayer.hpp +++ b/src/Utils/CCBlurLayer.hpp @@ -17,6 +17,7 @@ class CCBlurLayer : public CCLayerColor static inline bool setup; bool visiting = false; float blurStrength = 1; + CCRenderTexture* rtex; ~CCBlurLayer(); bool init(); diff --git a/src/Utils/CCBlurLayer.hpp.bak b/src/Utils/CCBlurLayer.hpp.bak new file mode 100644 index 0000000..effc67a --- /dev/null +++ b/src/Utils/CCBlurLayer.hpp.bak @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include +#include +#include "../Client/Client.h" + +using namespace geode::prelude; + +class CCBlurLayer : public CCLayerColor +{ + public: + CCGLProgram* program; + CCRenderTexture* render; + CCSprite* sprite; + float blurStrength = 1; + + ~CCBlurLayer(); + bool init(); + void visit(); + + static CCBlurLayer* create(); +}; \ No newline at end of file