From acff306cc132c4f8cc71f44f85ffd7bdd18a114e Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 9 Aug 2012 22:49:13 +0200 Subject: CPlanet implementation Added rewritten CPlanet implementation --- src/graphics/engine/planet.cpp | 164 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 163 insertions(+), 1 deletion(-) (limited to 'src/graphics/engine/planet.cpp') diff --git a/src/graphics/engine/planet.cpp b/src/graphics/engine/planet.cpp index 4f1f614..022de66 100644 --- a/src/graphics/engine/planet.cpp +++ b/src/graphics/engine/planet.cpp @@ -19,5 +19,167 @@ #include "graphics/engine/planet.h" +#include "common/iman.h" +#include "graphics/core/device.h" +#include "graphics/engine/engine.h" -// TODO implementation + +const int PLANET_PREALLOCATE_COUNT = 10; + + +Gfx::CPlanet::CPlanet(CInstanceManager* iMan, Gfx::CEngine* engine) +{ + m_iMan = iMan; + m_iMan->AddInstance(CLASS_PLANET, this); + + m_planet[0].reserve(PLANET_PREALLOCATE_COUNT); + m_planet[1].reserve(PLANET_PREALLOCATE_COUNT); + + m_engine = engine; + Flush(); + +} + +Gfx::CPlanet::~CPlanet() +{ + m_iMan = nullptr; +} + +void Gfx::CPlanet::Flush() +{ + for (int j = 0; j < 2; j++) + m_planet[j].clear(); + + m_planetExist = false; + m_mode = 0; + m_time = 0.0f; +} + +bool Gfx::CPlanet::EventProcess(const Event &event) +{ + if (event.type == EVENT_FRAME) + return EventFrame(event); + + return true; +} + +bool Gfx::CPlanet::EventFrame(const Event &event) +{ + if (m_engine->GetPause()) return true; + + m_time += event.rTime; + + for (int i = 0; i < static_cast( m_planet[m_mode].size() ); i++) + { + float a = m_time*m_planet[m_mode][i].speed; + if (a < 0.0f) + a += Math::PI*1000.0f; + + m_planet[m_mode][i].angle.x = a+m_planet[m_mode][i].start.x; + m_planet[m_mode][i].angle.y = sinf(a)*sinf(m_planet[m_mode][i].dir)+m_planet[m_mode][i].start.y; + } + + return true; +} + +void Gfx::CPlanet::LoadTexture() +{ + for (int j = 0; j < 2; j++) + { + for (int i = 0; i < static_cast( m_planet[j].size() ); i++) + { + m_engine->LoadTexture(m_planet[j][i].name); + } + } +} + +void Gfx::CPlanet::Draw() +{ + Gfx::CDevice* device = m_engine->GetDevice(); + float eyeDirH = m_engine->GetEyeDirH(); + float eyeDirV = m_engine->GetEyeDirV(); + + Math::Vector n = Math::Vector(0.0f, 0.0f, -1.0f); // normal + float dp = 0.5f/256.0f; + + for (int i = 0; i < static_cast( m_planet[m_mode].size() ); i++) + { + m_engine->SetTexture(m_planet[m_mode][i].name); + + if (m_planet[m_mode][i].transparent) + m_engine->SetState(Gfx::ENG_RSTATE_WRAP | Gfx::ENG_RSTATE_ALPHA); + else + m_engine->SetState(Gfx::ENG_RSTATE_WRAP | Gfx::ENG_RSTATE_TTEXTURE_BLACK); + + Math::Point p1, p2; + + float a = eyeDirH + m_planet[m_mode][i].angle.x; + p1.x = Math::Mod(a, Math::PI*2.0f)-0.5f; + + a = eyeDirV + m_planet[m_mode][i].angle.y; + p1.y = 0.4f+(Math::Mod(a+Math::PI, Math::PI*2.0f)-Math::PI)*(2.0f/Math::PI); + + p1.x -= m_planet[m_mode][i].dim/2.0f*0.75f; + p1.y -= m_planet[m_mode][i].dim/2.0f; + p2.x = p1.x+m_planet[m_mode][i].dim*0.75f; + p2.y = p1.y+m_planet[m_mode][i].dim; + + float u1 = m_planet[m_mode][i].uv1.x + dp; + float v1 = m_planet[m_mode][i].uv1.y + dp; + float u2 = m_planet[m_mode][i].uv2.x - dp; + float v2 = m_planet[m_mode][i].uv2.y - dp; + + Gfx::Vertex quad[4] = + { + Gfx::Vertex(Math::Vector(p1.x, p1.y, 0.0f), n, Math::Point(u1, v2)), + Gfx::Vertex(Math::Vector(p1.x, p2.y, 0.0f), n, Math::Point(u1, v1)), + Gfx::Vertex(Math::Vector(p2.x, p1.y, 0.0f), n, Math::Point(u2, v2)), + Gfx::Vertex(Math::Vector(p2.x, p2.y, 0.0f), n, Math::Point(u2, v1)) + }; + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, quad, 4); + m_engine->AddStatisticTriangle(2); + } +} + +void Gfx::CPlanet::Create(int mode, Math::Point start, float dim, float speed, + float dir, const std::string& name, Math::Point uv1, Math::Point uv2) +{ + if (mode < 0) mode = 0; + if (mode > 1) mode = 1; + + Gfx::Planet planet; + + planet.start = start; + planet.angle = start; + planet.dim = dim; + planet.speed = speed; + planet.dir = dir; + + planet.name = name; + planet.uv1 = uv1; + planet.uv2 = uv2; + + planet.transparent = planet.name.find("planet") != std::string::npos; + + m_planet[mode].push_back(planet); + + m_planetExist = true; +} + +bool Gfx::CPlanet::PlanetExist() +{ + return m_planetExist; +} + +void Gfx::CPlanet::SetMode(int mode) +{ + if (mode < 0) mode = 0; + if (mode > 1) mode = 1; + m_mode = mode; +} + +int Gfx::CPlanet::GetMode() +{ + return m_mode; +} -- cgit v1.2.3-1-g7c22