-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvideo.c
More file actions
316 lines (262 loc) · 7.74 KB
/
Copy pathvideo.c
File metadata and controls
316 lines (262 loc) · 7.74 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* OpenBOR - http://www.chronocrash.com
* -----------------------------------------------------------------------
* All rights reserved, see LICENSE in OpenBOR root for details.
*
* Copyright (c) 2004 - 2014 OpenBOR Team
*/
#if ANDROID
// CRxTRDude - changed the directory for neatness.
#include "android/jni/openbor/video.c"
#else
#include "sdlport.h"
#include "SDL2_framerate.h"
#include <math.h>
#include "types.h"
#include "video.h"
#include "vga.h"
#include "screen.h"
#include "opengl.h"
#include "savedata.h"
#include "gfxtypes.h"
#include "gfx.h"
#include "pngdec.h"
#include "videocommon.h"
#include "../resources/OpenBOR_Icon_32x32_png.h"
SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static SDL_Texture *texture = NULL;
FPSmanager framerate_manager;
s_videomodes stored_videomodes;
yuv_video_mode stored_yuv_mode;
int yuv_mode = 0;
char windowTitle[MAX_LABEL_LEN] = {"OpenBOR"};
int stretch = 0;
int opengl = 0; // OpenGL backend currently in use?
int nativeWidth, nativeHeight; // monitor resolution used in fullscreen mode
int brightness = 0;
void initSDL()
{
SDL_DisplayMode video_info;
int init_flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC;
/*#if EE_CURRENT_PLATFORM == EE_PLATFORM_WINDOWS
SDL_setenv("SDL_AUDIODRIVER", "directsound", true);
#endif*/
if(SDL_Init(init_flags) < 0)
{
printf("SDL Failed to Init!!!! (%s)\n", SDL_GetError());
borExit(0);
}
SDL_ShowCursor(SDL_DISABLE);
//atexit(SDL_Quit); //White Dragon: use SDL_Quit() into sdlport.c it's best practice!
#ifdef LOADGL
if(SDL_GL_LoadLibrary(NULL) < 0)
{
printf("Warning: couldn't load OpenGL library (%s)\n", SDL_GetError());
}
#endif
SDL_GetCurrentDisplayMode(0, &video_info);
nativeWidth = video_info.w;
nativeHeight = video_info.h;
printf("debug:nativeWidth, nativeHeight, bpp, Hz %d, %d, %d, %d\n", nativeWidth, nativeHeight, SDL_BITSPERPIXEL(video_info.format), video_info.refresh_rate);
SDL_initFramerate(&framerate_manager);
SDL_setFramerate(&framerate_manager, 200);
}
void video_set_window_title(const char* title)
{
if(window) SDL_SetWindowTitle(window, title);
strncpy(windowTitle, title, sizeof(windowTitle)-1);
}
static unsigned pixelformats[4] = {SDL_PIXELFORMAT_INDEX8, SDL_PIXELFORMAT_BGR565, SDL_PIXELFORMAT_BGR888, SDL_PIXELFORMAT_ABGR8888};
int SetVideoMode(int w, int h, int bpp, bool gl)
{
int flags = SDL_WINDOW_SHOWN | SDL_WINDOW_INPUT_FOCUS;
static bool last_gl = false;
static int last_x = SDL_WINDOWPOS_UNDEFINED;
static int last_y = SDL_WINDOWPOS_UNDEFINED;
if(gl) flags |= SDL_WINDOW_OPENGL;
if(savedata.fullscreen) flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
if(!(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP))
SDL_GetWindowPosition(window, &last_x, &last_y);
if(window && gl != last_gl)
{
SDL_DestroyWindow(window);
window = NULL;
}
last_gl = gl;
if(renderer) SDL_DestroyRenderer(renderer);
if(texture) SDL_DestroyTexture(texture);
renderer = NULL;
texture = NULL;
if(window)
{
if(savedata.fullscreen)
{
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
else
{
#ifndef WIN // hiding and showing the window is problematic on Windows
if(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP)
SDL_HideWindow(window);
#endif
SDL_SetWindowFullscreen(window, 0);
SDL_SetWindowSize(window, w, h);
SDL_SetWindowPosition(window, last_x, last_y);
SDL_ShowWindow(window);
}
}
else
{
window = SDL_CreateWindow(windowTitle, last_x, last_y, w, h, flags);
if(!window)
{
printf("Error: failed to create window: %s\n", SDL_GetError());
return 0;
}
SDL_Surface* icon = (SDL_Surface*)pngToSurface((void*)openbor_icon_32x32_png.data);
SDL_SetWindowIcon(window, icon);
SDL_FreeSurface(icon);
if(!savedata.fullscreen) SDL_GetWindowPosition(window, &last_x, &last_y);
}
if(!gl)
{
renderer = SDL_CreateRenderer(window, -1, 0);
if(!renderer)
{
printf("Error: failed to create renderer: %s\n", SDL_GetError());
return 0;
}
}
return 1;
}
int video_set_mode(s_videomodes videomodes)
{
stored_videomodes = videomodes;
yuv_mode = 0;
if(videomodes.hRes==0 && videomodes.vRes==0)
{
Term_Gfx();
return 0;
}
videomodes = setupPreBlitProcessing(videomodes);
// 8-bit color should be transparently converted to 32-bit
assert(videomodes.pixel == 2 || videomodes.pixel == 4);
// try OpenGL initialization first
if(savedata.usegl && video_gl_set_mode(videomodes)) return 1;
else opengl = 0;
if(!SetVideoMode(videomodes.hRes * videomodes.hScale,
videomodes.vRes * videomodes.vScale,
videomodes.pixel * 8, false))
{
return 0;
}
if(savedata.hwfilter ||
(videomodes.hScale == 1 && videomodes.vScale == 1 && !savedata.fullscreen))
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
else
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
texture = SDL_CreateTexture(renderer,
pixelformats[videomodes.pixel-1],
SDL_TEXTUREACCESS_STREAMING,
videomodes.hRes, videomodes.vRes);
SDL_ShowCursor(SDL_DISABLE);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
video_stretch(savedata.stretch);
return 1;
}
void video_fullscreen_flip()
{
int restore_yuv = yuv_mode;
savedata.fullscreen ^= 1;
if(window) video_set_mode(stored_videomodes);
if(restore_yuv) video_setup_yuv_overlay(&stored_yuv_mode);
}
void blit()
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
if (brightness > 0)
SDL_SetRenderDrawColor(renderer, 255, 255, 255, brightness-1);
else if (brightness < 0)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, (-brightness)-1);
SDL_RenderFillRect(renderer, NULL);
SDL_RenderPresent(renderer);
}
int video_copy_screen(s_screen* src)
{
// do any needed scaling and color conversion
s_videosurface *surface = getVideoSurface(src);
if(opengl) return video_gl_copy_screen(surface);
SDL_UpdateTexture(texture, NULL, surface->data, surface->pitch);
blit();
#if WIN || LINUX
SDL_framerateDelay(&framerate_manager);
#endif
return 1;
}
void video_clearscreen()
{
if(opengl) { video_gl_clearscreen(); return; }
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
void video_stretch(int enable)
{
stretch = enable;
if(window && !opengl)
{
if(stretch)
SDL_RenderSetLogicalSize(renderer, 0, 0);
else
SDL_RenderSetLogicalSize(renderer, stored_videomodes.hRes, stored_videomodes.vRes);
}
}
void video_set_color_correction(int gm, int br)
{
brightness = br;
if(opengl) video_gl_set_color_correction(gm, br);
}
int video_setup_yuv_overlay(const yuv_video_mode *mode)
{
stored_yuv_mode = *mode;
yuv_mode = 1;
if(opengl) return video_gl_setup_yuv_overlay(mode);
SDL_DestroyTexture(texture);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_YV12,
SDL_TEXTUREACCESS_STREAMING,
mode->width, mode->height);
if(!stretch)
SDL_RenderSetLogicalSize(renderer, mode->display_width, mode->display_height);
return texture ? 1 : 0;
}
int video_prepare_yuv_frame(yuv_frame *src)
{
if(opengl) return video_gl_prepare_yuv_frame(src);
SDL_UpdateYUVTexture(texture, NULL, src->lum, stored_yuv_mode.width,
src->cr, stored_yuv_mode.width/2, src->cb, stored_yuv_mode.width/2);
return 1;
}
int video_display_yuv_frame(void)
{
if(opengl) return video_gl_display_yuv_frame();
blit();
return 1;
}
void vga_vwait(void)
{
static int prevtick = 0;
int now = SDL_GetTicks();
int wait = 1000/60 - (now - prevtick);
if (wait>0)
{
SDL_Delay(wait);
}
else SDL_Delay(1);
prevtick = now;
}
#endif