]> git.localhorst.tv Git - l2e.git/blobdiff - src/battle/states/DefeatState.cpp
added basic defeat state
[l2e.git] / src / battle / states / DefeatState.cpp
diff --git a/src/battle/states/DefeatState.cpp b/src/battle/states/DefeatState.cpp
new file mode 100644 (file)
index 0000000..77b3f8e
--- /dev/null
@@ -0,0 +1,111 @@
+#include "DefeatState.h"
+
+#include "../Battle.h"
+#include "../BattleState.h"
+#include "../Resources.h"
+#include "../../app/Application.h"
+#include "../../app/Input.h"
+#include "../../math/Vector.h"
+#include "../../graphics/ColorFade.h"
+#include "../../graphics/Font.h"
+#include "../../graphics/Frame.h"
+#include "../../sdl/Desaturate.h"
+
+using app::Application;
+using app::Input;
+using graphics::ColorFade;
+using graphics::Font;
+using graphics::Frame;
+using math::Vector;
+using std::vector;
+
+namespace battle {
+
+DefeatState::DefeatState(
+               Battle *battle,
+               BattleState *parent)
+: battle(battle)
+, parent(parent)
+, cache(0)
+, format(0) {
+
+}
+
+
+void DefeatState::OnEnterState(SDL_Surface *screen) {
+       timer = GraphicsTimers().StartCountdown(1500);
+       format = screen->format;
+       OnResize(screen->w, screen->h);
+}
+
+void DefeatState::OnExitState(SDL_Surface *screen) {
+       if (cache) {
+               SDL_FreeSurface(cache);
+               cache = 0;
+       }
+}
+
+void DefeatState::OnResumeState(SDL_Surface *screen) {
+
+}
+
+void DefeatState::OnPauseState(SDL_Surface *screen) {
+
+}
+
+
+void DefeatState::OnResize(int width, int height) {
+       const Resources &res = parent->Res();
+       const Frame &frame = *res.titleFrame;
+       const Font &font = *res.titleFont;
+
+       framePosition = parent->ScreenOffset();
+       frameSize = Vector<int> (
+                       parent->Width(),
+                       frame.BorderHeight() * 2 + font.CharHeight());
+
+       textPosition = Vector<int>(
+                       (parent->Width() - strlen(res.defeatText) * font.CharWidth()) / 2,
+                       frame.BorderHeight());
+
+       parent->Resize(width, height);
+       if (cache) {
+               SDL_FreeSurface(cache);
+       }
+       cache = SDL_CreateRGBSurface(0, width, height, format->BitsPerPixel,
+                       format->Rmask, format->Gmask, format->Bmask, format->Amask);
+       parent->Render(cache);
+       parent->RenderHeroes(cache);
+       RenderTitleBar(cache);
+}
+
+
+void DefeatState::HandleEvents(const Input &input) {
+       if (timer.Finished()) {
+               Ctrl().PopState();
+       }
+}
+
+
+void DefeatState::UpdateWorld(Uint32 deltaT) {
+
+}
+
+void DefeatState::Render(SDL_Surface *screen) {
+       if (screen->format->palette
+                       || cache->format->palette) {
+               // I refuse to desaturate an indexed color urface
+               SDL_BlitSurface(cache, 0, screen, 0);
+               return;
+       }
+
+       sdl::Desaturate(cache, screen, timer.Remaining() * 255 / 1500);
+}
+
+void DefeatState::RenderTitleBar(SDL_Surface *screen) {
+       const Resources &res = parent->Res();
+       res.titleFrame->Draw(screen, framePosition, frameSize.X(), frameSize.Y());
+       res.titleFont->DrawString(res.defeatText, screen, textPosition);
+}
+
+}