]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/DefeatState.cpp
added basic defeat state
[l2e.git] / src / battle / states / DefeatState.cpp
1 #include "DefeatState.h"
2
3 #include "../Battle.h"
4 #include "../BattleState.h"
5 #include "../Resources.h"
6 #include "../../app/Application.h"
7 #include "../../app/Input.h"
8 #include "../../math/Vector.h"
9 #include "../../graphics/ColorFade.h"
10 #include "../../graphics/Font.h"
11 #include "../../graphics/Frame.h"
12 #include "../../sdl/Desaturate.h"
13
14 using app::Application;
15 using app::Input;
16 using graphics::ColorFade;
17 using graphics::Font;
18 using graphics::Frame;
19 using math::Vector;
20 using std::vector;
21
22 namespace battle {
23
24 DefeatState::DefeatState(
25                 Battle *battle,
26                 BattleState *parent)
27 : battle(battle)
28 , parent(parent)
29 , cache(0)
30 , format(0) {
31
32 }
33
34
35 void DefeatState::OnEnterState(SDL_Surface *screen) {
36         timer = GraphicsTimers().StartCountdown(1500);
37         format = screen->format;
38         OnResize(screen->w, screen->h);
39 }
40
41 void DefeatState::OnExitState(SDL_Surface *screen) {
42         if (cache) {
43                 SDL_FreeSurface(cache);
44                 cache = 0;
45         }
46 }
47
48 void DefeatState::OnResumeState(SDL_Surface *screen) {
49
50 }
51
52 void DefeatState::OnPauseState(SDL_Surface *screen) {
53
54 }
55
56
57 void DefeatState::OnResize(int width, int height) {
58         const Resources &res = parent->Res();
59         const Frame &frame = *res.titleFrame;
60         const Font &font = *res.titleFont;
61
62         framePosition = parent->ScreenOffset();
63         frameSize = Vector<int> (
64                         parent->Width(),
65                         frame.BorderHeight() * 2 + font.CharHeight());
66
67         textPosition = Vector<int>(
68                         (parent->Width() - strlen(res.defeatText) * font.CharWidth()) / 2,
69                         frame.BorderHeight());
70
71         parent->Resize(width, height);
72         if (cache) {
73                 SDL_FreeSurface(cache);
74         }
75         cache = SDL_CreateRGBSurface(0, width, height, format->BitsPerPixel,
76                         format->Rmask, format->Gmask, format->Bmask, format->Amask);
77         parent->Render(cache);
78         parent->RenderHeroes(cache);
79         RenderTitleBar(cache);
80 }
81
82
83 void DefeatState::HandleEvents(const Input &input) {
84         if (timer.Finished()) {
85                 Ctrl().PopState();
86         }
87 }
88
89
90 void DefeatState::UpdateWorld(Uint32 deltaT) {
91
92 }
93
94 void DefeatState::Render(SDL_Surface *screen) {
95         if (screen->format->palette
96                         || cache->format->palette) {
97                 // I refuse to desaturate an indexed color urface
98                 SDL_BlitSurface(cache, 0, screen, 0);
99                 return;
100         }
101
102         sdl::Desaturate(cache, screen, timer.Remaining() * 255 / 1500);
103 }
104
105 void DefeatState::RenderTitleBar(SDL_Surface *screen) {
106         const Resources &res = parent->Res();
107         res.titleFrame->Draw(screen, framePosition, frameSize.X(), frameSize.Y());
108         res.titleFont->DrawString(res.defeatText, screen, textPosition);
109 }
110
111 }