]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
made application and battle state resizable
[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 "../graphics/Sprite.h"
12
13 #include <iostream>
14 #include <stdexcept>
15
16 using app::Application;
17 using geometry::Point;
18
19 using std::vector;
20
21 namespace battle {
22
23 void BattleState::AddMonster(const Monster &m) {
24         if (monsters.size() >= monstersLayout->NumPositions()) {
25                 throw std::overflow_error("too many monsters for layout");
26         }
27         monsters.push_back(m);
28 }
29
30
31 void BattleState::Resize(int w, int h) {
32         width = w;
33         height = h;
34 }
35
36
37 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
38         monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
39         width = screen->w;
40         height = screen->h;
41 }
42
43 void BattleState::ExitState() {
44
45 }
46
47
48 void BattleState::HandleEvent(const SDL_Event &) {
49
50 }
51
52 void BattleState::UpdateWorld(float deltaT) {
53
54 }
55
56 void BattleState::Render(SDL_Surface *screen) {
57         // black for now
58         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
59         SDL_Rect destRect;
60         destRect.x = (width - background->w) / 2;
61         destRect.y = (height - background->h) / 2;
62         destRect.w = background->w;
63         destRect.h = background->h;
64
65         std::cout << "screen: " << screen->w << "x" << screen->h << std::endl;
66         std::cout << "drawing to " << destRect.w << "x" << destRect.h << "+" << destRect.x << "+" << destRect.y << std::endl;
67
68         // TODO: center background if screen bigger
69         SDL_BlitSurface(background, 0, screen, &destRect);
70
71         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
72                 monsters[i].Sprite()->DrawCenterBottom(screen, Point<int>(monsterPositions[i].X() + destRect.x, monsterPositions[i].Y() + destRect.y));
73         }
74 }
75
76 }