Changeset - r23630:58df4dafed3e
[Not reviewed]
master
0 10 0
glx - 5 years ago 2019-04-12 16:46:49
glx@openttd.org
Codechange: use std::vector for _resolutions
10 files changed with 83 insertions and 120 deletions:
0 comments (0 inline, 0 general)
src/core/geometry_type.hpp
Show inline comments
 
@@ -29,6 +29,20 @@ struct Point {
 
struct Dimension {
 
	uint width;
 
	uint height;
 

	
 
	Dimension(uint w = 0, uint h = 0) : width(w), height(h) {};
 

	
 
	bool operator< (const Dimension &other) const
 
	{
 
		int x = (*this).width - other.width;
 
		if (x != 0) return x < 0;
 
		return (*this).height < other.height;
 
	}
 

	
 
	bool operator== (const Dimension &other) const
 
	{
 
		return (*this).width == other.width && (*this).height == other.height;
 
	}
 
};
 

	
 
/** Specification of a rectangle with absolute coordinates of all edges */
src/driver.cpp
Show inline comments
 
@@ -18,18 +18,17 @@
 

	
 
#include "safeguards.h"
 

	
 
char *_ini_videodriver;     ///< The video driver a stored in the configuration file.
 
int _num_resolutions;       ///< The number of resolutions.
 
Dimension _resolutions[32]; ///< List of resolutions.
 
Dimension _cur_resolution;  ///< The current resolution.
 
bool _rightclick_emulate;   ///< Whether right clicking is emulated.
 
char *_ini_videodriver;              ///< The video driver a stored in the configuration file.
 
std::vector<Dimension> _resolutions; ///< List of resolutions.
 
Dimension _cur_resolution;           ///< The current resolution.
 
bool _rightclick_emulate;            ///< Whether right clicking is emulated.
 

	
 
char *_ini_sounddriver;     ///< The sound driver a stored in the configuration file.
 
char *_ini_sounddriver;              ///< The sound driver a stored in the configuration file.
 

	
 
char *_ini_musicdriver;     ///< The music driver a stored in the configuration file.
 
char *_ini_musicdriver;              ///< The music driver a stored in the configuration file.
 

	
 
char *_ini_blitter;         ///< The blitter as stored in the configuration file.
 
bool _blitter_autodetected; ///< Was the blitter autodetected or specified by the user?
 
char *_ini_blitter;                  ///< The blitter as stored in the configuration file.
 
bool _blitter_autodetected;          ///< Was the blitter autodetected or specified by the user?
 

	
 
/**
 
 * Get a string parameter the list of parameters.
src/gfx.cpp
Show inline comments
 
@@ -1691,20 +1691,13 @@ bool ChangeResInGame(int width, int heig
 
bool ToggleFullScreen(bool fs)
 
{
 
	bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs);
 
	if (_fullscreen != fs && _num_resolutions == 0) {
 
	if (_fullscreen != fs && _resolutions.empty()) {
 
		DEBUG(driver, 0, "Could not find a suitable fullscreen resolution");
 
	}
 
	return result;
 
}
 

	
 
static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
 
void SortResolutions()
 
{
 
	int x = pa->width - pb->width;
 
	if (x != 0) return x;
 
	return pa->height - pb->height;
 
	std::sort(_resolutions.begin(), _resolutions.end());
 
}
 

	
 
void SortResolutions(int count)
 
{
 
	QSortT(_resolutions, count, &compare_res);
 
}
src/gfx_func.h
Show inline comments
 
@@ -66,8 +66,7 @@ extern bool _right_button_clicked;
 
extern DrawPixelInfo _screen;
 
extern bool _screen_disable_anim;   ///< Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
 

	
 
extern int _num_resolutions;
 
extern Dimension _resolutions[32];
 
extern std::vector<Dimension> _resolutions;
 
extern Dimension _cur_resolution;
 
extern Palette _cur_palette; ///< Current palette
 

	
 
@@ -162,7 +161,7 @@ void SetAnimatedMouseCursor(const AnimCu
 
void CursorTick();
 
void UpdateCursorSize();
 
bool ChangeResInGame(int w, int h);
 
void SortResolutions(int count);
 
void SortResolutions();
 
bool ToggleFullScreen(bool fs);
 

	
 
/* gfx.cpp */
src/settings_gui.cpp
Show inline comments
 
@@ -104,17 +104,14 @@ static inline StringID TownName(int town
 

	
 
/**
 
 * Get index of the current screen resolution.
 
 * @return Index of the current screen resolution if it is a known resolution, #_num_resolutions otherwise.
 
 * @return Index of the current screen resolution if it is a known resolution, _resolutions.size() otherwise.
 
 */
 
static int GetCurRes()
 
static uint GetCurRes()
 
{
 
	int i;
 
	uint i;
 

	
 
	for (i = 0; i != _num_resolutions; i++) {
 
		if ((int)_resolutions[i].width == _screen.width &&
 
				(int)_resolutions[i].height == _screen.height) {
 
			break;
 
		}
 
	for (i = 0; i != _resolutions.size(); i++) {
 
		if (_resolutions[i] == Dimension(_screen.width, _screen.height)) break;
 
	}
 
	return i;
 
}
 
@@ -286,10 +283,10 @@ struct GameOptionsWindow : Window {
 
			}
 

	
 
			case WID_GO_RESOLUTION_DROPDOWN: // Setup resolution dropdown
 
				if (_num_resolutions == 0) break;
 
				if (_resolutions.empty()) break;
 

	
 
				*selected_index = GetCurRes();
 
				for (int i = 0; i < _num_resolutions; i++) {
 
				for (uint i = 0; i < _resolutions.size(); i++) {
 
					list.emplace_back(new DropDownListStringItem(SPECSTR_RESOLUTION_START + i, i, false));
 
				}
 
				break;
 
@@ -336,7 +333,7 @@ struct GameOptionsWindow : Window {
 
			case WID_GO_TOWNNAME_DROPDOWN:   SetDParam(0, TownName(this->opt->game_creation.town_name)); break;
 
			case WID_GO_AUTOSAVE_DROPDOWN:   SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break;
 
			case WID_GO_LANG_DROPDOWN:       SetDParamStr(0, _current_language->own_name); break;
 
			case WID_GO_RESOLUTION_DROPDOWN: SetDParam(0, GetCurRes() == _num_resolutions ? STR_GAME_OPTIONS_RESOLUTION_OTHER : SPECSTR_RESOLUTION_START + GetCurRes()); break;
 
			case WID_GO_RESOLUTION_DROPDOWN: SetDParam(0, GetCurRes() == _resolutions.size() ? STR_GAME_OPTIONS_RESOLUTION_OTHER : SPECSTR_RESOLUTION_START + GetCurRes()); break;
 
			case WID_GO_GUI_ZOOM_DROPDOWN:   SetDParam(0, _gui_zoom_dropdown[ZOOM_LVL_OUT_4X - _gui_zoom]); break;
 
			case WID_GO_FONT_ZOOM_DROPDOWN:  SetDParam(0, _font_zoom_dropdown[ZOOM_LVL_OUT_4X - _font_zoom]); break;
 
			case WID_GO_BASE_GRF_DROPDOWN:   SetDParamStr(0, BaseGraphics::GetUsedSet()->name); break;
 
@@ -535,7 +532,7 @@ struct GameOptionsWindow : Window {
 
				break;
 

	
 
			case WID_GO_RESOLUTION_DROPDOWN: // Change resolution
 
				if (index < _num_resolutions && ChangeResInGame(_resolutions[index].width, _resolutions[index].height)) {
 
				if ((uint)index < _resolutions.size() && ChangeResInGame(_resolutions[index].width, _resolutions[index].height)) {
 
					this->SetDirty();
 
				}
 
				break;
src/video/allegro_v.cpp
Show inline comments
 
@@ -28,6 +28,7 @@
 
#include "../thread.h"
 
#include "allegro_v.h"
 
#include <allegro.h>
 
#include <algorithm>
 

	
 
#include "../safeguards.h"
 

	
 
@@ -139,34 +140,25 @@ static void GetVideoModes()
 
	 * cards ourselves... and we need a card to get the modes. */
 
	set_gfx_mode(_fullscreen ? GFX_AUTODETECT_FULLSCREEN : GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
 

	
 
	_resolutions.clear();
 

	
 
	GFX_MODE_LIST *mode_list = get_gfx_mode_list(gfx_driver->id);
 
	if (mode_list == nullptr) {
 
		memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
 
		_num_resolutions = lengthof(default_resolutions);
 
		_resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
 
		return;
 
	}
 

	
 
	GFX_MODE *modes = mode_list->mode;
 

	
 
	int n = 0;
 
	for (int i = 0; modes[i].bpp != 0; i++) {
 
		uint w = modes[i].width;
 
		uint h = modes[i].height;
 
		if (w >= 640 && h >= 480) {
 
			int j;
 
			for (j = 0; j < n; j++) {
 
				if (_resolutions[j].width == w && _resolutions[j].height == h) break;
 
			}
 
		if (w < 640 || h < 480) continue;
 
		if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
 
		_resolutions.emplace_back(w, h);
 
	}
 

	
 
			if (j == n) {
 
				_resolutions[j].width  = w;
 
				_resolutions[j].height = h;
 
				if (++n == lengthof(_resolutions)) break;
 
			}
 
		}
 
	}
 
	_num_resolutions = n;
 
	SortResolutions(_num_resolutions);
 
	SortResolutions();
 

	
 
	destroy_gfx_mode_list(mode_list);
 
}
 
@@ -174,17 +166,15 @@ static void GetVideoModes()
 
static void GetAvailableVideoMode(uint *w, uint *h)
 
{
 
	/* No video modes, so just try it and see where it ends */
 
	if (_num_resolutions == 0) return;
 
	if (_resolutions.empty()) return;
 

	
 
	/* is the wanted mode among the available modes? */
 
	for (int i = 0; i != _num_resolutions; i++) {
 
		if (*w == _resolutions[i].width && *h == _resolutions[i].height) return;
 
	}
 
	if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
 

	
 
	/* use the closest possible resolution */
 
	int best = 0;
 
	uint best = 0;
 
	uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
 
	for (int i = 1; i != _num_resolutions; ++i) {
 
	for (uint i = 1; i != _resolutions.size(); ++i) {
 
		uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
 
		if (newdelta < delta) {
 
			best = i;
 
@@ -545,7 +535,7 @@ bool VideoDriver_Allegro::ToggleFullscre
 
{
 
	_fullscreen = fullscreen;
 
	GetVideoModes(); // get the list of available video modes
 
	if (_num_resolutions == 0 || !this->ChangeResolution(_cur_resolution.width, _cur_resolution.height)) {
 
	if (_resolutions.empty() || !this->ChangeResolution(_cur_resolution.width, _cur_resolution.height)) {
 
		/* switching resolution failed, put back full_screen to original status */
 
		_fullscreen ^= true;
 
		return false;
src/video/cocoa/cocoa_v.mm
Show inline comments
 
@@ -234,13 +234,13 @@ static void setupApplication()
 
}
 

	
 

	
 
static int CDECL ModeSorter(const OTTD_Point *p1, const OTTD_Point *p2)
 
static bool ModeSorter(const OTTD_Point &p1, const OTTD_Point &p2)
 
{
 
	if (p1->x < p2->x) return -1;
 
	if (p1->x > p2->x) return +1;
 
	if (p1->y < p2->y) return -1;
 
	if (p1->y > p2->y) return +1;
 
	return 0;
 
	if (p1.x < p2.x) return true;
 
	if (p1.x > p2.x) return false;
 
	if (p1.y < p2.y) return true;
 
	if (p1.y > p2.y) return false;
 
	return false;
 
}
 

	
 
static void QZ_GetDisplayModeInfo(CFArrayRef modes, CFIndex i, int &bpp, uint16 &width, uint16 &height)
 
@@ -326,7 +326,7 @@ uint QZ_ListModes(OTTD_Point *modes, uin
 
	}
 

	
 
	/* Sort list smallest to largest */
 
	QSortT(modes, count, &ModeSorter);
 
	std::sort(modes, modes + count, ModeSorter);
 

	
 
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
 
	if (MacOSVersionIsAtLeast(10, 6, 0)) CFRelease(mode_list);
 
@@ -363,12 +363,10 @@ static void QZ_UpdateVideoModes()
 
	OTTD_Point modes[32];
 
	uint count = _cocoa_subdriver->ListModes(modes, lengthof(modes));
 

	
 
	_resolutions.clear();
 
	for (uint i = 0; i < count; i++) {
 
		_resolutions[i].width  = modes[i].x;
 
		_resolutions[i].height = modes[i].y;
 
		_resolutions.emplace_back(modes[i].x, modes[i].y);
 
	}
 

	
 
	_num_resolutions = count;
 
}
 

	
 
/**
src/video/sdl_v.cpp
Show inline comments
 
@@ -27,6 +27,7 @@
 
#include <SDL.h>
 
#include <mutex>
 
#include <condition_variable>
 
#include <algorithm>
 

	
 
#include "../safeguards.h"
 

	
 
@@ -207,53 +208,40 @@ static void GetVideoModes()
 
	SDL_Rect **modes = SDL_ListModes(nullptr, SDL_SWSURFACE | SDL_FULLSCREEN);
 
	if (modes == nullptr) usererror("sdl: no modes available");
 

	
 
	_resolutions.clear();
 

	
 
	_all_modes = (SDL_ListModes(nullptr, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
 
	if (modes == (void*)-1) {
 
		int n = 0;
 
		for (uint i = 0; i < lengthof(_default_resolutions); i++) {
 
			if (SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
 
				_resolutions[n] = _default_resolutions[i];
 
				if (++n == lengthof(_resolutions)) break;
 
				_resolutions.push_back(_default_resolutions[i]);
 
			}
 
		}
 
		_num_resolutions = n;
 
	} else {
 
		int n = 0;
 
		for (int i = 0; modes[i]; i++) {
 
			uint w = modes[i]->w;
 
			uint h = modes[i]->h;
 
			if (w < 640 || h < 480) continue; // reject too small resolutions
 
			int j;
 
			for (j = 0; j < n; j++) {
 
				if (_resolutions[j].width == w && _resolutions[j].height == h) break;
 
			}
 

	
 
			if (j == n) {
 
				_resolutions[j].width  = w;
 
				_resolutions[j].height = h;
 
				if (++n == lengthof(_resolutions)) break;
 
			}
 
			if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
 
			_resolutions.emplace_back(w, h);
 
		}
 
		if (n == 0) usererror("No usable screen resolutions found!\n");
 
		_num_resolutions = n;
 
		SortResolutions(_num_resolutions);
 
		if (_resolutions.empty()) usererror("No usable screen resolutions found!\n");
 
		SortResolutions();
 
	}
 
}
 

	
 
static void GetAvailableVideoMode(uint *w, uint *h)
 
{
 
	/* All modes available? */
 
	if (_all_modes || _num_resolutions == 0) return;
 
	if (_all_modes || _resolutions.empty()) return;
 

	
 
	/* Is the wanted mode among the available modes? */
 
	for (int i = 0; i != _num_resolutions; i++) {
 
		if (*w == _resolutions[i].width && *h == _resolutions[i].height) return;
 
	}
 
	if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
 

	
 
	/* Use the closest possible resolution */
 
	int best = 0;
 
	uint best = 0;
 
	uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
 
	for (int i = 1; i != _num_resolutions; ++i) {
 
	for (uint i = 1; i != _resolutions.size(); ++i) {
 
		uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
 
		if (newdelta < delta) {
 
			best = i;
 
@@ -817,7 +805,7 @@ bool VideoDriver_SDL::ToggleFullscreen(b
 

	
 
	_fullscreen = fullscreen;
 
	GetVideoModes(); // get the list of available video modes
 
	bool ret = _num_resolutions != 0 && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);
 
	bool ret = !_resolutions.empty() && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);
 

	
 
	if (!ret) {
 
		/* switching resolution failed, put back full_screen to original status */
src/video/video_driver.hpp
Show inline comments
 
@@ -14,6 +14,7 @@
 

	
 
#include "../driver.h"
 
#include "../core/geometry_type.hpp"
 
#include <vector>
 

	
 
/** The base of all video drivers. */
 
class VideoDriver : public Driver {
 
@@ -101,8 +102,7 @@ public:
 
};
 

	
 
extern char *_ini_videodriver;
 
extern int _num_resolutions;
 
extern Dimension _resolutions[32];
 
extern std::vector<Dimension> _resolutions;
 
extern Dimension _cur_resolution;
 
extern bool _rightclick_emulate;
 

	
src/video/win32_v.cpp
Show inline comments
 
@@ -29,6 +29,7 @@
 
#include <imm.h>
 
#include <mutex>
 
#include <condition_variable>
 
#include <algorithm>
 

	
 
#include "../safeguards.h"
 

	
 
@@ -1085,45 +1086,29 @@ static const Dimension default_resolutio
 

	
 
static void FindResolutions()
 
{
 
	uint n = 0;
 
	uint i;
 
	DEVMODEA dm;
 

	
 
	/* Check modes for the relevant fullscreen bpp */
 
	uint bpp = _support8bpp != S8BPP_HARDWARE ? 32 : BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
 

	
 
	_resolutions.clear();
 

	
 
	/* XXX - EnumDisplaySettingsW crashes with unicows.dll on Windows95
 
	 * Doesn't really matter since we don't pass a string anyways, but still
 
	 * a letdown */
 
	for (i = 0; EnumDisplaySettingsA(nullptr, i, &dm) != 0; i++) {
 
		if (dm.dmBitsPerPel == bpp &&
 
				dm.dmPelsWidth >= 640 && dm.dmPelsHeight >= 480) {
 
			uint j;
 

	
 
			for (j = 0; j < n; j++) {
 
				if (_resolutions[j].width == dm.dmPelsWidth && _resolutions[j].height == dm.dmPelsHeight) break;
 
			}
 

	
 
			/* In the previous loop we have checked already existing/added resolutions if
 
			 * they are the same as the new ones. If this is not the case (j == n); we have
 
			 * looped all and found none, add the new one to the list. If we have reached the
 
			 * maximum amount of resolutions, then quit querying the display */
 
			if (j == n) {
 
				_resolutions[j].width  = dm.dmPelsWidth;
 
				_resolutions[j].height = dm.dmPelsHeight;
 
				if (++n == lengthof(_resolutions)) break;
 
			}
 
		}
 
		if (dm.dmBitsPerPel != bpp || dm.dmPelsWidth < 640 || dm.dmPelsHeight < 480) continue;
 
		if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(dm.dmPelsWidth, dm.dmPelsHeight)) != _resolutions.end()) continue;
 
		_resolutions.emplace_back(dm.dmPelsWidth, dm.dmPelsHeight);
 
	}
 

	
 
	/* We have found no resolutions, show the default list */
 
	if (n == 0) {
 
		memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
 
		n = lengthof(default_resolutions);
 
	if (_resolutions.empty()) {
 
		_resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
 
	}
 

	
 
	_num_resolutions = n;
 
	SortResolutions(_num_resolutions);
 
	SortResolutions();
 
}
 

	
 
static FVideoDriver_Win32 iFVideoDriver_Win32;
0 comments (0 inline, 0 general)