aboutsummaryrefslogtreecommitdiff
path: root/src/psp/graphics.c
blob: 469c12ed487fa7c70a131c0a0d50811df6bb66dc (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
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "graphics.h"
#include "../game.h"
#include "../qda.h"

int screenOffsetX = 80;
int screenOffsetY = 16;
int fullscreen = 0;
int blurFilter = 0;

//One time graphics setup
void PHL_GraphicsInit()
{
	oslInit(0);
	oslInitGfx(OSL_PF_8888, 1);

	screen = oslCreateImage(320, 240, OSL_IN_VRAM, OSL_PF_5650);
	
	PHL_StartDrawing();
}

void PHL_GraphicsExit()
{
	oslEndGfx();
	oslDeleteImage(screen);
	
	//closeQDA();
}

void PHL_StartDrawing()
{
	oslStartDrawing();
	oslSetDrawBuffer(screen);
}

void PHL_EndDrawing()
{
	oslSetDrawBuffer(OSL_DEFAULT_BUFFER);

	oslSetBilinearFilter(blurFilter);
	oslDrawImageXY(screen, screenOffsetX, screenOffsetY);
	oslSetBilinearFilter(0);
	
	oslEndDrawing();
	oslEndFrame();
	oslSyncFrame();
}

void PHL_ForceScreenUpdate()
{
	oslSetDrawBuffer(OSL_DEFAULT_BUFFER);
	oslDrawImageXY(screen, screenOffsetX, screenOffsetY);	
	oslEndDrawing();
	oslAudioVSync();
	oslSyncFrame();
	
	oslStartDrawing();
	oslSetDrawBuffer(screen);
}

void PHL_SetDrawbuffer(OSL_IMAGE* surf)
{
	//unused in psp version
}

void PHL_ResetDrawbuffer()
{
	//unused in psp version
}

void PHL_SetColorKey(OSL_IMAGE* surf, int r, int g, int b)
{
	
}

OSL_IMAGE* PHL_NewSurface(int w, int h)
{
	OSL_IMAGE* surf = oslCreateImage(w, h, OSL_IN_RAM, OSL_PF_8BIT);
	
	return surf;
}

void PHL_FreeSurface(OSL_IMAGE* surf)
{
	if (surf) {
		oslDeleteImage(surf);
		surf = NULL;
	}
}

OSL_IMAGE* PHL_LoadBMP(int index)
{
	OSL_IMAGE* result = NULL;
	
	FILE* f;
	if ( (f = fopen("bmp.qda", "rb")) ) {
		//Load QDA file data
		unsigned char* QDAFile = (unsigned char*)malloc(headers[index].size);
		fseek(f, headers[index].offset, SEEK_SET);
		fread(QDAFile, headers[index].size, 1, f);
		
		OSL_COLOR palette[20][18];
		
		//Read data from header
		unsigned short w, h;
				
		memcpy(&w, &QDAFile[18], 2);
		memcpy(&h, &QDAFile[22], 2);
		
		result = oslCreateImage(w, h, OSL_IN_RAM, OSL_PF_8888);
		oslUnswizzleImage(result);
		oslClearImage(result, RGBA(255, 255, 255, 255));
		
		//Load Palette
		int dx, dy;
		int count = 0;
		for (dx = 0; dx < 20; dx++) {
			for (dy = 0; dy < 16; dy++) {
				palette[dx][dy] = RGB(QDAFile[54 + count + 2], QDAFile[54 + count + 1], QDAFile[54 + count]);
				count += 4;
			}
		}
		
		OSL_COLOR alphaKey = palette[0][0];
		
		//Darkness special case
		if (index == 27) {
			alphaKey = RGB(0, 0, 0);
		}
		
		//Edit surface pixels
		oslLockImage(result);
		for (dx = w; dx > 0; dx--) {
			for (dy = h; dy >= 0; dy--) {		
				int pix = w - dx + w * dy;

				int px = QDAFile[1078 + pix] / 16;
				int py = QDAFile[1078 + pix] % 16;
				
				if (palette[px][py] == alphaKey) {
					oslSetImagePixel(result, w - dx, h - dy - 1, RGBA(0, 0, 0, 0));
				}else{					
					oslSetImagePixel(result, w - dx, h - dy - 1, palette[px][py]);
				}			
			}
		}
		oslUnlockImage(result);
		
		oslSwizzleImage(result);
		
		free(QDAFile);
	}
	
	fclose(f);
	
	return result;
}

void PHL_DrawRect(int x, int y, int w, int h, OSL_COLOR col)
{
	oslDrawFillRect(x/2, y/2, (x + w) / 2, (y + h) / 2, col);
}

void PHL_DrawSurface(double x, double y, OSL_IMAGE* surface)
{
	oslDrawImageXY(surface, (int)(x/2), (int)(y/2));
}

void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, OSL_IMAGE* surface)
{
	//Consider earthquake
	if (quakeTimer > 0) {
		int val = quakeTimer % 4;
		if (val == 0) {
			y -= 2;
		}else if (val == 2) {
			y += 2;
		}
	}
	
	OSL_IMAGE *crop = oslCreateImageTileSize(surface, cropx/2, cropy/2, cropw/2, croph/2);
	oslDrawImageXY(crop, (int)(x/2), (int)(y/2));
	oslDeleteImage(crop);
}

void PHL_DrawBackground(PHL_Background back, PHL_Background fore)
{
	int xx, yy;
	
	for (yy = 0; yy < 12; yy++)
	{
		for (xx = 0; xx < 16; xx++)
		{
			//Draw Background tiles
			PHL_DrawSurfacePart(xx * 40, yy * 40, back.tileX[xx][yy] * 40, back.tileY[xx][yy] * 40, 40, 40, images[imgTiles]);
			
			//Only draw foreground tile if not a blank tile
			if (fore.tileX[xx][yy] != 0 || fore.tileY[xx][yy] != 0) {
				PHL_DrawSurfacePart(xx * 40, yy * 40, fore.tileX[xx][yy] * 40, fore.tileY[xx][yy] * 40, 40, 40, images[imgTiles]);
			}
		}
	}
}

void PHL_UpdateBackground(PHL_Background back, PHL_Background fore)
{
	//Unused in psp version
}

void setBlur(int blur)
{
	blurFilter = blur;
}

int getBlur()
{
	return blurFilter;
}

void setScreenSize(int size)
{
	int screenW = 320;
	int screenH = 240;
	
	if (size == 1) {
		screenW = 368;
		screenH = 272;		
	}
	if (size == 2) {
		screenW = 480;
		screenH = 272;
	}
	
	screenOffsetX = (480 - screenW) / 2;
	screenOffsetY = (272 - screenH) / 2;
	
	screen->stretchX = screenW;
	screen->stretchY = screenH;
}

int getScreenSize()
{
	if (screen->stretchX == 368) {
		return 1;
	}
	if (screen->stretchX == 480) {
		return 2;
	}
	return 0;
}