summaryrefslogtreecommitdiff
path: root/Source/Gfx.c
blob: b9ffb6382f71c632af302421c93659a76f0ebbf8 (plain) (blame)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/* *************************************
 * 	Includes
 * *************************************/

#include "Gfx.h"

/* *************************************
 * 	Defines
 * *************************************/

#define PRIMITIVE_LIST_SIZE 0x1000
#define DOUBLE_BUFFERING_SWAP_Y	256
#define UPLOAD_IMAGE_FLAG 1
#define MAX_LUMINANCE 0xFF
#define ROTATE_BIT_SHIFT 12
#define GPUSTAT (*(unsigned int*)0x1F801814)
#define D2_CHCR (*(unsigned int*)0x1F8010A8)

/* *************************************
 * 	Structs and enums
 * *************************************/

enum
{
	BUTTON_SIZE = 16,
	
	BUTTON_CROSS_U = 48,
	BUTTON_CROSS_V = 0,
	
	BUTTON_SQUARE_U = 0,
	BUTTON_SQUARE_V = 0,
	
	BUTTON_TRIANGLE_U = 32,
	BUTTON_TRIANGLE_V = 0,
	
	BUTTON_CIRCLE_U = 16,
	BUTTON_CIRCLE_V = 0,

	BUTTON_DIRECTION_U = 64,
	BUTTON_DIRECTION_V = 0,
	
	BUTTON_LR_U = 80,
	BUTTON_LR_V = 0,
	BUTTON_LR_SIZE = 24,

	LETTER_SIZE = 8,

	LETTER_L1_U = 104,
	LETTER_L1_V = 0,

	LETTER_L2_U = 112,
	LETTER_L2_V = 0,

	LETTER_R1_U = 104,
	LETTER_R1_V = 8,

	LETTER_R2_U = 112,
	LETTER_R2_V = 8,

	LETTER_OFFSET_INSIDE_BUTTON_LR_X = 8,
	LETTER_OFFSET_INSIDE_BUTTON_LR_Y = 6
	
};

enum
{
	GFX_SECOND_DISPLAY_X = 384,
	GFX_SECOND_DISPLAY_Y = 256,
	GFX_SECOND_DISPLAY_TPAGE = 22
};

/* *************************************
 * 	Global Variables
 * *************************************/

GsSprite PSXButtons;

/* *************************************
 * 	Local Prototypes
 * *************************************/



/* *************************************
 * 	Local Variables
 * *************************************/

// Drawing environment
static GsDrawEnv DrawEnv;
// Display environment
static GsDispEnv DispEnv;
// Primitive list (it contains all the graphical data for the GPU)
static unsigned int prim_list[PRIMITIVE_LIST_SIZE];
// Tells other modules whether data is being loaded to GPU
static volatile bool gfx_busy;
// Dictates (R,G,B) brigthness to all sprites silently
static uint8_t global_lum;
// When true, it draws a rectangle on top of all primitives with
// information for development purposes.
static bool GfxDevMenuEnableFlag;

void GfxSwapBuffers(void)
{
	// Consistency check
#if PSXSDK_DEBUG

	if(GsListPos() >= PRIMITIVE_LIST_SIZE)
	{
		dprintf("Linked list iterator overflow!\n");
		while(1);
	}
	
	if( (DrawEnv.h != Y_SCREEN_RESOLUTION)
					||
		( (DrawEnv.w != X_SCREEN_RESOLUTION)
						&&
		  (DrawEnv.w != X_SCREEN_RESOLUTION >> 1) )
					||
		( (DispEnv.y != DOUBLE_BUFFERING_SWAP_Y)
						&&
		  (DispEnv.y != 0) )		)
	{
		dprintf("What the hell is happening?\n");
		DEBUG_PRINT_VAR(DispEnv.x);
		DEBUG_PRINT_VAR(DispEnv.y);
		DEBUG_PRINT_VAR(DrawEnv.x);
		DEBUG_PRINT_VAR(DrawEnv.y);

		while(1);
	}
#endif // PSXSDK_DEBUG

	if(DrawEnv.h == Y_SCREEN_RESOLUTION)
	{
		if(DispEnv.y == 0)
		{
			DispEnv.y = DOUBLE_BUFFERING_SWAP_Y;
			DrawEnv.y = 0;
		}
		else if(DispEnv.y == DOUBLE_BUFFERING_SWAP_Y)
		{
			DispEnv.y = 0;
			DrawEnv.y = DOUBLE_BUFFERING_SWAP_Y;
		}
			
		GsSetDispEnv(&DispEnv);
		GsSetDrawEnv(&DrawEnv);
	}
}

void GfxDevMenuEnable(void)
{
    GfxDevMenuEnableFlag = true;
}

void GfxInitDrawEnv(void)
{
	DrawEnv.x = 0;
	DrawEnv.y = 0;
	DrawEnv.draw_on_display = false;
	DrawEnv.w = X_SCREEN_RESOLUTION;
	DrawEnv.h = Y_SCREEN_RESOLUTION;
	
	GsSetDrawEnv(&DrawEnv);
}

void GfxInitDispEnv(void)
{
	DispEnv.x = 0;
	DispEnv.y = 0;
	
	GsSetDispEnv(&DispEnv);
}

void GfxSetPrimitiveList(void)
{
	GsSetList(prim_list);
}

void GfxDrawScene_Fast(void)
{	
	GfxSwapBuffers();
	FontCyclic();
	GsDrawList();
}

bool GfxReadyForDMATransfer(void)
{
	return ( (GPUSTAT & 1<<28) && !(D2_CHCR & 1<<24) );
}

void GfxDrawScene(void)
{
	while(	(SystemRefreshNeeded() == false) 
				||
			(GfxIsGPUBusy() == true)		);
			
	GfxDrawScene_Fast();
	
	SystemCyclicHandler();
}

void GfxDrawScene_Slow(void)
{
	GfxDrawScene();
	while(GfxIsGPUBusy() == true);
}

void GfxSortSprite(GsSprite * spr)
{
	uint8_t aux_r = spr->r;
	uint8_t aux_g = spr->g;
	uint8_t aux_b = spr->b;
	unsigned char aux_tpage = spr->tpage;
	short aux_w = spr->w;
	short aux_x = spr->x;
	
	if(	(spr->w <= 0) || (spr->h <= 0) )
	{
		// Invalid width or heigth
		return;
	}
	
	if(GfxIsSpriteInsideScreenArea(spr) == false)
	{
		return;
	}
	
	if(global_lum != NORMAL_LUMINANCE)
	{
		if(spr->r < NORMAL_LUMINANCE - global_lum)
		{
			spr->r = 0;
		}
		else
		{
			spr->r -= NORMAL_LUMINANCE - global_lum;
		}
		
		if(spr->g < NORMAL_LUMINANCE - global_lum)
		{
			spr->g = 0;
		}
		else
		{
			spr->g -= NORMAL_LUMINANCE - global_lum;
		}
		
		if(spr->b < NORMAL_LUMINANCE - global_lum)
		{
			spr->b = 0;
		}
		else
		{
			spr->b -= NORMAL_LUMINANCE - global_lum;
		}
	}

	if(spr->w > MAX_SIZE_FOR_GSSPRITE)
	{
		// GsSprites can't be bigger than 256x256, so since display
		// resolution is 384x240, it must be split into two primitives.
		
		spr->w = MAX_SIZE_FOR_GSSPRITE;
		GsSortSprite(spr);

		spr->x += MAX_SIZE_FOR_GSSPRITE;
		spr->w = X_SCREEN_RESOLUTION - MAX_SIZE_FOR_GSSPRITE;
		spr->tpage += MAX_SIZE_FOR_GSSPRITE / GFX_TPAGE_WIDTH;
		GsSortSprite(spr);
		
		// Restore original values after sorting
		spr->w = aux_w;
		spr->tpage = aux_tpage;
		spr->x = aux_x;
	}
	else
	{
		GsSortSprite(spr);
	}
	
	spr->r = aux_r;
	spr->g = aux_g;
	spr->b = aux_b;
}

uint8_t GfxGetGlobalLuminance(void)
{
	return global_lum;
}

void GfxSetGlobalLuminance(uint8_t value)
{
	global_lum = value;
}

void GfxIncreaseGlobalLuminance(int8_t step)
{	
	if( (	(global_lum + step) < MAX_LUMINANCE )
			&&
		(	(global_lum + step) > 0	)			)
	{
		global_lum += step;
	}
	else
	{
		global_lum = MAX_LUMINANCE;
	}
}

int GfxRotateFromDegrees(int deg)
{
	return deg << ROTATE_BIT_SHIFT;
}

bool GfxIsGPUBusy(void)
{
	return (GsIsDrawing() || gfx_busy || (GfxReadyForDMATransfer() == false) );
}

bool GfxSpriteFromFile(char* fname, GsSprite * spr)
{
	GsImage gsi;
	
	if(SystemLoadFile(fname) == false)
	{
		return false;
	}
	
	while(GfxIsGPUBusy() == true);
	
	gfx_busy = true;
		
	GsImageFromTim(&gsi,SystemGetBufferAddress() );
	
	GsSpriteFromImage(spr,&gsi,UPLOAD_IMAGE_FLAG);
	gfx_busy = false;

    DEBUG_PRINT_VAR(spr->tpage);
    DEBUG_PRINT_VAR(spr->u);
    DEBUG_PRINT_VAR(spr->v);
    DEBUG_PRINT_VAR(spr->w);
    DEBUG_PRINT_VAR(spr->h);
	
	return true;
}

bool GfxCLUTFromFile(char* fname)
{
	GsImage gsi;

	if(SystemLoadFile(fname) == false)
	{
		return false;
	}
	
	while(GfxIsGPUBusy() == true);
	
	gfx_busy = true;
		
	GsImageFromTim(&gsi,SystemGetBufferAddress() );
	
	GsUploadCLUT(&gsi);
	
	gfx_busy = false;
	
	return true;
}

bool GfxIsInsideScreenArea(short x, short y, short w, short h)
{
	if( ( (x + w) >= 0) 
			&&
		(x < DrawEnv.w)
			&&
		( (y + h) >= 0)
			&&
		(y < DrawEnv.h)	)
	{
		return true;
	}
		
	return false;
}

bool GfxIsSpriteInsideScreenArea(GsSprite * spr)
{
	return GfxIsInsideScreenArea(spr->x, spr->y, spr->w, spr->h);
}

void GfxButtonSetFlags(uint8_t flags)
{
	PSXButtons.attribute |= flags;
}

void GfxButtonRemoveFlags(uint8_t flags)
{
	PSXButtons.attribute &= ~flags;
}

void GfxDrawButton(short x, short y, unsigned short btn)
{
	static bool first_entered = true;
	static short orig_u;
	static short orig_v;
	
	if(first_entered == true)
	{
		first_entered = false;
		orig_u = PSXButtons.u;
		orig_v = PSXButtons.v;
	}
	
	PSXButtons.w = BUTTON_SIZE;
	PSXButtons.h = BUTTON_SIZE;
	
	PSXButtons.r = NORMAL_LUMINANCE;
	PSXButtons.g = NORMAL_LUMINANCE;
	PSXButtons.b = NORMAL_LUMINANCE;
	
	PSXButtons.x = x;
	PSXButtons.y = y;
	PSXButtons.mx = PSXButtons.w >> 1;
	PSXButtons.my = PSXButtons.h >> 1;
	
	switch(btn)
	{
		case PAD_CROSS:
			PSXButtons.u = BUTTON_CROSS_U;
			PSXButtons.v = BUTTON_CROSS_V;
		break;
		
		case PAD_SQUARE:
			PSXButtons.u = BUTTON_SQUARE_U;
			PSXButtons.v = BUTTON_SQUARE_V;
		break;
		
		case PAD_TRIANGLE:
			PSXButtons.u = BUTTON_TRIANGLE_U;
			PSXButtons.v = BUTTON_TRIANGLE_V;
		break;
		
		case PAD_CIRCLE:
			PSXButtons.u = BUTTON_CIRCLE_U;
			PSXButtons.v = BUTTON_CIRCLE_V;
		break;

		case PAD_RIGHT:
			PSXButtons.u = BUTTON_DIRECTION_U;
			PSXButtons.v = BUTTON_DIRECTION_V;
		break;
		
		case PAD_UP:
			PSXButtons.u = BUTTON_DIRECTION_U;
			PSXButtons.v = BUTTON_DIRECTION_V;
			PSXButtons.rotate = 90 << ROTATE_BIT_SHIFT;
		break;
		
		case PAD_DOWN:
			PSXButtons.u = BUTTON_DIRECTION_U;
			PSXButtons.v = BUTTON_DIRECTION_V;
			PSXButtons.rotate = 270 << ROTATE_BIT_SHIFT;
		break;

		case PAD_LEFT:
			PSXButtons.u = BUTTON_DIRECTION_U;
			PSXButtons.v = BUTTON_DIRECTION_V;
			PSXButtons.attribute |= H_FLIP;
		break;
		
		case PAD_L1:
			// Fall through
		case PAD_L2:
			// Fall through
		case PAD_R1:
			// Fall through
		case PAD_R2:
			PSXButtons.u = BUTTON_LR_U;
			PSXButtons.v = BUTTON_LR_V;
			PSXButtons.w = BUTTON_LR_SIZE;
		break;

		case PAD_SELECT:
			// Fall through
		case PAD_START:
			// Fall through
		default:
			// Set null width and height so that sprite doesn't get sorted
			PSXButtons.w = 0;
			PSXButtons.h = 0;
		break;
	}
	
	PSXButtons.u += orig_u;
	PSXButtons.v += orig_v;
	
	GfxSortSprite(&PSXButtons);

	PSXButtons.attribute &= ~H_FLIP;
	PSXButtons.rotate = 0;
}

void GfxSaveDisplayData(GsSprite *spr)
{
	while(GfxIsGPUBusy() == true);
	
	MoveImage(	DispEnv.x,
				DispEnv.y,
				GFX_SECOND_DISPLAY_X,
				GFX_SECOND_DISPLAY_Y,
				X_SCREEN_RESOLUTION,
				Y_SCREEN_RESOLUTION);
				
	spr->x = 0;
	spr->y = 0;
	spr->tpage = GFX_SECOND_DISPLAY_TPAGE;
	spr->attribute |= COLORMODE(COLORMODE_16BPP);
	spr->w = X_SCREEN_RESOLUTION;
	spr->h = Y_SCREEN_RESOLUTION;
	spr->u = 0;
	spr->v = 0;
	spr->r = NORMAL_LUMINANCE;
	spr->g = NORMAL_LUMINANCE;
	spr->b = NORMAL_LUMINANCE;

	while(GfxIsGPUBusy() == true);
}

bool GfxTPageOffsetFromVRAMPosition(GsSprite * spr, short x, short y)
{
	if(	(x >= VRAM_W) || (x < 0) || (y >= VRAM_H) || (y < 0) )
	{
		return false;
	}
	
	spr->tpage = x / GFX_TPAGE_WIDTH;
	spr->tpage += (short)(VRAM_W / GFX_TPAGE_WIDTH) * (short)(y / GFX_TPAGE_HEIGHT);
	
	spr->u = (x % GFX_TPAGE_WIDTH);
	
	if(spr->attribute & COLORMODE(COLORMODE_8BPP))
	{
		// On 8bpp images, it looks like U offset needs to be multiplied by 2.
		spr->u <<= 1;
	}
	
	spr->v = (y % GFX_TPAGE_HEIGHT);
	
	//dprintf("Sprite:\n\tTPAGE: %d\n\tU=%d\n\tV=%d\n",spr->tpage,spr->u, spr->v);
	
	return false;
}