1
2 /* Battle Tanks Game
3 * Copyright (C) 2006-2009 Battle Tanks team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program 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 this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * Additional rights can be granted beyond the GNU General Public License
22 * on the terms provided in the Exception. If you modify this file,
23 * you may extend this exception to your version of the file,
24 * but you are not obligated to do so. If you do not wish to provide this
25 * exception without modification, you must delete this exception statement
26 * from your version and license this file solely under the GPL without exception.
27 */
28 #include "map_details.h"
29 #include "mrt/exception.h"
30 #include "config.h"
31 #include "tooltip.h"
32 #include "finder.h"
33 #include "resource_manager.h"
34 #include "i18n.h"
35 #include "map_desc.h"
36 #include "mrt/chunk.h"
37
MapDetails(const int w,const int h)38 MapDetails::MapDetails(const int w, const int h) : _w(w), _h(h), _map_desc(0), _ai_hint(NULL), has_tactics(false) {
39 mrt::Chunk data;
40 Finder->load(data, "maps/null.png");
41 _null_screenshot.load_image(data);
42 _null_screenshot.display_format_alpha();
43 _small_font = ResourceManager->loadFont("small", true);
44
45 /*
46 if (hint && I18n->has("tips", "deathmatch-bots")) {
47 int mw, mh;
48 get_size(mw, mh);
49 _ai_hint = new Tooltip("tips", "deathmatch-bots", true, w);
50 int tw, th;
51 _ai_hint->get_size(tw, th);
52 add((mw - tw) / 2, mh + 2, _ai_hint);
53 }
54 */
55 }
56
get_size(int & w,int & h) const57 void MapDetails::get_size(int &w, int &h) const {
58 w = _w; h = _h;
59 }
60
onMouse(const int button,const bool pressed,const int x,const int y)61 bool MapDetails::onMouse(const int button, const bool pressed, const int x, const int y) {
62 _tactics.free();
63 if (!pressed)
64 return true;
65
66 TRY {
67 std::string fname = "maps/" + map + "_tactics.jpg";
68 if (Finder->exists(base, fname)) {
69 mrt::Chunk data;
70 Finder->load(data, fname);
71 _tactics.load_image(data);
72 _tactics.display_format_alpha();
73 has_tactics = true;
74 }
75 } CATCH("loading tactic map", {});
76
77 return true;
78 }
79
set(const MapDesc & map_desc)80 void MapDetails::set(const MapDesc & map_desc) {
81 base = map_desc.base;
82 map = map_desc.name;
83
84 //LOG_DEBUG(("selected base: %s, map: %s", base.c_str(), map.c_str()));
85
86 TRY {
87 _screenshot.free();
88 const std::string fname = "maps/" + map + ".jpg";
89 if (Finder->exists(base, fname)) {
90 mrt::Chunk data;
91 Finder->load(data, fname);
92 _screenshot.load_image(data);
93 _screenshot.display_format_alpha();
94 }
95 } CATCH("loading screenshot", {});
96
97 std::string fname = "maps/" + map + "_tactics.jpg";
98 has_tactics = Finder->exists(base, fname);
99
100 delete _map_desc;
101 _map_desc = NULL;
102
103 delete _map_desc;
104 //const std::string &comments = I18n->has("maps/descriptions", map)?I18n->get("maps/descriptions", map):
105 //I18n->get("maps/descriptions", "(default)");
106
107 _map_desc = new Tooltip("maps/descriptions", I18n->has("maps/descriptions", map)? map:"(default)" , false, _w);
108 if (_ai_hint != NULL) {
109 _ai_hint->hide(map_desc.game_type != GameTypeDeathMatch);
110 }
111 }
112
render(sdlx::Surface & surface,const int x,const int y) const113 void MapDetails::render(sdlx::Surface &surface, const int x, const int y) const {
114 Container::render(surface, x, y);
115
116 int mx = 16, my = 16;
117 int yp = my * 3 / 2;
118
119 const sdlx::Surface &screenshot = _screenshot.isNull()?_null_screenshot:_screenshot;
120 int xs = (_w - screenshot.get_width()) / 2;
121 surface.blit(screenshot, x + xs, y + yp);
122 int ys = screenshot.get_height();
123 yp += (ys < 140)?140:ys;
124
125 if (has_tactics) {
126 std::string click_here = I18n->get("menu", "view-map");
127 int w = _small_font->render(NULL, 0, 0, click_here);
128 _small_font->render(surface, x + (_w - w) / 2, y + yp, click_here);
129 }
130 yp += _small_font->get_height() + 12;
131
132 if (_map_desc)
133 _map_desc->render(surface, x + mx, y + yp);
134
135 if (!_tactics.isNull()) {
136 surface.blit(_tactics, x + _w / 2 - _tactics.get_width() / 2, y + _h / 2 - _tactics.get_height() / 2);
137 }
138 }
139
~MapDetails()140 MapDetails::~MapDetails() {
141 delete _map_desc;
142 }
143