Changeset - r26752:adf33386687c
[Not reviewed]
master
0 16 0
Rubidium - 23 months ago 2023-01-06 22:24:38
rubidium@openttd.org
Codechange: introduce GetMainWindow() to properly account for nullptr checks

Some nullptr checks have been removed as they were not triggered with nullptr
with the null video driver and in dedicated server mode.
16 files changed with 47 insertions and 40 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -295,7 +295,7 @@ DEF_CONSOLE_CMD(ConZoomToLevel)
 
				} else if (level > _settings_client.gui.zoom_max) {
 
					IConsolePrint(CC_ERROR, "Current client settings do not allow zooming out beyond level {}.", _settings_client.gui.zoom_max);
 
				} else {
 
					Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
					Window *w = GetMainWindow();
 
					Viewport *vp = w->viewport;
 
					while (vp->zoom > level) DoZoomInOutWindow(ZOOM_IN, w);
 
					while (vp->zoom < level) DoZoomInOutWindow(ZOOM_OUT, w);
src/error_gui.cpp
Show inline comments
 
@@ -235,7 +235,7 @@ public:
 
		int scr_bot = GetMainViewBottom() - 20;
 

	
 
		Point pt = RemapCoords(this->position.x, this->position.y, GetSlopePixelZOutsideMap(this->position.x, this->position.y));
 
		const Viewport *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 
		const Viewport *vp = GetMainWindow()->viewport;
 
		if (this->face == INVALID_COMPANY) {
 
			/* move x pos to opposite corner */
 
			pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
src/genworld.cpp
Show inline comments
 
@@ -321,9 +321,7 @@ void GenerateWorld(GenWorldMode mode, ui
 
	ShowGenerateWorldProgress();
 

	
 
	/* Centre the view on the map */
 
	if (FindWindowById(WC_MAIN_WINDOW, 0) != nullptr) {
 
		ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
 
	}
 
	ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
 

	
 
	_GenerateWorld();
 
}
src/intro_gui.cpp
Show inline comments
 
@@ -222,7 +222,7 @@ struct SelectGameWindow : public Window 
 
		}
 

	
 
		IntroGameViewportCommand &vc = intro_viewport_commands[this->cur_viewport_command_index];
 
		Window *mw = FindWindowByClass(WC_MAIN_WINDOW);
 
		Window *mw = GetMainWindow();
 
		Viewport *vp = mw->viewport;
 

	
 
		/* Early exit if the current command hasn't elapsed and isn't animated. */
src/linkgraph/linkgraph_gui.cpp
Show inline comments
 
@@ -553,7 +553,7 @@ LinkGraphLegendWindow::LinkGraphLegendWi
 
{
 
	this->InitNested(window_number);
 
	this->InvalidateData(0);
 
	this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
 
	this->SetOverlay(GetMainWindow()->viewport->overlay);
 
}
 

	
 
/**
src/main_gui.cpp
Show inline comments
 
@@ -157,7 +157,7 @@ void FixTitleGameZoom(int zoom_adjust)
 
{
 
	if (_game_mode != GM_MENU) return;
 

	
 
	Viewport *vp = FindWindowByClass(WC_MAIN_WINDOW)->viewport;
 
	Viewport *vp = GetMainWindow()->viewport;
 

	
 
	/* Adjust the zoom in/out.
 
	 * Can't simply add, since operator+ is not defined on the ZoomLevel type. */
src/misc_gui.cpp
Show inline comments
 
@@ -1264,7 +1264,7 @@ static WindowDesc _query_desc(
 
 */
 
void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
 
{
 
	if (parent == nullptr) parent = FindWindowById(WC_MAIN_WINDOW, 0);
 
	if (parent == nullptr) parent = GetMainWindow();
 

	
 
	for (Window *w : Window::Iterate()) {
 
		if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
src/network/network_gui.cpp
Show inline comments
 
@@ -2517,6 +2517,6 @@ void ShowNetworkAskRelay(const std::stri
 
{
 
	CloseWindowByClass(WC_NETWORK_ASK_RELAY);
 

	
 
	Window *parent = FindWindowById(WC_MAIN_WINDOW, 0);
 
	Window *parent = GetMainWindow();
 
	new NetworkAskRelayWindow(&_network_ask_relay_desc, parent, server_connection_string, relay_connection_string, token);
 
}
src/saveload/misc_sl.cpp
Show inline comments
 
@@ -34,18 +34,16 @@ ZoomLevel _saved_scrollpos_zoom;
 

	
 
void SaveViewportBeforeSaveGame()
 
{
 
	const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
	const Window *w = GetMainWindow();
 

	
 
	if (w != nullptr) {
 
		_saved_scrollpos_x = w->viewport->scrollpos_x;
 
		_saved_scrollpos_y = w->viewport->scrollpos_y;
 
		_saved_scrollpos_zoom = w->viewport->zoom;
 
	}
 
	_saved_scrollpos_x = w->viewport->scrollpos_x;
 
	_saved_scrollpos_y = w->viewport->scrollpos_y;
 
	_saved_scrollpos_zoom = w->viewport->zoom;
 
}
 

	
 
void ResetViewportAfterLoadGame()
 
{
 
	Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
	Window *w = GetMainWindow();
 

	
 
	w->viewport->scrollpos_x = _saved_scrollpos_x;
 
	w->viewport->scrollpos_y = _saved_scrollpos_y;
src/screenshot.cpp
Show inline comments
 
@@ -732,7 +732,7 @@ void SetupScreenshotViewport(ScreenshotT
 
		case SC_CRASHLOG: {
 
			assert(width == 0 && height == 0);
 

	
 
			Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
			Window *w = GetMainWindow();
 
			vp->virtual_left   = w->viewport->virtual_left;
 
			vp->virtual_top    = w->viewport->virtual_top;
 
			vp->virtual_width  = w->viewport->virtual_width;
 
@@ -776,7 +776,7 @@ void SetupScreenshotViewport(ScreenshotT
 
		default: {
 
			vp->zoom = (t == SC_ZOOMEDIN) ? _settings_client.gui.zoom_min : ZOOM_LVL_VIEWPORT;
 

	
 
			Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
			Window *w = GetMainWindow();
 
			vp->virtual_left   = w->viewport->virtual_left;
 
			vp->virtual_top    = w->viewport->virtual_top;
 

	
src/smallmap_gui.cpp
Show inline comments
 
@@ -941,7 +941,7 @@ void SmallMapWindow::DrawTowns(const Dra
 
void SmallMapWindow::DrawMapIndicators() const
 
{
 
	/* Find main viewport. */
 
	const Viewport *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 
	const Viewport *vp = GetMainWindow()->viewport;
 

	
 
	Point upper_left_smallmap_coord  = InverseRemapCoords2(vp->virtual_left, vp->virtual_top);
 
	Point lower_right_smallmap_coord = InverseRemapCoords2(vp->virtual_left + vp->virtual_width - 1, vp->virtual_top + vp->virtual_height - 1);
 
@@ -1433,7 +1433,7 @@ int SmallMapWindow::GetPositionOnLegend(
 
			if (click_count > 0) this->mouse_capture_widget = widget;
 

	
 
			const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
			Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
			Window *w = GetMainWindow();
 
			int sub;
 
			pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
 
			ScrollWindowTo(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, -1, w);
 
@@ -1665,7 +1665,7 @@ void SmallMapWindow::SetNewScroll(int sx
 
 */
 
void SmallMapWindow::SmallMapCenterOnCurrentPos()
 
{
 
	const Viewport *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 
	const Viewport *vp = GetMainWindow()->viewport;
 
	Point viewport_center = InverseRemapCoords2(vp->virtual_left + vp->virtual_width / 2, vp->virtual_top + vp->virtual_height / 2);
 

	
 
	int sub;
 
@@ -1882,7 +1882,7 @@ void ShowSmallMap()
 
 */
 
bool ScrollMainWindowTo(int x, int y, int z, bool instant)
 
{
 
	bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
 
	bool res = ScrollWindowTo(x, y, z, GetMainWindow(), instant);
 

	
 
	/* If a user scrolls to a tile (via what way what so ever) and already is on
 
	 * that tile (e.g.: pressed twice), move the smallmap to that location,
src/toolbar_gui.cpp
Show inline comments
 
@@ -884,7 +884,7 @@ static CallBackFunction MenuClickShowAir
 

	
 
static CallBackFunction ToolbarZoomInClick(Window *w)
 
{
 
	if (DoZoomInOutWindow(ZOOM_IN, FindWindowById(WC_MAIN_WINDOW, 0))) {
 
	if (DoZoomInOutWindow(ZOOM_IN, GetMainWindow())) {
 
		w->HandleButtonClick((_game_mode == GM_EDITOR) ? (byte)WID_TE_ZOOM_IN : (byte)WID_TN_ZOOM_IN);
 
		if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
	}
 
@@ -895,7 +895,7 @@ static CallBackFunction ToolbarZoomInCli
 

	
 
static CallBackFunction ToolbarZoomOutClick(Window *w)
 
{
 
	if (DoZoomInOutWindow(ZOOM_OUT, FindWindowById(WC_MAIN_WINDOW, 0))) {
 
	if (DoZoomInOutWindow(ZOOM_OUT, GetMainWindow())) {
 
		w->HandleButtonClick((_game_mode == GM_EDITOR) ? (byte)WID_TE_ZOOM_OUT : (byte)WID_TN_ZOOM_OUT);
 
		if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
	}
 
@@ -2146,7 +2146,7 @@ struct MainToolbarWindow : Window {
 
	void OnInvalidateData(int data = 0, bool gui_scope = true) override
 
	{
 
		if (!gui_scope) return;
 
		if (FindWindowById(WC_MAIN_WINDOW, 0) != nullptr) HandleZoomMessage(this, FindWindowById(WC_MAIN_WINDOW, 0)->viewport, WID_TN_ZOOM_IN, WID_TN_ZOOM_OUT);
 
		HandleZoomMessage(this, GetMainWindow()->viewport, WID_TN_ZOOM_IN, WID_TN_ZOOM_OUT);
 
	}
 

	
 
	static HotkeyList hotkeys;
 
@@ -2520,7 +2520,7 @@ struct ScenarioEditorToolbarWindow : Win
 
	void OnInvalidateData(int data = 0, bool gui_scope = true) override
 
	{
 
		if (!gui_scope) return;
 
		if (FindWindowById(WC_MAIN_WINDOW, 0) != nullptr) HandleZoomMessage(this, FindWindowById(WC_MAIN_WINDOW, 0)->viewport, WID_TE_ZOOM_IN, WID_TE_ZOOM_OUT);
 
		HandleZoomMessage(this, GetMainWindow()->viewport, WID_TE_ZOOM_IN, WID_TE_ZOOM_OUT);
 
	}
 

	
 
	void OnQueryTextFinished(char *str) override
src/vehicle_gui.cpp
Show inline comments
 
@@ -3118,7 +3118,7 @@ public:
 
				if (_ctrl_pressed) {
 
					ShowExtraViewportWindow(TileVirtXY(v->x_pos, v->y_pos));
 
				} else {
 
					const Window *mainwindow = FindWindowById(WC_MAIN_WINDOW, 0);
 
					const Window *mainwindow = GetMainWindow();
 
					if (click_count > 1 && mainwindow->viewport->zoom <= ZOOM_LVL_OUT_4X) {
 
						/* main window 'follows' vehicle */
 
						mainwindow->viewport->follow_vehicle = v->index;
 
@@ -3176,9 +3176,9 @@ public:
 
	{
 
		/* If the hotkey is not for any widget in the UI (i.e. for honking) */
 
		if (hotkey == WID_VV_HONK_HORN) {
 
			const Window* mainwindow = FindWindowById(WC_MAIN_WINDOW, 0);
 
			const Vehicle* v = Vehicle::Get(window_number);
 
			/*Only play the sound if we're following this vehicle */
 
			const Window *mainwindow = GetMainWindow();
 
			const Vehicle *v = Vehicle::Get(window_number);
 
			/* Only play the sound if we're following this vehicle */
 
			if (mainwindow->viewport->follow_vehicle == v->index) {
 
				v->PlayLeaveStationSound(true);
 
			}
 
@@ -3342,8 +3342,8 @@ bool VehicleClicked(const GUIVehicleGrou
 

	
 
void StopGlobalFollowVehicle(const Vehicle *v)
 
{
 
	Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
	if (w != nullptr && w->viewport->follow_vehicle == v->index) {
 
	Window *w = GetMainWindow();
 
	if (w->viewport->follow_vehicle == v->index) {
 
		ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos, true); // lock the main view on the vehicle's last position
 
		w->viewport->follow_vehicle = INVALID_VEHICLE;
 
	}
src/viewport_gui.cpp
Show inline comments
 
@@ -63,7 +63,7 @@ public:
 
		Point pt;
 
		if (tile == INVALID_TILE) {
 
			/* No tile? Use center of main viewport. */
 
			const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
			const Window *w = GetMainWindow();
 

	
 
			/* center on same place as main window (zoom is maximum, no adjustment needed) */
 
			pt.x = w->viewport->scrollpos_x + w->viewport->virtual_width / 2;
 
@@ -95,7 +95,7 @@ public:
 
			case WID_EV_ZOOM_OUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
 

	
 
			case WID_EV_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
 
				Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
				Window *w = GetMainWindow();
 
				int x = this->viewport->scrollpos_x; // Where is the main looking at
 
				int y = this->viewport->scrollpos_y;
 

	
 
@@ -107,7 +107,7 @@ public:
 
			}
 

	
 
			case WID_EV_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
 
				const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
				const Window *w = GetMainWindow();
 
				int x = w->viewport->scrollpos_x;
 
				int y = w->viewport->scrollpos_y;
 

	
src/window.cpp
Show inline comments
 
@@ -1179,6 +1179,18 @@ Window *FindWindowByClass(WindowClass cl
 
}
 

	
 
/**
 
 * Get the main window, i.e. FindWindowById(WC_MAIN_WINDOW, 0).
 
 * If the main window is not available, this function will trigger an assert.
 
 * @return Pointer to the main window.
 
 */
 
Window *GetMainWindow()
 
{
 
	Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
	assert(w != nullptr);
 
	return w;
 
}
 

	
 
/**
 
 * Close a window by its class and window number (if it is open).
 
 * @param cls Window class
 
 * @param number Number of the window within the window class
 
@@ -2431,7 +2443,7 @@ static EventState HandleViewportScroll()
 
		return ES_NOT_HANDLED;
 
	}
 

	
 
	if (_last_scroll_window == FindWindowById(WC_MAIN_WINDOW, 0) && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
 
	if (_last_scroll_window == GetMainWindow() && _last_scroll_window->viewport->follow_vehicle != INVALID_VEHICLE) {
 
		/* If the main window is following a vehicle, then first let go of it! */
 
		const Vehicle *veh = Vehicle::Get(_last_scroll_window->viewport->follow_vehicle);
 
		ScrollMainWindowTo(veh->x_pos, veh->y_pos, veh->z_pos, true); // This also resets follow_vehicle
 
@@ -2776,9 +2788,7 @@ const std::chrono::milliseconds TIME_BET
 
static void ScrollMainViewport(int x, int y)
 
{
 
	if (_game_mode != GM_MENU) {
 
		Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
		assert(w);
 

	
 
		Window *w = GetMainWindow();
 
		w->viewport->dest_scrollpos_x += ScaleByZoom(x, w->viewport->zoom);
 
		w->viewport->dest_scrollpos_y += ScaleByZoom(y, w->viewport->zoom);
 
	}
src/window_func.h
Show inline comments
 
@@ -16,6 +16,7 @@
 

	
 
Window *FindWindowById(WindowClass cls, WindowNumber number);
 
Window *FindWindowByClass(WindowClass cls);
 
Window *GetMainWindow();
 
void ChangeWindowOwner(Owner old_owner, Owner new_owner);
 

	
 
void ResizeWindow(Window *w, int x, int y, bool clamp_to_screen = true);
0 comments (0 inline, 0 general)