Codechange: use std::vector for _resolutions
This commit is contained in:
@@ -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 (j == n) {
|
||||
_resolutions[j].width = w;
|
||||
_resolutions[j].height = h;
|
||||
if (++n == lengthof(_resolutions)) 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);
|
||||
}
|
||||
_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::ToggleFullscreen(bool fullscreen)
|
||||
{
|
||||
_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;
|
||||
|
||||
@@ -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, uint max_modes, CGDirectDisplayID display_i
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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(bool fullscreen)
|
||||
|
||||
_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 */
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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_resolutions[] = {
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user