Source

sdl2pal / src / video.c

  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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "video.h"
#include "util.h"

static struct
{
	SDL_Window *window;
	SDL_Renderer *renderer;
	SDL_Surface *screenReal;
	SDL_Surface *screen;
	SDL_Surface *screenBak;

	Uint32 shakeTime;
	Uint32 shakeLevel;
} G;

/*
	Purpose:
		Initialize the video subsystem.

	Parameters:
		[IN] w - screen width.
		[IN] h - screen height.
		[IN] fullscreen - true to use fullscreen mode, false to use windowed mode.

	Return value:
		0  = success
		-1 = fail to create the screen surface
		-2 = fail to create screen buffer
*/
int VIDEO_Init(Uint32 w, Uint32 h, bool fullscreen)
{
	// TODO here
	w = 640;
	h = 480;

	G.window = SDL_CreateWindow("SDL2PAL",
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN);
	assert(G.window != NULL);

	G.renderer = SDL_CreateRenderer(G.window, -1, 0);
	assert(G.renderer != NULL);

	G.screenReal = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
	assert(G.screenReal != NULL);

	G.screen = SDL_CreateRGBSurface(0, 320, 200, 8, 0, 0, 0, 0);
	assert(G.screen != NULL);

	G.screenBak = SDL_CreateRGBSurface(0, 320, 200, 8, 0, 0, 0, 0);
	assert(G.screenBak != NULL);

	G.shakeTime = G.shakeLevel = 0;

	SDL_SetRenderDrawColor(G.renderer, 0, 0, 0, 255);
	SDL_RenderClear(G.renderer);

	// TODO, fullscreen

	return 0;
}

/*
	Purpose:
		Shutdown the video subsystem.

	Parameters:
		None.

	Return value:
		None.
*/
void VIDEO_Shutdown(void)
{
	SDL_FreeSurface(G.screenBak);
	G.screenBak = NULL;

	SDL_FreeSurface(G.screen);
	G.screen = NULL;

	SDL_FreeSurface(G.screenReal);
	G.screenReal = NULL;

	SDL_DestroyRenderer(G.renderer);
	G.renderer = NULL;

	SDL_DestroyWindow(G.window);
	G.window = NULL;

	G.shakeTime = G.shakeLevel = 0;
}

/*
	Purpose:
		Update the screen surface to vga card.

	Parameters:
		[IN] surface - surface to render
		[IN] rect - rect area to render, render the whole surface if it's NULL

	Return value:
		None.
*/
static void VIDEO_Present()
{
	SDL_Texture *texture;

	texture = SDL_CreateTextureFromSurface(G.renderer, G.screenReal);
	assert(texture != NULL);

	SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE);
	SDL_RenderCopy(G.renderer, texture, NULL, NULL);

	SDL_DestroyTexture(texture);

	SDL_RenderPresent(G.renderer);
}

/*
	Purpose:
		Update the screen area specified by rect.

	Parameters:
		[IN] rect - screen area to update.

	Return value:
		None.
*/
void VIDEO_UpdateScreen(SDL_Rect *rect)
{
	SDL_Rect srcrect, dstrect;
	Uint16 offset           = 240 - 200;
	Uint16 screenRealHeight = G.screenReal->h - offset;
	Uint16 screenRealY      = offset / 2;

	if (rect != NULL)
	{
		dstrect.x = rect->x * G.screenReal->w / G.screen->w;
		dstrect.y = (screenRealY + rect->y) * screenRealHeight / G.screen->h;
		dstrect.w = rect->w * G.screenReal->w / G.screen->w;
		dstrect.h = rect->h * screenRealHeight / G.screen->h;

		SDL_SoftStretch(G.screen, rect, G.screenReal, &dstrect);
		VIDEO_Present();
	}
	else if (G.shakeTime != 0)
	{
		srcrect.x = 0;
		srcrect.y = 0;
		srcrect.w = 320;
		srcrect.h = 200 - G.shakeLevel;

		dstrect.x = 0;
		dstrect.y = screenRealY;
		dstrect.w = 320 * G.screenReal->w / G.screen->w;
		dstrect.h = (200 - G.shakeLevel) * screenRealHeight / G.screen->h;

		if (G.shakeTime & 1)
		{
			srcrect.y = G.shakeLevel;
		}
		else
		{
			dstrect.y = (screenRealY + G.shakeLevel) * screenRealHeight / G.screen->h;
		}

		SDL_SoftStretch(G.screen, &srcrect, G.screenReal, &dstrect);

		if (G.shakeTime & 1)
		{
			dstrect.y = (screenRealY + screenRealHeight - G.shakeLevel) * screenRealHeight / G.screen->h;
		}
		else
		{
			dstrect.y = screenRealY;
		}

		dstrect.h = G.shakeLevel * screenRealHeight / G.screen->h;
		SDL_FillRect(G.screenReal, &dstrect, 0);

		VIDEO_Present();
		G.shakeTime--;
	}
	else
	{
		dstrect.x = 0;
		dstrect.y = screenRealY;
		dstrect.w = G.screenReal->w;
		dstrect.h = screenRealHeight;

		SDL_SoftStretch(G.screen, NULL, G.screenReal, &dstrect);
		//SDL_FillRect(G.screenReal, &dstrect, 0xff0000);
		VIDEO_Present();
	}
}

/*
	Purpose:
		Set the palette of the screen.

	Parameters:
		[IN] palette - array of 256 colors.

	Return value:
		None.
*/
void VIDEO_SetPalette(SDL_Color colors[256])
{
	SDL_Palette *palette = SDL_AllocPalette(256);
	assert(palette != NULL);

	SDL_SetPaletteColors(palette, colors, 0, 256);
	SDL_SetSurfacePalette(G.screenReal, palette);

	SDL_FreePalette(palette);
}

/*
	Purpose:
		Get the current palette of the screen.

	Parameters:
		None.

	Return value:
		Pointer to the current palette.
*/
SDL_Color *VIDEO_GetPalette(void)
{
	return G.screenReal->format->palette->colors;
}

/*
	Purpose:
		This function is called when user resized the window.

	Parameters:
		[IN] w - width of the window after resizing.
		[IN] h - height of the window after resizing.

	Return value:
		None.
*/
void VIDEO_Resize(Uint32 w, Uint32 h)
{
	// TODO
}

/*
	Purpose:
		Toggle fullscreen mode.

	Parameters:
		None.

	Return value:
		None.
*/
void VIDEO_ToggleFullscreen(void)
{
	// TODO
}

/*
	Purpose:
		Save the screenshot of current screen to a BMP file.

	Parameters:
		None.

	Return value:
		None.
*/
void VIDEO_SaveScreenshot(void)
{
	int i;
	FILE *fp;
	const char *file;

	for (i = 0; i <= 9999; i++)
	{
		file = va("%sscrn%.4d.bmp", PAL_PREFIX, i);
		fp = fopen(file, "rb");
		if (fp == NULL)
		{
			fclose(fp);
			break;
		}
		fclose(fp);
	}

	if (i > 9999)
		return;

	SDL_SaveBMP(G.screenReal, file);
}

/*
	Purpose:
		Backup the screen buffer.

	Parameters:
		None.

	Return value:
		None.
*/
void VIDEO_BackupScreen(void)
{
	SDL_BlitSurface(G.screen, NULL, G.screenBak, NULL);
}

/*
	Purpose:
		Restore the screen buffer which has been saved with VIDEO_BackupScreen().

	Parameters:
		None.

	Return value:
		None.
*/
void VIDEO_RestoreScreen(void)
{
	SDL_BlitSurface(G.screenBak, NULL, G.screen, NULL);
}

/*
	Purpose:
		Set the screen shake time and level.

	Parameters:
		[IN] shakeTime  - how many times should we shake the screen.
		[IN] shakeLevel - level of shaking.

	Return value:
		None.
*/
void VIDEO_ShakeScreen(Uint16 shakeTime, Uint16 shakeLevel)
{
	G.shakeTime  = shakeTime;
	G.shakeLevel = shakeLevel;
}

/*
	Purpose:
		Swtich the screen from the backup screen buffer to the current screen buffer.
		NOTE: This will destroy the backup buffer.

	Parameters:
		[IN] speed - speed of fading (the larger value, the slower).

	Return value:
		None.
*/
void VIDEO_SwitchScreen(Uint16 speed)
{
	int i, j;
	const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
	SDL_Rect dstrect;

	Uint16 offset           = 240 - 200;
	Uint16 screenRealHeight = G.screenReal->h - offset;
	Uint16 screenRealY      = offset / 2;

	speed++;
	speed *= 10;

	for (i = 0; i < 6; i++)
	{
		for (j = rgIndex[i]; j < G.screen->pitch*G.screen->h; j += 6)
		{
			((unsigned char*)(G.screenBak->pixels))[j] = ((unsigned char*)(G.screen->pixels))[j];
		}

		// draw the backup buffer to the screen
		dstrect.x = 0;
		dstrect.y = screenRealY;
		dstrect.w = G.screenReal->w;
		dstrect.h = screenRealHeight;

		SDL_SoftStretch(G.screenBak, NULL, G.screenReal, &dstrect);
		VIDEO_Present();

		UTIL_Delay(speed);
	}
}

/*
	Purpose:
		Fade the screen from the backup screen buffer to the current screen buffer.
		NOTE: This will destroy the backup buffer.

	Parameters:
		[IN] speed - speed of fading (the larger value, the slower).

	Return value:
		None.
*/
void VIDEO_FadeScreen(Uint16 speed)
{
	// TODO
}

/*
	Purpose:
		Return the screen surface for drawing.

	Parameters:
		None.

	Return value:
		the screen surface.
*/
SDL_Surface *VIDEO_GetScreen(void)
{
	return G.screen;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.