--- /dev/null
+#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);
+}
+
+}