summaryrefslogtreecommitdiff
path: root/packages/kobodeluxe/files/kobodeluxe-dimension-autoswap.patch
blob: 273e5231035f7329a497d6946070cf54a191fdf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
Index: KoboDeluxe-0.5.1/graphics/gfxengine.cpp
===================================================================
--- KoboDeluxe-0.5.1.orig/graphics/gfxengine.cpp	2008-02-11 13:17:27.000000000 +0100
+++ KoboDeluxe-0.5.1/graphics/gfxengine.cpp	2008-02-11 15:27:57.000000000 +0100
@@ -135,11 +135,13 @@
 
 	_width = w;
 	_height = h;
+
 	if(csengine)
 		cs_engine_set_size(csengine, w, h);
 
 	if(was_showing)
 		show();
+
 }
 
 void gfxengine_t::centered(int c)
@@ -759,33 +761,13 @@
 	Display show/hide
 ----------------------------------------------------------*/
 
-int gfxengine_t::show()
+/** Helper method that returns the flags value needed for
+ * SDL_SetVideoMode() and SDL_VideoModeOK().
+ */
+int gfxengine_t::video_flags()
 {
 	int flags = 0;
 
-	if(!is_open)
-		return -1;
-
-	if(is_showing)
-		return 0;
-
-  if(_centered && !_fullscreen)
-#if HAVE_DECL_SDL_PUTENV
-    SDL_putenv("SDL_VIDEO_CENTERED=1");
-#elif defined(HAVE_PUTENV)
-    putenv("SDL_VIDEO_CENTERED=1");
-#else
-    #error Neither SDL_putenv() nor putenv() are available. Fix it!
-#endif
-
-	log_printf(DLOG, "Opening screen...\n");
-	if(!SDL_WasInit(SDL_INIT_VIDEO))
-		if(SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
-		{
-			log_printf(ELOG, "Failed to initialize SDL!\n");
-			return -2;
-		}
-
 	switch(_driver)
 	{
 	  case GFX_DRIVER_SDL2D:
@@ -830,11 +812,72 @@
 	glSDL_VSync(_vsync);
 	flags |= xflags;
 
+	return flags;
+}
+
+bool gfxengine_t::check_mode_autoswap(int *w, int *h)
+{
+	log_printf(VLOG, "Trying display modes %dx%d and %dx%d if the first fails.\n", *w, *h, *h, *w);
+
+	int flags = video_flags();
+
+	SDL_Surface *test_surface = NULL;
+
+	// On some platforms SDL_VideoModeOK() cannot be trusted unfortunately.
+	if(!(test_surface = SDL_SetVideoMode(*w, *h, _depth, flags)))
+	{
+		if(!(test_surface = SDL_SetVideoMode(*h, *w, _depth, flags)))
+		{
+			log_printf(ELOG, "Failed with both display mode. Giving up!\n");
+			return false;
+		}
+
+		int temp = *w;
+		*w = *h;
+		*h = temp;
+
+		log_printf(VLOG, "Display dimensions swapped. Using %dx%d!\n", *w, *h);
+	}
+	else
+		log_printf(VLOG, "Stored display dimension worked. Using %dx%d!\n", *w, *h);
+
+	SDL_FreeSurface(test_surface);
+
+	return true;
+}
+
+int gfxengine_t::show()
+{
+	if(!is_open)
+		return -1;
+
+	if(is_showing)
+		return 0;
+
+  if(_centered && !_fullscreen)
+#if HAVE_DECL_SDL_PUTENV
+    SDL_putenv("SDL_VIDEO_CENTERED=1");
+#elif defined(HAVE_PUTENV)
+    putenv("SDL_VIDEO_CENTERED=1");
+#else
+    #error Neither SDL_putenv() nor putenv() are available. Fix it!
+#endif
+
+	log_printf(DLOG, "Opening screen...\n");
+	if(!SDL_WasInit(SDL_INIT_VIDEO))
+		if(SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
+		{
+			log_printf(ELOG, "Failed to initialize SDL!\n");
+			return -2;
+		}
+
+	int flags = video_flags();
+
 	screen_surface = SDL_SetVideoMode(_width, _height, _depth, flags);
 	if(!screen_surface)
 	{
-		log_printf(ELOG, "Failed to open display!\n");
-		return -3;
+		log_printf(ELOG, "Failed to open display with %dx%d! Giving up.\n", _width, _height);
+    return -3;
 	}
 
 	if(_driver != GFX_DRIVER_GLSDL)
Index: KoboDeluxe-0.5.1/graphics/gfxengine.h
===================================================================
--- KoboDeluxe-0.5.1.orig/graphics/gfxengine.h	2008-02-11 13:24:51.000000000 +0100
+++ KoboDeluxe-0.5.1/graphics/gfxengine.h	2008-02-11 15:15:50.000000000 +0100
@@ -54,6 +54,9 @@
 class gfxengine_t
 {
 	friend class window_t;
+
+	int video_flags();
+
   public:
 	gfxengine_t();
 	virtual ~gfxengine_t();
@@ -135,6 +138,7 @@
 	void title(const char *win, const char *icon);
 
 	/* Display show/hide */
+	bool check_mode_autoswap(int *, int *);
 	int show();
 	void hide();
 
Index: KoboDeluxe-0.5.1/kobo.cpp
===================================================================
--- KoboDeluxe-0.5.1.orig/kobo.cpp	2008-02-11 13:16:24.000000000 +0100
+++ KoboDeluxe-0.5.1/kobo.cpp	2008-02-11 14:57:29.000000000 +0100
@@ -641,8 +641,27 @@
 	gengine->title("Kobo Deluxe " VERSION, "kobodl");
 	gengine->driver((gfx_drivers_t)p->videodriver);
 
+	// Initializes gfxengine with all kinds of display properties.
+	// We need this at this point to make the autoswap check work
+	// properly. Since these properties are independent of other
+	// values that is no problem.
+	gengine->mode(0, p->fullscreen);
+	gengine->doublebuffer(p->doublebuf);
+	gengine->pages(p->pages);
+	gengine->vsync(p->vsync);
+	gengine->shadow(p->shadow);
+	gengine->cursor(0);
+
+	// Do the auto swap dance only if configured so.
+	if (prefs->autoswap)
+	{
+		if (!gengine->check_mode_autoswap(&p->width, &p->height))
+			return -1;
+	}
+	
 	dw = p->width;
 	dh = p->height;
+
 	if(p->fullscreen)
 	{
 		// This game assumes 1:1 pixel aspect ratio, or 4:3
@@ -696,13 +715,6 @@
 	yoffs = (int)((dh - gh) / 2 / gengine->yscale());
 	gengine->size(dw, dh);
 
-	gengine->mode(0, p->fullscreen);
-	gengine->doublebuffer(p->doublebuf);
-	gengine->pages(p->pages);
-	gengine->vsync(p->vsync);
-	gengine->shadow(p->shadow);
-	gengine->cursor(0);
-
 	gengine->period(game.speed);
 	sound.period(game.speed);
 	gengine->timefilter(p->timefilter * 0.01f);
Index: KoboDeluxe-0.5.1/prefs.cpp
===================================================================
--- KoboDeluxe-0.5.1.orig/prefs.cpp	2008-02-11 14:28:01.000000000 +0100
+++ KoboDeluxe-0.5.1/prefs.cpp	2008-02-11 14:30:15.000000000 +0100
@@ -86,6 +86,7 @@
 			desc("Display Driver");
 	key("width", width, 640); desc("Horizontal Resolution");
 	key("height", height, 480); desc("Vertical Resolution");
+	yesno("autoswap", autoswap, 0); desc("Automatically swap display dimension");
 	key("aspect", aspect, 1000); desc("Pixel Aspect Ratio");
 	key("depth", depth, 0); desc("Display Depth");
 	key("maxfps", max_fps, 100); desc("Maximum fps");
Index: KoboDeluxe-0.5.1/prefs.h
===================================================================
--- KoboDeluxe-0.5.1.orig/prefs.h	2008-02-11 14:28:01.000000000 +0100
+++ KoboDeluxe-0.5.1/prefs.h	2008-02-11 14:31:24.000000000 +0100
@@ -76,6 +76,7 @@
 	int	videodriver;	//Internal video driver
 	int	width;		//Screen/window width
 	int	height;		//Screen/window height
+	int autoswap; // Automatically swap dimensions if initialization fails
 	int	aspect;		//Pixel aspect ratio * 1000
 	int	depth;		//Bits per pixel
 	int	max_fps;	//Maximum fps
Index: KoboDeluxe-0.5.1/states.cpp
===================================================================
--- KoboDeluxe-0.5.1.orig/states.cpp	2008-02-11 14:32:13.000000000 +0100
+++ KoboDeluxe-0.5.1/states.cpp	2008-02-11 15:00:13.000000000 +0100
@@ -117,6 +117,8 @@
 	switch (button)
 	{
 	  case BTN_EXIT:
+		gsm.push(&st_ask_exit);
+		break;
 	  case BTN_CLOSE:
 		gsm.push(&st_main_menu);
 		break;