summaryrefslogtreecommitdiff
path: root/src/graphics.c
blob: 50cb0ce224347795db6cd2ef1bcee9bb26f9ac31 (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
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
/*
	Simple graphic handling library
	Under GPL v2 License
	2011 by bitrider
*/
#include <stdlib.h>
#include <stdio.h>

#include "graphics.h"

static int clipping_x1 = 0; 
static int clipping_x2 = SCREEN_WIDTH - 1;
static int clipping_y1 = 0;
static int clipping_y2 = SCREEN_HEIGHT - 1;

void gSetClipping(int x1, int y1, int x2, int y2) {
	if (x1 < 0) x1 = 0;
	if (x1 > (SCREEN_WIDTH -1)) x1 = (SCREEN_WIDTH -1);
	if (x2 < 0) x2 = 0;
	if (x2 > (SCREEN_WIDTH -1)) x2 = (SCREEN_WIDTH -1);
	if (y1 < 0) y1 = 0;
	if (y1 > (SCREEN_HEIGHT - 1)) y1 = (SCREEN_HEIGHT - 1);
	if (y2 < 0) y2 = 0;
	if (y2 > (SCREEN_HEIGHT - 1)) y2 = (SCREEN_HEIGHT - 1);

	if (x1 < x2) {
		clipping_x1 = x1;
		clipping_x2 = x2;
	} else {
		clipping_x2 = x1;
		clipping_x1 = x2;
	}

	if (y1 < y2) {
		clipping_y1 = y1;
		clipping_y2 = y2;
	} else {
		clipping_y2 = y1;
		clipping_y1 = y2;
	}
}

void gClearClipping() {
	clipping_x1 = 0;
	clipping_y1 = 0;
	clipping_x2 = SCREEN_WIDTH - 1;
	clipping_y2 = SCREEN_HEIGHT - 1;
}

void gDestroyBitmap(gBITMAP *img) {
   if (img) {
	free(img->data);
	free(img);
	}
}

gBITMAP *gCreateBitmap(unsigned int width, unsigned int height, unsigned char bpp) {
   gBITMAP *img;

   if (bpp != 32) return NULL; // supported BPPs
   if ((!width) || (!height)) return NULL; 

   img = malloc(sizeof(gBITMAP));
   if (!img) return NULL;
   img->bpp = bpp;
   img->w = width;
   img->h = height;
   img->data = malloc(width * height * (bpp >> 3));
   if (!img->data) {
 	free(img);
	return NULL;
	}
   return img;
}

void gDrawPixel16(unsigned short *screen, int x, int y, unsigned char r, unsigned char g, unsigned char b) {
	unsigned int sr, sg, sb;
	unsigned short pixel;

	if ((x < clipping_x1) || (x > clipping_x2) || (y < clipping_y1) || (y > clipping_y2)) return;
	
	// blend 
	screen[y * SCREEN_WIDTH + x] = RGB16(r, g, b);
}

void gBlendPixel16(unsigned short *screen, int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
	unsigned int sr, sg, sb;
	unsigned short pixel;

	if ((x < clipping_x1) || (x > clipping_x2) || (y < clipping_y1) || (y > clipping_y2)) return;
	
	// get screen pixel
	pixel = screen[y * SCREEN_WIDTH + x];
	sr = PIXEL16_R(pixel); //((pixel >> 11) << 3);
	sg = PIXEL16_G(pixel); //((pixel >> 5) & 63) << 2;
	sb = PIXEL16_B(pixel); //(pixel & 31) << 3;
	// blend 
	screen[y * SCREEN_WIDTH + x] = RGB16(BLEND(r, a, sr), BLEND(g, a, sg), BLEND(b, a, sb));
}

void gBlendBitmap16(unsigned short *screen, int sx, int sy, gBITMAP *img, unsigned int ix, unsigned int iy, unsigned int iw, unsigned int ih) {
	int ssx;
	unsigned int sw, ijmp, sjmp;
	unsigned char *iaddr;
	unsigned int pixel;

	if ((!img) || (!img->data)) return; // sanity check
	if (img->bpp != 32) return; // supported BPPs
	if ((sx > clipping_x2) || (sy > clipping_y2) || 
	    ((sx + iw - 1) < clipping_x1) || ((sy + ih - 1) < clipping_y1)) return; // Out of screen	

	// Image dimensions
	if ((iw + ix) > img->w) iw = img->w - ix;
	if ((ih + iy) > img->h) ih = img->h - iy;

	// Clipping 
	if (sx < clipping_x1) {
		iw -= clipping_x1 - sx;
		sx = clipping_x1;
	}
	if (sy < clipping_y1) {
		ih -= clipping_y1 - sy;
		sy = clipping_y1;
	}
	if ((sx + iw - 1) > clipping_x2) iw -=  (sx + iw - 1) - clipping_x2; 
	if ((sy + ih - 1) > clipping_y2) ih -=  (sy + ih - 1) - clipping_y2; 

	ssx = sx;
	sw = iw;

	ijmp = (img->w - iw) * (img->bpp >> 3);
	iaddr = &img->data[(iy * img->w + ix) * 4 + 0];
	sjmp = SCREEN_WIDTH - iw;
	screen = &screen[sy * SCREEN_WIDTH + sx];

	for (; (ih > 0); ih--, iaddr += ijmp, screen += sjmp) { 
		for (iw = sw; (iw > 0); iw--, iaddr += 4, screen++) {
			// blend
			pixel = *((unsigned int *)iaddr);
			/* 
			*screen = RGB16(BLEND(iaddr[0], iaddr[3], PIXEL16_R(*screen)), 
					BLEND(iaddr[1], iaddr[3], PIXEL16_G(*screen)), 
					BLEND(iaddr[2], iaddr[3], PIXEL16_B(*screen)));
			*/
			*screen = RGB16(BLEND(PIXEL32_R(pixel), PIXEL32_A(pixel), PIXEL16_R(*screen)), 
					BLEND(PIXEL32_G(pixel), PIXEL32_A(pixel), PIXEL16_G(*screen)), 
					BLEND(PIXEL32_B(pixel), PIXEL32_A(pixel), PIXEL16_B(*screen)));
		}
	}
}

void gDrawBitmap16(unsigned short *screen, int sx, int sy, gBITMAP *img, unsigned int ix, unsigned int iy, unsigned int iw, unsigned int ih) {
	int ssx;
	unsigned int sw, ijmp, sjmp;
	unsigned char *iaddr;
	unsigned short pixel;

	if ((!img) || (!img->data)) return; // sanity check
	if ((img->bpp != 32) || (img->bpp != 32)) return; // supported BPPs
	if ((sx > clipping_x2) || (sy > clipping_y2) || 
	    ((sx + iw - 1) < clipping_x1) || ((sy + ih - 1) < clipping_y1)) return; // Out of screen	

	// Image dimensions
	if ((iw + ix) > img->w) iw = img->w - ix;
	if ((ih + iy) > img->h) ih = img->h - iy;

	// Clipping 
	if (sx < clipping_x1) {
		iw -= clipping_x1 - sx;
		sx = clipping_x1;
	}
	if (sy < clipping_y1) {
		ih -= clipping_y1 - sy;
		sy = clipping_y1;
	}
	if ((sx + iw - 1) > clipping_x2) iw -=  (sx + iw - 1) - clipping_x2; 
	if ((sy + ih - 1) > clipping_y2) ih -=  (sy + ih - 1) - clipping_y2; 

	ssx = sx;
	sw = iw;

	ijmp = (img->w - iw) * (img->bpp >> 3);
	iaddr = &img->data[(iy * img->w + ix) * (img->bpp >> 3) + 0];
	sjmp = SCREEN_WIDTH - iw;
	screen = &screen[sy * SCREEN_WIDTH + sx];

	switch (img->bpp) {
		case 32:
			for (; (ih > 0); ih--, iaddr += ijmp, screen += sjmp) { 
				for (iw = sw; (iw > 0); iw--, iaddr += 4, screen++) {
					*screen = RGB16(iaddr[0], iaddr[1], iaddr[2]); 
				}
			}
			break;
		case 16:
			for (; (ih > 0); ih--, iaddr += ijmp, screen += sjmp) { 
				for (iw = sw; (iw > 0); iw--, iaddr += 2) {
					*screen++ = *(unsigned short *)iaddr; 
				}
			}
			break;
	}
}

void gDrawScaledBitmap16(unsigned short *screen, int sx, int sy, gBITMAP *img, unsigned int iw, unsigned int ih) {
	int dx, dy;
	int x , y, fp_x, w, h;
	unsigned int *addr;
	unsigned int pixel;

	if ((!img) || (!img->data)) return; // sanity check
	if ((img->bpp != 32) || (img->bpp != 32)) return; // supported BPPs

	#define FP	10
	dx = (img->w << FP) / iw;
	dy = (img->h << FP) / ih;

	w = SCREEN_WIDTH;
	if (w > iw) w = iw;
	h = SCREEN_HEIGHT;
	if (h > ih) h = ih;

	for (y = 0; y < h; y++) {
		addr = &((unsigned int *)img->data)[img->w * ((y * dy) >> FP)]; 
		for (x = 0, fp_x = 0; x < w ; x++, fp_x += dx) {
			pixel = addr[fp_x >> FP];
			gDrawPixel16(screen, sx+x, sy+y, PIXEL32_R(pixel), PIXEL32_G(pixel), PIXEL32_B(pixel));
		}
	}
}

gBITMAP *gStretchBitmap(gBITMAP *img, unsigned int iw, unsigned int ih) {
	int dx, dy;
	int x , y, fp_x, fx, w, h;
	unsigned int *srcCurLineAddr, *srcPrevLineAddr, *srcNextLineAddr;
	unsigned int *dstAddr;
	unsigned int pixel, r, g, b, a, div;
	gBITMAP *toBmp;

	if ((!img) || (!img->data)) return NULL; // sanity check
	if ((img->bpp != 32) || (img->bpp != 32)) return NULL; // supported BPPs
	
	toBmp = gCreateBitmap(iw, ih, img->bpp);
	if (!toBmp) return NULL;

	#define FP	10
	dx = (img->w << FP) / iw;
	dy = (img->h << FP) / ih;

	w = img->w;
	if (w > iw) w = iw;
	h = img->h;
	if (h > ih) h = ih;

	for (y = 0; y < ih; y++) {
		// Current line
		srcCurLineAddr = &((unsigned int *)img->data)[img->w * ((y * dy) >> FP)];
		// Previous line
		if (y == 0) srcPrevLineAddr = NULL;
		else srcPrevLineAddr = &((unsigned int *)img->data)[img->w * (((y * dy) - 1) >> FP)];
		// Next line
		if ((((y * dy) + 1) >> FP) >= img->h) srcNextLineAddr = NULL;
		else srcNextLineAddr = &((unsigned int *)img->data)[img->w * (((y * dy) + 1) >> FP)];
		// Destination
		dstAddr = &((unsigned int*)toBmp->data)[toBmp->w * y]; 
		for (x = 0, fp_x = 0; x < iw ; x++, fp_x += dx) {
			// Current line
			div = 4;
			fx = fp_x >> FP;
			pixel = srcCurLineAddr[fx];
			r = PIXEL32_R(pixel) * 4;
			g = PIXEL32_G(pixel) * 4;
			b = PIXEL32_B(pixel) * 4;
			a = PIXEL32_A(pixel) * 4;
#define INCLUDE_PIXEL(p) \
		{\
		pixel = p;\
		r += PIXEL32_R(pixel);\
		g += PIXEL32_G(pixel);\
		b += PIXEL32_B(pixel);\
		a += PIXEL32_A(pixel);\
		div++;\
		}

			if (fx > 0) INCLUDE_PIXEL(srcCurLineAddr[fx - 1]);
			if ((fx + 1) < img->w) INCLUDE_PIXEL(srcCurLineAddr[fx + 1]);
			// Previous line
			if (srcPrevLineAddr) {
				INCLUDE_PIXEL(srcPrevLineAddr[fx]);
				if (fx > 0) INCLUDE_PIXEL(srcPrevLineAddr[fx - 1]);
				if ((fx + 1) < img->w) INCLUDE_PIXEL(srcPrevLineAddr[fx + 1]);
			}
			// Next line
			if (srcNextLineAddr) {
				INCLUDE_PIXEL(srcNextLineAddr[fx]);
				if (fx > 0) INCLUDE_PIXEL(srcNextLineAddr[fx - 1]);
				if ((fx + 1) < img->w) INCLUDE_PIXEL(srcNextLineAddr[fx + 1]);
			}
#undef INCLUDE_PIXEL
			dstAddr[x] = RGB32(r/div, g/div, b/div, a/div);
		}
	}

	return toBmp;
}



void gClearScreen(unsigned short *screen, unsigned short color) {
	int x, y;
   
	for (y = 0; y < SCREEN_HEIGHT; y++)
	    for (x = 0; x < SCREEN_WIDTH; x++) {
		screen[y * SCREEN_WIDTH + x] = color;
		}
}