Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSillyDoggo committed Aug 20, 2024
1 parent ee61067 commit e5e8e4b
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Utils/CCBlurLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ using namespace Blur;

CCBlurLayer::~CCBlurLayer()
{

if (rtex)
rtex->release();
}

bool CCBlurLayer::init()
Expand Down Expand Up @@ -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;
Expand All @@ -95,6 +104,20 @@ void CCBlurLayer::draw()
if (blurStrength == 0)
return CCLayerColor::draw();

bool once = false;

if (once)
{
blurStrength = 1;

rtex = CCRenderTexture::create(as<int>(CCDirector::get()->getWinSize().width), as<int>(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);
Expand Down Expand Up @@ -161,6 +184,9 @@ void CCBlurLayer::draw()

glBindVertexArray(0);

if (rtex)
rtex->end();

#endif
}

Expand Down
101 changes: 101 additions & 0 deletions src/Utils/CCBlurLayer.cpp.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "CCBlurLayer.hpp"

std::vector<CCBlurLayer*> 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();
}
}
}
1 change: 1 addition & 0 deletions src/Utils/CCBlurLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CCBlurLayer : public CCLayerColor
static inline bool setup;
bool visiting = false;
float blurStrength = 1;
CCRenderTexture* rtex;

~CCBlurLayer();
bool init();
Expand Down
24 changes: 24 additions & 0 deletions src/Utils/CCBlurLayer.hpp.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <numbers>
#include <Geode/Geode.hpp>
#include <Geode/modify/CCEGLView.hpp>
#include <Geode/modify/CCDirector.hpp>
#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();
};

0 comments on commit e5e8e4b

Please sign in to comment.