Adonthell  0.4
data_screen.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2001/2002 by Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 /**
21  * @file data_screen.cc
22  * @author Kai Sterker <kai.sterker@gmail.com>
23  *
24  * @brief Defines the data_screen class.
25  *
26  *
27  */
28 
29 
30 #include <string.h>
31 
32 #include "pnm.h"
33 #include "gamedata.h"
34 #include "gamedate.h"
35 #include "image.h"
36 #include "input.h"
37 #include "window.h"
38 #include "data_screen.h"
39 #include "gametime.h"
40 #include "win_manager.h"
41 
43 {
44  mode = m;
45  aborted = false;
46 
47  entry = NULL;
48 
49  //Init Position and Size
50  win_container::move (30, 15);
51  win_container::resize (260, 210);
52 
53  // Create GUI
54  font = win_manager::get_font ("original");
55  theme = win_manager::get_theme ("original");
56 
57  //Set features used
58  set_border(*theme);
59  set_background(*theme);
60 
61  //Set special Features
62  set_trans_background(true);
63 
64  //Create a win_select
65  image_list = new win_select();
66  image_list->move (10, 0);
67  image_list->resize (250, 210);
68  image_list->set_mode (win_select::MODE_BRIGHTNESS);
69  image_list->set_layout (win_container::LIST_LAYOUT);
70  image_list->set_circle (true);
71  image_list->set_space_with_border (9);
72  image_list->set_space_with_object (9);
73 
74  image_list->set_scrollbar (*theme);
75  image_list->set_visible_scrollbar (true);
76 
77  // activate the list
78  image_list->set_activate (true);
79 
80  // give focus to the list
81  set_focus_object (image_list);
82 
83  image_list->set_signal_connect (
84  makeFunctor (*this, &data_screen::on_select),
85  win_event::ACTIVATE_KEY);
86 
87  // add the win_select to *this
88  add (image_list);
89 
90  // add all the saved games to the list
91  init ();
92 
93  // show everything
94  set_visible_background (true);
95  set_visible_border (true);
96  set_visible_all (true);
97 }
98 
100 {
101  // fade in from black after loading a game
102  if (mode == LOAD_SCREEN && !aborted && data::engine->update_map()) data::engine->fade_in ();
103 }
104 
105 void data_screen::init ()
106 {
107  string filepath;
108  u_int16 num = 0;
109  win_image *shot;
110  win_write *entry;
111  win_label *date;
112  win_container *box = NULL;
113  win_font *yellow = win_manager::get_font ("yellow");
114  gamedata *gdata;
115 
116  // display all the available saved games
117  while ((gdata = gamedata::next_save ()) != NULL)
118  {
119  filepath = gdata->directory ();
120  filepath += "/preview.pnm";
121 
122  shot = new win_image ();
123  shot->image::load_pnm (filepath);
124  shot->move (5, 2);
125  shot->set_border (*theme, win_border::MINI);
126  shot->set_visible_border (true);
127  shot->pack();
128 
129  date = new win_label ();
130  date->move (100, 2);
131  ((label*)date)->resize (130, 14);
132  date->set_font (*yellow);
133  date->set_text (gdata->gametime ());
134  date->set_cursor_visible (false);
135  date->pack();
136 
137  entry = new win_write ();
138  entry->move (100, 18);
139  ((label_input*)entry)->resize (130, 40);
140  entry->set_font (*font);
141  entry->set_text (gdata->description ());
142  entry->set_cursor_visible (false);
143  entry->pack();
144 
145  entry_list.push_back (entry);
146 
147  box = new win_container ();
148  box->move (0, 0);
149  box->resize (230, 58);
150  box->add (shot);
151  box->add (date);
152  box->add (entry);
153  box->set_visible_all (true);
154 
155  // when the box is activated, we set the entry as
156  // focus object of the box
157  box->set_focus_object (entry);
158 
159  image_list->add (box);
160 
161  num++;
162  }
163 
164  // If we're saving the game, add "Empty Slot"
165  if (mode == SAVE_SCREEN)
166  {
167  sprintf (gametime, "Day %i - %02i:%02i", gamedate::day (),
169 
170  shot = new win_image ();
171  shot->move (5, 2);
172  shot->load_pnm ("gfx/empty_slot.pnm");
173  shot->set_border (*theme, win_border::MINI);
174  shot->set_visible_border (true);
175  shot->pack ();
176 
177  date = new win_label ();
178  date->move (100, 2);
179  ((label*)date)->resize (130, 14);
180  date->set_font (*yellow);
181  date->set_text (gametime);
182  date->set_cursor_visible (false);
183  date->pack();
184 
185  entry = new win_write ();
186  entry->set_font (*font);
187  entry->move (100, 18);
188  ((label_input*) entry)->resize (130, 40);
189  entry->set_text ("Empty Slot");
190  entry->set_cursor_visible (false);
191  entry->pack ();
192 
193  entry_list.push_back (entry);
194 
195  box = new win_container ();
196  box->move (0, 0);
197  box->resize (230, 58);
198  box->add (shot);
199  box->add (date);
200  box->add (entry);
201  box->set_visible_all (true);
202 
203  // when the box is activated, we set the entry as
204  // focus object of the box
205  box->set_focus_object(entry);
206 
207  image_list->add (box);
208  image_list->set_default_object (box);
209 
210  num++;
211  }
212  else image_list->set_default_position (0);
213 
214  // If there are no saved games, display a message.
215  if (!num)
216  {
217  box = new win_container ();
218  box->move(0, 0);
219  box->resize(230, 150);
220 
221  win_label * mess = new win_label ();
222  mess->set_font (*font);
223  mess->move (0, 65);
224  mess->set_form (label::AUTO_SIZE);
225  mess->set_text ("You have no saved games yet!");
226  mess->set_align (win_base::ALIGN_CENTER);
227  mess->pack ();
228  box->add (mess);
229 
230  mess = new win_label ();
231  mess->set_font (*font);
232  mess->move (0, 115);
233  mess->set_form (label::AUTO_SIZE);
234  mess->set_text ("Press ESC.");
235  mess->set_align (win_base::ALIGN_CENTER);
236  mess->pack ();
237  box->add (mess);
238 
239  box->set_visible_all (true);
240  box->set_can_be_selected (false);
241 
242  image_list->add (box);
243  }
244 }
245 
247 {
248  if (!win_container::update() || input::has_been_pushed (SDLK_ESCAPE))
249  {
250  aborted = true;
251  data::engine->main_quit ();
252  }
253 
254  return true;
255 }
256 
257 // Select a game
258 void data_screen::on_select ()
259 {
260  int pos = image_list->get_selected_position ();
261 
262  // loading
263  if (mode == LOAD_SCREEN)
264  {
265  // fade to black before doing the actual work
266  data::engine->fade_out ();
267  set_visible (false);
268  if (!gamedata::load (pos)) aborted = true;
269  data::engine->main_quit ();
270  }
271  // saving
272  else
273  {
274  win_container * tmp = (win_container*)image_list->get_selected_object();
275  image_list->set_focus_object(tmp);
276 
277  entry = (win_write*) tmp->focus_object();
278  entry->set_cursor_visible (true);
279  entry->set_cursor_moveable (true);
280 
281  const char *txt = entry->text_char ();
282  if (txt && !strncmp (txt, "Empty Slot", 10))
283  entry->set_text ("");
284 
285  entry->set_signal_connect (makeFunctor (*this, &data_screen::on_save),
286  win_event::ACTIVATE_KEY);
287  entry->set_activate (true);
289  }
290 }
291 
292 void data_screen::on_save ()
293 {
294  const char* description = entry->text_char ();
295  int pos = image_list->get_selected_position ();
296 
297  gamedata::save (pos, description, gametime);
298  gamedata *gdata = gamedata::get_saved_game (pos);
299 
300  // save sucessful --> save preview
301  if (gdata != NULL)
302  {
303  string filepath = gdata->directory ();
304  filepath += "/preview.pnm";
305  save_preview (filepath);
306  }
307 
309 }
310 
311 // Save the small thumbnail image
312 void data_screen::save_preview (string path)
313 {
314  drawing_area da (0, 0, screen::length () >> 1, screen::height () >> 1);
315  image temp (da.length (), da.height());
316  image preview (72, 54);
317 
318  mapview *view = data::engine->get_mapview ();
320  (data::the_player->submap ());
321 
322  u_int16 offx = 0;
323  u_int16 offy = 0;
324 
325  // In those cases where the mapview is smaller than the physical screen,
326  // it will be centered on the screen -> get the offset
327  if (area->area_length () * MAPSQUARE_SIZE < view->length ())
328  offx = (view->length () - area->area_length () * MAPSQUARE_SIZE) >> 1;
329  if (area->area_height () * MAPSQUARE_SIZE < view->height ())
330  offy = (view->height () - area->area_height () * MAPSQUARE_SIZE) >> 1;
331 
332  // Calculate the player's absolute position on the screen
333  s_int16 x = (data::the_player->posx () - view->posx ()) * MAPSQUARE_SIZE + offx;
334  s_int16 y = (data::the_player->posy () - view->posy ()) * MAPSQUARE_SIZE + offy;
335 
336  // this is a quarter of the screen's size.
337  u_int16 length = da.length() >> 1;
338  u_int16 height = da.height() >> 1;
339 
340  // If the player is too close to the border, make sure we still stay
341  // within the screen
342  if (x + length > screen::length ()) x = -da.length ();
343  else if (x - length < 0) x = 0;
344  else x = length - x;
345 
346  if (y + height > screen::height ()) y = -da.height ();
347  else if (y - height < 0) y = 0;
348  else y = height - y;
349 
350  data::engine->draw (x, y, &da, &temp);
351  preview.zoom (temp);
352  preview.save_pnm (path);
353 }
static bool has_been_pushed(SDL_Keycode key)
Returns whether a key has been pushed since last function call, false otherwise.
Definition: input.cc:119
static bool save(u_int32 pos, string desc, string time)
Save a game.
Definition: gamedata.cc:340
void fade_in()
Fades in from a black screen.
Definition: adonthell.cc:138
static win_font * get_font(string name)
Returns a pointer to a font.
Definition: win_manager.cc:252
virtual void move(s_int16 tx, s_int16 ty)
Move the win_*.
Definition: win_base.cc:75
s_int16 y() const
Return the relative vertical position of the win_*.
Definition: win_base.h:80
u_int16 height() const
Returns the height of the drawable.
Definition: drawable.h:91
Declares the input class.
void fade_out()
Fades the screen to black.
Definition: adonthell.cc:120
const char * description()
Returns the description of the saved game.
Definition: gamedata.h:114
static bool load(u_int32 pos)
Loads a previously saved game.
Definition: gamedata.cc:303
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
const char * directory()
A bunch of methods to access the private attributes.
Definition: gamedata.h:107
static win_theme * get_theme(string name)
Returns a pointer to a theme.
Definition: win_manager.cc:221
u_int16 length() const
Returns the length of the drawing_area.
Definition: drawing_area.h:93
u_int16 posx() const
Returns the X position of the mapcharacter.
Definition: mapcharacter.h:544
void set_visible(const bool b)
Set the visible parameter.
Definition: win_base.h:140
u_int16 length() const
Returns the length of the drawable.
Definition: drawable.h:80
void draw(s_int16 x, s_int16 y, drawing_area *da_opt=NULL, surface *target=NULL)
Definition: adonthell.h:196
static u_int16 hour()
Return the hour of the current day.
Definition: gamedate.cc:92
~data_screen()
Destructor.
Definition: data_screen.cc:99
Image manipulation class.
Definition: image.h:45
Declares the gametime class.
void resize(u_int16, u_int16)
Rezise the win_*.
static u_int16 length()
Returns the length of the screen.
Definition: screen.h:84
Tehe gametime class makes the speed of the game independent of the machine it runs on...
Definition: gametime.h:65
void move(s_int16, s_int16)
Move the win_*.
u_int16 area_length() const
Returns the length of the area.
Definition: mapsquare.h:405
mapsquare_area * get_submap(u_int16 pos)
Returns a pointer to a submap belonging to this landmap.
Definition: landmap.h:167
mapview * get_mapview()
Definition: adonthell.h:188
static gamedata * next_save()
Returns a pointer to the next saved game.
Definition: gamedata.cc:521
void main_quit()
Quit the main loop.
Definition: adonthell.cc:114
static u_int16 day()
Returns the current day in the gameworld.
Definition: gamedate.cc:83
Declares the image class.
u_int16 posx() const
Returns the X position of the mapview.
Definition: mapview.h:152
static u_int16 minute()
Return the minute of the current hour.
Definition: gamedate.cc:98
Declares the data_screen class.
Allows you to display a landmap on a specified area of a surface.
Definition: mapview.h:48
void resize(u_int16 tl, u_int16 th)
Rezise the win_*.
Definition: win_scroll.cc:57
Declares the gamedata and data classes.
s_int16 x() const
Return the relative horizontal position of the win_*.
Definition: win_base.h:72
Implements "drawing zones" for drawing operations.
Definition: drawing_area.h:54
Definition: label.h:29
Declares the gamedate class.
#define s_int16
16 bits long signed integer
Definition: types.h:47
void set_align(const u_int8 a)
Set alignement of win_*.
Definition: win_base.h:205
data_screen(int m)
Constructor.
Definition: data_screen.cc:42
static gamedata * get_saved_game(u_int32 pos)
Returns a pointer to a saved game.
Definition: gamedata.h:295
A* pathfinding algorithm implementation class.
Definition: path.h:52
landmap * get_landmap()
Definition: adonthell.h:164
void set_can_be_selected(const bool b)
Set the object to be selected A win_obj can be selectable or not when it is inside a win_select...
Definition: win_base.h:226
const u_int16 MAPSQUARE_SIZE
Size of a mapsquare (in pixels).
bool update()
React to input.
Definition: data_screen.cc:246
Declares the pnm static class.
static void clear_keys_queue()
Totally clears the key queue.
Definition: input.cc:168
const char * gametime()
Returns the in-game time of the saved game.
Definition: gamedata.h:128
character * the_player
The player character.
Definition: character.cc:32
Declares the win_manager class.
u_int16 posy() const
Returns the Y position of the mapview.
Definition: mapview.h:163
u_int16 posy() const
Returns the Y position of the mapcharacter.
Definition: mapcharacter.h:555
virtual bool update()
Update process.
u_int16 area_height() const
Returns the height of the area.
Definition: mapsquare.h:416
u_int16 height() const
Returns the height of the drawing_area.
Definition: drawing_area.h:101
void set_activate(const bool b)
Set the activate parameter When a win_* is setup on, the keys queue is cleared.
Definition: win_base.h:156
adonthell * engine
Engine used during the game.
Definition: adonthell.cc:35
static u_int16 height()
Returns the height of the screen.
Definition: screen.h:92
Area of mapsquares, for use with landmap.
Definition: mapsquare.h:372
Contains all the attributes related to a saved game and the high level methods for loading/saving the...
Definition: gamedata.h:52