-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_camera.h
238 lines (184 loc) · 6.26 KB
/
sdl_camera.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifndef sdl_camera_h
#define sdl_camera_h
#include "character.h"
#include "map.h"
#include "job.h"
#include "camera.h"
#include "button.h"
#include "font.h"
#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
class SDLButton;
/********************************************************************/
class MapData;
class Camera;
class SDLButtonManager;
class LoadView : public View {
public:
LoadView(SDLCamera* sdl_camera);
virtual ~LoadView();
bool gameSelected() { return !file_selected_.empty(); };
const std::string& fileSelected() const { return file_selected_; }
virtual void do_render(Camera* camera, double delay_in_ms) override;
virtual bool handleEvent(Camera* camera) override;
void initButtons();
protected:
void renderSplashScreen(SDLCamera* sdl_camera);
void resetFilenameButtons();
private:
SDLCamera* sdl_camera_ = nullptr;
bool display_splash_screen_ = true;
std::string file_selected_;
SDLButtonManager* manager_ = nullptr;
SDLButton* new_game_ = nullptr;
SDLButton* continue_game_ = nullptr;
SDLButton* quit_button_ = nullptr;
std::vector<SDLButton*> save_buttons_;
std::vector<SDLButton*> delete_buttons_;
};
class MapView : public View {
public:
MapView(SDLCamera* camera, MapData* data, PeopleGroup* group, JobMgr* manager);
virtual ~MapView();
virtual void do_render(Camera* camera, double delay_in_ms) override;
virtual bool handleEvent(Camera* camera) override;
MapData* data() const { return data_; }
void addWallJob(int x, int y);
void removeFoundationJob(int x, int y);
void addFloorJob(int x, int y);
void addDoorJob(int x, int y);
void addFieldJob(int x, int y);
void extractItemJob(int x, int y, int nb=1);
void cleanItemJob(int x, int y);
void addObjectJob(const std::string& object_name, int x, int y);
void removeObjectJob(int x, int y);
Object* getObject(int x,int y) const;
Position onTile(int mouse_x, int mouse_y) const;
void setTile(int tile_x, int tile_y);
const SDL_Rect& onTileRect() const { return ontile_rect_; }
bool getCurTilePosition(int& tile_x, int& tile_y);
void resetCenters(int width, int height);
Position getCenterTile() const;
void restoreCenterTile(Position position);
PeopleGroup* group() const;
void selectPeople(Character* people) { selected_people_ = people; }
static MapView* cur_map;
protected:
SDL_Rect getPeopleRect(Character* people) const;
SDL_Rect getTileRect(int tile_x, int tile_y) const;
void renderObjects(SDLCamera* sdl_camera, std::string& tile_text);
void renderJobs(SDLCamera* sdl_camera);
void renderGroup(SDLCamera* sdl_camera);
private:
MapData* data_;
PeopleGroup* group_;
JobMgr* job_manager_;
SDL_Texture* map_background_;
SDL_Texture* window_background_;
SDLCamera* camera_;
float scale_;
float delta_x_;
float delta_y_;
float delta_speed_;
float translate_x_;
float translate_y_;
int center_x_;
int center_y_;
int scaled_start_x_;
int scaled_start_y_;
int scaled_tile_size_;
Character* selected_people_ = nullptr;
int tile_x_;
int tile_y_;
SDL_Rect ontile_rect_;
};
/********************************************************************/
class SDLText {
public:
SDLText(
const std::string& text, const std::string& family, int font_size = FontLib::fontSize(),
const SDL_Color& color = SDLText::black(), const SDL_Color& background_color = SDLText::white()
);
~SDLText();
SDL_Texture* texture(SDL_Renderer* renderer);
void releaseTexture();
const SDL_Rect& rect() const { return rect_; }
void set_position(int x, int y);
void setBackgroundColor(const SDL_Color& bgcolor);
const SDL_Color& getBackgroundColor() const;
static SDL_Color& black() {
static SDL_Color black = {0, 0, 0, 255};
return black;
}
static SDL_Color& red() {
static SDL_Color red = {250, 50, 50, 255};
return red;
}
static SDL_Color& yellow() { // royal
static SDL_Color red = {250, 218, 94, 255};
return red;
}
static SDL_Color& green() {
static SDL_Color red = {0, 128, 0, 255};
return red;
}
static SDL_Color& blue() {
static SDL_Color blue = {50, 50, 250, 255};
return blue;
}
static SDL_Color& white() {
static SDL_Color white = {250, 250, 250, 255};
return white;
}
private:
std::string text_;
std::string family_;
int size_; // font size
SDL_Texture* texture_;
SDL_Color color_;
SDL_Rect rect_;
SDL_Color background_color_;
};
/********************************************************************/
class SDLTool;
class SDLButtonManager;
class SDLButton;
class SDLCamera : public Camera {
public:
SDLCamera(int width, int height);
virtual ~SDLCamera();
void initManager();
virtual bool valid() const override;
virtual void render(double delay_in_ms) override;
virtual bool handleEvent() override;
virtual void onMouseMove(int x, int y) override;
virtual void onMouseWheelScroll(int x, int y) override;
SDLTool* tool() { return tool_; }
void setTool(SDLTool* tool);
void setMapView(MapView* view);
MapView* mapView() const { return map_view_; }
void displayTexture(SDL_Texture* texture, const SDL_Rect* rect, const SDL_Rect* dest = NULL);
void displayText(SDLText& text, bool background=false, bool croppable=false);
void displayButton(SDLButton* button, int offset_x=0, int offset_y=0);
void getSize(int& screen_width, int& screen_height);
SDL_Window* window() const { return window_; }
SDL_Renderer* main_renderer() const { return main_renderer_; }
const SDL_Event& event() const { return event_; }
static void openOptionsDialog();
int speedTime() const { return speed_time_; }
void resetButtons(int width, int height);
private:
SDL_Event event_;
SDL_Window* window_;
SDL_Renderer* main_renderer_;
SDLTool* tool_;
SDLButtonManager* manager_ = nullptr;
SDLButton* quit_button_;
SDLButton* options_button_;
MapView* map_view_;
int speed_time_ = 1;
};
/********************************************************************/
#endif // camera_h