Skip to content

Commit

Permalink
Add interpolation to tile tools
Browse files Browse the repository at this point in the history
This uses DDA (https://w.wiki/6RSQ) to draw a line between the previous
frame's mouse position, and the current frame's mouse position. This
means that there will no longer be gaps in lines of tiles if you move
your mouse fast enough (which is actually rather slow, so it gets
annoying quickly).

The editor's timestep is no longer hardcoded to 24, as I assume that
was only done so there would be less gaps in lines of tiles drawn.
With interpolation, that is no longer an issue, so I've removed the
editor's special case for the timestep.
  • Loading branch information
AllyTally authored and InfoTeddy committed Mar 21, 2023
1 parent 6cae666 commit d152730
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
42 changes: 36 additions & 6 deletions desktop_version/src/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ void editorclass::reset(void)
direct_mode_drawer = 0;
entcol = 0;

old_tilex = 0;
old_tiley = 0;
tilex = 0;
tiley = 0;
levx = 0;
Expand Down Expand Up @@ -1838,6 +1840,31 @@ int editorclass::get_entity_at(int xp, int yp)
return -1;
}

static void set_tile_interpolated(const int x1, const int x2, const int y1, const int y2, const int tile)
{
extern editorclass ed;

// draw a line between (x1, y1) and (x2, y2)

const int dx = x2 - x1;
const int dy = y2 - y1;

const int steps = SDL_max(abs(dx), abs(dy));

if (steps == 0)
{
ed.set_tile(x1, y1, tile);
return;
}

for (int i = 0; i <= steps; i++)
{
const int x = x1 + (dx * i) / steps;
const int y = y1 + (dy * i) / steps;

ed.set_tile(x, y, tile);
}
}

void editorclass::handle_tile_placement(const int tile)
{
Expand All @@ -1848,7 +1875,7 @@ void editorclass::handle_tile_placement(const int tile)
// Vertical line
for (int i = 0; i < 30; i++)
{
set_tile(tilex, i, tile);
set_tile_interpolated(old_tilex, tilex, i, i, tile);
}
return;
}
Expand All @@ -1857,7 +1884,7 @@ void editorclass::handle_tile_placement(const int tile)
// Horizontal line
for (int i = 0; i < 40; i++)
{
set_tile(i, tiley, tile);
set_tile_interpolated(i, i, old_tiley, tiley, tile);
}
return;
}
Expand All @@ -1879,15 +1906,15 @@ void editorclass::handle_tile_placement(const int tile)
}
else
{
set_tile(tilex, tiley, tile);
set_tile_interpolated(old_tilex, tilex, old_tiley, tiley, tile);
return;
}

for (int i = -range; i <= range; i++)
{
for (int j = -range; j <= range; j++)
{
set_tile(tilex + i, tiley + j, tile);
set_tile_interpolated(old_tilex + i, tilex + i, old_tiley + j, tiley + j, tile);
}
}
}
Expand All @@ -1901,7 +1928,7 @@ void editorclass::tool_remove()
handle_tile_placement(0);
break;
case EditorTool_SPIKES:
set_tile(tilex, tiley, 0);
set_tile_interpolated(old_tilex, tilex, old_tiley, tiley, 0);
break;
default:
break;
Expand Down Expand Up @@ -2015,7 +2042,7 @@ void editorclass::tool_place()
break;
}
case EditorTool_SPIKES:
set_tile(tilex, tiley, 8);
set_tile_interpolated(old_tilex, tilex, old_tiley, tiley, 8);
break;
case EditorTool_TRINKETS:
if (cl.numtrinkets() < 100)
Expand Down Expand Up @@ -2624,6 +2651,9 @@ void editorinput(void)
return;
}

ed.old_tilex = ed.tilex;
ed.old_tiley = ed.tiley;

ed.tilex = (key.mx - (key.mx % 8)) / 8;
ed.tiley = (key.my - (key.my % 8)) / 8;

Expand Down
1 change: 1 addition & 0 deletions desktop_version/src/Editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class editorclass
std::string filename;
std::string loaded_filepath;

int old_tilex, old_tiley;
int tilex, tiley;
int keydelay, lclickdelay;
bool savekey, loadkey;
Expand Down
2 changes: 0 additions & 2 deletions desktop_version/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7365,8 +7365,6 @@ int Game::get_timestep(void)
{
switch (gamestate)
{
case EDITORMODE:
return 24;
case GAMEMODE:
return get_framerate(slowdown);
default:
Expand Down

0 comments on commit d152730

Please sign in to comment.