Skip to content

Commit

Permalink
editor: Use texture for mouseover hex overlay, not surface
Browse files Browse the repository at this point in the history
Uses IPF to construct the overlay textures, where necessary.
  • Loading branch information
mesilliac committed Jun 24, 2022
1 parent f290871 commit 6521878
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 152 deletions.
15 changes: 9 additions & 6 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ display::display(const display_context* dc,
, menu_buttons_()
, action_buttons_()
, invalidated_()
, mouseover_hex_overlay_(nullptr)
, mouseover_hex_overlay_()
, tod_hex_mask1(nullptr)
, tod_hex_mask2(nullptr)
, fog_images_()
Expand Down Expand Up @@ -2630,11 +2630,14 @@ void display::draw_hex(const map_location& loc)
}

// Paint mouseover overlays
if(loc == mouseoverHex_ && (on_map || (in_editor() && get_map().on_board_with_border(loc)))
&& !map_screenshot_
&& mouseover_hex_overlay_ != nullptr) {
// TODO: highdpi - texture, yes this is terrible
drawing_buffer_add(LAYER_MOUSEOVER_OVERLAY, loc, dest, texture(mouseover_hex_overlay_));
if(loc == mouseoverHex_
&& (on_map || (in_editor() && get_map().on_board_with_border(loc)))
&& !map_screenshot_
&& bool(mouseover_hex_overlay_))
{
const uint8_t alpha = 196;
drawing_buffer_add(LAYER_MOUSEOVER_OVERLAY, loc, dest,
mouseover_hex_overlay_, SDL_Rect(), false, false, alpha);
}

// Paint arrows
Expand Down
11 changes: 5 additions & 6 deletions src/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,14 @@ class display : public video2::draw_layering
void reset_standing_animations();

/**
* mouseover_hex_overlay_ require a prerendered surface
* mouseover_hex_overlay_ requires a prerendered texture
* and is drawn underneath the mouse's location
*/
void set_mouseover_hex_overlay(const surface& image)
void set_mouseover_hex_overlay(const texture& image)
{ mouseover_hex_overlay_ = image; }

void clear_mouseover_hex_overlay()
{ mouseover_hex_overlay_ = nullptr; }
{ mouseover_hex_overlay_.reset(); }

/** Toggle to continuously redraw the screen. */
static void toggle_benchmark();
Expand Down Expand Up @@ -757,11 +757,10 @@ class display : public video2::draw_layering
std::map<std::string, config> reports_;
std::vector<std::shared_ptr<gui::button>> menu_buttons_, action_buttons_;
std::set<map_location> invalidated_;
// TODO: highdpi - texture
surface mouseover_hex_overlay_;
texture mouseover_hex_overlay_;
// If we're transitioning from one time of day to the next,
// then we will use these two masks on top of all hexes when we blit.
surface tod_hex_mask1, tod_hex_mask2;
surface tod_hex_mask1, tod_hex_mask2; // TODO: highdpi - texture
std::vector<std::string> fog_images_;
std::vector<std::string> shroud_images_;

Expand Down
106 changes: 39 additions & 67 deletions src/editor/action/mouse/mouse_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ std::unique_ptr<editor_action> mouse_action::key_event(

void mouse_action::set_mouse_overlay(editor_display& disp)
{
disp.set_mouseover_hex_overlay(nullptr);
disp.clear_mouseover_hex_overlay();
}

bool mouse_action::has_alt_modifier() const
Expand All @@ -142,58 +142,44 @@ bool mouse_action::has_ctrl_modifier() const
#endif
}

void mouse_action::set_terrain_mouse_overlay(editor_display& disp, const t_translation::terrain_code & fg,
const t_translation::terrain_code & bg)
void mouse_action::set_terrain_mouse_overlay(
editor_display& disp,
const t_translation::terrain_code & fg,
const t_translation::terrain_code & bg)
{
surface image_fg(image::get_image(disp.get_map().get_terrain_info(fg).editor_image()));
surface image_bg(image::get_image(disp.get_map().get_terrain_info(bg).editor_image()));
const std::string fg_path = disp.get_map().get_terrain_info(fg).editor_image();
const std::string bg_path = disp.get_map().get_terrain_info(bg).editor_image();
const std::string blank_hex = "misc/blank-hex.png";

if (image_fg == nullptr || image_bg == nullptr) {
if (!image::exists(fg_path) || !image::exists(bg_path)) {
ERR_ED << "Missing terrain icon" << std::endl;
disp.set_mouseover_hex_overlay(nullptr);
disp.clear_mouseover_hex_overlay();
return;
}

// Create a transparent surface of the right size.
surface image(image_fg->w, image_fg->h);

// For efficiency the size of the tile is cached.
// We assume all tiles are of the same size.
// The zoom factor can change, so it's not cached.
// NOTE: when zooming and not moving the mouse, there are glitches.
// Since the optimal alpha factor is unknown, it has to be calculated
// on the fly, and caching the surfaces makes no sense yet.
static const int32_t alpha = 196;
static const int size = image_fg->w;
static const int size = image::get_size(blank_hex).x;
static const int half_size = size / 2;
static const int quarter_size = size / 4;
static const int offset = 2;
static const int new_size = half_size - 2;

// Blit left side
image_fg = scale_surface(image_fg, new_size, new_size);
SDL_Rect rcDestLeft {offset, quarter_size, 0, 0};
sdl_blit( image_fg, nullptr, image, &rcDestLeft );

// Blit right side
image_bg = scale_surface(image_bg, new_size, new_size);
SDL_Rect rcDestRight {half_size, quarter_size, 0, 0};
sdl_blit( image_bg, nullptr, image, &rcDestRight );

//apply mask so the overlay is contained within the mouseover hex
image = mask_surface(image, image::get_hexmask());

// Add the alpha factor
adjust_surface_alpha(image, alpha);
// Inserting scaled FG and BG hexes via IPF.
std::stringstream path;
// "hex~BLIT(fg~SCALE(ns,ns),2,qs)~BLIT(bg~SCALE(ns,ns),hs,qs)"
path << blank_hex
<< "~BLIT(" << fg_path << "~SCALE(" << new_size << "," << new_size
<< ")," << offset << "," << quarter_size << ")"
<< "~BLIT(" << bg_path << "~SCALE(" << new_size << "," << new_size
<< ")," << half_size << "," << quarter_size << ")";

// scale the image
const unsigned int zoom = disp.hex_size();
if (zoom != game_config::tile_size) {
image = scale_surface(image, zoom, zoom);
}

// Set as mouseover
disp.set_mouseover_hex_overlay(image);
// Set as mouseover overlay.
disp.set_mouseover_hex_overlay(image::get_texture(path.str()));
}

std::set<map_location> brush_drag_mouse_action::affected_hexes(
Expand Down Expand Up @@ -338,22 +324,15 @@ std::unique_ptr<editor_action> mouse_action_paste::click_right(editor_display& /

void mouse_action_paste::set_mouse_overlay(editor_display& disp)
{
surface image60 = image::get_image("icons/action/editor-paste_60.png");

//TODO avoid hardcoded hex field size
surface image(72,72);

SDL_Rect r {6, 6, 0, 0};
sdl_blit(image60, nullptr, image, &r);

uint8_t alpha = 196;
int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());

// Add the alpha factor and scale the image
adjust_surface_alpha(image, alpha);
image = scale_surface(image, zoom, zoom);
disp.set_mouseover_hex_overlay(image);
disp.set_mouseover_hex_overlay(
image::get_texture(
// center 60px icon on blank hex template
image::locator(
"misc/blank-hex.png",
"~BLIT(icons/action/editor-paste_60.png,6,6)"
)
)
);
}

std::set<map_location> mouse_action_fill::affected_hexes(
Expand Down Expand Up @@ -454,22 +433,15 @@ std::unique_ptr<editor_action> mouse_action_starting_position::click_right(edito

void mouse_action_starting_position::set_mouse_overlay(editor_display& disp)
{
surface image60 = image::get_image("icons/action/editor-tool-starting-position_60.png");

//TODO avoid hardcoded hex field size
surface image(72,72);

SDL_Rect r {6, 6, 0, 0};
sdl_blit(image60, nullptr, image, &r);

uint8_t alpha = 196;
int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());

// Add the alpha factor and scale the image
adjust_surface_alpha(image, alpha);
image = scale_surface(image, zoom, zoom);
disp.set_mouseover_hex_overlay(image);
disp.set_mouseover_hex_overlay(
image::get_texture(
// center 60px icon on blank hex template
image::locator(
"misc/blank-hex.png",
"~BLIT(icons/action/editor-tool-starting-position_60.png,6,6)"
)
)
);
}


Expand Down
3 changes: 2 additions & 1 deletion src/editor/action/mouse/mouse_action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ class mouse_action
/**
* Helper function for derived classes that need a active-terrain mouse overlay
*/
void set_terrain_mouse_overlay(editor_display& disp, const t_translation::terrain_code & fg,
void set_terrain_mouse_overlay(editor_display& disp,
const t_translation::terrain_code & fg,
const t_translation::terrain_code & bg);

/**
Expand Down
17 changes: 1 addition & 16 deletions src/editor/action/mouse/mouse_action_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,7 @@ void mouse_action_item::set_mouse_overlay(editor_display& disp)

void mouse_action_item::set_item_mouse_overlay(editor_display& disp, const overlay& u)
{

std::stringstream filename;
filename << u.image; // << "~RC(" << u.flag_rgb() << '>'
// << team::get_side_color_id(disp.viewing_side()) << ')';

surface image(image::get_image(filename.str()));
uint8_t alpha = 196;
//TODO don't hardcode
int size = 72;
//int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());

// Add the alpha factor and scale the image
adjust_surface_alpha(image, alpha);
image = scale_surface(image, zoom, zoom);
disp.set_mouseover_hex_overlay(image);
disp.set_mouseover_hex_overlay(image::get_texture(u.image));
}


Expand Down
25 changes: 9 additions & 16 deletions src/editor/action/mouse/mouse_action_map_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,15 @@ std::unique_ptr<editor_action> mouse_action_map_label::drag_end_left(editor_disp

void mouse_action_map_label::set_mouse_overlay(editor_display& disp)
{
surface image60 = image::get_image("icons/action/editor-tool-label_60.png");

//TODO avoid hardcoded hex field size
surface image(72,72);

SDL_Rect r {6, 6, 0, 0};
sdl_blit(image60, nullptr, image, &r);

uint8_t alpha = 196;
int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());

// Add the alpha factor and scale the image
adjust_surface_alpha(image, alpha);
image = scale_surface(image, zoom, zoom);
disp.set_mouseover_hex_overlay(image);
disp.set_mouseover_hex_overlay(
image::get_texture(
// center 60px icon on blank hex template
image::locator(
"misc/blank-hex.png",
"~BLIT(icons/action/editor-tool-label_60.png,6,6)"
)
)
);
}


Expand Down
15 changes: 4 additions & 11 deletions src/editor/action/mouse/mouse_action_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,13 @@ std::unique_ptr<editor_action> mouse_action_select::click_right(editor_display&

void mouse_action_select::set_mouse_overlay(editor_display& disp)
{
surface image;
texture tex;
if (has_shift_modifier()) {
image = image::get_image("editor/tool-overlay-select-wand.png");
tex = image::get_texture("editor/tool-overlay-select-wand.png");
} else {
image = image::get_image("editor/tool-overlay-select-brush.png");
tex = image::get_texture("editor/tool-overlay-select-brush.png");
}
uint8_t alpha = 196;
int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());

// Add the alpha factor and scale the image
adjust_surface_alpha(image, alpha);
image = scale_surface(image, zoom, zoom);
disp.set_mouseover_hex_overlay(image);
disp.set_mouseover_hex_overlay(tex);
}


Expand Down
15 changes: 2 additions & 13 deletions src/editor/action/mouse/mouse_action_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void mouse_action_unit::move(editor_display& disp, const map_location& hex)
const unit_map::const_unit_iterator unit_it = units.find(hex);
if (unit_it != units.end()) {

disp.set_mouseover_hex_overlay(nullptr);
disp.clear_mouseover_hex_overlay();

SDL_Rect rect;
rect.x = disp.get_location_x(hex);
Expand Down Expand Up @@ -152,22 +152,11 @@ void mouse_action_unit::set_mouse_overlay(editor_display& disp)

void mouse_action_unit::set_unit_mouse_overlay(editor_display& disp, const unit_type& u)
{

std::stringstream filename;
filename << u.image() << "~RC(" << u.flag_rgb() << '>'
<< team::get_side_color_id(disp.viewing_side()) << ')';

surface image(image::get_image(filename.str()));
uint8_t alpha = 196;
//TODO don't hardcode
int size = 72;
//int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());

// Add the alpha factor and scale the image
adjust_surface_alpha(image, alpha);
image = scale_surface(image, zoom, zoom);
disp.set_mouseover_hex_overlay(image);
disp.set_mouseover_hex_overlay(image::get_texture(filename.str()));
}


Expand Down
25 changes: 9 additions & 16 deletions src/editor/action/mouse/mouse_action_village.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,15 @@ std::unique_ptr<editor_action> mouse_action_village::up_right(editor_display& di

void mouse_action_village::set_mouse_overlay(editor_display& disp)
{
surface image60 = image::get_image("icons/action/editor-tool-village_60.png");

//TODO avoid hardcoded hex field size
surface image(72,72);

SDL_Rect r {6, 6, 0, 0};
sdl_blit(image60, nullptr, image, &r);

uint8_t alpha = 196;
int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());

// Add the alpha factor and scale the image
adjust_surface_alpha(image, alpha);
image = scale_surface(image, zoom, zoom);
disp.set_mouseover_hex_overlay(image);
disp.set_mouseover_hex_overlay(
image::get_texture(
// center 60px icon on blank hex template
image::locator(
"misc/blank-hex.png",
"~BLIT(icons/action/editor-tool-village_60.png,6,6)"
)
)
);
}


Expand Down

0 comments on commit 6521878

Please sign in to comment.