Skip to content

Commit

Permalink
Merge pull request #32732 from jbytheway/point_in_graphics_code
Browse files Browse the repository at this point in the history
Use point more in graphics code
  • Loading branch information
kevingranade authored Jul 29, 2019
2 parents 550567b + 671a232 commit 49c3d5e
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 137 deletions.
4 changes: 1 addition & 3 deletions src/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ tripoint relative_view_pos( const player &u, const tripoint &p ) noexcept
// Convert p to screen position relative to the current terrain view
tripoint relative_view_pos( const game &g, const tripoint &p ) noexcept
{
return { POSX + p.x - g.ter_view_x,
POSY + p.y - g.ter_view_y,
p.z - g.ter_view_z };
return p - g.ter_view_p + point( POSX, POSY );
}

void draw_explosion_curses( game &g, const tripoint &center, const int r, const nc_color &col )
Expand Down
95 changes: 40 additions & 55 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,6 @@ cata_tiles::cata_tiles( const SDL_Renderer_Ptr &renderer ) :

nv_goggles_activated = false;

last_pos_x = 0;
last_pos_y = 0;

on_options_changed();
}

Expand Down Expand Up @@ -629,8 +626,8 @@ void tileset_loader::load_internal( JsonObject &config, const std::string &tiles
sprite_width = tile_part_def.get_int( "sprite_width", ts.tile_width );
sprite_height = tile_part_def.get_int( "sprite_height", ts.tile_height );
// Now load the tile definitions for the loaded tileset image.
sprite_offset_x = tile_part_def.get_int( "sprite_offset_x", 0 );
sprite_offset_y = tile_part_def.get_int( "sprite_offset_y", 0 );
sprite_offset.x = tile_part_def.get_int( "sprite_offset_x", 0 );
sprite_offset.y = tile_part_def.get_int( "sprite_offset_y", 0 );
// First load the tileset image to get the number of available tiles.
dbg( D_INFO ) << "Attempting to Load Tileset file " << tileset_image_path;
load_tileset( tileset_image_path );
Expand All @@ -645,8 +642,7 @@ void tileset_loader::load_internal( JsonObject &config, const std::string &tiles
} else {
sprite_width = ts.tile_width;
sprite_height = ts.tile_height;
sprite_offset_x = 0;
sprite_offset_y = 0;
sprite_offset = point_zero;
R = -1;
G = -1;
B = -1;
Expand Down Expand Up @@ -768,8 +764,7 @@ void tileset_loader::load_ascii_set( JsonObject &entry )
}
const std::string id = get_ascii_tile_id( ascii_char, FG, -1 );
tile_type curr_tile;
curr_tile.offset.x = sprite_offset_x;
curr_tile.offset.y = sprite_offset_y;
curr_tile.offset = sprite_offset;
auto &sprites = *( curr_tile.fg.add( std::vector<int>( {index_in_image + offset} ), 1 ) );
switch( ascii_char ) {
// box bottom/top side (horizontal line)
Expand Down Expand Up @@ -849,8 +844,7 @@ void tileset_loader::load_tilejson_from_file( JsonObject &config )
}
for( const std::string &t_id : ids ) {
tile_type &curr_tile = load_tile( entry, t_id );
curr_tile.offset.x = sprite_offset_x;
curr_tile.offset.y = sprite_offset_y;
curr_tile.offset = sprite_offset;
bool t_multi = entry.get_bool( "multitile", false );
bool t_rota = entry.get_bool( "rotates", t_multi );
int t_h3d = entry.get_int( "height_3d", 0 );
Expand All @@ -862,8 +856,7 @@ void tileset_loader::load_tilejson_from_file( JsonObject &config )
const std::string s_id = subentry.get_string( "id" );
const std::string m_id = t_id + "_" + s_id;
tile_type &curr_subtile = load_tile( subentry, m_id );
curr_subtile.offset.x = sprite_offset_x;
curr_subtile.offset.y = sprite_offset_y;
curr_subtile.offset = sprite_offset;
curr_subtile.rotates = true;
curr_subtile.height_3d = t_h3d;
curr_tile.available_subtiles.push_back( s_id );
Expand Down Expand Up @@ -995,8 +988,7 @@ void cata_tiles::draw( int destx, int desty, const tripoint &center, int width,
render_fill_rect( renderer, clipRect, 0, 0, 0 );
}

int posx = center.x;
int posy = center.y;
const point pos = center.xy();

int sx = 0;
int sy = 0;
Expand All @@ -1008,11 +1000,9 @@ void cata_tiles::draw( int destx, int desty, const tripoint &center, int width,

const bool iso_mode = tile_iso;

o_x = iso_mode ? posx : posx - POSX;
o_y = iso_mode ? posy : posy - POSY;
o = iso_mode ? pos : pos - point( POSX, POSY );

op_x = destx;
op_y = desty;
op = point( destx, desty );
// Rounding up to include incomplete tiles at the bottom/right edges
screentile_width = divide_round_up( width, tile_width );
screentile_height = divide_round_up( height, tile_height );
Expand Down Expand Up @@ -1062,14 +1052,14 @@ void cata_tiles::draw( int destx, int desty, const tripoint &center, int width,
if( iso_mode ) {
//in isometric, rows and columns represent a checkerboard screen space, and we place
//the appropriate tile in valid squares by getting position relative to the screen center.
if( ( row + o_y ) % 2 != ( col + o_x ) % 2 ) {
if( ( row + o.y ) % 2 != ( col + o.x ) % 2 ) {
continue;
}
x = ( col - row - sx / 2 + sy / 2 ) / 2 + o_x;
y = ( row + col - sy / 2 - sx / 2 ) / 2 + o_y;
x = ( col - row - sx / 2 + sy / 2 ) / 2 + o.x;
y = ( row + col - sy / 2 - sx / 2 ) / 2 + o.y;
} else {
x = col + o_x;
y = row + o_y;
x = col + o.x;
y = row + o.y;
}
if( y < min_visible_y || y > max_visible_y || x < min_visible_x || x > max_visible_x ) {
int height_3d = 0;
Expand Down Expand Up @@ -1176,12 +1166,12 @@ void cata_tiles::draw( int destx, int desty, const tripoint &center, int width,
//Memorize everything the character just saw even if it wasn't displayed.
for( int mem_y = 0; mem_y < MAPSIZE_Y; mem_y++ ) {
for( int mem_x = 0; mem_x < MAPSIZE_X; mem_x++ ) {
//just finished o_x,o_y through sx+o_x,sy+o_y so skip them
if( mem_x >= o_x && mem_x < sx + o_x &&
mem_y >= o_y && mem_y < sy + o_y ) {
tripoint p( mem_x, mem_y, center.z );
//just finished o through s+o so skip them
rectangle skip_region( o, o + point( sx, sy ) );
if( skip_region.contains_half_open( p.xy() ) ) {
continue;
}
tripoint p( mem_x, mem_y, center.z );
int height_3d = 0;
if( iso_mode ) {
//Iso_mode skips in a checkerboard
Expand All @@ -1193,8 +1183,8 @@ void cata_tiles::draw( int destx, int desty, const tripoint &center, int width,
p.x = ( mem_x - mem_y - MAPSIZE_X / 2 + MAPSIZE_Y / 2 ) / 2 + MAPSIZE_X / 2;
p.y = ( mem_y + mem_x - MAPSIZE_Y / 2 - MAPSIZE_X / 2 ) / 2 + MAPSIZE_Y / 2;
//Check if we're in previously done iso_mode space
if( p.x >= ( 0 - sy - sx / 2 + sy / 2 ) / 2 + o_x && p.x < ( sx - 0 - sx / 2 + sy / 2 ) / 2 + o_x &&
p.y >= ( 0 + 0 - sy / 2 - sx / 2 ) / 2 + o_y && p.y < ( sy + sx - sy / 2 - sx / 2 ) / 2 + o_y ) {
if( p.x >= ( 0 - sy - sx / 2 + sy / 2 ) / 2 + o.x && p.x < ( sx - 0 - sx / 2 + sy / 2 ) / 2 + o.x &&
p.y >= ( 0 + 0 - sy / 2 - sx / 2 ) / 2 + o.y && p.y < ( sy + sx - sy / 2 - sx / 2 ) / 2 + o.y ) {
continue;
}
}
Expand Down Expand Up @@ -1257,8 +1247,8 @@ void cata_tiles::draw( int destx, int desty, const tripoint &center, int width,
} else if( g->u.view_offset != tripoint_zero && !g->u.in_vehicle ) {
// check to see if player is located at ter
draw_from_id_string( "cursor", C_NONE, empty_string,
{g->ter_view_x, g->ter_view_y, center.z}, 0, 0, LL_LIT,
false );
tripoint( g->ter_view_p.xy(), center.z ), 0, 0, LL_LIT,
false );
}
if( g->u.controlling_vehicle ) {
if( cata::optional<tripoint> indicator_offset = g->get_veh_dir_indicator_location( true ) ) {
Expand Down Expand Up @@ -1440,9 +1430,9 @@ bool cata_tiles::draw_from_id_string( std::string id, TILE_CATEGORY category,
// check to make sure that we are drawing within a valid area
// [0->width|height / tile_width|height]

rectangle screen_bounds( o, o + point( screentile_width, screentile_height ) );
if( !tile_iso &&
( pos.x - o_x < 0 || pos.x - o_x >= screentile_width ||
pos.y - o_y < 0 || pos.y - o_y >= screentile_height ) ) {
!screen_bounds.contains_half_open( pos.xy() ) ) {
return false;
}

Expand Down Expand Up @@ -1929,16 +1919,16 @@ bool cata_tiles::draw_terrain_below( const tripoint &p, lit_level /*ll*/, int &/
int screen_x = 0;
int screen_y = 0;
if( tile_iso ) {
screen_x = ( ( pbelow.x - o_x ) - ( o_y - pbelow.y ) + screentile_width - 2 ) * tile_width / 2 +
op_x;
screen_x = ( ( pbelow.x - o.x ) - ( o.y - pbelow.y ) + screentile_width - 2 ) * tile_width / 2 +
op.x;
// y uses tile_width because width is definitive for iso tiles
// tile footprints are half as tall as wide, arbitrarily tall
screen_y = ( ( pbelow.y - o_y ) - ( pbelow.x - o_x ) - 4 ) * tile_width / 4 +
screen_y = ( ( pbelow.y - o.y ) - ( pbelow.x - o.x ) - 4 ) * tile_width / 4 +
screentile_height * tile_height / 2 + // TODO: more obvious centering math
op_y;
op.y;
} else {
screen_x = ( pbelow.x - o_x ) * tile_width + op_x;
screen_y = ( pbelow.y - o_y ) * tile_height + op_y;
screen_x = ( pbelow.x - o.x ) * tile_width + op.x;
screen_y = ( pbelow.y - o.y ) * tile_height + op.y;
}
belowRect.x = screen_x + ( tile_width - belowRect.w ) / 2;
belowRect.y = screen_y + ( tile_height - belowRect.h ) / 2;
Expand Down Expand Up @@ -2654,17 +2644,12 @@ void cata_tiles::draw_weather_frame()

for( auto &vdrop : anim_weather.vdrops ) {
// TODO: Z-level awareness if weather ever happens on anything but z-level 0.
int x = 0;
int y = 0;
if( tile_iso ) {
x = vdrop.first;
y = vdrop.second;
} else {
tripoint p( vdrop.first, vdrop.second, 0 );
if( !tile_iso ) {
// currently in ASCII screen coordinates
x = vdrop.first + o_x;
y = vdrop.second + o_y;
p += o;
}
draw_from_id_string( weather_name, C_WEATHER, empty_string, {x, y, 0}, 0, 0,
draw_from_id_string( weather_name, C_WEATHER, empty_string, p, 0, 0,
LL_LIT, nv_goggles_activated );
}
}
Expand Down Expand Up @@ -2902,16 +2887,16 @@ point cata_tiles::player_to_screen( const int x, const int y ) const
int screen_x = 0;
int screen_y = 0;
if( tile_iso ) {
screen_x = ( ( x - o_x ) - ( o_y - y ) + screentile_width - 2 ) * tile_width / 2 +
op_x;
screen_x = ( ( x - o.x ) - ( o.y - y ) + screentile_width - 2 ) * tile_width / 2 +
op.x;
// y uses tile_width because width is definitive for iso tiles
// tile footprints are half as tall as wide, arbitrarily tall
screen_y = ( ( y - o_y ) - ( x - o_x ) - 4 ) * tile_width / 4 +
screen_y = ( ( y - o.y ) - ( x - o.x ) - 4 ) * tile_width / 4 +
screentile_height * tile_height / 2 + // TODO: more obvious centering math
op_y;
op.y;
} else {
screen_x = ( x - o_x ) * tile_width + op_x;
screen_y = ( y - o_y ) * tile_height + op_y;
screen_x = ( x - o.x ) * tile_width + op.x;
screen_y = ( y - o.y ) * tile_height + op.y;
}
return {screen_x, screen_y};
}
Expand Down
11 changes: 3 additions & 8 deletions src/cata_tiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ class tileset_loader
tileset &ts;
const SDL_Renderer_Ptr &renderer;

int sprite_offset_x;
int sprite_offset_y;
point sprite_offset;

int sprite_width;
int sprite_height;
Expand Down Expand Up @@ -481,15 +480,11 @@ class cata_tiles
tripoint zone_offset;

// offset values, in tile coordinates, not pixels
int o_x = 0;
int o_y = 0;
point o;
// offset for drawing, in pixels.
int op_x = 0;
int op_y = 0;
point op;

private:
int last_pos_x = 0;
int last_pos_y = 0;
/**
* Tracks active night vision goggle status for each draw call.
* Allows usage of night vision tilesets during sprite rendering.
Expand Down
18 changes: 8 additions & 10 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2199,14 +2199,14 @@ tripoint game::mouse_edge_scrolling( input_context ctxt, const int speed )
if( event.type == CATA_INPUT_MOUSE ) {
const int threshold_x = projected_window_width() / 100;
const int threshold_y = projected_window_height() / 100;
if( event.mouse_x <= threshold_x ) {
if( event.mouse_pos.x <= threshold_x ) {
ret.x -= speed;
} else if( event.mouse_x >= projected_window_width() - threshold_x ) {
} else if( event.mouse_pos.x >= projected_window_width() - threshold_x ) {
ret.x += speed;
}
if( event.mouse_y <= threshold_y ) {
if( event.mouse_pos.y <= threshold_y ) {
ret.y -= speed;
} else if( event.mouse_y >= projected_window_height() - threshold_y ) {
} else if( event.mouse_pos.y >= projected_window_height() - threshold_y ) {
ret.y += speed;
}
last_mouse_edge_scroll_vector = ret;
Expand Down Expand Up @@ -3187,9 +3187,9 @@ void game::draw()
}

//temporary fix for updating visibility for minimap
ter_view_z = ( u.pos() + u.view_offset ).z;
m.build_map_cache( ter_view_z );
m.update_visibility_cache( ter_view_z );
ter_view_p.z = ( u.pos() + u.view_offset ).z;
m.build_map_cache( ter_view_p.z );
m.update_visibility_cache( ter_view_p.z );

werase( w_terrain );
draw_ter();
Expand Down Expand Up @@ -3313,9 +3313,7 @@ void game::draw_ter( const bool draw_sounds )

void game::draw_ter( const tripoint &center, const bool looking, const bool draw_sounds )
{
ter_view_x = center.x;
ter_view_y = center.y;
ter_view_z = center.z;
ter_view_p = center;
const int posx = center.x;
const int posy = center.y;

Expand Down
4 changes: 1 addition & 3 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -913,9 +913,7 @@ class game
std::vector<monster> coming_to_stairs;
int monstairz;

int ter_view_x;
int ter_view_y;
int ter_view_z;
tripoint ter_view_p;
catacurses::window w_terrain;
catacurses::window w_overmap;
catacurses::window w_omlegend;
Expand Down
28 changes: 10 additions & 18 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,7 @@ const std::string &input_context::handle_input( const int timeout )
}

coordinate_input_received = true;
coordinate_x = next_action.mouse_x;
coordinate_y = next_action.mouse_y;
coordinate = next_action.mouse_pos;
} else {
coordinate_input_received = false;
}
Expand Down Expand Up @@ -1262,28 +1261,21 @@ cata::optional<tripoint> input_context::get_coordinates( const catacurses::windo
if( !coordinate_input_received ) {
return cata::nullopt;
}
int view_columns = getmaxx( capture_win );
int view_rows = getmaxy( capture_win );
int win_left = getbegx( capture_win ) - VIEW_OFFSET_X;
int win_right = win_left + view_columns - 1;
int win_top = getbegy( capture_win ) - VIEW_OFFSET_Y;
int win_bottom = win_top + view_rows - 1;
if( coordinate_x < win_left || coordinate_x > win_right || coordinate_y < win_top ||
coordinate_y > win_bottom ) {
const point view_size( getmaxx( capture_win ), getmaxy( capture_win ) );
const point win_min( getbegx( capture_win ) - VIEW_OFFSET_X,
getbegy( capture_win ) - VIEW_OFFSET_Y );
const rectangle win_bounds( win_min, win_min + view_size );
if( !win_bounds.contains_half_open( coordinate ) ) {
return cata::nullopt;
}

int view_offset_x = 0;
int view_offset_y = 0;
point view_offset;
if( capture_win == g->w_terrain ) {
view_offset_x = g->ter_view_x;
view_offset_y = g->ter_view_y;
view_offset = g->ter_view_p.xy();
}

const int x = view_offset_x - ( ( view_columns / 2 ) - coordinate_x );
const int y = view_offset_y - ( ( view_rows / 2 ) - coordinate_y );

return tripoint( x, y, g->get_levz() );
const point p = view_offset - ( view_size / 2 - coordinate );
return tripoint( p, g->get_levz() );
}
#endif

Expand Down
Loading

0 comments on commit 49c3d5e

Please sign in to comment.