Skip to content

Commit

Permalink
Prevent possible mouse-related crashes in the world editor
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Nov 23, 2023
1 parent 71e9423 commit 566335d
Showing 1 changed file with 46 additions and 34 deletions.
80 changes: 46 additions & 34 deletions src/worldeditor/worldeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

#define SMW_EDITOR

#ifdef _MSC_VER
#define NOMINMAX
#endif

#include "CmdArgs.h"
#include "FileIO.h"
#include "FileList.h"
Expand Down Expand Up @@ -111,6 +115,14 @@ void update_mouse_coords() {
if (mouse_y > 480 - 1) mouse_y = 480 - 1;
}

int bound_to_window_w(int x) {
return std::max(0, std::min(x, 640));
}

int bound_to_window_h(int y) {
return std::max(0, std::min(y, 480));
}

//Vehicle structure that holds the current vehicle "stamp"
WorldVehicle g_wvVehicleStamp;

Expand Down Expand Up @@ -1323,8 +1335,8 @@ int editor_edit()
}

case SDL_MOUSEBUTTONDOWN: {
short iButtonX = event.button.x - draw_offset_x;
short iButtonY = event.button.y - draw_offset_y;
short iButtonX = bound_to_window_w(event.button.x) - draw_offset_x;
short iButtonY = bound_to_window_h(event.button.y) - draw_offset_y;
short iCol = iButtonX / TILESIZE + draw_offset_col;
short iRow = iButtonY / TILESIZE + draw_offset_row;

Expand Down Expand Up @@ -1502,8 +1514,8 @@ int editor_edit()
//Painting tiles with mouse movement
case SDL_MOUSEMOTION: {
update_mouse_coords();
short iButtonX = event.button.x - draw_offset_x;
short iButtonY = event.button.y - draw_offset_y;
short iButtonX = bound_to_window_w(event.motion.x) - draw_offset_x;
short iButtonY = bound_to_window_h(event.motion.y) - draw_offset_y;
short iCol = (iButtonX >> 5) + draw_offset_col;
short iRow = (iButtonY >> 5) + draw_offset_row;

Expand Down Expand Up @@ -2510,8 +2522,8 @@ int editor_warp()
}

case SDL_MOUSEBUTTONDOWN: {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

if (event.button.button == SDL_BUTTON_LEFT) {
if (iButtonX >= 0 && iButtonX <= 9 && iButtonY == 0) {
Expand Down Expand Up @@ -2607,8 +2619,8 @@ int editor_start_items()
}

case SDL_MOUSEBUTTONDOWN: {
short iButtonX = event.button.x;
short iButtonY = event.button.y;
short iButtonX = bound_to_window_w(event.button.x);
short iButtonY = bound_to_window_h(event.button.y);

if (event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT) {
if (g_worldmap.iNumInitialBonuses < 32) {
Expand Down Expand Up @@ -2706,8 +2718,8 @@ int editor_boundary()
}

case SDL_MOUSEBUTTONDOWN: {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

if (event.button.button == SDL_BUTTON_LEFT) {
if (iButtonX >= 0 && iButtonX <= 9 && iButtonY >= 0 && iButtonY <= 9) {
Expand Down Expand Up @@ -2787,8 +2799,8 @@ int editor_type()

case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

//Start and doors
if (iButtonX >= 0 && iButtonX <= 5 && iButtonY == 0) {
Expand Down Expand Up @@ -2859,8 +2871,8 @@ int editor_water()

case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

if (iButtonY == 0) {
if (iButtonX >= 0 && iButtonX <= 2) {
Expand Down Expand Up @@ -2935,8 +2947,8 @@ int editor_background()

case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

short iTileStyleOffset = ((iButtonX / 4) + (iPage * 5)) * WORLD_BACKGROUND_SPRITE_SET_SIZE;

Expand Down Expand Up @@ -3029,8 +3041,8 @@ int editor_stageforeground()

case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

if (iButtonX >= 0 && iButtonX < 10) {
if (iButtonY >= 0 && iButtonY < 10) {
Expand Down Expand Up @@ -3100,8 +3112,8 @@ int editor_bridges()

case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

if (iButtonX >= 0 && iButtonX < 4) {
if (iButtonY >= 0 && iButtonY < 1) {
Expand Down Expand Up @@ -3165,8 +3177,8 @@ int editor_structureforeground()

case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

if (iButtonY >= 0 && iButtonY < 15) {
if (iButtonX >= 0 && iButtonX < 12) {
Expand Down Expand Up @@ -3235,8 +3247,8 @@ int editor_pathsprite()

case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

if (iButtonX >= 0 && iButtonX < 8) {
if (iButtonY >= 0 && iButtonY < 6)
Expand Down Expand Up @@ -3332,8 +3344,8 @@ int editor_vehicles()
}

case SDL_MOUSEBUTTONDOWN: {
short iButtonX = event.button.x;
short iButtonY = event.button.y;
short iButtonX = bound_to_window_w(event.button.x);
short iButtonY = bound_to_window_h(event.button.y);

if (event.button.button == SDL_BUTTON_LEFT) {
code = mCurrentMenu->MouseClick(iButtonX, iButtonY);
Expand Down Expand Up @@ -3425,8 +3437,8 @@ int editor_path()

case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
short iButtonX = event.button.x / TILESIZE;
short iButtonY = event.button.y / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

if (iButtonX >= 0 && iButtonX <= 15 && iButtonY == 0) {
set_tile = iButtonX + 1;
Expand Down Expand Up @@ -3962,10 +3974,10 @@ int editor_stage()
}

case SDL_MOUSEBUTTONDOWN: {
short iTileX = event.button.x / TILESIZE;
short iTileY = event.button.y / TILESIZE;
short iButtonX = event.button.x;
short iButtonY = event.button.y;
short iTileX = bound_to_window_w(event.button.x) / TILESIZE;
short iTileY = bound_to_window_h(event.button.y) / TILESIZE;
short iButtonX = bound_to_window_w(event.button.x);
short iButtonY = bound_to_window_h(event.button.y);

if (event.button.button == SDL_BUTTON_LEFT) {
//Stages
Expand Down Expand Up @@ -4036,8 +4048,8 @@ int editor_stage()
if (iEditStage == -1) {
// if (event.button.x >= 0 && event.button.y >= 0)
{
short iMouseX = event.button.x / TILESIZE;
short iMouseY = event.button.y / TILESIZE;
short iMouseX = bound_to_window_w(event.button.x) / TILESIZE;
short iMouseY = bound_to_window_h(event.button.y) / TILESIZE;

if (iMouseX >= 0 && iMouseX < g_worldmap.iNumStages - (iMouseY * 20) && iMouseY >= 0 && iMouseY <= (g_worldmap.iNumStages - 1) / 20) {
iStageDisplay = iMouseX + (iMouseY * 20);
Expand Down

0 comments on commit 566335d

Please sign in to comment.