]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
split BattleState's render function
[l2e.git] / src / battle / BattleState.cpp
1 /*
2  * BattleState.cpp
3  *
4  *  Created on: Aug 5, 2012
5  *      Author: holy
6  */
7
8 #include "BattleState.h"
9
10 #include "PartyLayout.h"
11 #include "../geometry/operators.h"
12 #include "../graphics/Sprite.h"
13
14 #include <stdexcept>
15
16 using app::Application;
17 using geometry::Point;
18 using geometry::Vector;
19
20 using std::vector;
21
22 namespace battle {
23
24 void BattleState::AddMonster(const Monster &m) {
25         if (monsters.size() >= monstersLayout->NumPositions()) {
26                 throw std::overflow_error("too many monsters for layout");
27         }
28         monsters.push_back(m);
29 }
30
31 void BattleState::AddHero(const Hero &h) {
32         if (heroes.size() >= heroesLayout->NumPositions()) {
33                 throw std::overflow_error("too many heroes for layout");
34         }
35         heroes.push_back(h);
36 }
37
38
39 void BattleState::Resize(int w, int h) {
40
41 }
42
43
44 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
45         monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
46         heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
47 }
48
49 void BattleState::ExitState() {
50
51 }
52
53
54 void BattleState::HandleEvent(const SDL_Event &) {
55
56 }
57
58 void BattleState::UpdateWorld(float deltaT) {
59
60 }
61
62 void BattleState::Render(SDL_Surface *screen) {
63         Vector<int> offset(
64                         (screen->w - background->w) / 2,
65                         (screen->h - background->h) / 2);
66
67         RenderBackground(screen, offset);
68         RenderMonsters(screen, offset);
69         RenderHeroes(screen, offset);
70 }
71
72 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
73         // black for now
74         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
75         SDL_Rect destRect;
76         destRect.x = offset.X();
77         destRect.y = offset.Y();
78         destRect.w = background->w;
79         destRect.h = background->h;
80         SDL_BlitSurface(background, 0, screen, &destRect);
81 }
82
83 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
84         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
85                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
86         }
87 }
88
89 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
90         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
91                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
92         }
93 }
94
95 }