#include "AnimationMap.h" #include "EngineCore/Game.h" #include "EngineFrame/Render/RenderManager.h" AnimationMap::~AnimationMap() { } void AnimationMap::CompleteConstruction() { m_size = {800, 600}; // 遍历自己的所持有的所有Ani 取最大的尺寸 auto &ac = this->GetAllChildren(); RefPtr child = ac.GetFirst(); while (child) { Animation *anim = static_cast(child.Get()); auto size = anim->GetMaxSize(); if (size.width > m_size.width) m_size.width = size.width; if (size.height > m_size.height) m_size.height = size.height; child = child->GetNext(); } SetSize(m_size); // 创建指定大小的纹理 m_texture = new Texture(); m_texture->Init(m_size); // 构造FBO glGenFramebuffers(1, &m_fbo); glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); // 将目标纹理附加到FBO的颜色附着点 glFramebufferTexture2D( GL_FRAMEBUFFER, // 帧缓冲类型 GL_COLOR_ATTACHMENT0, // 颜色附着点(可多个,这里用第一个) GL_TEXTURE_2D, // 纹理类型 m_texture->getID(), // 目标纹理ID 0 // 多级渐远纹理级别 ); glBindFramebuffer(GL_FRAMEBUFFER, 0); // 解绑FBO } void AnimationMap::AddAnimation(RefPtr animation) { this->AddChild(animation); } void AnimationMap::Reset() { auto &ac = this->GetAllChildren(); RefPtr child = ac.GetFirst(); while (child) { Animation *anim = static_cast(child.Get()); anim->Reset(); child = child->GetNext(); } } void AnimationMap::Render() { Actor::PreRender(); Actor::PrepareToRender(); Actor::OnRender(); } void AnimationMap::OnRender() { RenderManager *renderer = Game::GetInstance().GetRenderer(); renderer->DrawTexture(m_texture); } void AnimationMap::OnUpdate(float deltaTime) { Actor::OnUpdate(deltaTime); RenderManager *renderer = Game::GetInstance().GetRenderer(); // 绑定FBO glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); // 清屏 glClear(GL_COLOR_BUFFER_BIT); // 保存原始的正交矩阵 设置纹理对应的正交矩阵 和 视口 Oom = renderer->GetOrthoMatrix(); renderer->SetOrthoMatrix(glm::ortho(0.0f, (float)m_size.width, 0.0f, (float)m_size.height, -1.0f, 1.0f)); Oviewport = renderer->GetViewport(); renderer->SetViewport({0, 0, m_size.width, m_size.height}); // 以上步骤将渲染目标调整至AniMap的纹理上 SDL_Rect buf; buf.x = 0; buf.y = 0; buf.w = m_size.width; buf.h = m_size.height; glm::vec4 red = {0.0f, 0.0f, 1.0f, 0.4f}; // RGBA:红色,不透明基础色 Game::GetInstance().GetRenderer()->DrawRect(&buf, red); auto &ac = this->GetAllChildren(); RefPtr child = ac.GetFirst(); while (child) { child->Render(); child = child->GetNext(); } // 渲染完毕后,将渲染目标恢复为默认 // 解绑FBO glBindFramebuffer(GL_FRAMEBUFFER, 0); // 恢复原始的正交矩阵 和 视口 renderer->SetOrthoMatrix(Oom); renderer->SetViewport(Oviewport); Actor::OnUpdate(deltaTime); } // void AnimationMap::SetVisible(bool visible) // { // if (this->IsVisible() == visible) // return; // Actor::SetVisible(visible); // auto ac = this->GetAllChildren(); // RefPtr next; // for (RefPtr child = ac.GetFirst(); child; child = next) // { // next = child->GetNext(); // child->SetVisible(visible); // } // } AnimationMap::AnimationMap() { }