aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/graphics.c
blob: 4af23817f81e4507512fcc19792d9f8cddcb1673 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <SDL/SDL.h>
#include "../PHL.h"
#include "../game.h"
#include "../qda.h"
#include "graphics.h"

SDL_Surface* screen = NULL;
SDL_Surface* drawbuffer = NULL;
SDL_Surface* backbuffer = NULL;

int wantFullscreen = 0;
int screenScale = 2;
int desktopFS = 0;

int deltaX = 0;
int deltaY = 0;

int screenW = 640;
int screenH = 480;

int drawscreen = 0;

static uint32_t tframe;

extern void Input_InitJoystick();
extern void Input_CloseJoystick();

SDL_Color PHL_NewRGB(uint8_t r, uint8_t g, uint8_t b)
{
    SDL_Color ret = {.r = r, .b = b, .g = g};
    return ret;
}

void PHL_GraphicsInit()
{
	SDL_ShowCursor(SDL_DISABLE);

	Input_InitJoystick();
    
    uint32_t flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
	if(wantFullscreen || desktopFS)
    	flags |= SDL_FULLSCREEN;
    screen = SDL_SetVideoMode((desktopFS)?0:screenW, (desktopFS)?0:screenH, 0, flags);
	if(desktopFS)
	{
		const SDL_VideoInfo* infos = SDL_GetVideoInfo();
		screenH = infos->current_h;
		screenW = infos->current_w;
		if(screenW/320 < screenH/240)
			screenScale = screenW/320;
		else
			screenScale = screenH/240; // automatic guess the scale
		deltaX = (screenW-320*screenScale)/2;
		deltaY = (screenH-240*screenScale)/2;
	}
	drawbuffer = screen;
	drawscreen = 1;
	backbuffer = PHL_NewSurface(320*screenScale, 240*screenScale);
	tframe = SDL_GetTicks();
}

void PHL_GraphicsExit()
{
	Input_CloseJoystick();
    SDL_FreeSurface(backbuffer);
	SDL_Quit();    
}

void PHL_StartDrawing()
{
	PHL_ResetDrawbuffer();
}
void PHL_EndDrawing()
{
	// handle black borders
	if(deltaX) {
		SDL_Rect rect = {0, 0, deltaX, screenH};
		SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
		rect.x = screenW - deltaX -1;
		SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
	}
	if(deltaY) {
		SDL_Rect rect = {0, 0, screenW, deltaY};
		SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
		rect.y = screenH - deltaY -1;
		SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
	}

    SDL_Flip(screen);
	uint32_t tnext = tframe + 1000/60;
	while((tframe = SDL_GetTicks())<tnext)
        SDL_Delay(10);
}

void PHL_ForceScreenUpdate()
{

}

void PHL_SetDrawbuffer(PHL_Surface surf)
{
	drawbuffer = surf;
	drawscreen = (surf==screen);
}
void PHL_ResetDrawbuffer()
{
	drawbuffer = screen;
	drawscreen = 1;
}

//PHL_RGB PHL_NewRGB(int r, int g, int b);
void PHL_SetColorKey(PHL_Surface surf, int r, int g, int b)
{
    SDL_SetColorKey(surf, SDL_SRCCOLORKEY, SDL_MapRGB(surf->format, r, g, b));
}

PHL_Surface PHL_NewSurface(int w, int h)
{
    return SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0);
}
void PHL_FreeSurface(PHL_Surface surf)
{
    SDL_FreeSurface(surf);
}

//PHL_Surface PHL_LoadBMP(char* fname);
PHL_Surface PHL_LoadBMP(int index)
{
	SDL_Surface* surf;
	
	FILE* f;
	if ( (f = fopen("data/bmp.qda", "rb")) ) {
		uint8_t* QDAFile = (uint8_t*)malloc(headers[index].size);
		fseek(f, headers[index].offset, SEEK_SET);
		fread(QDAFile, 1, headers[index].size, f);
		fclose(f);
		
		PHL_RGB palette[20][18];
		
		uint16_t w, h;
		
		//Read data from header
		memcpy(&w, &QDAFile[18], 2);
		memcpy(&h, &QDAFile[22], 2);
		
        surf = PHL_NewSurface(w * screenScale, h * screenScale);
		//surf = PHL_NewSurface(200, 200);

		//Load Palette
		int dx, dy;
		int count = 0;
		for (dx = 0; dx < 20; dx++) {
			for (dy = 0; dy < 16; dy++) {
				palette[dx][dy].b = QDAFile[54 + count];
				palette[dx][dy].g = QDAFile[54 + count + 1];
				palette[dx][dy].r = QDAFile[54 + count + 2];
				count += 4;
			}
		}
		
		for (dx = 0; dx < w; dx++) {
			for (dy = 0; dy < h; dy++) {
			
				int pix = dx + w * dy;
				int px = QDAFile[1078 + pix] / 16;
				int py = QDAFile[1078 + pix] % 16;
				//Get transparency from first palette color
				if (dx == 0 && dy == 0) {					
					//Darkness special case
					if (index == 27) {
						SDL_SetColorKey(surf, SDL_SRCCOLORKEY, SDL_MapRGB(surf->format, 0x00, 0x00, 0x00));
					}else{
						SDL_SetColorKey(surf, SDL_SRCCOLORKEY, SDL_MapRGB(surf->format, palette[0][0].r, palette[0][0].g, palette[0][0].b));
					}
				}
				
				PHL_RGB c = palette[px][py];
				
				//PHL_DrawRect(dx * 2, dy * 2, 2, 2, c);
				SDL_Rect rect = {dx * screenScale, (h-1-dy) * screenScale, screenScale, screenScale};	
				SDL_FillRect(surf, &rect, SDL_MapRGB(surf->format, c.r, c.g, c.b));
			}
		}
		free(QDAFile);
	}
	
	return surf;
}

void PHL_DrawRect(int x, int y, int w, int h, SDL_Color col)
{
	SDL_Rect rect = {x*screenScale/2 + (drawscreen?deltaX:0), y*screenScale/2 + (drawscreen?deltaY:0), w*screenScale/2, h*screenScale/2};
	
	SDL_FillRect(drawbuffer, &rect, SDL_MapRGB(drawbuffer->format, col.r, col.g, col.b));
}

void PHL_DrawSurface(double x, double y, PHL_Surface surface)
{
	if (quakeTimer > 0) {
		int val = quakeTimer % 4;
		if (val == 0) {
			y -= 1;
		} else if (val == 2) {
			y += 1;
		}
	}
	
	SDL_Rect offset;
	offset.x = x*screenScale/2 + (drawscreen?deltaX:0);
	offset.y = y*screenScale/2 + (drawscreen?deltaY:0);
	
	SDL_BlitSurface(surface, NULL, drawbuffer, &offset);
}
void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, PHL_Surface surface)
{
	if (quakeTimer > 0) {
		int val = quakeTimer % 4;
		if (val == 0) {
			y -= (screenScale==1)?2:1;
		}else if (val == 2) {
			y += (screenScale==1)?2:1;
		}
	}
	
	SDL_Rect crop, offset;
	crop.x = cropx*screenScale/2;
	crop.y = cropy*screenScale/2;
	crop.w = cropw*screenScale/2;
	crop.h = croph*screenScale/2;
	
	offset.x = x*screenScale/2 + (drawscreen?deltaX:0);
	offset.y = y*screenScale/2 + (drawscreen?deltaY:0);
	
	SDL_BlitSurface(surface, &crop, drawbuffer, &offset);
}

void PHL_DrawBackground(PHL_Background back, PHL_Background fore)
{
	PHL_DrawSurface(0, 0, backbuffer);
}
void PHL_UpdateBackground(PHL_Background back, PHL_Background fore)
{
	PHL_SetDrawbuffer(backbuffer);
	
	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]);
			}
		}
	}
	
	PHL_ResetDrawbuffer();
}