Author: esr
Date: Fri Jun 22 14:32:43 2007
New Revision: 18375
URL: http://svn.gna.org/viewcvs/wesnoth?rev=18375&view=rev
Log:
Lift some bounds-checking code into display.cpp.
Modified:
trunk/src/display.cpp
trunk/src/display.hpp
Modified: trunk/src/display.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/display.cpp?rev=18375&r1=18374&r2=18375&view=diff
==============================================================================
--- trunk/src/display.cpp (original)
+++ trunk/src/display.cpp Fri Jun 22 14:32:43 2007
@@ -63,7 +63,9 @@
size_t sunset_timer = 0;
}
-map_display::map_display(CVideo& video, const gamemap& map, const config&
theme_cfg) : screen_(video), map_(map), theme_(theme_cfg,screen_area()),
zoom_(DefaultZoom)
+map_display::map_display(CVideo& video, const gamemap& map, const config&
theme_cfg) :
+ screen_(video), map_(map), xpos_(0), ypos_(0),
+ theme_(theme_cfg,screen_area()), zoom_(DefaultZoom)
{
if(non_interactive()) {
screen_.lock_updates(true);
@@ -103,7 +105,7 @@
return res;
}
-bool display::outside_area(const SDL_Rect& area, const int x, const int y)
const
+bool map_display::outside_area(const SDL_Rect& area, const int x, const int y)
const
{
const int x_thresh = hex_width();
const int y_thresh = hex_size();
@@ -111,195 +113,8 @@
y < area.y || y > area.y + area.h - y_thresh);
}
-void map_display::screenshot()
-{
- std::string datadir = get_screenshot_dir();
- static unsigned int counter = 0;
- std::string name;
-
- do {
- std::stringstream filename;
-
- filename << datadir << "/" << _("Screenshot") << "_";
- filename.width(5);
- filename.fill('0');
- filename.setf(std::ios_base::right);
- filename << counter << ".bmp";
-
- counter++;
- name = filename.str();
-
- } while(file_exists(name));
-
- SDL_SaveBMP(screen_.getSurface().get(), name.c_str());
-}
-
-// Methods for superclass aware of units go here
-
-std::map<gamemap::location,fixed_t> display::debugHighlights_;
-
-display::display(unit_map& units, CVideo& video, const gamemap& map,
- const gamestatus& status, const std::vector<team>& t,
- const config& theme_cfg, const config& cfg, const config&
level) :
- map_display(video, map, theme_cfg),
- _scroll_event("scrolled"),
- xpos_(0), ypos_(0),
- units_(units),
- temp_unit_(NULL),
- minimap_(NULL), redrawMinimap_(false), redraw_background_(true),
- status_(status),
- teams_(t), nextDraw_(0),
- invalidateAll_(true), invalidateUnit_(true),
- invalidateGameStatus_(true), panelsDrawn_(false),
- currentTeam_(0), activeTeam_(0),
- turbo_speed_(2), turbo_(false), grid_(false), sidebarScaling_(1.0),
- builder_(cfg, level, map),
- first_turn_(true), in_game_(false), map_labels_(*this,map, 0),
- tod_hex_mask1(NULL), tod_hex_mask2(NULL), reach_map_changed_(true),
- diagnostic_label_(0), fps_handle_(0)
-{
- singleton_ = this;
- std::fill(reportRects_,reportRects_+reports::NUM_REPORTS,empty_rect);
-
- //inits the flag list
- flags_.reserve(teams_.size());
- for(size_t i = 0; i != teams_.size(); ++i) {
- std::string flag = teams_[i].flag();
- std::string old_rgb = game_config::flag_rgb;
- std::string new_rgb = team::get_side_colour_index(i+1);
-
- if(flag.empty()) {
- flag = game_config::flag_image;
- }
-
- LOG_STREAM(info, display) << "Adding flag for team " << i << "
from animation " << flag << "\n";
-
- //must recolor flag image
- animated<image::locator> temp_anim;
-
- std::vector<std::string> items = utils::split(flag);
- std::vector<std::string>::const_iterator itor = items.begin();
- for(; itor != items.end(); ++itor) {
- const std::vector<std::string>& items =
utils::split(*itor, ':');
- std::string str;
- int time;
-
- if(items.size() > 1) {
- str = items.front();
- time = atoi(items.back().c_str());
- } else {
- str = *itor;
- time = 100;
- }
- std::stringstream temp;
- temp << str << "~RC(" << old_rgb << ">"<< new_rgb <<
")";
- image::locator flag_image(temp.str());
- temp_anim.add_frame(time, flag_image);
- }
- flags_.push_back(temp_anim);
-
-
flags_.back().start_animation(rand()%flags_.back().get_end_time(), true);
- }
-
- //clear the screen contents
- surface const disp(screen_.getSurface());
- SDL_Rect area = screen_area();
- SDL_FillRect(disp,&area,SDL_MapRGB(disp->format,0,0,0));
-}
-
-display::~display()
-{
- // SDL_FreeSurface(minimap_);
- prune_chat_messages(true);
- singleton_ = NULL;
-}
-
-void display::new_turn()
-{
- const time_of_day& tod = status_.get_time_of_day();
-
- if( !first_turn_) {
- image::set_image_mask("");
-
- const time_of_day& old_tod = status_.get_previous_time_of_day();
-
- if(old_tod.image_mask != tod.image_mask) {
- const surface
old_mask(image::get_image(old_tod.image_mask,image::UNMASKED));
- const surface
new_mask(image::get_image(tod.image_mask,image::UNMASKED));
-
- const int niterations = (int)(10/turbo_speed());
- const int frame_time = 30;
- const int starting_ticks = SDL_GetTicks();
- for(int i = 0; i != niterations; ++i) {
-
- if(old_mask != NULL) {
- const fixed_t proportion = ftofxp(1.0)
- fxpdiv(i,niterations);
-
tod_hex_mask1.assign(adjust_surface_alpha(old_mask,proportion));
- }
-
- if(new_mask != NULL) {
- const fixed_t proportion =
fxpdiv(i,niterations);
-
tod_hex_mask2.assign(adjust_surface_alpha(new_mask,proportion));
- }
-
- invalidate_all();
- draw();
-
- const int cur_ticks = SDL_GetTicks();
- const int wanted_ticks = starting_ticks +
i*frame_time;
- if(cur_ticks < wanted_ticks) {
- SDL_Delay(wanted_ticks - cur_ticks);
- }
- }
- }
-
- tod_hex_mask1.assign(NULL);
- tod_hex_mask2.assign(NULL);
- }
-
- first_turn_ = false;
-
- image::set_colour_adjustment(tod.red,tod.green,tod.blue);
- image::set_image_mask(tod.image_mask);
-
- invalidate_all();
- draw();
-}
-
-void display::adjust_colours(int r, int g, int b)
-{
- const time_of_day& tod = status_.get_time_of_day();
- image::set_colour_adjustment(tod.red+r,tod.green+g,tod.blue+b);
-}
-
-void display::select_hex(gamemap::location hex)
-{
- if(fogged(hex)) {
- return;
- }
-
- invalidate(selectedHex_);
- selectedHex_ = hex;
- invalidate(selectedHex_);
- invalidate_unit();
-}
-
-void display::highlight_hex(gamemap::location hex)
-{
- const int has_unit = units_.count(mouseoverHex_) + units_.count(hex);
-
- invalidate(mouseoverHex_);
- mouseoverHex_ = hex;
- invalidate(mouseoverHex_);
- invalidate_game_status();
-
- if(has_unit) {
- invalidate_unit();
- }
-}
-
// This function use the screen as reference
-const gamemap::location display::hex_clicked_on(int xclick, int yclick,
+const gamemap::location map_display::hex_clicked_on(int xclick, int yclick,
gamemap::location::DIRECTION* nearest_hex,
gamemap::location::DIRECTION* second_nearest_hex) const
{
@@ -316,7 +131,7 @@
// This function use the rect of map_area as reference
-const gamemap::location display::pixel_position_to_hex(int x, int y,
+const gamemap::location map_display::pixel_position_to_hex(int x, int y,
gamemap::location::DIRECTION* nearest_hex,
gamemap::location::DIRECTION* second_nearest_hex) const
{
@@ -433,7 +248,7 @@
return res;
}
-void display::get_rect_hex_bounds(SDL_Rect rect, gamemap::location &topleft,
gamemap::location &bottomright) const
+void map_display::get_rect_hex_bounds(SDL_Rect rect, gamemap::location
&topleft, gamemap::location &bottomright) const
{
// change the coordinates of the rect send to be relative
// to the map area instead of the screen area
@@ -472,7 +287,7 @@
}
}
-gamemap::location display::minimap_location_on(int x, int y)
+gamemap::location map_display::minimap_location_on(int x, int y)
{
const SDL_Rect rect = minimap_area();
@@ -485,6 +300,192 @@
const double ydiv = double(rect.h) / double(map_.y());
return gamemap::location(int((x - rect.x)/xdiv),int((y-rect.y)/ydiv));
+}
+
+void map_display::screenshot()
+{
+ std::string datadir = get_screenshot_dir();
+ static unsigned int counter = 0;
+ std::string name;
+
+ do {
+ std::stringstream filename;
+
+ filename << datadir << "/" << _("Screenshot") << "_";
+ filename.width(5);
+ filename.fill('0');
+ filename.setf(std::ios_base::right);
+ filename << counter << ".bmp";
+
+ counter++;
+ name = filename.str();
+
+ } while(file_exists(name));
+
+ SDL_SaveBMP(screen_.getSurface().get(), name.c_str());
+}
+
+// Methods for superclass aware of units go here
+
+std::map<gamemap::location,fixed_t> display::debugHighlights_;
+
+display::display(unit_map& units, CVideo& video, const gamemap& map,
+ const gamestatus& status, const std::vector<team>& t,
+ const config& theme_cfg, const config& cfg, const config&
level) :
+ map_display(video, map, theme_cfg),
+ _scroll_event("scrolled"),
+ units_(units),
+ temp_unit_(NULL),
+ minimap_(NULL), redrawMinimap_(false), redraw_background_(true),
+ status_(status),
+ teams_(t), nextDraw_(0),
+ invalidateAll_(true), invalidateUnit_(true),
+ invalidateGameStatus_(true), panelsDrawn_(false),
+ currentTeam_(0), activeTeam_(0),
+ turbo_speed_(2), turbo_(false), grid_(false), sidebarScaling_(1.0),
+ builder_(cfg, level, map),
+ first_turn_(true), in_game_(false), map_labels_(*this,map, 0),
+ tod_hex_mask1(NULL), tod_hex_mask2(NULL), reach_map_changed_(true),
+ diagnostic_label_(0), fps_handle_(0)
+{
+ singleton_ = this;
+ std::fill(reportRects_,reportRects_+reports::NUM_REPORTS,empty_rect);
+
+ //inits the flag list
+ flags_.reserve(teams_.size());
+ for(size_t i = 0; i != teams_.size(); ++i) {
+ std::string flag = teams_[i].flag();
+ std::string old_rgb = game_config::flag_rgb;
+ std::string new_rgb = team::get_side_colour_index(i+1);
+
+ if(flag.empty()) {
+ flag = game_config::flag_image;
+ }
+
+ LOG_STREAM(info, display) << "Adding flag for team " << i << "
from animation " << flag << "\n";
+
+ //must recolor flag image
+ animated<image::locator> temp_anim;
+
+ std::vector<std::string> items = utils::split(flag);
+ std::vector<std::string>::const_iterator itor = items.begin();
+ for(; itor != items.end(); ++itor) {
+ const std::vector<std::string>& items =
utils::split(*itor, ':');
+ std::string str;
+ int time;
+
+ if(items.size() > 1) {
+ str = items.front();
+ time = atoi(items.back().c_str());
+ } else {
+ str = *itor;
+ time = 100;
+ }
+ std::stringstream temp;
+ temp << str << "~RC(" << old_rgb << ">"<< new_rgb <<
")";
+ image::locator flag_image(temp.str());
+ temp_anim.add_frame(time, flag_image);
+ }
+ flags_.push_back(temp_anim);
+
+
flags_.back().start_animation(rand()%flags_.back().get_end_time(), true);
+ }
+
+ //clear the screen contents
+ surface const disp(screen_.getSurface());
+ SDL_Rect area = screen_area();
+ SDL_FillRect(disp,&area,SDL_MapRGB(disp->format,0,0,0));
+}
+
+display::~display()
+{
+ // SDL_FreeSurface(minimap_);
+ prune_chat_messages(true);
+ singleton_ = NULL;
+}
+
+void display::new_turn()
+{
+ const time_of_day& tod = status_.get_time_of_day();
+
+ if( !first_turn_) {
+ image::set_image_mask("");
+
+ const time_of_day& old_tod = status_.get_previous_time_of_day();
+
+ if(old_tod.image_mask != tod.image_mask) {
+ const surface
old_mask(image::get_image(old_tod.image_mask,image::UNMASKED));
+ const surface
new_mask(image::get_image(tod.image_mask,image::UNMASKED));
+
+ const int niterations = (int)(10/turbo_speed());
+ const int frame_time = 30;
+ const int starting_ticks = SDL_GetTicks();
+ for(int i = 0; i != niterations; ++i) {
+
+ if(old_mask != NULL) {
+ const fixed_t proportion = ftofxp(1.0)
- fxpdiv(i,niterations);
+
tod_hex_mask1.assign(adjust_surface_alpha(old_mask,proportion));
+ }
+
+ if(new_mask != NULL) {
+ const fixed_t proportion =
fxpdiv(i,niterations);
+
tod_hex_mask2.assign(adjust_surface_alpha(new_mask,proportion));
+ }
+
+ invalidate_all();
+ draw();
+
+ const int cur_ticks = SDL_GetTicks();
+ const int wanted_ticks = starting_ticks +
i*frame_time;
+ if(cur_ticks < wanted_ticks) {
+ SDL_Delay(wanted_ticks - cur_ticks);
+ }
+ }
+ }
+
+ tod_hex_mask1.assign(NULL);
+ tod_hex_mask2.assign(NULL);
+ }
+
+ first_turn_ = false;
+
+ image::set_colour_adjustment(tod.red,tod.green,tod.blue);
+ image::set_image_mask(tod.image_mask);
+
+ invalidate_all();
+ draw();
+}
+
+void display::adjust_colours(int r, int g, int b)
+{
+ const time_of_day& tod = status_.get_time_of_day();
+ image::set_colour_adjustment(tod.red+r,tod.green+g,tod.blue+b);
+}
+
+void display::select_hex(gamemap::location hex)
+{
+ if(fogged(hex)) {
+ return;
+ }
+
+ invalidate(selectedHex_);
+ selectedHex_ = hex;
+ invalidate(selectedHex_);
+ invalidate_unit();
+}
+
+void display::highlight_hex(gamemap::location hex)
+{
+ const int has_unit = units_.count(mouseoverHex_) + units_.count(hex);
+
+ invalidate(mouseoverHex_);
+ mouseoverHex_ = hex;
+ invalidate(mouseoverHex_);
+ invalidate_game_status();
+
+ if(has_unit) {
+ invalidate_unit();
+ }
}
void display::scroll(int xmove, int ymove)
Modified: trunk/src/display.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/display.hpp?rev=18375&r1=18374&r2=18375&view=diff
==============================================================================
--- trunk/src/display.hpp (original)
+++ trunk/src/display.hpp Fri Jun 22 14:32:43 2007
@@ -86,6 +86,9 @@
const SDL_Rect& map_outside_area() const
{ return theme_.main_map_location(screen_area()); }
+ //check if pixel x,y is outside specified area
+ bool outside_area(const SDL_Rect& area, const int x,const int y) const;
+
//function which returns the width of a pixel, up to where the
//next hex starts (i.e. not entirely from tip to tip -- use
//hex_size() to get the distance from tip to tip)
@@ -97,12 +100,42 @@
// Returns the current zoom factor.
double get_zoom_factor() { return
double(zoom_)/double(image::tile_size); }
+
+ //given x,y co-ordinates of an onscreen pixel, will return the
+ //location of the hex that this pixel corresponds to. Returns an
+ //invalid location is the mouse isn't over any valid location.
+ const gamemap::location hex_clicked_on(int x, int y,
+ gamemap::location::DIRECTION* nearest_hex=NULL,
+ gamemap::location::DIRECTION* second_nearest_hex=NULL) const;
+
+ //given x,y co-ordinates of a pixel on the map, will return the
+ //location of the hex that this pixel corresponds to. Returns an
+ //invalid location if the mouse isn't over any valid location.
+ const gamemap::location pixel_position_to_hex(int x, int y,
+ gamemap::location::DIRECTION* nearest_hex=NULL,
+ gamemap::location::DIRECTION* second_nearest_hex=NULL) const;
+
+ //given x,y co-ordinates of the mouse, will return the location of the
+ //hex in the minimap that the mouse is currently over, or an invalid
+ //location if the mouse isn't over the minimap.
+ gamemap::location minimap_location_on(int x, int y);
+
+ void get_rect_hex_bounds(SDL_Rect rect, gamemap::location &topleft,
gamemap::location &bottomright) const;
+
+ //functions to get the on-screen positions of hexes.
+ // we have a 1 hex border so need to offset the loction with 1
+ int get_location_x(const gamemap::location& loc) const
+ { return map_area().x + (loc.x + 1) * hex_width() - xpos_; }
+ int get_location_y(const gamemap::location& loc) const
+ { return map_area().y + (loc.y + 1) * zoom_ - ypos_ +
(is_odd(loc.x) ? zoom_/2 : 0); }
+
//function to make a screenshot and save it in a default location
void screenshot();
protected:
CVideo& screen_;
const gamemap& map_;
+ int xpos_, ypos_;
theme theme_;
int zoom_;
};
@@ -167,9 +200,6 @@
//even if running behind.
void draw(bool update=true,bool force=false);
- //check if pixel x,y is outside specified area
- bool outside_area(const SDL_Rect& area, const int x, const int y) const;
-
//function to display a location as selected. If a unit is in the
location,
//and there is no unit in the currently highlighted hex, the unit will
be
//displayed in the sidebar.
@@ -183,25 +213,6 @@
//over
void highlight_hex(gamemap::location hex);
- //given x,y co-ordinates of an onscreen pixel, will return the
- //location of the hex that this pixel corresponds to. Returns an
- //invalid location is the mouse isn't over any valid location.
- const gamemap::location hex_clicked_on(int x, int y,
- gamemap::location::DIRECTION* nearest_hex=NULL,
- gamemap::location::DIRECTION* second_nearest_hex=NULL) const;
-
- //given x,y co-ordinates of a pixel on the map, will return the
- //location of the hex that this pixel corresponds to. Returns an
- //invalid location if the mouse isn't over any valid location.
- const gamemap::location pixel_position_to_hex(int x, int y,
- gamemap::location::DIRECTION* nearest_hex=NULL,
- gamemap::location::DIRECTION* second_nearest_hex=NULL) const;
-
- //given x,y co-ordinates of the mouse, will return the location of the
- //hex in the minimap that the mouse is currently over, or an invalid
- //location if the mouse isn't over the minimap.
- gamemap::location minimap_location_on(int x, int y);
-
//sets the paths that are currently displayed as available for the unit
//to move along. All other paths will be greyed out.
void highlight_reach(const paths &paths_list);
@@ -217,13 +228,6 @@
//unit. If NULL, no route is displayed.
//route does not have to remain valid after being set
void set_route(const paths::route* route);
-
- //functions to get the on-screen positions of hexes.
- // we have a 1 hex border so need to offset the loction with 1
- int get_location_x(const gamemap::location& loc) const
- { return map_area().x + (loc.x + 1) * hex_width() - xpos_; }
- int get_location_y(const gamemap::location& loc) const
- { return map_area().y + (loc.y + 1) * zoom_ - ypos_ +
(is_odd(loc.x) ? zoom_/2 : 0); }
//returns the locations of 2 hexes that bind the visible area of the
map.
void get_visible_hex_bounds(gamemap::location &topleft,
gamemap::location &bottomright) const;
@@ -442,8 +446,6 @@
bool in_game() const { return in_game_; }
void draw_bar(const std::string& image, int xpos, int ypos, size_t
height, double filled, const SDL_Color& col, fixed_t alpha);
- void get_rect_hex_bounds(SDL_Rect rect, gamemap::location &topleft,
gamemap::location &bottomright) const;
-
private:
display(const display&);
void operator=(const display&);
@@ -474,7 +476,6 @@
surface get_minimap(int w, int h);
CKey keys_;
- int xpos_, ypos_;
std::map<gamemap::location, surface> hex_overlay_;
surface selected_hex_overlay_;
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits