1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 #include "BzfDisplay.h"
14 #include <string.h>
15 
16 //
17 // BzfDisplay::ResInfo
18 //
19 
ResInfo(const char * _name,int w,int h,int r)20 BzfDisplay::ResInfo::ResInfo(const char* _name, int w, int h, int r)
21 {
22     name = new char[strlen(_name) + 1];
23     strcpy(name, _name);
24     width = w;
25     height = h;
26     refresh = r;
27 }
28 
~ResInfo()29 BzfDisplay::ResInfo::~ResInfo()
30 {
31     delete[] name;
32 }
33 
34 //
35 // BzfDisplay
36 //
37 
BzfDisplay()38 BzfDisplay::BzfDisplay() : passWidth(640), passHeight(480),
39     numResolutions(0),
40     defaultResolution(-1),
41     currentResolution(-1),
42     resolutions(NULL),
43     modeIndex(-1)
44 {
45     // do nothing
46 }
47 
~BzfDisplay()48 BzfDisplay::~BzfDisplay()
49 {
50     for (int i = 0; i < numResolutions; i++)
51         delete resolutions[i];
52     delete[] resolutions;
53 }
54 
getWidth() const55 int         BzfDisplay::getWidth() const
56 {
57     if (currentResolution == -1) return 640;
58     return resolutions[currentResolution]->width;
59 }
60 
getHeight() const61 int         BzfDisplay::getHeight() const
62 {
63     if (currentResolution == -1) return 480;
64     return resolutions[currentResolution]->height;
65 }
66 
setPassthroughSize(int w,int h)67 void            BzfDisplay::setPassthroughSize(int w, int h)
68 {
69     passWidth = w;
70     passHeight = h;
71 }
72 
getPassthroughWidth() const73 int         BzfDisplay::getPassthroughWidth() const
74 {
75     return passWidth;
76 }
77 
getPassthroughHeight() const78 int         BzfDisplay::getPassthroughHeight() const
79 {
80     return passHeight;
81 }
82 
getNumResolutions() const83 int         BzfDisplay::getNumResolutions() const
84 {
85     return numResolutions;
86 }
87 
getResolution(int index) const88 const BzfDisplay::ResInfo* BzfDisplay::getResolution(int index) const
89 {
90     if (index < 0 || index >= numResolutions) return NULL;
91     return resolutions[index];
92 }
93 
getResolution() const94 int         BzfDisplay::getResolution() const
95 {
96     return currentResolution;
97 }
98 
getDefaultResolution() const99 int         BzfDisplay::getDefaultResolution() const
100 {
101     return defaultResolution;
102 }
103 
setResolution(int index)104 bool            BzfDisplay::setResolution(int index)
105 {
106     if (index < 0 || index >= numResolutions) return false;
107     if (index == currentResolution) return true;
108     if (!resolutions[index]) return false;
109     if (!doSetResolution(index))
110     {
111         delete resolutions[index];
112         resolutions[index] = NULL;
113         return false;
114     }
115     currentResolution = index;
116     setFullScreenFormat(index);
117     return true;
118 }
119 
setDefaultResolution()120 bool            BzfDisplay::setDefaultResolution()
121 {
122     const int oldResolution = currentResolution;
123     currentResolution = -1;
124     if (!doSetDefaultResolution())
125     {
126         currentResolution = oldResolution;
127         return false;
128     }
129     return true;
130 }
131 
doSetDefaultResolution()132 bool            BzfDisplay::doSetDefaultResolution()
133 {
134     if (numResolutions >= 2 && defaultResolution < numResolutions)
135         return setResolution(defaultResolution);
136     else
137         return false;
138 }
139 
findResolution(const char * name) const140 int         BzfDisplay::findResolution(const char* name) const
141 {
142     for (int i = 0; i < numResolutions; i++)
143         if (strcmp(name, resolutions[i]->name) == 0)
144             return i;
145     return -1;
146 }
147 
isValidResolution(int index) const148 bool            BzfDisplay::isValidResolution(int index) const
149 {
150     if (index < 0 || index >= numResolutions) return false;
151     return resolutions[index] != NULL;
152 }
153 
initResolutions(ResInfo ** _resolutions,int _numResolutions,int _currentResolution)154 void            BzfDisplay::initResolutions(ResInfo** _resolutions,
155         int _numResolutions, int _currentResolution)
156 {
157     resolutions = _resolutions;
158     numResolutions = _numResolutions;
159     currentResolution = _currentResolution;
160     defaultResolution = currentResolution;
161 }
162 
setFullScreenFormat(int index)163 void BzfDisplay::setFullScreenFormat(int index)
164 {
165     modeIndex = index;
166 }
167 
168 // Local Variables: ***
169 // mode: C++ ***
170 // tab-width: 4 ***
171 // c-basic-offset: 4 ***
172 // indent-tabs-mode: nil ***
173 // End: ***
174 // ex: shiftwidth=4 tabstop=4
175