+/GENN

genetically evolving neural networks

git clone https://git.julia.blue/+/GENN

imdone.io app userTODO comments updated by @imdoneio [ci skip] [imdone skip]d7063ee

imdone-todo-updates
123.8 KiB4012 linesraw
1//////// Start of inlined file: tigr_amalgamated.c ////////
2
3
4#include "tigr.h"
5
6//////// Start of inlined file: tigr_bitmaps.c ////////
7
8//////// Start of inlined file: tigr_internal.h ////////
9
10// can't use pragma once here because this file probably will endup in .c
11#ifndef __TIGR_INTERNAL_H__
12#define __TIGR_INTERNAL_H__
13
14#define _CRT_SECURE_NO_WARNINGS NOPE
15
16// Creates a new bitmap, with extra payload bytes.
17Tigr *tigrBitmap2(int w, int h, int extra);
18
19// Resizes an existing bitmap.
20void tigrResize(Tigr *bmp, int w, int h);
21
22// Calculates the biggest scale that a bitmap can fit into an area at.
23int tigrCalcScale(int bmpW, int bmpH, int areaW, int areaH);
24
25// Calculates a new scale, taking minimum-scale flags into account.
26int tigrEnforceScale(int scale, int flags);
27
28// Calculates the correct position for a bitmap to fit into a window.
29void tigrPosition(Tigr *bmp, int scale, int windowW, int windowH, int out[4]);
30
31// ----------------------------------------------------------
32#ifdef _WIN32
33#define WIN32_LEAN_AND_MEAN
34#include <windows.h>
35#endif
36
37#ifdef TIGR_GAPI_D3D9
38#include <d3d9.h>
39typedef struct {
40	int lost;
41	IDirect3DDevice9 *dev;
42	D3DPRESENT_PARAMETERS params;
43	IDirect3DTexture9 *sysTex[2], *vidTex[2];
44	IDirect3DVertexShader9 *vs;
45	IDirect3DPixelShader9 *ps;
46} D3D9Stuff;
47#endif
48
49#ifdef TIGR_GAPI_GL
50#ifdef _WIN32
51#include <GL/gl.h>
52#else
53#include <OpenGL/gl3.h>
54#endif
55typedef struct {
56	#ifdef _WIN32
57	HGLRC hglrc;
58	HDC dc;
59	#endif
60	GLuint tex[2];
61	GLuint vao;
62	GLuint program;
63	GLuint uniform_projection;
64	GLuint uniform_model;
65	GLuint uniform_parameters;
66	int gl_legacy;
67} GLStuff;
68#endif
69
70typedef struct {
71	int shown, closed;
72	#ifdef TIGR_GAPI_D3D9
73	D3D9Stuff d3d9;
74	#endif
75	#ifdef TIGR_GAPI_GL
76	GLStuff gl;
77	#endif
78
79	#ifdef _WIN32
80	wchar_t *wtitle;
81	DWORD dwStyle;
82	RECT oldPos;
83	#endif
84	#ifdef __APPLE__
85	void *glContext;
86	#endif
87
88	Tigr *widgets;
89	int widgetsWanted;
90	unsigned char widgetAlpha;
91	float widgetsScale;
92
93	int hblur, vblur;
94	float scanlines, contrast;
95
96	int flags;
97	int scale;
98	int pos[4];
99	int lastChar;
100	char keys[256], prev[256];
101	#ifdef __APPLE__
102	int mouseButtons;
103	#endif
104} TigrInternal;
105// ----------------------------------------------------------
106
107TigrInternal *tigrInternal(Tigr *bmp);
108
109void tigrGAPICreate(Tigr *bmp);
110void tigrGAPIDestroy(Tigr *bmp);
111void tigrGAPIBegin(Tigr *bmp);
112void tigrGAPIEnd(Tigr *bmp);
113void tigrGAPIResize(Tigr *bmp, int width, int height);
114void tigrGAPIPresent(Tigr *bmp, int w, int h);
115
116#endif
117
118
119//////// End of inlined file: tigr_internal.h ////////
120
121#include <stdlib.h>
122#include <string.h>
123
124// Expands 0-255 into 0-256
125#define EXPAND(X) ((X) + ((X) > 0))
126
127#define CLIP0(X, X2, W) if (X < 0) { W += X; X2 -= X; X = 0; }
128#define CLIP1(X, DW, W) if (X + W > DW) W = DW - X;
129#define CLIP() \
130	CLIP0(dx, sx, w);		\
131	CLIP0(dy, sy, h);		\
132	CLIP0(sx, dx, w);		\
133	CLIP0(sy, dy, h);		\
134	CLIP1(dx, dst->w, w);	\
135	CLIP1(dy, dst->h, h);	\
136	CLIP1(sx, src->w, w);	\
137	CLIP1(sy, src->h, h);	\
138	if (w <= 0 || h <= 0)	\
139		return
140
141
142Tigr *tigrBitmap2(int w, int h, int extra)
143{
144	Tigr *tigr = (Tigr *)calloc(1, sizeof(Tigr) + extra);
145	tigr->w = w;
146	tigr->h = h;
147	tigr->pix = (TPixel *)calloc(w*h, sizeof(TPixel));
148	return tigr;
149}
150
151Tigr *tigrBitmap(int w, int h)
152{
153	return tigrBitmap2(w, h, 0);
154}
155
156void tigrResize(Tigr *bmp, int w, int h)
157{
158	int y, cw, ch;
159	TPixel *newpix = (TPixel *)calloc(w*h, sizeof(TPixel));
160	cw = (w < bmp->w) ? w : bmp->w;
161	ch = (h < bmp->h) ? h : bmp->h;
162
163	// Copy any old data across.
164	for (y=0;y<ch;y++)
165		memcpy(newpix+y*w, bmp->pix+y*bmp->w, cw*sizeof(TPixel));
166
167	free(bmp->pix);
168	bmp->pix = newpix;
169	bmp->w = w;
170	bmp->h = h;
171}
172
173int tigrCalcScale(int bmpW, int bmpH, int areaW, int areaH)
174{
175	// We want it as big as possible in the window, but still
176	// maintaining the correct aspect ratio, and always
177	// having an integer pixel size.
178	int scale = 0;
179	for(;;)
180	{
181		scale++;
182		if (bmpW*scale > areaW || bmpH*scale > areaH)
183		{
184			scale--;
185			break;
186		}
187	}
188	return (scale > 1) ? scale : 1;
189}
190
191int tigrEnforceScale(int scale, int flags)
192{
193	if ((flags & TIGR_4X) && scale < 4) scale = 4;
194	if ((flags & TIGR_3X) && scale < 3) scale = 3;
195	if ((flags & TIGR_2X) && scale < 2) scale = 2;
196	return scale;
197}
198
199void tigrPosition(Tigr *bmp, int scale, int windowW, int windowH, int out[4])
200{
201	// Center the image on screen at this scale.
202	out[0] = (windowW - bmp->w*scale) / 2;
203	out[1] = (windowH - bmp->h*scale) / 2;
204	out[2] = out[0] + bmp->w*scale;
205	out[3] = out[1] + bmp->h*scale;
206}
207
208void tigrClear(Tigr *bmp, TPixel color)
209{
210	int count = bmp->w * bmp->h;
211	int n;
212	for (n=0;n<count;n++)
213		bmp->pix[n] = color;
214}
215
216void tigrFill(Tigr *bmp, int x, int y, int w, int h, TPixel color)
217{
218	TPixel *td;
219	int dt, i;
220
221	if (x < 0) { w += x; x = 0; }
222	if (y < 0) { h += y; y = 0; }
223	if (x + w > bmp->w) { w = bmp->w - x; }
224	if (y + h > bmp->h) { h = bmp->h - y; }
225	if (w <= 0 || h <= 0)
226		return;
227
228	td = &bmp->pix[y*bmp->w + x];
229	dt = bmp->w;
230	do {
231		for (i=0;i<w;i++)
232			td[i] = color;
233		td += dt;
234	} while(--h);
235}
236
237void tigrLine(Tigr *bmp, int x0, int y0, int x1, int y1, TPixel color)
238{
239	int sx, sy, dx, dy, err, e2;
240	dx = abs(x1 - x0);
241	dy = abs(y1 - y0);
242	if (x0 < x1) sx = 1; else sx = -1;
243	if (y0 < y1) sy = 1; else sy = -1;
244	err = dx - dy;
245
246	tigrPlot(bmp, x0, y0, color);
247	while (x0 != x1 || y0 != y1)
248	{
249		tigrPlot(bmp, x0, y0, color);
250		e2 = 2*err;
251		if (e2 > -dy) { err -= dy; x0 += sx; }
252		if (e2 <  dx) { err += dx; y0 += sy; }
253	}
254}
255
256void tigrRect(Tigr *bmp, int x, int y, int w, int h, TPixel color)
257{
258	int x1, y1;
259	if (w <= 0 || h <= 0)
260		return;
261	
262	x1 = x + w-1;
263	y1 = y + h-1;
264	tigrLine(bmp, x, y, x1, y, color);
265	tigrLine(bmp, x1, y, x1, y1, color);
266	tigrLine(bmp, x1, y1, x, y1, color);
267	tigrLine(bmp, x, y1, x, y, color);
268}
269
270TPixel tigrGet(Tigr *bmp, int x, int y)
271{
272	TPixel empty = { 0,0,0,0 };
273	if (x >= 0 && y >= 0 && x < bmp->w && y < bmp->h)
274		return bmp->pix[y*bmp->w+x];
275	return empty;
276}
277
278void tigrPlot(Tigr *bmp, int x, int y, TPixel pix)
279{
280	int xa, i, a;
281	if (x >= 0 && y >= 0 && x < bmp->w && y < bmp->h)
282	{
283		xa = EXPAND(pix.a);
284		i = y*bmp->w+x;
285
286		a = xa * EXPAND(pix.a);
287		bmp->pix[i].r += (unsigned char)((pix.r - bmp->pix[i].r)*a >> 16);
288		bmp->pix[i].g += (unsigned char)((pix.g - bmp->pix[i].g)*a >> 16);
289		bmp->pix[i].b += (unsigned char)((pix.b - bmp->pix[i].b)*a >> 16);
290		bmp->pix[i].a += (unsigned char)((pix.a - bmp->pix[i].a)*a >> 16);
291	}
292}
293
294void tigrBlit(Tigr *dst, Tigr *src, int dx, int dy, int sx, int sy, int w, int h)
295{
296	TPixel *td, *ts;
297	int st, dt;
298	CLIP();
299
300	ts = &src->pix[sy*src->w + sx];
301	td = &dst->pix[dy*dst->w + dx];
302	st = src->w;
303	dt = dst->w;
304	do {
305		memcpy(td, ts, w*sizeof(TPixel));
306		ts += st;
307		td += dt;
308	} while(--h);
309}
310
311void tigrBlitTint(Tigr *dst, Tigr *src, int dx, int dy, int sx, int sy, int w, int h, TPixel tint)
312{
313	TPixel *td, *ts;
314	int x, st, dt, xr,xg,xb,xa;
315	CLIP();
316
317	xr = EXPAND(tint.r);
318	xg = EXPAND(tint.g);
319	xb = EXPAND(tint.b);
320	xa = EXPAND(tint.a);
321
322	ts = &src->pix[sy*src->w + sx];
323	td = &dst->pix[dy*dst->w + dx];
324	st = src->w;
325	dt = dst->w;
326	do {
327		for (x=0;x<w;x++)
328		{
329			unsigned r = (xr * ts[x].r) >> 8;
330			unsigned g = (xg * ts[x].g) >> 8;
331			unsigned b = (xb * ts[x].b) >> 8;
332			unsigned a = xa * EXPAND(ts[x].a);
333			td[x].r += (unsigned char)((r - td[x].r)*a >> 16);
334			td[x].g += (unsigned char)((g - td[x].g)*a >> 16);
335			td[x].b += (unsigned char)((b - td[x].b)*a >> 16);
336			td[x].a += (unsigned char)((ts[x].a - td[x].a)*a >> 16);
337		}
338		ts += st;
339		td += dt;
340	} while(--h);
341}
342
343void tigrBlitAlpha(Tigr *dst, Tigr *src, int dx, int dy, int sx, int sy, int w, int h, float alpha)
344{
345	alpha = (alpha < 0) ? 0 : (alpha > 1 ? 1 : alpha);
346	tigrBlitTint(dst, src, dx, dy, sx, sy, w, h, tigrRGBA(0xff,0xff,0xff,(unsigned char)(alpha*255)));
347}
348
349#undef CLIP0
350#undef CLIP1
351#undef CLIP
352
353//////// End of inlined file: tigr_bitmaps.c ////////
354
355//////// Start of inlined file: tigr_loadpng.c ////////
356
357//#include "tigr_internal.h"
358#include <stdlib.h>
359#include <string.h>
360#include <errno.h>
361
362typedef struct {
363	const unsigned char *p, *end;
364} PNG;
365
366static unsigned get32(const unsigned char *v)
367{
368	return (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | v[3];
369}
370
371static const unsigned char *find(PNG *png, const char *chunk, unsigned minlen)
372{
373	const unsigned char *start;
374	while (png->p < png->end)
375	{
376		unsigned len = get32(png->p+0);
377		start = png->p;
378		png->p += len + 12;
379		if (memcmp(start+4, chunk, 4) == 0 && len >= minlen && png->p <= png->end)
380			return start+8;
381	}
382
383	return NULL;
384}
385
386static unsigned char paeth(unsigned char a, unsigned char b, unsigned char c)
387{
388	int p  = a + b - c;
389	int pa = abs(p-a), pb = abs(p-b), pc = abs(p-c);
390	return (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
391}
392
393static int unfilter(int w, int h, int bpp, unsigned char *raw)
394{
395	int len = w * bpp, x, y;
396	unsigned char *prev = raw;
397	for (y=0;y<h;y++,prev=raw,raw+=len)
398	{
399#define LOOP(A, B) for (x=0;x<bpp;x++) raw[x] += A; for (;x<len;x++) raw[x] += B; break
400		switch (*raw++)
401		{
402		case 0: break;
403		case 1: LOOP(0,         raw[x-bpp]);
404		case 2: LOOP(prev[x],   prev[x]);
405		case 3: LOOP(prev[x]/2, (raw[x-bpp] + prev[x])/2);
406		case 4: LOOP(prev[x],   paeth(raw[x-bpp], prev[x], prev[x-bpp]));
407		default: return 0;
408		}
409#undef LOOP
410	}
411	return 1;
412}
413
414static void convert(int bpp, int w, int h, unsigned char *src, TPixel *dest)
415{
416	int x,y;
417	for (y=0;y<h;y++)
418	{
419		src++; // skip filter byte
420		for (x=0;x<w;x++,src+=bpp)
421		{
422			switch (bpp) {
423			case 1: *dest++ = tigrRGB (src[0], src[0], src[0]); break;
424			case 2: *dest++ = tigrRGBA(src[0], src[0], src[0], src[1]); break;
425			case 3: *dest++ = tigrRGB (src[0], src[1], src[2]); break;
426			case 4: *dest++ = tigrRGBA(src[0], src[1], src[2], src[3]); break;
427			}
428		}
429	}
430}
431
432static void depalette(int w, int h, unsigned char *src, TPixel *dest, const unsigned char *plte)
433{
434	int x,y,c;
435	for (y=0;y<h;y++)
436	{
437		src++; // skip filter byte
438		for (x=0;x<w;x++,src++)
439		{
440			c = src[0];
441			*dest++ = tigrRGBA(plte[c*3+0], plte[c*3+1], plte[c*3+2], c?255:0);
442		}
443	}
444}
445
446#define FAIL() { errno = EINVAL; goto err; }
447#define CHECK(X) if (!(X)) FAIL()
448static int outsize(Tigr *bmp, int bpp) { return (bmp->w+1)*bmp->h * bpp; }
449
450static Tigr *tigrLoadPng(PNG *png)
451{
452	const unsigned char *ihdr, *idat, *plte, *first;
453	int depth, ctype, bpp;
454	int datalen = 0;
455	unsigned char *data = NULL, *out;
456	Tigr *bmp = NULL;
457
458	CHECK(memcmp(png->p, "\211PNG\r\n\032\n", 8) == 0); // PNG signature
459	png->p += 8;
460	first = png->p;
461
462	// Read IHDR
463	ihdr = find(png, "IHDR", 13);
464	CHECK(ihdr);
465	depth = ihdr[8];
466	ctype = ihdr[9];
467	switch (ctype) {
468		case 0: bpp = 1; break; // greyscale
469		case 2: bpp = 3; break; // RGB
470		case 3: bpp = 1; break; // paletted
471		case 4: bpp = 2; break; // grey+alpha
472		case 6: bpp = 4; break; // RGBA
473		default: FAIL();
474	}
475
476	// Allocate bitmap (+1 width to save room for stupid PNG filter bytes)
477	bmp = tigrBitmap(get32(ihdr+0) + 1, get32(ihdr+4));
478	CHECK(bmp);
479	bmp->w--;
480
481	// We don't support non-8bpp, interlacing, or wacky filter types.
482	CHECK(depth == 8 && ihdr[10] == 0 && ihdr[11] == 0 && ihdr[12] == 0);
483
484	// Join IDAT chunks.
485	for (idat=find(png, "IDAT", 0); idat; idat=find(png, "IDAT", 0))
486	{
487		unsigned len = get32(idat-8);
488		data = (unsigned char *)realloc(data, datalen + len);
489		if (!data)
490			break;
491
492		memcpy(data+datalen, idat, len);
493		datalen += len;
494	}
495
496	// Find palette.
497	png->p = first;
498	plte = find(png, "PLTE", 0);
499
500	CHECK(data && datalen >= 6);
501	CHECK((data[0] & 0x0f) == 0x08	// compression method (RFC 1950)
502	   && (data[0] & 0xf0) <= 0x70	// window size
503	   && (data[1] & 0x20) == 0);	// preset dictionary present
504
505	out = (unsigned char *)bmp->pix + outsize(bmp, 4) - outsize(bmp, bpp);
506	CHECK(tigrInflate(out, outsize(bmp, bpp), data+2, datalen-6));
507	CHECK(unfilter(bmp->w, bmp->h, bpp, out));
508
509	if (ctype == 3) {
510		CHECK(plte);
511		depalette(bmp->w, bmp->h, out, bmp->pix, plte);
512	} else {
513		convert(bpp, bmp->w, bmp->h, out, bmp->pix);
514	}
515	
516	free(data);
517	return bmp;
518
519err:
520	if (data) free(data);
521	if (bmp)  tigrFree(bmp);
522	return NULL;
523}
524
525#undef CHECK
526#undef FAIL
527
528Tigr *tigrLoadImageMem(const void *data, int length)
529{
530	PNG png;
531	png.p = (unsigned char *)data;
532	png.end = (unsigned char *)data + length;
533	return tigrLoadPng(&png);
534}
535
536Tigr *tigrLoadImage(const char *fileName)
537{
538	int len;
539	void *data;
540	Tigr *bmp;
541
542	data = tigrReadFile(fileName, &len);
543	if (!data)
544		return NULL;
545
546	bmp = tigrLoadImageMem(data, len);
547	free(data);
548	return bmp;
549}
550
551
552//////// End of inlined file: tigr_loadpng.c ////////
553
554//////// Start of inlined file: tigr_savepng.c ////////
555
556//#include "tigr_internal.h"
557#include <stdio.h>
558#include <stdlib.h>
559#include <string.h>
560#include <errno.h>
561
562typedef struct {
563	unsigned crc, adler, bits, prev, runlen;
564	FILE *out;
565	unsigned crcTable[256];
566} Save;
567
568static const unsigned crctable[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
569	0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
570
571static void put(Save *s, unsigned v)
572{
573	fputc(v, s->out);
574	s->crc = (s->crc >> 4) ^ crctable[(s->crc & 15) ^ (v & 15)];
575	s->crc = (s->crc >> 4) ^ crctable[(s->crc & 15) ^ (v >> 4)];
576}
577
578static void updateAdler(Save *s, unsigned v)
579{
580	unsigned s1 = s->adler & 0xffff, s2 = (s->adler >> 16) & 0xffff;
581	s1 = (s1 +  v) % 65521;
582	s2 = (s2 + s1) % 65521;
583	s->adler = (s2 << 16) + s1;
584}
585
586static void put32(Save *s, unsigned v)
587{
588	put(s, (v >> 24) & 0xff);
589	put(s, (v >> 16) & 0xff);
590	put(s, (v >>  8) & 0xff);
591	put(s, v & 0xff);
592}
593
594void putbits(Save *s, unsigned data, unsigned bitcount)
595{
596	while (bitcount--)
597	{
598		unsigned prev = s->bits;
599		s->bits = (s->bits >> 1) | ((data & 1) << 7);
600		data >>= 1;
601		if (prev & 1)
602		{
603			put(s, s->bits);
604			s->bits = 0x80;
605		}
606	}
607}
608
609void putbitsr(Save *s, unsigned data, unsigned bitcount)
610{
611	while (bitcount--)
612		putbits(s, data >> bitcount, 1);
613}
614
615static void begin(Save *s, const char *id, unsigned len)
616{
617	put32(s, len);
618	s->crc = 0xffffffff;
619	put(s, id[0]); put(s, id[1]); put(s, id[2]); put(s, id[3]);
620}
621
622static void literal(Save *s, unsigned v)
623{
624	// Encode a literal/length using the built-in tables.
625	// Could do better with a custom table but whatever.
626	     if (v < 144)   putbitsr(s, 0x030+v-  0, 8);
627	else if (v < 256)   putbitsr(s, 0x190+v-144, 9);
628	else if (v < 280)   putbitsr(s, 0x000+v-256, 7);
629	else                putbitsr(s, 0x0c0+v-280, 8);
630}
631
632static void encodelen(Save *s, unsigned code, unsigned bits, unsigned len)
633{
634	literal(s, code + (len >> bits));
635	putbits(s, len, bits);
636	putbits(s, 0, 5);
637}
638
639static void endrun(Save *s)
640{
641	s->runlen--;
642	literal(s, s->prev);
643
644	     if (s->runlen >= 67) encodelen(s, 277, 4, s->runlen - 67);
645	else if (s->runlen >= 35) encodelen(s, 273, 3, s->runlen - 35);
646	else if (s->runlen >= 19) encodelen(s, 269, 2, s->runlen - 19);
647	else if (s->runlen >= 11) encodelen(s, 265, 1, s->runlen - 11);
648	else if (s->runlen >=  3) encodelen(s, 257, 0, s->runlen -  3);
649	else while (s->runlen--) literal(s, s->prev);
650}
651
652static void encodeByte(Save *s, unsigned char v)
653{
654	updateAdler(s, v);
655
656	// Simple RLE compression. We could do better by doing a search
657	// to find matches, but this works pretty well TBH.
658	if (s->prev == v && s->runlen < 115)
659	{
660		s->runlen++;
661	} else {
662		if (s->runlen)
663			endrun(s);
664
665		s->prev = v;
666		s->runlen = 1;
667	}
668}
669
670static void savePngHeader(Save *s, Tigr *bmp)
671{
672	fwrite("\211PNG\r\n\032\n", 8, 1, s->out);
673	begin(s, "IHDR", 13);
674	put32(s, bmp->w);
675	put32(s, bmp->h);
676	put(s, 8); // bit depth
677	put(s, 6); // RGBA
678	put(s, 0); // compression (deflate)
679	put(s, 0); // filter (standard)
680	put(s, 0); // interlace off
681	put32(s, ~s->crc);
682}
683
684static long savePngData(Save *s, Tigr *bmp, long dataPos)
685{
686	int x, y;
687	long dataSize;
688	begin(s, "IDAT", 0);
689	put(s, 0x08); // zlib compression method
690	put(s, 0x1d); // zlib compression flags
691	putbits(s, 3, 3); // zlib last block + fixed dictionary
692	for (y=0;y<bmp->h;y++)
693	{
694		TPixel *row = &bmp->pix[y*bmp->w];
695		TPixel prev = tigrRGBA(0, 0, 0, 0);
696
697		encodeByte(s, 1); // sub filter
698		for (x=0;x<bmp->w;x++)
699		{
700			encodeByte(s, row[x].r - prev.r);
701			encodeByte(s, row[x].g - prev.g);
702			encodeByte(s, row[x].b - prev.b);
703			encodeByte(s, row[x].a - prev.a);
704			prev = row[x];
705		}
706	}
707	endrun(s);
708	literal(s, 256); // terminator
709	while (s->bits != 0x80)
710		putbits(s, 0, 1);
711	put32(s, s->adler);
712	dataSize = (ftell(s->out) - dataPos) - 8;
713	put32(s, ~s->crc);
714	return dataSize;
715}
716
717int tigrSaveImage(const char *fileName, Tigr *bmp)
718{
719	Save s;
720	long dataPos, dataSize, err;
721	
722	// TODO - unicode? id:0 gh:1
723	FILE *out = fopen(fileName, "wb");
724	if (!out)
725		return 1;
726
727	s.out = out;
728	s.adler = 1;
729	s.bits = 0x80;
730	s.prev = 0xffff;
731	s.runlen = 0;
732
733	savePngHeader(&s, bmp);
734	dataPos = ftell(s.out);
735	dataSize = savePngData(&s, bmp, dataPos);
736
737	// End chunk.
738	begin(&s, "IEND", 0);
739	put32(&s, ~s.crc);
740
741	// Write back payload size.
742	fseek(out, dataPos, SEEK_SET);
743	put32(&s, dataSize);
744
745	err = ferror(out);
746	fclose(out);
747	return !err;
748}
749
750//////// End of inlined file: tigr_savepng.c ////////
751
752//////// Start of inlined file: tigr_utils.c ////////
753
754//#include "tigr_internal.h"
755#include <stdio.h>
756#include <stdlib.h>
757
758void *tigrReadFile(const char *fileName, int *length)
759{
760	// TODO - unicode? id:3 gh:4
761	FILE *file;
762	char *data;
763	size_t len;
764
765	if (length)
766		*length = 0;
767
768	file = fopen(fileName, "rb");
769	if (!file)
770		return NULL;
771
772	fseek(file, 0, SEEK_END);
773	len = ftell(file);
774	fseek(file, 0, SEEK_SET);
775
776	data = (char *)malloc(len+1);
777	if (!data)
778	{
779		fclose(file);
780		return NULL;
781	}
782
783	if (fread(data, 1, len, file) != len) {
784		free(data);
785		fclose(file);
786		return NULL;
787	}
788	data[len] = '\0';
789	fclose(file);
790
791	if (length)
792		*length = len;
793
794	return data;
795}
796
797// Reads a single UTF8 codepoint.
798const char *tigrDecodeUTF8(const char *text, int *cp)
799{
800	unsigned char c = *text++;
801	int extra = 0, min = 0;
802	*cp = 0;
803		 if (c >= 0xf0) { *cp = c & 0x07; extra = 3; min = 0x10000; }
804	else if (c >= 0xe0) { *cp = c & 0x0f; extra = 2; min = 0x800; }
805	else if (c >= 0xc0) { *cp = c & 0x1f; extra = 1; min = 0x80; }
806	else if (c >= 0x80) { *cp = 0xfffd; }
807	else *cp = c;
808	while (extra--) {
809		c = *text++;
810		if ((c & 0xc0) != 0x80) { *cp = 0xfffd; break; }
811		(*cp) = ((*cp) << 6) | (c & 0x3f);
812	}
813	if (*cp < min) *cp = 0xfffd;
814	return text;
815}
816
817char *tigrEncodeUTF8(char *text, int cp)
818{
819	if (cp < 0 || cp > 0x10ffff) cp = 0xfffd;
820
821#define EMIT(X,Y,Z) *text++ = X | ((cp >> Y)&Z)
822		 if (cp <     0x80) { EMIT(0x00,0,0x7f); }
823	else if (cp <    0x800) { EMIT(0xc0,6,0x1f); EMIT(0x80, 0, 0x3f); }
824	else if (cp <  0x10000) { EMIT(0xe0,12,0xf); EMIT(0x80, 6, 0x3f); EMIT(0x80, 0, 0x3f); }
825	else                    { EMIT(0xf0,18,0x7); EMIT(0x80, 12, 0x3f); EMIT(0x80, 6, 0x3f); EMIT(0x80, 0, 0x3f); }
826	return text;
827#undef EMIT
828}
829
830void tigrSetPostFX(Tigr *bmp, int hblur, int vblur, float scanlines, float contrast)
831{
832	TigrInternal *win = tigrInternal(bmp);
833	win->hblur = hblur;
834	win->vblur = vblur;
835	win->scanlines = scanlines;
836	win->contrast = contrast;
837}
838
839//////// End of inlined file: tigr_utils.c ////////
840
841//////// Start of inlined file: tigr_inflate.c ////////
842
843//#include "tigr_internal.h"
844#include <stdlib.h>
845#include <setjmp.h>
846
847typedef struct {
848	unsigned bits, count;
849	const unsigned char *in, *inend;
850	unsigned char *out, *outend;
851	jmp_buf jmp;
852	unsigned litcodes[288], distcodes[32], lencodes[19];
853	int tlit, tdist, tlen;
854} State;
855
856#define FAIL() longjmp(s->jmp, 1)
857#define CHECK(X) if (!(X)) FAIL()
858
859// Built-in DEFLATE standard tables.
860static char order[] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
861static char lenBits[29+2] = { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,  0,0 };
862static int lenBase[29+2] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,  0,0 };
863static char distBits[30+2] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,  0,0 };
864static int distBase[30+2] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577 };
865
866// Table to bit-reverse a byte.
867static const unsigned char reverseTable[256] = {
868#define R2(n)    n,     n + 128,     n + 64,     n + 192
869#define R4(n) R2(n), R2(n +  32), R2(n + 16), R2(n +  48)
870#define R6(n) R4(n), R4(n +   8), R4(n +  4), R4(n +  12)
871	R6(0), R6(2), R6(1), R6(3)
872};
873
874static unsigned rev16(unsigned n) { return (reverseTable[n&0xff] << 8) | reverseTable[(n>>8)&0xff]; }
875
876static int bits(State *s, int n)
877{
878	int v = s->bits & ((1 << n)-1);
879	s->bits >>= n;
880	s->count -= n;
881	while (s->count < 16)
882	{
883		CHECK(s->in != s->inend);
884		s->bits |= (*s->in++) << s->count;
885		s->count += 8;
886	}
887	return v;
888}
889
890static unsigned char *emit(State *s, int len)
891{
892	s->out += len;
893	CHECK(s->out <= s->outend);
894	return s->out-len;
895}
896
897static void copy(State *s, const unsigned char *src, int len)
898{
899	unsigned char *dest = emit(s, len);
900	while (len--) *dest++ = *src++;
901}
902
903static int build(State *s, unsigned *tree, unsigned char *lens, int symcount)
904{
905	int n, codes[16], first[16], counts[16]={0};
906
907	// Frequency count.
908	for (n=0;n<symcount;n++) counts[lens[n]]++;
909
910	// Distribute codes.
911	counts[0] = codes[0] = first[0] = 0;
912	for (n=1;n<=15;n++) {
913		codes[n] = (codes[n-1] + counts[n-1]) << 1;
914		first[n] = first[n-1] + counts[n-1];
915	}
916	CHECK(first[15]+counts[15] <= symcount);
917
918	// Insert keys into the tree for each symbol.
919	for (n=0;n<symcount;n++)
920	{
921		int len = lens[n];
922		if (len != 0) {
923			int code = codes[len]++, slot = first[len]++;
924			tree[slot] = (code << (32-len)) | (n << 4) | len;
925		}
926	}
927
928	return first[15];
929}
930
931static int decode(State *s, unsigned tree[], int max)
932{
933	// Find the next prefix code.
934	unsigned lo = 0, hi = max, key;
935	unsigned search = (rev16(s->bits) << 16) | 0xffff;
936	while (lo < hi) {
937		unsigned guess = (lo + hi) / 2;
938		if (search < tree[guess]) hi = guess;
939		else lo = guess + 1;
940	}
941
942	// Pull out the key and check it.
943	key = tree[lo-1];
944	CHECK(((search^key) >> (32-(key&0xf))) == 0);
945
946	bits(s, key & 0xf);
947	return (key >> 4) & 0xfff;
948}
949
950static void run(State *s, int sym)
951{
952	int length = bits(s, lenBits[sym]) + lenBase[sym];
953	int dsym = decode(s, s->distcodes, s->tdist);
954	int offs = bits(s, distBits[dsym]) + distBase[dsym];
955	copy(s, s->out - offs, length);
956}
957
958static void block(State *s)
959{
960	for (;;) {
961		int sym = decode(s, s->litcodes, s->tlit);
962		     if (sym < 256) *emit(s, 1) = (unsigned char)sym;
963		else if (sym > 256) run(s, sym-257);
964		else break;
965	}
966}
967
968static void stored(State *s)
969{
970	// Uncompressed data block.
971	int len; 
972	bits(s, s->count & 7);
973	len = bits(s, 16);
974	CHECK(((len^s->bits)&0xffff) == 0xffff);
975	CHECK(s->in + len <= s->inend);
976
977	copy(s, s->in, len);
978	s->in += len;
979	bits(s, 16);
980}
981
982static void fixed(State *s)
983{
984	// Fixed set of Huffman codes.
985	int n;
986	unsigned char lens[288+32];
987	for (n=  0;n<=143;n++) lens[n] = 8;
988	for (n=144;n<=255;n++) lens[n] = 9;
989	for (n=256;n<=279;n++) lens[n] = 7;
990	for (n=280;n<=287;n++) lens[n] = 8;
991	for (n=0;n<32;n++) lens[288+n] = 5;
992
993	// Build lit/dist trees.
994	s->tlit  = build(s, s->litcodes, lens, 288);
995	s->tdist = build(s, s->distcodes, lens+288, 32);
996}
997
998static void dynamic(State *s)
999{
1000	int n, i, nlit, ndist, nlen;
1001	unsigned char lenlens[19] = {0}, lens[288+32];
1002	nlit = 257 + bits(s, 5);
1003	ndist = 1 + bits(s, 5);
1004	nlen = 4 + bits(s, 4);
1005	for (n=0;n<nlen;n++)
1006		lenlens[order[n]] = (unsigned char)bits(s, 3);
1007
1008	// Build the tree for decoding code lengths.
1009	s->tlen = build(s, s->lencodes, lenlens, 19);
1010
1011	// Decode code lengths.
1012	for (n=0;n<nlit+ndist;)
1013	{
1014		int sym = decode(s, s->lencodes, s->tlen);
1015		switch (sym) {
1016		case 16: for (i =  3+bits(s,2); i; i--,n++) lens[n] = lens[n-1]; break;
1017		case 17: for (i =  3+bits(s,3); i; i--,n++) lens[n] = 0; break;
1018		case 18: for (i = 11+bits(s,7); i; i--,n++) lens[n] = 0; break;
1019		default: lens[n++] = (unsigned char)sym; break;
1020		}
1021	}
1022
1023	// Build lit/dist trees.
1024	s->tlit  = build(s, s->litcodes, lens, nlit);
1025	s->tdist = build(s, s->distcodes, lens+nlit, ndist);
1026}
1027
1028int tigrInflate(void *out, unsigned outlen, const void *in, unsigned inlen)
1029{
1030	int last;
1031	State *s = (State *)calloc(1, sizeof(State));
1032
1033	// We assume we can buffer 2 extra bytes from off the end of 'in'.
1034	s->in  = (unsigned char *)in;  s->inend  = s->in  + inlen + 2;
1035	s->out = (unsigned char *)out; s->outend = s->out + outlen;
1036	s->bits = 0; s->count = 0; bits(s, 0);
1037
1038	if (setjmp(s->jmp) == 1) {
1039		free(s);
1040		return 0;
1041	}
1042
1043	do {
1044		last = bits(s, 1);
1045		switch (bits(s, 2)) {
1046		case 0: stored(s); break;
1047		case 1: fixed(s); block(s); break;
1048		case 2: dynamic(s); block(s); break;
1049		case 3: FAIL();
1050		}
1051	} while(!last);
1052
1053	free(s);
1054	return 1;
1055}
1056
1057#undef CHECK
1058#undef FAIL
1059
1060//////// End of inlined file: tigr_inflate.c ////////
1061
1062//////// Start of inlined file: tigr_print.c ////////
1063
1064//#include "tigr_internal.h"
1065//////// Start of inlined file: tigr_font.h ////////
1066
1067// Auto-generated by incbin.pl from font.png
1068
1069const unsigned char tigr_font[] = {
1070	0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
1071	0x00,0x00,0x00,0xfd,0x00,0x00,0x00,0x5c,0x08,0x03,0x00,0x00,0x00,0x92,0xab,0x43,
1072	0x85,0x00,0x00,0x03,0x00,0x50,0x4c,0x54,0x45,0x5f,0x53,0x87,0x00,0x00,0x00,0x54,
1073	0x54,0x54,0xff,0xff,0xfa,0x31,0x2a,0x42,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1074	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1075	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1076	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1077	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1078	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1079	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1080	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1081	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1082	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1083	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1084	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1085	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1086	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1087	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1088	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1089	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1090	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1091	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1092	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1093	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1094	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1095	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1096	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1097	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1098	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1099	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1100	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1101	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1102	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1103	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1104	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1105	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1106	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1107	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1108	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1109	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1110	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1111	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1112	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1113	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1114	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1115	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1116	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1117	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,
1118	0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,
1119	0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,
1120	0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x4a,0x44,0xb7,0x5a,0x00,0x00,0x0b,
1121	0xc5,0x49,0x44,0x41,0x54,0x78,0x9c,0xed,0x5c,0x8b,0x82,0xe4,0x26,0x0e,0x44,0xc0,
1122	0xff,0x7f,0xf3,0x8d,0x91,0x4a,0x2f,0xc0,0x8d,0x7b,0x7a,0x76,0x93,0x9b,0x74,0x32,
1123	0xc6,0xc6,0xbc,0x0a,0x09,0x50,0x21,0xbc,0xa5,0xff,0xe6,0x5f,0xe9,0x85,0x7f,0xbd,
1124	0x35,0xba,0xfe,0xae,0xa0,0x34,0xba,0xfe,0xff,0x7a,0xf5,0x75,0x2d,0xee,0xd1,0x52,
1125	0x16,0x49,0x8e,0x58,0x17,0xa4,0x5b,0x4e,0x2c,0x49,0x9b,0x14,0x28,0xcf,0x1a,0x7f,
1126	0xdd,0x5d,0xe5,0x8d,0x08,0x8d,0xcf,0xcf,0x2e,0xbd,0x2f,0x5d,0x5a,0x45,0xc5,0x15,
1127	0xfd,0x09,0xf4,0xd2,0x08,0xba,0xfe,0x1b,0x8f,0xe4,0x52,0xd6,0xce,0x89,0xa5,0x8b,
1128	0x10,0x2c,0xd0,0x7f,0x25,0x23,0xa0,0x20,0x2e,0x80,0x9a,0x8f,0xe7,0x36,0x8f,0x67,
1129	0xfa,0x2a,0x17,0xf1,0xd7,0x93,0x4f,0xe7,0xc2,0x8e,0xaa,0x2b,0xaa,0xa3,0x50,0xf4,
1130	0x3b,0xe8,0xe9,0xca,0x6b,0x32,0x60,0xd8,0x7d,0x5c,0xeb,0x48,0x31,0x6a,0x20,0x15,
1131	0xbb,0x4b,0xb4,0x43,0xdf,0x18,0x8d,0xca,0x76,0x14,0xf0,0x55,0x1a,0xa3,0xe3,0xb0,
1132	0x48,0xaf,0x73,0xef,0x8e,0x8a,0x0a,0x09,0x2e,0xc4,0xa7,0x50,0x14,0x90,0x9b,0xc5,
1133	0x0a,0x32,0x24,0x84,0x22,0xdf,0x41,0x5f,0xbe,0xf2,0x0e,0x99,0x5e,0x9d,0xc0,0xb2,
1134	0xbd,0x1a,0x41,0x15,0xfd,0xca,0x08,0x19,0x90,0xa0,0x1f,0xc0,0xaa,0xb6,0xa1,0x14,
1135	0x7f,0x3b,0xfa,0x44,0x7b,0x73,0x88,0x45,0x34,0x82,0xa5,0x86,0xbc,0x5f,0x35,0xa0,
1136	0xd7,0x91,0x9e,0x2c,0x1d,0xb5,0x66,0x21,0xf7,0xda,0xf8,0xbb,0xc0,0x97,0xa1,0x21,
1137	0xa3,0x30,0x16,0x02,0x67,0x79,0x0b,0xbd,0x48,0x0d,0x9a,0x4f,0x18,0xae,0x5c,0x4d,
1138	0xe3,0x10,0xe2,0x09,0xb2,0xbf,0xfa,0x8b,0xc5,0x65,0xcd,0x29,0x26,0x1d,0xd5,0x70,
1139	0x19,0x25,0x23,0xfe,0xab,0xc1,0xe4,0xc7,0x7b,0xd6,0x74,0xa4,0xd3,0x67,0x42,0x3c,
1140	0x37,0x02,0xca,0xde,0xfc,0x30,0xe0,0x2c,0x4f,0xd1,0x93,0x0d,0xa1,0xa4,0xf9,0x57,
1141	0xb9,0x2c,0x12,0x3c,0x7b,0xf4,0x97,0x62,0x70,0xb5,0x2b,0xcd,0x87,0xd2,0xd4,0x80,
1142	0xde,0xba,0xb1,0x78,0xd4,0x59,0xd3,0x91,0x4e,0x9f,0xed,0x7d,0xf7,0xca,0xce,0x9d,
1143	0x2d,0x1d,0xca,0x55,0x7c,0x43,0xf6,0x4e,0xf3,0x1b,0x97,0xdb,0x08,0x22,0x11,0xa1,
1144	0x1a,0xfa,0x52,0x04,0x09,0x1a,0xda,0x5d,0x81,0xb9,0xf5,0x3a,0xeb,0xd5,0xb2,0x46,
1145	0x95,0xc3,0x90,0x6e,0x1a,0xf7,0x50,0x76,0x9e,0xf3,0xa5,0x19,0x23,0xfc,0x8c,0xe6,
1146	0x8f,0xfe,0xf4,0x9d,0x21,0xb2,0x87,0x86,0x56,0x27,0xe9,0xb2,0x42,0x2f,0x89,0x6d,
1147	0x6e,0xf0,0x9a,0x2e,0xf1,0xaa,0x03,0x7e,0x7c,0xcb,0xe4,0xe2,0xf3,0x6b,0xc8,0x73,
1148	0xd3,0x95,0xcf,0xcd,0xf9,0xb5,0x94,0xf7,0x65,0xef,0x34,0x9f,0x27,0x3f,0x95,0xa7,
1149	0x3e,0x5e,0x1a,0x86,0x01,0x2d,0x33,0x4f,0x5e,0xef,0x9d,0x30,0x46,0x8a,0x2a,0x89,
1150	0x47,0x52,0x12,0x25,0x45,0x88,0xf8,0x2b,0x79,0x78,0xd6,0xf4,0xb5,0xf8,0xfc,0x2e,
1151	0x7d,0xd7,0x8e,0x0f,0x0d,0x78,0x1b,0x7d,0x2e,0x68,0xf7,0x68,0x29,0xfb,0x9c,0xa8,
1152	0x47,0x6d,0xe8,0xdb,0x02,0xbe,0x1b,0x76,0xed,0xf8,0x8c,0xfe,0x3d,0xcd,0xff,0x0c,
1153	0xfa,0xa8,0x0d,0x3f,0x88,0x3e,0x97,0x9e,0xaa,0xfa,0x3b,0xe8,0x57,0xb7,0xff,0x64,
1154	0xf4,0xbf,0xf9,0xc7,0xf3,0x84,0x2e,0xc3,0xd1,0xd4,0x4a,0xcf,0x79,0xca,0xb6,0xf7,
1155	0x58,0x88,0x5c,0x7c,0x5a,0xc0,0x8a,0x2c,0x5c,0x79,0x3d,0xbf,0xad,0x30,0xb3,0x9d,
1156	0xc5,0xca,0x57,0xe6,0x1b,0xdf,0x94,0x66,0x0b,0x8f,0x9a,0x44,0xa6,0x2e,0x9d,0xd7,
1157	0x33,0x82,0x35,0xb3,0x23,0x15,0x1a,0x82,0x84,0x78,0x32,0x82,0x2a,0x49,0x2d,0x63,
1158	0xd2,0x05,0x2b,0xa2,0x37,0x7b,0xff,0x65,0x45,0x3e,0x44,0xc1,0xe7,0xe8,0x9d,0xd5,
1159	0x58,0x6d,0xd9,0xa5,0x2e,0x76,0x9b,0xa1,0x1f,0x76,0x18,0xac,0x99,0xa9,0xf0,0x5c,
1160	0xa9,0xca,0xb0,0x94,0xe9,0xbd,0x19,0xb2,0x05,0x3a,0x11,0x96,0xed,0x96,0x95,0x67,
1161	0x55,0x61,0x92,0xa0,0xd8,0xcc,0x0d,0x56,0xd2,0x1a,0x3d,0xf9,0xaa,0x1a,0xcc,0x7d,
1162	0x90,0x31,0x52,0x03,0xf3,0x02,0xeb,0xd6,0xe4,0xce,0xfc,0x15,0xb6,0x9c,0x6b,0x5c,
1163	0x68,0xb5,0xa9,0x13,0x0c,0x6b,0x0a,0xbd,0xa0,0x96,0x76,0x42,0xdf,0x60,0xa6,0xa3,
1164	0x1c,0x95,0xf9,0x0b,0xf4,0x66,0xdd,0x28,0x2f,0xb8,0x43,0x5f,0x48,0xaa,0x32,0xfb,
1165	0xc9,0x20,0xf4,0x62,0xe8,0xaf,0xd5,0xd0,0xd6,0x64,0x41,0x5f,0xcc,0x92,0x8d,0x61,
1166	0x42,0x4f,0x30,0x5c,0x11,0x36,0x9f,0x7e,0x2d,0xfb,0x80,0x1e,0x5c,0x2e,0x0e,0x19,
1167	0xdf,0xdd,0xa1,0x40,0x92,0xf1,0xbf,0x47,0x6f,0xf4,0x49,0xe8,0x67,0x13,0xf3,0xd4,
1168	0x3a,0x5e,0xe8,0x80,0xda,0xd4,0x25,0x6a,0xfe,0xd0,0x9b,0xdd,0xb0,0x4a,0x95,0x26,
1169	0xcd,0x0f,0x20,0x16,0xe8,0xa7,0x5e,0xdc,0x69,0x3a,0xa1,0xad,0xb1,0x40,0x0a,0x4a,
1170	0xb5,0xd4,0x91,0xd6,0x74,0x83,0xc0,0xf7,0x93,0x31,0x0c,0x9e,0xea,0x36,0xe8,0xa9,
1171	0xb1,0x01,0xbb,0x56,0xac,0x5d,0x6f,0xe4,0x5e,0x49,0x8d,0x54,0x99,0x9f,0xa2,0xd7,
1172	0xa1,0x91,0x7b,0x65,0x85,0xde,0xf1,0x06,0xcd,0x50,0x73,0x4a,0xe5,0x47,0x5d,0xc8,
1173	0xd0,0x12,0x7d,0x21,0x5b,0xd1,0xe6,0x61,0x67,0x1b,0x17,0x9e,0xa4,0x44,0xf2,0x11,
1174	0x45,0x81,0xfd,0x8e,0x79,0x25,0x9c,0xd0,0xef,0xd8,0x4c,0x7e,0x0f,0x4c,0xfa,0x4c,
1175	0x5a,0x41,0xa5,0x50,0xb2,0x93,0xbd,0xad,0x78,0x45,0xb6,0xd7,0x9a,0x23,0x60,0xe3,
1176	0xa9,0xb3,0x65,0x6c,0xa4,0x42,0x49,0x48,0x20,0x1b,0x99,0xa4,0x44,0xf2,0x11,0xf2,
1177	0x09,0x59,0xe1,0x4d,0x97,0x18,0x3f,0x62,0x71,0xe3,0x59,0x8d,0x2f,0x70,0xa0,0x4e,
1178	0xef,0x61,0xbc,0xe7,0xe7,0x50,0xa0,0xec,0x85,0xe1,0x8d,0xf2,0xa9,0x31,0xc7,0xb9,
1179	0xdb,0x52,0x74,0x4b,0xc0,0x2c,0xdd,0x9f,0x31,0x48,0x57,0xe1,0x7d,0x02,0x6b,0xe3,
1180	0xf3,0x02,0xd7,0x29,0x7a,0xbe,0x0d,0x9a,0x7f,0xd4,0xa8,0x0f,0x86,0x3f,0x57,0xe0,
1181	0x7f,0xe8,0xe7,0xf0,0x16,0xfd,0x6f,0xfe,0xf9,0xbd,0x28,0x5b,0xcb,0x5c,0x17,0xf9,
1182	0xd7,0x62,0x3e,0xe6,0xd4,0x98,0x57,0xd3,0xe2,0xf6,0xae,0xac,0xbb,0x99,0x0d,0xb7,
1183	0x92,0x9e,0xb2,0xf9,0x3d,0x3f,0xff,0xf6,0x53,0xe8,0x5b,0xf3,0x93,0x92,0x2e,0x54,
1184	0x32,0xaf,0x26,0x83,0xe9,0x8f,0xa3,0xc7,0x6a,0x09,0x1f,0xd3,0x11,0xfa,0xb1,0x6b,
1185	0xbb,0xa2,0x96,0x4a,0x55,0xe3,0x6b,0xc7,0x60,0xd5,0x4e,0x70,0xb2,0x6f,0xce,0xbe,
1186	0x56,0xb3,0xc7,0x17,0x9b,0x9e,0x5d,0x41,0x25,0x54,0xb3,0xb4,0x89,0xd6,0x2f,0xd4,
1187	0xe0,0x73,0x4e,0x46,0x26,0xb3,0xaf,0xd0,0x0f,0xb3,0x61,0xe1,0x30,0x8b,0x14,0x96,
1188	0xd0,0xa7,0x8b,0xe8,0x80,0xde,0x0c,0x1d,0x33,0x44,0x17,0x19,0xf6,0xa1,0xaf,0xe6,
1189	0x0c,0xbd,0x63,0xb3,0x70,0x49,0xb5,0xc6,0xfe,0xb7,0x2b,0xfa,0xc6,0xad,0x35,0xf4,
1190	0x46,0x5d,0x44,0xa1,0xa2,0xe5,0xb6,0xc5,0x86,0x6c,0x38,0xf4,0x66,0x5f,0x37,0xc7,
1191	0xe9,0x5e,0x9a,0xd0,0xe4,0x3c,0x67,0xdb,0x6a,0xe2,0xc6,0x37,0x5e,0x98,0xbf,0xcb,
1192	0xcb,0xbe,0x0e,0x32,0x7b,0x6d,0x2b,0xbf,0x42,0xdf,0x26,0xd2,0x95,0xd0,0xc3,0x5d,
1193	0x72,0x80,0x5e,0x93,0x8c,0x6c,0x35,0x17,0xbb,0xd5,0xe9,0x4b,0x49,0x85,0xaa,0x6c,
1194	0xab,0x81,0x8b,0x39,0x5a,0xe6,0xa5,0x81,0xd7,0x45,0xcd,0x2f,0xb2,0x2d,0x7f,0x80,
1195	0x7e,0xd1,0x9c,0x29,0xfa,0x04,0xbd,0x11,0x9c,0xbb,0x81,0xba,0x61,0x39,0xea,0xe8,
1196	0xde,0x70,0x9a,0x69,0x20,0x21,0x7b,0x2d,0x4b,0xf4,0xaf,0xc7,0x7d,0xd4,0x7c,0xcf,
1197	0x6f,0xcc,0x67,0x0a,0x76,0xd1,0x8c,0x6c,0x6c,0xd1,0xeb,0x0c,0x31,0xa3,0xcf,0xb4,
1198	0xc9,0x17,0x84,0x66,0x08,0x89,0xf1,0xb5,0x0a,0xa7,0x99,0x36,0x70,0x62,0xab,0x39,
1199	0x1d,0x56,0xbc,0xfa,0x00,0x7d,0xf1,0x4e,0x16,0xe5,0x37,0x1a,0xed,0x7c,0x29,0x20,
1200	0x1b,0xcd,0x91,0x11,0x59,0xf1,0x7c,0x54,0xe4,0x1e,0x99,0xb5,0xb4,0x66,0xd5,0x20,
1201	0x17,0x9a,0x21,0x24,0x66,0xcd,0x71,0x6a,0x60,0x55,0x96,0x1d,0x2e,0x6c,0x78,0xb9,
1202	0xcc,0xab,0x70,0x84,0x7e,0xb3,0x82,0xae,0xa3,0x75,0x20,0x58,0xb4,0xa8,0xfc,0xc2,
1203	0xc2,0x5c,0x95,0xa3,0xe3,0xbc,0x64,0x6b,0xea,0x75,0xed,0xa9,0x18,0xcb,0x6e,0x06,
1204	0x6d,0x4f,0xd9,0x3e,0x8b,0xde,0x3b,0x1f,0x51,0x21,0x4d,0x51,0x3f,0x8f,0x3e,0xb6,
1205	0xe3,0x4f,0xa1,0xf7,0xce,0x47,0xad,0x70,0x8a,0xba,0x41,0xaf,0x9a,0x5e,0x56,0x8c,
1206	0xf6,0x18,0x7d,0x6c,0xc7,0x7b,0xe8,0x7f,0xf3,0x2f,0xf4,0x91,0x84,0x79,0x2c,0x15,
1207	0x8b,0xf7,0x61,0x93,0xcd,0x62,0x31,0x28,0xcd,0x4f,0x20,0x16,0x2c,0x17,0x63,0x2f,
1208	0x3a,0xec,0x80,0xb0,0x36,0x16,0x9b,0x31,0x72,0xa5,0xbd,0x60,0xe1,0x6d,0x84,0x54,
1209	0xbe,0x16,0xff,0xeb,0x2e,0x8f,0x6b,0xd8,0x01,0x7a,0x31,0xb2,0xad,0xe9,0xf9,0x4c,
1210	0xc2,0x12,0xbd,0x98,0x91,0x7c,0x92,0xa9,0xe8,0x49,0x87,0xa2,0x53,0x20,0xeb,0xb4,
1211	0xbd,0x78,0x8e,0xde,0xce,0x8c,0x55,0x1c,0x9a,0x71,0xb5,0x6c,0xd0,0x4b,0x0a,0x39,
1212	0x73,0xf0,0x12,0xfd,0x30,0xf2,0x29,0xa0,0x9f,0x1a,0x52,0x66,0x2f,0xde,0x38,0xc8,
1213	0x20,0x87,0xd9,0x44,0xde,0x14,0xd0,0x17,0x00,0x56,0x2d,0xf0,0x47,0x3e,0x20,0x4d,
1214	0x2c,0xe2,0x2b,0xf4,0x62,0xab,0xb2,0x1c,0xaf,0x1e,0x08,0xb5,0xb0,0xc9,0x67,0x54,
1215	0xeb,0xfa,0x4d,0x14,0xe9,0x15,0xfa,0x91,0x7a,0x78,0xbb,0xba,0xa2,0x5c,0x69,0x3e,
1216	0x9c,0x69,0xea,0xad,0xe3,0x9e,0xae,0xc6,0xe8,0x71,0xc2,0xa9,0x4b,0xa3,0xc6,0x41,
1217	0x47,0xe5,0x20,0xe3,0x7f,0x77,0xe4,0x83,0x57,0xef,0x8e,0x45,0xdc,0x99,0x2d,0x6e,
1218	0xdc,0x14,0x27,0xc7,0x02,0x82,0x45,0x1d,0xb6,0x20,0x57,0x23,0xe5,0x0f,0x21,0x3a,
1219	0x07,0xde,0x11,0x7a,0x12,0xcf,0x0c,0x00,0xa9,0x28,0x5e,0x68,0x3e,0x22,0x65,0xfe,
1220	0x56,0x86,0x42,0x15,0x9a,0x5f,0xa5,0xd0,0x26,0x64,0x43,0xbb,0x2c,0x68,0xaa,0xaa,
1221	0x96,0x9a,0x2d,0xa4,0x9b,0xc3,0xb3,0x08,0x44,0x02,0x66,0xe6,0xab,0x4a,0x5e,0x42,
1222	0xf8,0x7a,0x99,0x8e,0xf8,0x1d,0xc9,0xde,0xcf,0x7a,0x1b,0xcd,0xcf,0x5b,0xe5,0x8a,
1223	0x5e,0xa6,0x19,0x31,0xf0,0x71,0xa8,0x53,0x35,0x1f,0x1c,0xc4,0xcf,0xab,0xeb,0x55,
1224	0x70,0x9e,0xf5,0xe6,0x46,0xc8,0x64,0x61,0x4e,0xd7,0x06,0x87,0x32,0xd0,0x4b,0x75,
1225	0x62,0x1e,0x1f,0xc9,0x1e,0xd3,0x33,0x38,0x86,0x4c,0x46,0x18,0x9d,0x0d,0x3c,0xd2,
1226	0x8d,0x00,0xe9,0x2a,0xa3,0xd1,0x62,0xe0,0xd3,0x34,0xee,0xe5,0x28,0xd9,0x3d,0xfa,
1227	0x05,0xda,0xd5,0xe4,0x03,0x1e,0xa4,0xe6,0x3e,0x54,0x1c,0xe8,0xb5,0x3a,0x6e,0xed,
1228	0x2b,0xf4,0x45,0xcd,0xb4,0x33,0xf4,0x51,0xf3,0xc5,0xff,0x5b,0x1d,0x43,0x01,0xfd,
1229	0x90,0xb9,0x2e,0xcc,0x7a,0xdf,0x45,0x2f,0xb5,0x38,0x7f,0x2d,0x18,0x53,0x98,0x94,
1230	0xe9,0x1c,0xbd,0x9a,0x69,0xbd,0xe1,0xd0,0xac,0xda,0xed,0x95,0x27,0x25,0xef,0x26,
1231	0xb1,0xb0,0x60,0x42,0xd2,0xe3,0x6c,0xe3,0x2d,0xd8,0x91,0xac,0x78,0x76,0xf7,0x01,
1232	0xf4,0x52,0x8b,0xf3,0xec,0x04,0x62,0xa6,0x44,0xad,0x9f,0x9d,0xdd,0xf2,0xd5,0x77,
1233	0x73,0x2b,0x75,0x85,0x9c,0x9a,0x12,0xc3,0x9e,0x5f,0xcf,0xb1,0xdd,0xdf,0x7d,0x1f,
1234	0xfd,0xaa,0x96,0x45,0x79,0xfa,0xf8,0x00,0xbd,0xe5,0xdb,0x56,0xfc,0x1f,0xfa,0xff,
1235	0x27,0xf4,0xbf,0xf9,0x97,0x3b,0x5f,0xa7,0x75,0x5b,0xf4,0xc3,0x46,0x9e,0xbe,0xc3,
1236	0xd9,0x75,0x18,0x01,0xf2,0x31,0x01,0xe9,0x97,0x32,0x7a,0xd5,0x24,0x32,0x1b,0x7b,
1237	0x7b,0xa2,0xe9,0x26,0x60,0x24,0xfa,0x66,0x53,0xb8,0x38,0x21,0x2f,0xdd,0x33,0xab,
1238	0xa2,0x67,0xe0,0xa7,0xbb,0x03,0xf1,0xaf,0xd1,0xc3,0x06,0xe7,0x35,0x34,0xa2,0x17,
1239	0x5a,0xc0,0xe7,0x95,0xf9,0x61,0x18,0x01,0xf2,0x7d,0x40,0x3a,0x3d,0xec,0x93,0xc0,
1240	0x2c,0x71,0x1f,0xb2,0x10,0x3e,0x6c,0x88,0xe8,0x9b,0xfb,0x12,0xc0,0x8b,0xa0,0x83,
1241	0xf5,0x50,0x42,0x4a,0x24,0x5d,0xe2,0xee,0x1e,0xa1,0x6f,0x8e,0x44,0x34,0x66,0x68,
1242	0xf3,0xe1,0x0c,0x66,0x75,0x38,0xf6,0xc4,0xa7,0xdd,0xe4,0xb8,0x17,0xdb,0x20,0x4a,
1243	0xc1,0x70,0x65,0xa3,0xd7,0x23,0x13,0xf5,0x00,0x4d,0x36,0xeb,0xb8,0xde,0xa2,0x27,
1244	0xd3,0x38,0x81,0xdf,0x19,0x68,0x95,0x72,0xa6,0xbb,0xd7,0x4b,0x7e,0x44,0x0f,0xc3,
1245	0xa6,0x81,0x58,0xeb,0x0e,0x2d,0x7a,0xa1,0x13,0x3e,0xce,0x11,0x68,0x6c,0x28,0x8a,
1246	0xb9,0x57,0xe1,0xcb,0x80,0xdd,0x01,0x0e,0xec,0xd0,0x63,0x5f,0x56,0xde,0xb2,0x55,
1247	0xca,0x3b,0x9b,0x69,0xeb,0x5e,0x77,0x8b,0x4d,0xf3,0xa5,0x24,0x92,0x0e,0xd5,0x31,
1248	0x70,0x95,0x63,0x4c,0x1b,0x76,0xe6,0x03,0xf4,0xf0,0x51,0x69,0xd3,0x66,0x5f,0x0e,
1249	0x89,0x31,0x67,0x54,0x96,0x58,0xf7,0x58,0xf6,0xaa,0x91,0x32,0x36,0x44,0xd1,0x5b,
1250	0x13,0x4b,0x5a,0x50,0x88,0x55,0x21,0xde,0x2a,0xbf,0x01,0x1d,0xf6,0xaa,0x33,0x7a,
1251	0x54,0x51,0x6d,0x84,0xb1,0xb1,0x87,0xa3,0x86,0xda,0x61,0xda,0xdd,0x2f,0x0d,0x9e,
1252	0x49,0xf6,0x10,0x8f,0x17,0x85,0x3b,0x29,0xca,0x1d,0xaf,0x6c,0x58,0x4e,0xbb,0xc9,
1253	0xa1,0x18,0x68,0x24,0xcf,0x16,0xaa,0xf9,0x51,0xf6,0xb8,0xa2,0x9f,0xdc,0x06,0xb4,
1254	0xed,0xd2,0xca,0xf9,0x49,0xbf,0xe9,0x27,0x55,0x54,0x53,0x6e,0xee,0x65,0xd4,0xaf,
1255	0x77,0xba,0x13,0xf2,0x4d,0xcd,0x8f,0x5e,0x14,0x8c,0xfb,0x42,0x2a,0x2b,0xe2,0x29,
1256	0x4d,0x99,0x2b,0xe9,0x10,0xd7,0xb9,0x8f,0x1a,0xf6,0x28,0x12,0x7a,0x67,0x2e,0xeb,
1257	0x45,0xd1,0x33,0x77,0x8c,0x73,0xbe,0xa5,0x72,0x23,0x9b,0x8f,0xd9,0xee,0xee,0x9e,
1258	0xa1,0xa7,0x84,0x96,0xa0,0xc3,0x26,0x7b,0x1c,0x47,0x13,0x2d,0x0e,0x4f,0x45,0xbe,
1259	0xe7,0x0b,0x30,0x7d,0x12,0x8b,0x8e,0x34,0xd9,0xd0,0x83,0x71,0xb5,0x1b,0xf4,0xb2,
1260	0xd9,0x21,0x0a,0x64,0x4b,0x48,0xbe,0x6b,0xed,0x99,0xe6,0xcb,0xa6,0x98,0x9e,0xf7,
1261	0xaa,0x90,0x22,0xc1,0x33,0xab,0xc7,0xd1,0x20,0x41,0x8a,0x32,0x0c,0xe2,0x9d,0x92,
1262	0x58,0xb4,0xdb,0xbf,0x88,0xb2,0x37,0xf7,0x4d,0xd8,0xee,0x5a,0x98,0x89,0x50,0x20,
1263	0x1e,0x65,0xf3,0x5d,0x93,0x3d,0xb7,0x73,0xf4,0x71,0xa5,0xcc,0x61,0xde,0x7b,0x8f,
1264	0x78,0x77,0xe8,0x57,0x85,0xf5,0x9c,0xcf,0x34,0x3f,0xa5,0xed,0x76,0x0a,0x61,0x5d,
1265	0x8f,0x3b,0x76,0x1c,0xef,0x30,0x8c,0x1e,0xa0,0x9f,0xc5,0x34,0x35,0xe6,0x07,0xd1,
1266	0x7b,0x37,0x87,0x56,0xd8,0x5e,0xa1,0xdf,0xdc,0x1d,0x6c,0x6e,0xad,0x04,0xfb,0xf7,
1267	0xd0,0x2f,0x2b,0x24,0x0c,0xf2,0x87,0xe8,0xe1,0xea,0xb9,0x45,0xff,0x9b,0x7f,0x61,
1268	0x5c,0x61,0xd0,0xc7,0x1d,0x2c,0xf1,0x8a,0xf8,0x1d,0x2d,0xed,0xde,0x06,0x3a,0xe4,
1269	0xb2,0x76,0xb3,0x62,0x41,0x4c,0x24,0x4e,0xae,0x62,0xeb,0x36,0xcb,0x6c,0x49,0x8e,
1270	0x1a,0x32,0x89,0x7b,0x93,0xd1,0x32,0x28,0xcf,0xa2,0xd8,0xee,0x3e,0x56,0x69,0x29,
1271	0x03,0x8b,0x28,0xe7,0x81,0xdb,0x1c,0x1f,0x66,0xce,0xe8,0x61,0x0a,0x17,0xf9,0x00,
1272	0x40,0xbe,0x04,0xc6,0x2e,0x6f,0x85,0x1d,0x5b,0xba,0x7b,0xd3,0x65,0x59,0x74,0x99,
1273	0xb1,0x65,0x76,0xd8,0x90,0x09,0xfd,0x26,0xa3,0xb5,0x9c,0xdc,0x37,0xe4,0xc5,0x4e,
1274	0x69,0xb9,0x7e,0x38,0x0d,0x23,0xfa,0x62,0xa6,0xf1,0x3e,0x58,0x5d,0x8b,0x33,0xea,
1275	0xb1,0x9d,0x7a,0xd4,0x00,0x31,0xae,0x8b,0xf8,0x72,0xc2,0x49,0x96,0x9a,0xc2,0xe2,
1276	0x9f,0xa1,0x6d,0x52,0xc4,0x40,0xef,0xf2,0x1e,0x85,0xea,0x41,0x50,0xcb,0x18,0xeb,
1277	0xf4,0x3a,0x60,0x56,0x33,0x5f,0xb9,0xed,0xc2,0x11,0x9e,0x34,0x40,0x36,0x91,0xa1,
1278	0x3b,0xeb,0xa3,0x6d,0xab,0x10,0x39,0x78,0xec,0x8d,0x32,0x8e,0xf3,0xba,0x90,0x7d,
1279	0x94,0x14,0xbf,0xb6,0xa2,0x4d,0x50,0x36,0x57,0x32,0x11,0x59,0x57,0x1e,0x81,0x20,
1280	0x3d,0xa2,0x95,0xc9,0xd8,0x3a,0x0c,0xc7,0x8e,0x80,0x9e,0x20,0x7b,0xa7,0xa8,0xc7,
1281	0xa1,0x68,0x90,0x93,0x7d,0xdd,0x04,0x3b,0xf4,0x5e,0xf3,0x9f,0xa1,0xc7,0x6c,0xda,
1282	0x16,0x5f,0xc0,0x6c,0xba,0x0b,0xfd,0x25,0x56,0xf7,0xfb,0x9a,0x0f,0xd9,0xfb,0x7f,
1283	0x56,0x80,0x7b,0x61,0x1d,0x9c,0xa0,0x0f,0x8e,0xc7,0x97,0xa1,0x7c,0x53,0x27,0xba,
1284	0x73,0x80,0x5e,0x17,0x8d,0x26,0x56,0x77,0x40,0xff,0x54,0xf3,0x0b,0xf4,0x20,0x1e,
1285	0x41,0x7b,0x3c,0xeb,0x21,0x73,0x54,0xd0,0x97,0xa1,0xb8,0x73,0x09,0x92,0x7c,0x95,
1286	0xc1,0x7b,0x20,0x71,0x30,0x50,0xde,0xf8,0x73,0x67,0x0f,0x42,0x3d,0xca,0x66,0xb4,
1287	0xa8,0xee,0x83,0xd5,0x75,0xfa,0x1c,0x68,0xfa,0x37,0x06,0x76,0x21,0xaf,0x37,0xec,
1288	0xc6,0x09,0x9f,0xeb,0x6c,0x33,0xc0,0x43,0x03,0x4b,0x7a,0xe4,0xad,0x25,0xee,0x6a,
1289	0x3e,0x09,0x61,0x67,0x38,0x1b,0xe0,0x26,0xd8,0x5d,0xdf,0xa8,0x78,0xb5,0xde,0x3f,
1290	0x6c,0xb9,0xc5,0x7c,0x13,0xbd,0x67,0xaf,0xbf,0x0f,0xfd,0xa2,0x41,0xff,0x2a,0xf4,
1291	0xbf,0xf9,0x57,0xf6,0x1c,0xc1,0x91,0x8f,0xb6,0x63,0x39,0x1a,0x6e,0xca,0x68,0x04,
1292	0x5e,0xd3,0x99,0xd9,0xf8,0xe3,0x66,0x0d,0xdc,0x00,0xe1,0x6d,0x33,0xda,0x87,0x38,
1293	0x8e,0x3f,0xcc,0x36,0xc6,0xae,0xa2,0x5c,0x92,0x0b,0xb0,0x95,0x5b,0xf4,0xf7,0x3c,
1294	0x43,0x76,0xfb,0xed,0x94,0xdb,0x68,0x53,0x53,0xdb,0xa5,0x1e,0x34,0xc3,0x51,0x96,
1295	0x84,0xfe,0x19,0xc7,0xf1,0x87,0xd9,0xca,0x73,0x96,0x63,0xc6,0xde,0x79,0x1e,0x5e,
1296	0xa6,0xe5,0x94,0x9b,0xec,0xd8,0xc9,0xf1,0xa8,0xd1,0xb7,0x0f,0x8a,0xc2,0xf6,0xd3,
1297	0x78,0x7e,0x83,0xe3,0xe8,0x8b,0x2e,0x5f,0xa0,0xeb,0x91,0xaf,0x07,0xe1,0x53,0xfb,
1298	0x10,0x7f,0xe6,0xb4,0x44,0x5b,0xb4,0x4d,0x87,0x45,0xc1,0x56,0x7a,0x93,0xe3,0x44,
1299	0xf4,0xe4,0xed,0xb6,0xe3,0x10,0x56,0x9b,0x63,0xb8,0xb7,0xdc,0x20,0xa3,0x6f,0x4a,
1300	0xb7,0x1e,0xda,0xb9,0xb0,0xb3,0xda,0x29,0xc7,0xf1,0xe8,0x43,0x44,0xef,0xdf,0xb0,
1301	0xf3,0x23,0xc7,0xd3,0xe7,0x5d,0x38,0xa3,0x2f,0x8f,0x1a,0xaf,0x20,0xd4,0xcd,0x7c,
1302	0xc8,0x71,0x5e,0xa2,0x7f,0xc3,0xce,0x7f,0x9c,0xe7,0x73,0xe8,0x31,0x4d,0x1c,0x72,
1303	0x9c,0x7b,0xf4,0x6f,0x6b,0xfe,0x09,0x21,0x91,0x70,0x46,0x4f,0x79,0x0e,0x3a,0x2d,
1304	0x4a,0x66,0xfa,0x73,0x8e,0x83,0x76,0x2b,0xab,0x42,0x04,0xcf,0xf9,0xc6,0x58,0x1e,
1305	0x84,0xca,0x23,0xda,0x09,0x31,0x11,0x76,0x64,0x87,0xc2,0xfe,0x24,0xc7,0x21,0xfb,
1306	0xd2,0x2b,0x44,0xf4,0xfe,0x51,0x96,0x73,0x1b,0xe2,0xcf,0x5d,0xde,0xa9,0xf6,0xb9,
1307	0x9d,0x1b,0x34,0xdf,0x47,0x7c,0x00,0xfd,0xc2,0x47,0xf7,0x0f,0x43,0xef,0xf7,0x20,
1308	0x82,0x67,0xfa,0x03,0xe8,0x8f,0xc3,0xbf,0x86,0xde,0xff,0xeb,0x21,0xe1,0xdf,0x3a,
1309	0xec,0xbf,0xdd,0x97,0xf3,0x3f,0xca,0x1b,0xaa,0xdf,0xfc,0xa4,0x05,0x0a,0x00,0x00,
1310	0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82 };
1311
1312int tigr_font_size = (int)sizeof(tigr_font);
1313
1314
1315//////// End of inlined file: tigr_font.h ////////
1316
1317#include <stdlib.h>
1318#include <stdarg.h>
1319#include <errno.h>
1320#include <stdio.h>
1321
1322#ifdef _MSC_VER
1323#define vsnprintf _vsnprintf
1324#endif
1325
1326TigrFont tigrStockFont;
1327TigrFont *tfont = &tigrStockFont;
1328
1329// Converts 8-bit codepage entries into Unicode code points.
1330static int cp1252[] = {
1331	0x20ac,0xfffd,0x201a,0x0192,0x201e,0x2026,0x2020,0x2021,0x02c6,0x2030,0x0160,0x2039,0x0152,0xfffd,0x017d,0xfffd,
1332	0xfffd,0x2018,0x2019,0x201c,0x201d,0x2022,0x2013,0x2014,0x02dc,0x2122,0x0161,0x203a,0x0153,0xfffd,0x017e,0x0178,
1333	0x00a0,0x00a1,0x00a2,0x00a3,0x00a4,0x00a5,0x00a6,0x00a7,0x00a8,0x00a9,0x00aa,0x00ab,0x00ac,0x00ad,0x00ae,0x00af,
1334	0x00b0,0x00b1,0x00b2,0x00b3,0x00b4,0x00b5,0x00b6,0x00b7,0x00b8,0x00b9,0x00ba,0x00bb,0x00bc,0x00bd,0x00be,0x00bf,
1335	0x00c0,0x00c1,0x00c2,0x00c3,0x00c4,0x00c5,0x00c6,0x00c7,0x00c8,0x00c9,0x00ca,0x00cb,0x00cc,0x00cd,0x00ce,0x00cf,
1336	0x00d0,0x00d1,0x00d2,0x00d3,0x00d4,0x00d5,0x00d6,0x00d7,0x00d8,0x00d9,0x00da,0x00db,0x00dc,0x00dd,0x00de,0x00df,
1337	0x00e0,0x00e1,0x00e2,0x00e3,0x00e4,0x00e5,0x00e6,0x00e7,0x00e8,0x00e9,0x00ea,0x00eb,0x00ec,0x00ed,0x00ee,0x00ef,
1338	0x00f0,0x00f1,0x00f2,0x00f3,0x00f4,0x00f5,0x00f6,0x00f7,0x00f8,0x00f9,0x00fa,0x00fb,0x00fc,0x00fd,0x00fe,0x00ff,
1339};
1340static int border(Tigr *bmp, int x, int y)
1341{
1342	TPixel top = tigrGet(bmp, 0, 0);
1343	TPixel c = tigrGet(bmp, x, y);
1344	return (c.r == top.r && c.g == top.g && c.b == top.b) || x >= bmp->w || y >= bmp->h;
1345}
1346
1347static void scan(Tigr *bmp, int *x, int *y, int *rowh)
1348{
1349	while (*y < bmp->h)
1350	{
1351		if (*x >= bmp->w) {
1352			*x = 0;
1353			(*y) += *rowh;
1354			*rowh = 1;
1355		}
1356		if (!border(bmp, *x, *y))
1357			return;
1358		(*x)++;
1359	}
1360}
1361
1362int tigrLoadGlyphs(TigrFont *font, int codepage)
1363{
1364	int i, x=0, y=0, w, h, rowh=1;
1365	TigrGlyph *g;
1366	switch (codepage) {
1367		case 0:    font->numGlyphs = 128-32; break;
1368		case 1252: font->numGlyphs = 256-32; break;
1369	}
1370
1371	font->glyphs = (TigrGlyph *)calloc(font->numGlyphs, sizeof(TigrGlyph));
1372	for (i=32;i<font->numGlyphs+32;i++)
1373	{
1374		// Find the next glyph.
1375		scan(font->bitmap, &x, &y, &rowh);
1376		if (y >= font->bitmap->h)
1377		{
1378			errno = EINVAL;
1379			return 0;
1380		}
1381
1382		// Scan the width and height
1383		w = h = 0;
1384		while (!border(font->bitmap, x+w, y)) w++;
1385		while (!border(font->bitmap, x, y+h)) h++;
1386
1387		// Look up the Unicode code point.
1388		g = &font->glyphs[i-32];
1389		if (i < 128) g->code = i; // ASCII
1390		else if (codepage == 1252) g->code = cp1252[i-128];
1391		else { errno = EINVAL; return 0; }
1392
1393		g->x = x; g->y = y; g->w = w; g->h = h;
1394		x += w;
1395		if (h != font->glyphs[0].h) { errno = EINVAL; return 0; }
1396
1397		if (h > rowh)
1398			rowh = h;
1399	}
1400
1401	// Sort by code point.
1402	for (i=1;i<font->numGlyphs;i++)
1403	{
1404		int j = i;
1405		TigrGlyph g = font->glyphs[i];
1406		while (j > 0 && font->glyphs[j-1].code > g.code) {
1407			font->glyphs[j] = font->glyphs[j-1];
1408			j--;
1409		}
1410		font->glyphs[j] = g;
1411	}
1412
1413	return 1;
1414}
1415
1416TigrFont *tigrLoadFont(Tigr *bitmap, int codepage)
1417{
1418	TigrFont *font = (TigrFont *)calloc(1, sizeof(TigrFont));
1419	font->bitmap = bitmap;
1420	if (!tigrLoadGlyphs(font, codepage))
1421	{
1422		tigrFreeFont(font);
1423		return NULL;
1424	}
1425	return font;
1426}
1427
1428void tigrFreeFont(TigrFont *font)
1429{
1430	tigrFree(font->bitmap);
1431	free(font->glyphs);
1432	free(font);
1433}
1434
1435static TigrGlyph *get(TigrFont *font, int code)
1436{
1437	unsigned lo = 0, hi = font->numGlyphs;
1438	while (lo < hi) {
1439		unsigned guess = (lo + hi) / 2;
1440		if (code < font->glyphs[guess].code) hi = guess;
1441		else lo = guess + 1;
1442	}
1443
1444	if (lo == 0 || font->glyphs[lo-1].code != code)
1445		return &font->glyphs['?' - 32];
1446	else
1447		return &font->glyphs[lo-1];
1448}
1449
1450void tigrSetupFont(TigrFont *font)
1451{
1452	// Load the stock font if needed.
1453	if (font == tfont && !tfont->bitmap)
1454	{
1455		tfont->bitmap = tigrLoadImageMem(tigr_font, tigr_font_size);
1456		tigrLoadGlyphs(tfont, 1252);
1457	}
1458}
1459
1460void tigrPrint(Tigr *dest, TigrFont *font, int x, int y, TPixel color, const char *text, ...)
1461{
1462	char tmp[1024];
1463	TigrGlyph *g;
1464	va_list args;
1465	const char *p;
1466	int start = x, c;
1467
1468	tigrSetupFont(font);
1469
1470	// Expand the formatting string.
1471	va_start(args, text);
1472	vsnprintf(tmp, sizeof(tmp), text, args);
1473	tmp[sizeof(tmp)-1] = 0;
1474	va_end(args);
1475
1476	// Print each glyph.
1477	p = tmp;
1478	while (*p)
1479	{
1480		p = tigrDecodeUTF8(p, &c);
1481		if (c == '\r')
1482			continue;
1483		if (c == '\n') {
1484			x = start;
1485			y += tigrTextHeight(font, "");
1486			continue;
1487		}
1488		g = get(font, c);
1489		tigrBlitTint(dest, font->bitmap, x, y, g->x, g->y, g->w, g->h, color);
1490		x += g->w;
1491	}
1492}
1493
1494int tigrTextWidth(TigrFont *font, const char *text)
1495{
1496	int x = 0, w = 0, c;
1497	tigrSetupFont(font);
1498
1499	while (*text)
1500	{
1501		text = tigrDecodeUTF8(text, &c);
1502		if (c == '\n' || c == '\r') {
1503			x = 0;
1504		} else {
1505			x += get(font, c)->w;
1506			w = (x > w) ? x : w;
1507		}
1508	}
1509	return w;
1510}
1511
1512int tigrTextHeight(TigrFont *font, const char *text)
1513{
1514	int rowh, h, c;
1515	tigrSetupFont(font);
1516
1517	h = rowh = get(font, 0)->h;
1518	while (*text)
1519	{
1520		text = tigrDecodeUTF8(text, &c);
1521		if (c == '\n' && *text)
1522			h += rowh; 
1523	}
1524	return h;
1525}
1526
1527//////// End of inlined file: tigr_print.c ////////
1528
1529
1530#ifdef _WIN32
1531#define WIN32_LEAN_AND_MEAN
1532#include <windows.h>
1533//////// Start of inlined file: tigr_upscale_d3d9_vs.h ////////
1534
1535#if 0
1536//
1537// Generated by Microsoft (R) HLSL Shader Compiler 10.0.10011.16384
1538//
1539// Parameters:
1540//
1541//   float4 screenSize;
1542//   float4 texSize;
1543//
1544//
1545// Registers:
1546//
1547//   Name         Reg   Size
1548//   ------------ ----- ----
1549//   screenSize   c0       1
1550//   texSize      c1       1
1551//
1552
1553    vs_2_0
1554    def c2, -0.5, 2, -1, 0
1555    def c3, 1, -1, 0, 0
1556    dcl_position v0
1557    dcl_texcoord v1
1558    mul oT0.xy, v1, c1
1559    add r0.xyz, v0.xyxw, c2.x
1560    rcp r1.xw, c0.x
1561    rcp r1.y, c0.y
1562    mul r0.xyz, r0, r1.xyww
1563    mad r0.xyz, r0, c2.y, c2.z
1564    mad oPos.xyw, r0.xyzz, c3.xyzz, c3.zzzx
1565    mov oPos.z, v0.z
1566
1567// approximately 8 instruction slots used
1568#endif
1569
1570const BYTE tigrUpscaleVSCode[] =
1571{
1572      0,   2, 254, 255, 254, 255, 
1573     42,   0,  67,  84,  65,  66, 
1574     28,   0,   0,   0, 111,   0, 
1575      0,   0,   0,   2, 254, 255, 
1576      2,   0,   0,   0,  28,   0, 
1577      0,   0,   8, 131,   0,   0, 
1578    104,   0,   0,   0,  68,   0, 
1579      0,   0,   2,   0,   0,   0, 
1580      1,   0,   2,   0,  80,   0, 
1581      0,   0,   0,   0,   0,   0, 
1582     96,   0,   0,   0,   2,   0, 
1583      1,   0,   1,   0,   6,   0, 
1584     80,   0,   0,   0,   0,   0, 
1585      0,   0, 115,  99, 114, 101, 
1586    101, 110,  83, 105, 122, 101, 
1587      0, 171,   1,   0,   3,   0, 
1588      1,   0,   4,   0,   1,   0, 
1589      0,   0,   0,   0,   0,   0, 
1590    116, 101, 120,  83, 105, 122, 
1591    101,   0, 118, 115,  95,  50, 
1592     95,  48,   0,  77, 105,  99, 
1593    114, 111, 115, 111, 102, 116, 
1594     32,  40,  82,  41,  32,  72, 
1595     76,  83,  76,  32,  83, 104, 
1596     97, 100, 101, 114,  32,  67, 
1597    111, 109, 112, 105, 108, 101, 
1598    114,  32,  49,  48,  46,  48, 
1599     46,  49,  48,  48,  49,  49, 
1600     46,  49,  54,  51,  56,  52, 
1601      0, 171,  81,   0,   0,   5, 
1602      2,   0,  15, 160,   0,   0, 
1603      0, 191,   0,   0,   0,  64, 
1604      0,   0, 128, 191,   0,   0, 
1605      0,   0,  81,   0,   0,   5, 
1606      3,   0,  15, 160,   0,   0, 
1607    128,  63,   0,   0, 128, 191, 
1608      0,   0,   0,   0,   0,   0, 
1609      0,   0,  31,   0,   0,   2, 
1610      0,   0,   0, 128,   0,   0, 
1611     15, 144,  31,   0,   0,   2, 
1612      5,   0,   0, 128,   1,   0, 
1613     15, 144,   5,   0,   0,   3, 
1614      0,   0,   3, 224,   1,   0, 
1615    228, 144,   1,   0, 228, 160, 
1616      2,   0,   0,   3,   0,   0, 
1617      7, 128,   0,   0, 196, 144, 
1618      2,   0,   0, 160,   6,   0, 
1619      0,   2,   1,   0,   9, 128, 
1620      0,   0,   0, 160,   6,   0, 
1621      0,   2,   1,   0,   2, 128, 
1622      0,   0,  85, 160,   5,   0, 
1623      0,   3,   0,   0,   7, 128, 
1624      0,   0, 228, 128,   1,   0, 
1625    244, 128,   4,   0,   0,   4, 
1626      0,   0,   7, 128,   0,   0, 
1627    228, 128,   2,   0,  85, 160, 
1628      2,   0, 170, 160,   4,   0, 
1629      0,   4,   0,   0,  11, 192, 
1630      0,   0, 164, 128,   3,   0, 
1631    164, 160,   3,   0,  42, 160, 
1632      1,   0,   0,   2,   0,   0, 
1633      4, 192,   0,   0, 170, 144, 
1634    255, 255,   0,   0
1635};
1636
1637//////// End of inlined file: tigr_upscale_d3d9_vs.h ////////
1638
1639//////// Start of inlined file: tigr_upscale_d3d9_ps.h ////////
1640
1641#if 0
1642//
1643// Generated by Microsoft (R) HLSL Shader Compiler 10.0.10011.16384
1644//
1645// Parameters:
1646//
1647//   sampler2D image;
1648//   float4 params;
1649//   float4 texSize;
1650//
1651//
1652// Registers:
1653//
1654//   Name         Reg   Size
1655//   ------------ ----- ----
1656//   texSize      c1       1
1657//   params       c2       1
1658//   image        s0       1
1659//
1660
1661    ps_2_0
1662    def c0, 0.5, -0.5, 0, 0
1663    dcl t0.xy
1664    dcl_2d s0
1665    frc r0.w, t0.y
1666    add r0.x, -r0.w, c0.x
1667    mov r1.w, c0.x
1668    mad r0.x, c2.z, r0.x, r1.w
1669    add r0.x, r0.x, r0.x
1670    frc r0.yz, t0.zxyw
1671    add r0.yz, -r0, t0.zxyw
1672    add r0.yz, r0, c0.x
1673    lrp r1.xy, c2, t0, r0.yzxw
1674    mul r2.x, r1.x, c1.z
1675    mul r2.y, r1.y, c1.w
1676    texld r2, r2, s0
1677    mad r0.xyz, r2, r0.x, c0.y
1678    mad r2.xyz, c2.w, r0, r1.w
1679    mov oC0, r2
1680
1681// approximately 15 instruction slots used (1 texture, 14 arithmetic)
1682#endif
1683
1684const BYTE tigrUpscalePSCode[] =
1685{
1686      0,   2, 255, 255, 254, 255, 
1687     52,   0,  67,  84,  65,  66, 
1688     28,   0,   0,   0, 151,   0, 
1689      0,   0,   0,   2, 255, 255, 
1690      3,   0,   0,   0,  28,   0, 
1691      0,   0,   8, 131,   0,   0, 
1692    144,   0,   0,   0,  88,   0, 
1693      0,   0,   3,   0,   0,   0, 
1694      1,   0,   0,   0,  96,   0, 
1695      0,   0,   0,   0,   0,   0, 
1696    112,   0,   0,   0,   2,   0, 
1697      2,   0,   1,   0,  10,   0, 
1698    120,   0,   0,   0,   0,   0, 
1699      0,   0, 136,   0,   0,   0, 
1700      2,   0,   1,   0,   1,   0, 
1701      6,   0, 120,   0,   0,   0, 
1702      0,   0,   0,   0, 105, 109, 
1703     97, 103, 101,   0, 171, 171, 
1704      4,   0,  12,   0,   1,   0, 
1705      1,   0,   1,   0,   0,   0, 
1706      0,   0,   0,   0, 112,  97, 
1707    114,  97, 109, 115,   0, 171, 
1708      1,   0,   3,   0,   1,   0, 
1709      4,   0,   1,   0,   0,   0, 
1710      0,   0,   0,   0, 116, 101, 
1711    120,  83, 105, 122, 101,   0, 
1712    112, 115,  95,  50,  95,  48, 
1713      0,  77, 105,  99, 114, 111, 
1714    115, 111, 102, 116,  32,  40, 
1715     82,  41,  32,  72,  76,  83, 
1716     76,  32,  83, 104,  97, 100, 
1717    101, 114,  32,  67, 111, 109, 
1718    112, 105, 108, 101, 114,  32, 
1719     49,  48,  46,  48,  46,  49, 
1720     48,  48,  49,  49,  46,  49, 
1721     54,  51,  56,  52,   0, 171, 
1722     81,   0,   0,   5,   0,   0, 
1723     15, 160,   0,   0,   0,  63, 
1724      0,   0,   0, 191,   0,   0, 
1725      0,   0,   0,   0,   0,   0, 
1726     31,   0,   0,   2,   0,   0, 
1727      0, 128,   0,   0,   3, 176, 
1728     31,   0,   0,   2,   0,   0, 
1729      0, 144,   0,   8,  15, 160, 
1730     19,   0,   0,   2,   0,   0, 
1731      8, 128,   0,   0,  85, 176, 
1732      2,   0,   0,   3,   0,   0, 
1733      1, 128,   0,   0, 255, 129, 
1734      0,   0,   0, 160,   1,   0, 
1735      0,   2,   1,   0,   8, 128, 
1736      0,   0,   0, 160,   4,   0, 
1737      0,   4,   0,   0,   1, 128, 
1738      2,   0, 170, 160,   0,   0, 
1739      0, 128,   1,   0, 255, 128, 
1740      2,   0,   0,   3,   0,   0, 
1741      1, 128,   0,   0,   0, 128, 
1742      0,   0,   0, 128,  19,   0, 
1743      0,   2,   0,   0,   6, 128, 
1744      0,   0, 210, 176,   2,   0, 
1745      0,   3,   0,   0,   6, 128, 
1746      0,   0, 228, 129,   0,   0, 
1747    210, 176,   2,   0,   0,   3, 
1748      0,   0,   6, 128,   0,   0, 
1749    228, 128,   0,   0,   0, 160, 
1750     18,   0,   0,   4,   1,   0, 
1751      3, 128,   2,   0, 228, 160, 
1752      0,   0, 228, 176,   0,   0, 
1753    201, 128,   5,   0,   0,   3, 
1754      2,   0,   1, 128,   1,   0, 
1755      0, 128,   1,   0, 170, 160, 
1756      5,   0,   0,   3,   2,   0, 
1757      2, 128,   1,   0,  85, 128, 
1758      1,   0, 255, 160,  66,   0, 
1759      0,   3,   2,   0,  15, 128, 
1760      2,   0, 228, 128,   0,   8, 
1761    228, 160,   4,   0,   0,   4, 
1762      0,   0,   7, 128,   2,   0, 
1763    228, 128,   0,   0,   0, 128, 
1764      0,   0,  85, 160,   4,   0, 
1765      0,   4,   2,   0,   7, 128, 
1766      2,   0, 255, 160,   0,   0, 
1767    228, 128,   1,   0, 255, 128, 
1768      1,   0,   0,   2,   0,   8, 
1769     15, 128,   2,   0, 228, 128, 
1770    255, 255,   0,   0
1771};
1772
1773//////// End of inlined file: tigr_upscale_d3d9_ps.h ////////
1774
1775//////// Start of inlined file: tigr_d3d9.c ////////
1776
1777//#include "tigr_internal.h"
1778
1779#ifdef TIGR_GAPI_D3D9
1780IDirect3D9 *tigrD3D = NULL;
1781extern const unsigned char tigrUpscaleVSCode[], tigrUpscalePSCode[];
1782
1783void tigrGAPICreate(Tigr *bmp)
1784{
1785	TigrInternal *win = tigrInternal(bmp);
1786	D3D9Stuff *d3d = &win->d3d9;
1787	DWORD flags;
1788	D3DCAPS9 caps;
1789
1790	if (!tigrD3D)
1791	{
1792		tigrD3D = Direct3DCreate9(D3D_SDK_VERSION);
1793		if (!tigrD3D)
1794			tigrError(bmp, "Cannot initialize Direct3D 9.");
1795	}
1796
1797	// Initialize D3D
1798	ZeroMemory(&d3d->params, sizeof(d3d->params));
1799	d3d->params.Windowed				= TRUE;
1800	d3d->params.SwapEffect				= D3DSWAPEFFECT_DISCARD;
1801	d3d->params.BackBufferFormat		= D3DFMT_A8R8G8B8;
1802	d3d->params.EnableAutoDepthStencil	= FALSE;
1803	d3d->params.PresentationInterval	= D3DPRESENT_INTERVAL_ONE; // TODO- vsync off if fps suffers?
1804	d3d->params.Flags					= 0;
1805	d3d->params.BackBufferWidth			= bmp->w;
1806	d3d->params.BackBufferHeight		= bmp->h;
1807
1808	// Check device caps.
1809	if (FAILED(IDirect3D9_GetDeviceCaps(tigrD3D, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps)))
1810		tigrError(bmp, "Cannot query Direct3D 9 capabilities.\n");
1811
1812	// Check vertex processing mode. Ideally I'd just always use software,
1813	// but some hardware only supports hardware mode (!)
1814	flags = D3DCREATE_PUREDEVICE | D3DCREATE_FPU_PRESERVE;
1815	if (caps.VertexProcessingCaps != 0)
1816		flags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
1817	else
1818		flags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
1819
1820	// Create the device.
1821	if (IDirect3D9_CreateDevice(tigrD3D, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, (HWND)bmp->handle,
1822		flags, &d3d->params, &d3d->dev))
1823		tigrError(bmp, "Cannot create Direct3D 9 device.\n");
1824
1825	// Create upscaling shaders.
1826	if (FAILED(IDirect3DDevice9_CreateVertexShader(d3d->dev, (DWORD *)tigrUpscaleVSCode, &d3d->vs))
1827	 || FAILED(IDirect3DDevice9_CreatePixelShader(d3d->dev, (DWORD *)tigrUpscalePSCode, &d3d->ps)))
1828		tigrError(bmp, "Cannot create Direct3D 9 shaders.\n");
1829}
1830
1831void tigrGAPIBegin(Tigr *bmp)
1832{
1833	HRESULT hr;
1834	TigrInternal *win = tigrInternal(bmp);
1835	D3D9Stuff *d3d = &win->d3d9;
1836
1837	if (d3d->lost)
1838	{
1839		// Check if it's OK to resume work yet.
1840		if (IDirect3DDevice9_TestCooperativeLevel(d3d->dev) == D3DERR_DEVICELOST)
1841			return;
1842
1843		hr = IDirect3DDevice9_Reset(d3d->dev, &d3d->params);
1844		if (hr == D3DERR_DEVICELOST)
1845			return;
1846
1847		if (FAILED(hr)
1848		 || FAILED(IDirect3DDevice9_CreateTexture(d3d->dev, bmp->w, bmp->h, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8,
1849			D3DPOOL_SYSTEMMEM, &d3d->sysTex[0], NULL))
1850		 || FAILED(IDirect3DDevice9_CreateTexture(d3d->dev, bmp->w, bmp->h, 1, 0, D3DFMT_A8R8G8B8,
1851			D3DPOOL_DEFAULT, &d3d->vidTex[0], NULL))
1852		 || FAILED(IDirect3DDevice9_CreateTexture(d3d->dev, win->widgets->w, win->widgets->h, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8,
1853			D3DPOOL_SYSTEMMEM, &d3d->sysTex[1], NULL))
1854		 || FAILED(IDirect3DDevice9_CreateTexture(d3d->dev, win->widgets->w, win->widgets->h, 1, 0, D3DFMT_A8R8G8B8,
1855			D3DPOOL_DEFAULT, &d3d->vidTex[1], NULL)))
1856		{
1857			tigrError(bmp, "Error creating Direct3D 9 textures.\n");
1858		}
1859
1860		d3d->lost = 0;
1861	}
1862}
1863
1864void tigrGAPIEnd(Tigr *bmp)
1865{
1866	TigrInternal *win = tigrInternal(bmp);
1867	D3D9Stuff *d3d = &win->d3d9;
1868	if (!d3d->lost)
1869	{
1870		IDirect3DTexture9_Release(d3d->sysTex[0]);
1871		IDirect3DTexture9_Release(d3d->vidTex[0]);
1872		IDirect3DTexture9_Release(d3d->sysTex[1]);
1873		IDirect3DTexture9_Release(d3d->vidTex[1]);
1874		IDirect3DDevice9_Reset(d3d->dev, &d3d->params);
1875		d3d->lost = 1;
1876	}
1877}
1878
1879void tigrGAPIDestroy(Tigr *bmp)
1880{
1881	TigrInternal *win = tigrInternal(bmp);
1882	D3D9Stuff *d3d = &win->d3d9;
1883
1884	IDirect3DVertexShader9_Release(d3d->vs);
1885	IDirect3DPixelShader9_Release(d3d->ps);
1886	IDirect3DDevice9_Release(d3d->dev);
1887}
1888
1889void tigrGAPIResize(Tigr *bmp, int width, int height)
1890{
1891	TigrInternal *win = tigrInternal(bmp);
1892	D3D9Stuff *d3d = &win->d3d9;
1893
1894	tigrGAPIEnd(bmp);
1895	d3d->params.BackBufferWidth = width;
1896	d3d->params.BackBufferHeight = height;
1897	tigrGAPIBegin(bmp);
1898}
1899
1900void tigrDxUpdate(IDirect3DDevice9 *dev, IDirect3DTexture9 *sysTex, IDirect3DTexture9 *vidTex, Tigr *bmp)
1901{
1902	D3DLOCKED_RECT rect;
1903	TPixel *src, *dest;
1904	int y;
1905
1906	// Lock the system memory texture.
1907	IDirect3DTexture9_LockRect(sysTex, 0, &rect, NULL, D3DLOCK_DISCARD);
1908
1909	// Copy our bitmap into it.
1910	src = bmp->pix;
1911	for (y=0;y<bmp->h;y++)
1912	{
1913		dest = (TPixel *)( (char *)rect.pBits + rect.Pitch*y );
1914		memcpy(dest, src, bmp->w*sizeof(TPixel));
1915		src += bmp->w;
1916	}
1917
1918	IDirect3DTexture9_UnlockRect(sysTex, 0);
1919
1920	// Update that into somewhere useful.
1921	IDirect3DDevice9_UpdateTexture(dev, (IDirect3DBaseTexture9 *)sysTex, (IDirect3DBaseTexture9 *)vidTex);
1922}
1923
1924void tigrDxQuad(IDirect3DDevice9 *dev, IDirect3DTexture9 *tex, int ix0, int iy0, int ix1, int iy1)
1925{
1926	float tri[6][5];
1927	float x0 = (float)ix0;
1928	float y0 = (float)iy0;
1929	float x1 = (float)ix1;
1930	float y1 = (float)iy1;
1931
1932	//          x               y               z              u              v
1933	tri[0][0] = x0; tri[0][1] = y0; tri[0][2] = 0; tri[0][3] = 0; tri[0][4] = 0;
1934	tri[1][0] = x1; tri[1][1] = y0; tri[1][2] = 0; tri[1][3] = 1; tri[1][4] = 0;
1935	tri[2][0] = x0; tri[2][1] = y1; tri[2][2] = 0; tri[2][3] = 0; tri[2][4] = 1;
1936
1937	tri[3][0] = x1; tri[3][1] = y0; tri[3][2] = 0; tri[3][3] = 1; tri[3][4] = 0;
1938	tri[4][0] = x1; tri[4][1] = y1; tri[4][2] = 0; tri[4][3] = 1; tri[4][4] = 1;
1939	tri[5][0] = x0; tri[5][1] = y1; tri[5][2] = 0; tri[5][3] = 0; tri[5][4] = 1;
1940
1941	IDirect3DDevice9_SetTexture(dev, 0, (IDirect3DBaseTexture9 *)tex);
1942	IDirect3DDevice9_DrawPrimitiveUP(dev, D3DPT_TRIANGLELIST, 2, &tri, 5*4);
1943}
1944
1945void tigrGAPIPresent(Tigr *bmp, int dw, int dh)
1946{
1947	TigrInternal *win;
1948	D3D9Stuff *d3d;
1949	HWND hWnd;
1950	HRESULT hr;
1951
1952	win = tigrInternal(bmp);
1953	d3d = &win->d3d9;
1954	hWnd = (HWND)bmp->handle;
1955
1956	// Make sure we have a device.
1957	// If we don't, then sleep to simulate the vsync until it comes back.
1958	if (d3d->lost)
1959	{
1960		tigrGAPIBegin(bmp);
1961		if (d3d->lost) {
1962			Sleep(15);
1963			return;
1964		}
1965	}
1966
1967	IDirect3DDevice9_BeginScene(d3d->dev);
1968
1969	// Upload the pixel data.
1970	tigrDxUpdate(d3d->dev, d3d->sysTex[0], d3d->vidTex[0], bmp);
1971	tigrDxUpdate(d3d->dev, d3d->sysTex[1], d3d->vidTex[1], win->widgets);
1972
1973	// Set up our upscale shader.
1974	IDirect3DDevice9_SetRenderState(d3d->dev, D3DRS_ALPHABLENDENABLE, FALSE);
1975	IDirect3DDevice9_SetVertexShader(d3d->dev, d3d->vs);
1976	IDirect3DDevice9_SetPixelShader(d3d->dev, d3d->ps);
1977	IDirect3DDevice9_SetFVF(d3d->dev, D3DFVF_XYZ|D3DFVF_TEX1);
1978	IDirect3DDevice9_SetSamplerState(d3d->dev, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
1979	IDirect3DDevice9_SetSamplerState(d3d->dev, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
1980	IDirect3DDevice9_SetSamplerState(d3d->dev, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
1981	IDirect3DDevice9_SetSamplerState(d3d->dev, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
1982
1983	// Let the shader know about the window size.
1984	{
1985		float consts[12];
1986		consts[0] = (float)dw;
1987		consts[1] = (float)dh;
1988		consts[2] = 1.0f / dw;
1989		consts[3] = 1.0f / dh;
1990		consts[4] = (float)bmp->w;
1991		consts[5] = (float)bmp->h;
1992		consts[6] = 1.0f / bmp->w;
1993		consts[7] = 1.0f / bmp->h;
1994		consts[8] = win->hblur ? 1.0f : 0.0f;
1995		consts[9] = win->vblur ? 1.0f : 0.0f;
1996		consts[10] = win->scanlines;
1997		consts[11] = win->contrast;
1998		IDirect3DDevice9_SetVertexShaderConstantF(d3d->dev, 0, consts, 3);
1999		IDirect3DDevice9_SetPixelShaderConstantF(d3d->dev, 0, consts, 3);
2000	}
2001
2002	// We clear so that a) we fill the border, and b) to let the driver
2003	// know it can discard the contents.
2004	IDirect3DDevice9_Clear(d3d->dev, 0, NULL, D3DCLEAR_TARGET, 0, 0, 0);
2005
2006	// Blit the final image to the screen.
2007	tigrDxQuad(d3d->dev, d3d->vidTex[0], win->pos[0], win->pos[1], win->pos[2], win->pos[3]);
2008
2009	// Draw the widget overlay.
2010	IDirect3DDevice9_SetRenderState(d3d->dev, D3DRS_ALPHABLENDENABLE, TRUE);
2011	IDirect3DDevice9_SetRenderState(d3d->dev, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
2012	IDirect3DDevice9_SetRenderState(d3d->dev, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
2013	tigrDxQuad(d3d->dev, d3d->vidTex[1], 
2014		(int)(dw - win->widgets->w * win->widgetsScale), 0, 
2015		dw, (int)(win->widgets->h * win->widgetsScale));
2016
2017	// Et fini.
2018	IDirect3DDevice9_EndScene(d3d->dev);
2019	hr = IDirect3DDevice9_Present(d3d->dev, NULL, NULL, hWnd, NULL );
2020
2021	// See if we lost our device.
2022	if (hr == D3DERR_DEVICELOST)
2023		tigrGAPIEnd(bmp);
2024}
2025
2026#endif
2027
2028//////// End of inlined file: tigr_d3d9.c ////////
2029
2030#endif // _WIN32
2031
2032//////// Start of inlined file: tigr_upscale_gl_vs.h ////////
2033
2034// Auto-generated by incbin.pl from tigr_upscale_gl_vs.glsl
2035
2036const unsigned char tigr_upscale_gl_vs[] = {
2037	0x23,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x33,0x30,0x20,0x63,0x6f,0x72,
2038	0x65,0x0d,0x0a,0x0d,0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x20,0x28,0x6c,0x6f,0x63,
2039	0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x30,0x29,0x20,0x69,0x6e,0x20,0x76,0x65,
2040	0x63,0x32,0x20,0x70,0x6f,0x73,0x5f,0x69,0x6e,0x3b,0x0d,0x0a,0x6c,0x61,0x79,0x6f,
2041	0x75,0x74,0x20,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x31,
2042	0x29,0x20,0x69,0x6e,0x20,0x76,0x65,0x63,0x32,0x20,0x75,0x76,0x5f,0x69,0x6e,0x3b,
2043	0x0d,0x0a,0x0d,0x0a,0x6f,0x75,0x74,0x20,0x76,0x65,0x63,0x32,0x20,0x75,0x76,0x3b,
2044	0x0d,0x0a,0x0d,0x0a,0x75,0x6e,0x69,0x66,0x6f,0x72,0x6d,0x20,0x6d,0x61,0x74,0x34,
2045	0x20,0x6d,0x6f,0x64,0x65,0x6c,0x3b,0x0d,0x0a,0x75,0x6e,0x69,0x66,0x6f,0x72,0x6d,
2046	0x20,0x6d,0x61,0x74,0x34,0x20,0x70,0x72,0x6f,0x6a,0x65,0x63,0x74,0x69,0x6f,0x6e,
2047	0x3b,0x0d,0x0a,0x0d,0x0a,0x76,0x6f,0x69,0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,
2048	0x0d,0x0a,0x7b,0x0d,0x0a,0x09,0x75,0x76,0x20,0x3d,0x20,0x75,0x76,0x5f,0x69,0x6e,
2049	0x3b,0x0d,0x0a,0x09,0x67,0x6c,0x5f,0x50,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x20,
2050	0x3d,0x20,0x70,0x72,0x6f,0x6a,0x65,0x63,0x74,0x69,0x6f,0x6e,0x20,0x2a,0x20,0x6d,
2051	0x6f,0x64,0x65,0x6c,0x20,0x2a,0x20,0x76,0x65,0x63,0x34,0x28,0x70,0x6f,0x73,0x5f,
2052	0x69,0x6e,0x2c,0x20,0x30,0x2e,0x30,0x2c,0x20,0x31,0x2e,0x30,0x29,0x3b,0x0d,0x0a,
2053	0x7d,0x0d,0x0a };
2054
2055int tigr_upscale_gl_vs_size = (int)sizeof(tigr_upscale_gl_vs);
2056
2057
2058//////// End of inlined file: tigr_upscale_gl_vs.h ////////
2059
2060//////// Start of inlined file: tigr_upscale_gl_fs.h ////////
2061
2062// Auto-generated by incbin.pl from tigr_upscale_gl_fs.glsl
2063
2064const unsigned char tigr_upscale_gl_fs[] = {
2065	0x23,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x33,0x30,0x20,0x63,0x6f,0x72,
2066	0x65,0x0d,0x0a,0x0d,0x0a,0x69,0x6e,0x20,0x76,0x65,0x63,0x32,0x20,0x75,0x76,0x3b,
2067	0x0d,0x0a,0x0d,0x0a,0x6f,0x75,0x74,0x20,0x76,0x65,0x63,0x34,0x20,0x63,0x6f,0x6c,
2068	0x6f,0x72,0x3b,0x0d,0x0a,0x0d,0x0a,0x75,0x6e,0x69,0x66,0x6f,0x72,0x6d,0x20,0x73,
2069	0x61,0x6d,0x70,0x6c,0x65,0x72,0x32,0x44,0x20,0x69,0x6d,0x61,0x67,0x65,0x3b,0x0d,
2070	0x0a,0x75,0x6e,0x69,0x66,0x6f,0x72,0x6d,0x20,0x76,0x65,0x63,0x34,0x20,0x70,0x61,
2071	0x72,0x61,0x6d,0x65,0x74,0x65,0x72,0x73,0x3b,0x0d,0x0a,0x0d,0x0a,0x76,0x6f,0x69,
2072	0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,0x0d,0x0a,0x7b,0x0d,0x0a,0x09,0x76,0x65,
2073	0x63,0x32,0x20,0x74,0x65,0x78,0x5f,0x73,0x69,0x7a,0x65,0x20,0x3d,0x20,0x74,0x65,
2074	0x78,0x74,0x75,0x72,0x65,0x53,0x69,0x7a,0x65,0x28,0x69,0x6d,0x61,0x67,0x65,0x2c,
2075	0x20,0x30,0x29,0x3b,0x0d,0x0a,0x09,0x76,0x65,0x63,0x32,0x20,0x75,0x76,0x5f,0x62,
2076	0x6c,0x75,0x72,0x20,0x3d,0x20,0x6d,0x69,0x78,0x28,0x66,0x6c,0x6f,0x6f,0x72,0x28,
2077	0x75,0x76,0x20,0x2a,0x20,0x74,0x65,0x78,0x5f,0x73,0x69,0x7a,0x65,0x29,0x20,0x2b,
2078	0x20,0x30,0x2e,0x35,0x2c,0x20,0x75,0x76,0x20,0x2a,0x20,0x74,0x65,0x78,0x5f,0x73,
2079	0x69,0x7a,0x65,0x2c,0x20,0x70,0x61,0x72,0x61,0x6d,0x65,0x74,0x65,0x72,0x73,0x2e,
2080	0x78,0x79,0x29,0x20,0x2f,0x20,0x74,0x65,0x78,0x5f,0x73,0x69,0x7a,0x65,0x3b,0x0d,
2081	0x0a,0x09,0x76,0x65,0x63,0x34,0x20,0x63,0x20,0x3d,0x20,0x74,0x65,0x78,0x74,0x75,
2082	0x72,0x65,0x28,0x69,0x6d,0x61,0x67,0x65,0x2c,0x20,0x75,0x76,0x5f,0x62,0x6c,0x75,
2083	0x72,0x29,0x3b,0x0d,0x0a,0x09,0x63,0x2e,0x72,0x67,0x62,0x20,0x2a,0x3d,0x20,0x6d,
2084	0x69,0x78,0x28,0x30,0x2e,0x35,0x2c,0x20,0x31,0x2e,0x30,0x20,0x2d,0x20,0x66,0x72,
2085	0x61,0x63,0x74,0x28,0x75,0x76,0x2e,0x79,0x20,0x2a,0x20,0x74,0x65,0x78,0x5f,0x73,
2086	0x69,0x7a,0x65,0x2e,0x79,0x29,0x2c,0x20,0x70,0x61,0x72,0x61,0x6d,0x65,0x74,0x65,
2087	0x72,0x73,0x2e,0x7a,0x29,0x20,0x2a,0x20,0x32,0x2e,0x30,0x3b,0x20,0x2f,0x2f,0x73,
2088	0x63,0x61,0x6e,0x6c,0x69,0x6e,0x65,0x0d,0x0a,0x09,0x63,0x20,0x3d,0x20,0x6d,0x69,
2089	0x78,0x28,0x76,0x65,0x63,0x34,0x28,0x30,0x2e,0x35,0x29,0x2c,0x20,0x63,0x2c,0x20,
2090	0x70,0x61,0x72,0x61,0x6d,0x65,0x74,0x65,0x72,0x73,0x2e,0x77,0x29,0x3b,0x20,0x2f,
2091	0x2f,0x63,0x6f,0x6e,0x74,0x72,0x61,0x73,0x74,0x20,0x0d,0x0a,0x09,0x63,0x6f,0x6c,
2092	0x6f,0x72,0x20,0x3d,0x20,0x63,0x3b,0x0d,0x0a,0x7d,0x0d,0x0a };
2093
2094int tigr_upscale_gl_fs_size = (int)sizeof(tigr_upscale_gl_fs);
2095
2096
2097//////// End of inlined file: tigr_upscale_gl_fs.h ////////
2098
2099//////// Start of inlined file: tigr_win.c ////////
2100
2101//#include "tigr_internal.h"
2102#include <assert.h>
2103
2104// not really windows stuff
2105TigrInternal *tigrInternal(Tigr *bmp)
2106{
2107	assert(bmp->handle);
2108	return (TigrInternal *)(bmp + 1);
2109}
2110
2111#ifdef _WIN32
2112#define WIN32_LEAN_AND_MEAN
2113#include <windows.h>
2114#include <shellapi.h>
2115#include <stdio.h>
2116#include <stdlib.h>
2117
2118#define WIDGET_SCALE	3
2119#define WIDGET_FADE		16
2120
2121int main(int argc, char *argv[]);
2122
2123#ifndef TIGR_DO_NOT_PRESERVE_WINDOW_POSITION
2124HKEY tigrRegKey;
2125#endif
2126
2127static wchar_t *unicode(const char *str)
2128{
2129	int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, 0, 0);
2130	wchar_t *dest = (wchar_t *)malloc(sizeof(wchar_t) * len);
2131	MultiByteToWideChar(CP_UTF8, 0, str, -1, dest, len);
2132	return dest;
2133}
2134
2135void tigrError(Tigr *bmp, const char *message, ...)
2136{
2137	char tmp[1024];
2138
2139	va_list args;
2140	va_start(args, message);
2141	_vsnprintf(tmp, sizeof(tmp), message, args);
2142	tmp[sizeof(tmp)-1] = 0;
2143	va_end(args);
2144
2145	MessageBoxW(bmp ? (HWND)bmp->handle : NULL, unicode(tmp), bmp ? tigrInternal(bmp)->wtitle : L"Error", MB_OK|MB_ICONERROR);
2146	exit(1);
2147}
2148
2149void tigrEnterBorderlessWindowed(Tigr *bmp)
2150{
2151	// Enter borderless windowed mode.
2152	MONITORINFO mi = { sizeof(mi) };
2153	TigrInternal *win = tigrInternal(bmp);
2154
2155	GetWindowRect((HWND)bmp->handle, &win->oldPos);
2156
2157	GetMonitorInfo(MonitorFromWindow((HWND)bmp->handle, MONITOR_DEFAULTTONEAREST), &mi);
2158	win->dwStyle = WS_VISIBLE | WS_POPUP;
2159	SetWindowLong((HWND)bmp->handle, GWL_STYLE, win->dwStyle);
2160	SetWindowPos((HWND)bmp->handle, HWND_TOP,
2161		mi.rcMonitor.left,
2162		mi.rcMonitor.top,
2163		mi.rcMonitor.right - mi.rcMonitor.left,
2164		mi.rcMonitor.bottom - mi.rcMonitor.top,
2165		0);
2166}
2167
2168void tigrLeaveBorderlessWindowed(Tigr *bmp)
2169{
2170	TigrInternal *win = tigrInternal(bmp);
2171
2172	win->dwStyle = WS_VISIBLE | WS_OVERLAPPEDWINDOW;
2173	SetWindowLong((HWND)bmp->handle, GWL_STYLE, win->dwStyle);
2174
2175	SetWindowPos((HWND)bmp->handle, NULL,
2176		win->oldPos.left,
2177		win->oldPos.top,
2178		win->oldPos.right - win->oldPos.left,
2179		win->oldPos.bottom - win->oldPos.top,
2180		0);
2181}
2182
2183void tigrDxUpdateWidgets(Tigr *bmp, int dw, int dh)
2184{
2185	POINT pt;
2186	int i, x, clicked=0;
2187	char str[8];
2188	TPixel col;
2189	TPixel off = tigrRGB(255,255,255);
2190	TPixel on = tigrRGB(0,200,255);
2191	TigrInternal *win = tigrInternal(bmp);
2192	(void)dh;
2193
2194	tigrClear(win->widgets, tigrRGBA(0,0,0,0));
2195
2196	if (!(win->dwStyle & WS_POPUP))
2197	{
2198		win->widgetsWanted = 0;
2199		win->widgetAlpha = 0;
2200		return;
2201	}
2202
2203	// See if we want to be showing widgets or not.
2204	GetCursorPos(&pt);
2205	ScreenToClient((HWND)bmp->handle, &pt);
2206	if (pt.y == 0)
2207		win->widgetsWanted = 1;
2208	if (pt.y > win->widgets->h*WIDGET_SCALE)
2209		win->widgetsWanted = 0;
2210
2211	// Track the alpha.
2212	if (win->widgetsWanted)
2213		win->widgetAlpha = (win->widgetAlpha <= 255-WIDGET_FADE) ? win->widgetAlpha+WIDGET_FADE : 255;
2214	else
2215		win->widgetAlpha = (win->widgetAlpha >= WIDGET_FADE) ? win->widgetAlpha-WIDGET_FADE : 0;
2216
2217	// Get relative coords.
2218	pt.x -= (dw - win->widgets->w*WIDGET_SCALE);
2219	pt.x /= WIDGET_SCALE;
2220	pt.y /= WIDGET_SCALE;
2221
2222	tigrClear(win->widgets, tigrRGBA(0,0,0,win->widgetAlpha));
2223
2224	// Render it.
2225	for (i=0;i<3;i++)
2226	{
2227		switch(i) {
2228			case 0: str[0] = '_'; str[1] = 0; break; // "_" (minimize)
2229			case 1: str[0] = 0xEF; str[1] = 0xBF; str[2] = 0xBD; str[3] = 0; break; // "[]" (maximize)
2230			case 2: str[0] = 0xC3; str[1] = 0x97; str[2] = 0; break; // "x" (close)
2231		}
2232		x = win->widgets->w + (i-3)*12;
2233		if (i == 2)
2234			off = tigrRGB(255,0,0);
2235		if (pt.x >= x && pt.x < x+10 && pt.y < win->widgets->h)
2236		{
2237			col = on;
2238			if (GetAsyncKeyState(VK_LBUTTON) & 0x8000)
2239				clicked |= 1<<i;
2240		} else {
2241			col = off;
2242		}
2243		col.a = win->widgetAlpha;
2244		tigrPrint(win->widgets, tfont, x, 2, col, str);
2245	}
2246
2247	if (clicked & 1)
2248		ShowWindow((HWND)bmp->handle, SW_MINIMIZE);
2249	if (clicked & 2)
2250		tigrLeaveBorderlessWindowed(bmp);
2251	if (clicked & 4)
2252		SendMessage((HWND)bmp->handle, WM_CLOSE, 0, 0);
2253}
2254
2255void tigrUpdate(Tigr *bmp)
2256{
2257	MSG msg;
2258	RECT rc;
2259	int dw, dh;
2260	TigrInternal *win = tigrInternal(bmp);
2261
2262	if (!win->shown)
2263	{
2264		win->shown = 1;
2265		UpdateWindow((HWND)bmp->handle);
2266		ShowWindow((HWND)bmp->handle, SW_SHOW);
2267	}
2268
2269	// Get the window size.
2270	GetClientRect((HWND)bmp->handle, &rc);
2271	dw = rc.right - rc.left;
2272	dh = rc.bottom - rc.top;
2273
2274	// Update the widget overlay.
2275	tigrDxUpdateWidgets(bmp, dw, dh);
2276
2277	tigrGAPIPresent(bmp, dw, dh);
2278
2279	memcpy(win->prev, win->keys, 256);
2280
2281	// Run the message pump.
2282	while (PeekMessage(&msg, (HWND)bmp->handle, 0, 0, PM_REMOVE))
2283	{
2284		if (msg.message == WM_QUIT)
2285			break;
2286
2287		TranslateMessage(&msg);
2288		DispatchMessage(&msg);
2289	}
2290}
2291
2292static BOOL UnadjustWindowRectEx(LPRECT prc, DWORD dwStyle, BOOL fMenu, DWORD dwExStyle)
2293{
2294	BOOL fRc;
2295	RECT rc;
2296	SetRectEmpty(&rc);
2297	fRc = AdjustWindowRectEx(&rc, dwStyle, fMenu, dwExStyle);
2298	if (fRc) {
2299		prc->left -= rc.left;
2300		prc->top -= rc.top;
2301		prc->right -= rc.right;
2302		prc->bottom -= rc.bottom;
2303		}
2304	return fRc;
2305}
2306
2307LRESULT CALLBACK tigrWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2308{
2309	Tigr *bmp;
2310	TigrInternal *win = NULL;
2311	RECT rc;
2312	int dw, dh;
2313
2314	GetClientRect(hWnd, &rc);
2315	dw = rc.right - rc.left;
2316	dh = rc.bottom - rc.top;
2317
2318	bmp = (Tigr *)GetPropW(hWnd, L"Tigr");
2319	if (bmp)
2320		win = tigrInternal(bmp);
2321
2322	switch (message)
2323	{
2324	case WM_PAINT:
2325		tigrGAPIPresent(bmp, dw, dh);
2326		ValidateRect(hWnd, NULL);
2327		break;
2328	case WM_CLOSE:
2329		if (win)
2330			win->closed = 1;
2331		break;
2332	case WM_GETMINMAXINFO:
2333		if (bmp)
2334		{
2335			MINMAXINFO *info = (MINMAXINFO *)lParam;
2336			RECT rc;
2337			rc.left = 0;
2338			rc.top = 0;
2339			if (win->flags & TIGR_AUTO)
2340			{
2341				rc.right = 32;
2342				rc.bottom = 32;
2343			} else {
2344				int minscale = tigrEnforceScale(1, win->flags);
2345				rc.right = bmp->w * minscale;
2346				rc.bottom = bmp->h * minscale;
2347			}
2348			AdjustWindowRectEx(&rc, win->dwStyle, FALSE, 0);
2349			info->ptMinTrackSize.x = rc.right - rc.left;
2350			info->ptMinTrackSize.y = rc.bottom - rc.top;
2351		}
2352		return 0;
2353	case WM_SIZING:
2354		if (win)
2355		{
2356			// Calculate scale-constrained sizes.
2357			RECT *rc = (RECT *)lParam;
2358			int dx, dy;
2359			UnadjustWindowRectEx(rc, win->dwStyle, FALSE, 0);
2360			dx = (rc->right - rc->left) % win->scale;
2361			dy = (rc->bottom - rc->top) % win->scale;
2362			switch (wParam) {
2363			case WMSZ_LEFT: rc->left += dx; break;
2364			case WMSZ_RIGHT: rc->right -= dx; break;
2365			case WMSZ_TOP: rc->top += dy; break;
2366			case WMSZ_TOPLEFT: rc->left += dx; rc->top += dy; break;
2367			case WMSZ_TOPRIGHT: rc->right -= dx; rc->top += dy; break;
2368			case WMSZ_BOTTOM: rc->bottom -= dy; break;
2369			case WMSZ_BOTTOMLEFT: rc->left += dx; rc->bottom -= dy; break;
2370			case WMSZ_BOTTOMRIGHT: rc->right -= dx; rc->bottom -= dy; break;
2371			}
2372			AdjustWindowRectEx(rc, win->dwStyle, FALSE, 0);
2373		}
2374		return TRUE;
2375	case WM_SIZE:
2376		if (win)
2377		{
2378			if (wParam != SIZE_MINIMIZED)
2379			{
2380				// Detect window size changes and update our bitmap accordingly.
2381				dw = LOWORD(lParam);
2382				dh = HIWORD(lParam);
2383				if (win->flags & TIGR_AUTO)
2384				{
2385					tigrResize(bmp, dw/win->scale, dh/win->scale);
2386				} else {
2387					win->scale = tigrEnforceScale(tigrCalcScale(bmp->w, bmp->h, dw, dh), win->flags);
2388				}
2389				tigrPosition(bmp, win->scale, dw, dh, win->pos);
2390				tigrGAPIResize(bmp, dw, dh);
2391			}
2392
2393			// If someone tried to maximize us (e.g. via shortcut launch options),
2394			// prefer instead to be borderless.
2395			if (wParam == SIZE_MAXIMIZED)
2396			{
2397				ShowWindow((HWND)bmp->handle, SW_NORMAL);
2398				tigrEnterBorderlessWindowed(bmp);
2399			}
2400		}
2401		return 0;
2402	#ifndef TIGR_DO_NOT_PRESERVE_WINDOW_POSITION
2403	case WM_WINDOWPOSCHANGED:
2404		{
2405			// Save our position.
2406			WINDOWPLACEMENT wp = { sizeof(WINDOWPLACEMENT) };
2407			GetWindowPlacement(hWnd, &wp);
2408			if (win->dwStyle & WS_POPUP)
2409				wp.showCmd = SW_MAXIMIZE;
2410			RegSetValueExW(tigrRegKey, win->wtitle, 0, REG_BINARY, (BYTE *)&wp, sizeof(wp));
2411			return DefWindowProcW(hWnd, message, wParam, lParam);
2412		}
2413	#endif
2414	case WM_ACTIVATE:
2415		if (win) {
2416			memset(win->keys, 0, 256);
2417			memset(win->prev, 0, 256);
2418			win->lastChar = 0;
2419		}
2420		return 0;
2421	case WM_CHAR:
2422		if (win) {
2423			if (wParam == '\r') wParam = '\n';
2424				win->lastChar = wParam;
2425		}
2426		return DefWindowProcW(hWnd, message, wParam, lParam);
2427	case WM_MENUCHAR:
2428		// Disable beep on Alt+Enter
2429		if (LOWORD(wParam) == VK_RETURN)
2430			return MNC_CLOSE<<16;
2431		return DefWindowProcW(hWnd, message, wParam, lParam);
2432	case WM_SYSKEYDOWN:
2433		if (win)
2434		{
2435			if (wParam == VK_RETURN)
2436			{
2437				// Alt+Enter
2438				if (win->dwStyle & WS_POPUP)
2439					tigrLeaveBorderlessWindowed(bmp);
2440				else
2441					tigrEnterBorderlessWindowed(bmp);
2442				return 0;
2443			}
2444		}
2445		// fall-thru
2446	case WM_KEYDOWN:
2447		if (win)
2448			win->keys[wParam] = 1;
2449		return DefWindowProcW(hWnd, message, wParam, lParam);
2450	case WM_SYSKEYUP:
2451		// fall-thru
2452	case WM_KEYUP:
2453		if (win)
2454			win->keys[wParam] = 0;
2455		return DefWindowProcW(hWnd, message, wParam, lParam);
2456	default:
2457		return DefWindowProcW(hWnd, message, wParam, lParam);
2458	}
2459	return 0;
2460}
2461
2462Tigr *tigrWindow(int w, int h, const char *title, int flags)
2463{
2464	WNDCLASSEXW wcex = {0};
2465	int maxW, maxH, scale;
2466	HWND hWnd;
2467	DWORD dwStyle;
2468	RECT rc;
2469	DWORD err;
2470	Tigr *bmp;
2471	TigrInternal *win;
2472	#ifndef TIGR_DO_NOT_PRESERVE_WINDOW_POSITION
2473	WINDOWPLACEMENT wp;
2474	DWORD wpsize = sizeof(wp);
2475	#endif
2476	
2477	wchar_t *wtitle = unicode(title);
2478
2479	// Find our registry key.
2480	#ifndef TIGR_DO_NOT_PRESERVE_WINDOW_POSITION
2481	RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\TIGR", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &tigrRegKey, NULL);
2482	#endif
2483
2484	// Register a window class.
2485	wcex.cbSize			= sizeof(WNDCLASSEXW);
2486	wcex.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
2487	wcex.lpfnWndProc	= tigrWndProc;
2488	wcex.hInstance		= GetModuleHandle(NULL);
2489	wcex.hIcon			= NULL;
2490	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
2491	wcex.lpszClassName	= L"TIGR";
2492	RegisterClassExW(&wcex);
2493
2494	if (flags & TIGR_AUTO)
2495	{
2496		// Always use a 1:1 pixel size.
2497		scale = 1;
2498	} else {
2499		// See how big we can make it and still fit on-screen.
2500		maxW = GetSystemMetrics(SM_CXSCREEN) * 3/4;
2501		maxH = GetSystemMetrics(SM_CYSCREEN) * 3/4;
2502		scale = tigrCalcScale(w, h, maxW, maxH);
2503	}
2504
2505	scale = tigrEnforceScale(scale, flags);
2506
2507	// Get the final window size.
2508	dwStyle = WS_OVERLAPPEDWINDOW;
2509	rc.left = 0; rc.top = 0; rc.right = w*scale; rc.bottom = h*scale;
2510	AdjustWindowRect(&rc, dwStyle, FALSE);
2511
2512	// Make a window.
2513	hWnd = CreateWindowW(L"TIGR", wtitle, dwStyle,
2514		CW_USEDEFAULT, CW_USEDEFAULT, rc.right-rc.left, rc.bottom-rc.top,
2515		NULL, NULL, wcex.hInstance, NULL);
2516	err = GetLastError();
2517	if (!hWnd)
2518		ExitProcess(1);
2519
2520	// Wrap a bitmap around it.
2521	bmp = tigrBitmap2(w, h, sizeof(TigrInternal));
2522	bmp->handle = hWnd;
2523
2524	// Set up the Windows parts.
2525	win = tigrInternal(bmp);
2526	win->dwStyle = dwStyle;
2527	win->wtitle = wtitle;
2528	win->shown = 0;
2529	win->closed = 0;
2530	#ifdef TIGR_GAPI_D3D9
2531	win->d3d9.lost = 1;
2532	#endif
2533	win->scale = scale;
2534	win->lastChar = 0;
2535	win->flags = flags;
2536
2537	win->hblur = win->vblur = 0;
2538	win->scanlines = 0.0f;
2539	win->contrast = 1.0f;
2540
2541	win->widgetsWanted = 0;
2542	win->widgetAlpha = 0;
2543	win->widgetsScale = WIDGET_SCALE;
2544	win->widgets = tigrBitmap(40, 14);
2545
2546	SetPropW(hWnd, L"Tigr", bmp);
2547
2548	tigrGAPICreate(bmp);
2549	tigrGAPIBegin(bmp);
2550
2551	// Try and restore our window position.
2552	#ifndef TIGR_DO_NOT_PRESERVE_WINDOW_POSITION
2553	if (RegQueryValueExW(tigrRegKey, wtitle, NULL, NULL, (BYTE *)&wp, &wpsize) == ERROR_SUCCESS)
2554	{
2555		if (wp.showCmd == SW_MAXIMIZE)
2556			tigrEnterBorderlessWindowed(bmp);
2557		else
2558			SetWindowPlacement(hWnd, &wp);
2559	}
2560	#endif
2561
2562	return bmp;
2563}
2564
2565void tigrFree(Tigr *bmp)
2566{
2567	if (bmp->handle)
2568	{
2569		TigrInternal *win = tigrInternal(bmp);
2570		DestroyWindow((HWND)bmp->handle);
2571		tigrGAPIEnd(bmp);
2572		tigrGAPIDestroy(bmp);
2573		free(win->wtitle);
2574		tigrFree(win->widgets);
2575	}
2576	free(bmp->pix);
2577	free(bmp);
2578}
2579
2580int tigrClosed(Tigr *bmp)
2581{
2582	TigrInternal *win = tigrInternal(bmp);
2583	int val = win->closed;
2584	win->closed = 0;
2585	return val;
2586}
2587
2588float tigrTime()
2589{
2590	static int first = 1;
2591	static LARGE_INTEGER prev;
2592
2593	LARGE_INTEGER cnt, freq;
2594	ULONGLONG diff;
2595	QueryPerformanceCounter(&cnt);
2596	QueryPerformanceFrequency(&freq);
2597
2598	if (first)
2599	{
2600		first = 0;
2601		prev = cnt;
2602	}
2603
2604	diff = cnt.QuadPart - prev.QuadPart;
2605	prev = cnt;
2606	return (float)(diff / (double)freq.QuadPart);
2607}
2608
2609void tigrMouse(Tigr *bmp, int *x, int *y, int *buttons)
2610{
2611	POINT pt;
2612	TigrInternal *win;
2613
2614	win = tigrInternal(bmp);
2615	GetCursorPos(&pt);
2616	ScreenToClient((HWND)bmp->handle, &pt);
2617	*x = (pt.x - win->pos[0]) / win->scale;
2618	*y = (pt.y - win->pos[1]) / win->scale;
2619	*buttons = 0;
2620	if (GetFocus() != bmp->handle)
2621		return;
2622	if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) *buttons |= 1;
2623	if (GetAsyncKeyState(VK_MBUTTON) & 0x8000) *buttons |= 2;
2624	if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) *buttons |= 4;
2625}
2626
2627static int tigrWinVK(int key)
2628{
2629	if (key >= 'A' && key <= 'Z') return key;
2630	if (key >= '0' && key <= '9') return key;
2631	switch (key) {
2632	case TK_BACKSPACE: return VK_BACK;
2633	case TK_TAB: return VK_TAB;
2634	case TK_RETURN: return VK_RETURN;
2635	case TK_SHIFT: return VK_SHIFT;
2636	case TK_CONTROL: return VK_CONTROL;
2637	case TK_ALT: return VK_MENU;
2638	case TK_PAUSE: return VK_PAUSE;
2639	case TK_CAPSLOCK: return VK_CAPITAL;
2640	case TK_ESCAPE: return VK_ESCAPE;
2641	case TK_SPACE: return VK_SPACE;
2642	case TK_PAGEUP: return VK_PRIOR;
2643	case TK_PAGEDN: return VK_NEXT;
2644	case TK_END: return VK_END;
2645	case TK_HOME: return VK_HOME;
2646	case TK_LEFT: return VK_LEFT;
2647	case TK_UP: return VK_UP;
2648	case TK_RIGHT: return VK_RIGHT;
2649	case TK_DOWN: return VK_DOWN;
2650	case TK_INSERT: return VK_INSERT;
2651	case TK_DELETE: return VK_DELETE;
2652	case TK_LWIN: return VK_LWIN;
2653	case TK_RWIN: return VK_RWIN;
2654	//case TK_APPS: return VK_APPS; // this key doesn't exist on OS X
2655	case TK_PAD0: return VK_NUMPAD0;
2656	case TK_PAD1: return VK_NUMPAD1;
2657	case TK_PAD2: return VK_NUMPAD2;
2658	case TK_PAD3: return VK_NUMPAD3;
2659	case TK_PAD4: return VK_NUMPAD4;
2660	case TK_PAD5: return VK_NUMPAD5;
2661	case TK_PAD6: return VK_NUMPAD6;
2662	case TK_PAD7: return VK_NUMPAD7;
2663	case TK_PAD8: return VK_NUMPAD8;
2664	case TK_PAD9: return VK_NUMPAD9;
2665	case TK_PADMUL: return VK_MULTIPLY;
2666	case TK_PADADD: return VK_ADD;
2667	case TK_PADENTER: return VK_SEPARATOR;
2668	case TK_PADSUB: return VK_SUBTRACT;
2669	case TK_PADDOT: return VK_DECIMAL;
2670	case TK_PADDIV: return VK_DIVIDE;
2671	case TK_F1: return VK_F1;
2672	case TK_F2: return VK_F2;
2673	case TK_F3: return VK_F3;
2674	case TK_F4: return VK_F4;
2675	case TK_F5: return VK_F5;
2676	case TK_F6: return VK_F6;
2677	case TK_F7: return VK_F7;
2678	case TK_F8: return VK_F8;
2679	case TK_F9: return VK_F9;
2680	case TK_F10: return VK_F10;
2681	case TK_F11: return VK_F11;
2682	case TK_F12: return VK_F12;
2683	case TK_NUMLOCK: return VK_NUMLOCK;
2684	case TK_SCROLL: return VK_SCROLL;
2685	case TK_LSHIFT: return VK_LSHIFT;
2686	case TK_RSHIFT: return VK_RSHIFT;
2687	case TK_LCONTROL: return VK_LCONTROL;
2688	case TK_RCONTROL: return VK_RCONTROL;
2689	case TK_LALT: return VK_LMENU;
2690	case TK_RALT: return VK_RMENU;
2691	case TK_SEMICOLON: return VK_OEM_1;
2692	case TK_EQUALS: return VK_OEM_PLUS;
2693	case TK_COMMA: return VK_OEM_COMMA;
2694	case TK_MINUS: return VK_OEM_MINUS;
2695	case TK_DOT: return VK_OEM_PERIOD;
2696	case TK_SLASH: return VK_OEM_2;
2697	case TK_BACKTICK: return VK_OEM_3;
2698	case TK_LSQUARE: return VK_OEM_4;
2699	case TK_BACKSLASH: return VK_OEM_5;
2700	case TK_RSQUARE: return VK_OEM_6;
2701	case TK_TICK: return VK_OEM_7;
2702	}
2703	return 0;
2704}
2705
2706int tigrKeyDown(Tigr *bmp, int key)
2707{
2708	TigrInternal *win;
2709	int k = tigrWinVK(key);
2710	if (GetFocus() != bmp->handle)
2711		return 0;
2712	win = tigrInternal(bmp);
2713	return win->keys[k] && !win->prev[k];
2714}
2715
2716int tigrKeyHeld(Tigr *bmp, int key)
2717{
2718	TigrInternal *win;
2719	int k = tigrWinVK(key);
2720	if (GetFocus() != bmp->handle)
2721		return 0;
2722	win = tigrInternal(bmp);
2723	return win->keys[k];
2724}
2725
2726int tigrReadChar(Tigr *bmp)
2727{
2728	TigrInternal *win = tigrInternal(bmp);
2729	int c = win->lastChar;
2730	win->lastChar = 0;
2731	return c;
2732}
2733
2734// We supply our own WinMain and just chain through to the user's
2735// real entry point.
2736#ifdef UNICODE
2737int CALLBACK wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
2738#else
2739int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
2740#endif
2741{
2742	int n, argc;
2743	LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
2744	char **argv = (char **)calloc(argc+1, sizeof(int));
2745
2746	(void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
2747
2748	for (n=0;n<argc;n++)
2749	{
2750		int len = WideCharToMultiByte(CP_UTF8, 0, wargv[n], -1, 0, 0, NULL, NULL);
2751		argv[n] = (char *)malloc(len);
2752		WideCharToMultiByte(CP_UTF8, 0, wargv[n], -1, argv[n], len, NULL, NULL);
2753	}
2754	return main(argc, argv);
2755}
2756#endif
2757
2758//////// End of inlined file: tigr_win.c ////////
2759
2760//////// Start of inlined file: tigr_osx.c ////////
2761
2762// this one is based on https://github.com/jimon/osx_app_in_plain_c
2763
2764//#include "tigr_internal.h"
2765
2766#ifdef __APPLE__
2767#include <TargetConditionals.h>
2768#ifdef TARGET_OS_MAC
2769
2770#include <stdlib.h>
2771#include <stdbool.h>
2772#include <stdint.h>
2773#include <stdio.h>
2774#include <stdarg.h>
2775#include <limits.h>
2776#include <math.h>
2777#include <assert.h>
2778
2779#include <CoreFoundation/CoreFoundation.h>
2780#include <CoreGraphics/CoreGraphics.h>
2781#include <objc/objc.h>
2782#include <objc/runtime.h>
2783#include <objc/message.h>
2784#include <objc/NSObjCRuntime.h>
2785#include <mach/mach_time.h>
2786
2787// maybe this is available somewhere in objc runtime?
2788#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
2789#define NSIntegerEncoding "q"
2790#define NSUIntegerEncoding "L"
2791#else
2792#define NSIntegerEncoding "i"
2793#define NSUIntegerEncoding "I"
2794#endif
2795
2796#ifdef __OBJC__
2797#import <Cocoa/Cocoa.h>
2798#else
2799// this is how they are defined originally
2800#include <CoreGraphics/CGBase.h>
2801#include <CoreGraphics/CGGeometry.h>
2802typedef CGPoint NSPoint;
2803typedef CGSize NSSize;
2804typedef CGRect NSRect;
2805
2806extern id NSApp;
2807extern id const NSDefaultRunLoopMode;
2808#endif
2809
2810#if defined(__OBJC__) && __has_feature(objc_arc)
2811//#define ARC_AVAILABLE
2812#error "Can't compile as objective-c code just yet! (see autorelease pool todo below)"
2813#endif
2814
2815// ABI is a bit different between platforms
2816#ifdef __arm64__
2817#define abi_objc_msgSend_stret objc_msgSend
2818#else
2819#define abi_objc_msgSend_stret objc_msgSend_stret
2820#endif
2821#ifdef __i386__
2822#define abi_objc_msgSend_fpret objc_msgSend_fpret
2823#else
2824#define abi_objc_msgSend_fpret objc_msgSend
2825#endif
2826
2827#define objc_msgSend_id				((id (*)(id, SEL))objc_msgSend)
2828#define objc_msgSend_void			((void (*)(id, SEL))objc_msgSend)
2829#define objc_msgSend_void_id		((void (*)(id, SEL, id))objc_msgSend)
2830#define objc_msgSend_void_bool		((void (*)(id, SEL, BOOL))objc_msgSend)
2831#define objc_msgSend_id_const_char	((id (*)(id, SEL, const char*))objc_msgSend)
2832
2833bool terminated = false;
2834
2835// we gonna construct objective-c class by hand in runtime, so wow, so hacker!
2836NSUInteger applicationShouldTerminate(id self, SEL _sel, id sender)
2837{
2838	terminated = true;
2839	return 0;
2840}
2841
2842void windowWillClose(id self, SEL _sel, id notification)
2843{
2844	NSUInteger value = true;
2845	object_setInstanceVariable(self, "closed", (void*)value);
2846}
2847
2848void windowDidBecomeKey(id self, SEL _sel, id notification)
2849{
2850	TigrInternal * win;
2851	Tigr * bmp = 0;
2852	object_getInstanceVariable(self, "tigrHandle", (void**)&bmp);
2853	win = bmp ? tigrInternal(bmp) : NULL;
2854
2855	if(win)
2856	{
2857		memset(win->keys, 0, 256);
2858		memset(win->prev, 0, 256);
2859		win->lastChar = 0;
2860		win->mouseButtons = 0;
2861	}
2862}
2863
2864bool _tigrCocoaIsWindowClosed(id window)
2865{
2866	id wdg = objc_msgSend_id(window, sel_registerName("delegate"));
2867	if(!wdg)
2868		return false;
2869	NSUInteger value = 0;
2870	object_getInstanceVariable(wdg, "closed", (void**)&value);
2871	return value ? true : false;
2872}
2873
2874static bool tigrOSXInited = false;
2875static id autoreleasePool = NULL;
2876
2877void _tigrCleanupOSX()
2878{
2879	#ifdef ARC_AVAILABLE
2880	// TODO autorelease pool id:2 gh:3
2881	#else
2882	objc_msgSend_void(autoreleasePool, sel_registerName("drain"));
2883	#endif
2884}
2885
2886void tigrInitOSX()
2887{
2888	if(tigrOSXInited)
2889		return;
2890
2891	#ifdef ARC_AVAILABLE
2892	// TODO and what do we do now? it's a bit too tricky to use @autoreleasepool here id:5 gh:6
2893	#error TODO this code should be compiled as C for now
2894	#else
2895	//would be nice to use objc_autoreleasePoolPush instead, but it's not publically available in the headers
2896	id poolAlloc = objc_msgSend_id((id)objc_getClass("NSAutoreleasePool"), sel_registerName("alloc"));
2897	autoreleasePool = objc_msgSend_id(poolAlloc, sel_registerName("init"));
2898	atexit(&_tigrCleanupOSX);
2899	#endif
2900
2901	objc_msgSend_id((id)objc_getClass("NSApplication"), sel_registerName("sharedApplication"));
2902	((void (*)(id, SEL, NSInteger))objc_msgSend)(NSApp, sel_registerName("setActivationPolicy:"), 0);
2903
2904	Class appDelegateClass = objc_allocateClassPair((Class)objc_getClass("NSObject"), "AppDelegate", 0);
2905	bool resultAddProtoc = class_addProtocol(appDelegateClass, objc_getProtocol("NSApplicationDelegate"));
2906	assert(resultAddProtoc);
2907	bool resultAddMethod = class_addMethod(appDelegateClass, sel_registerName("applicationShouldTerminate:"), (IMP)applicationShouldTerminate, NSUIntegerEncoding "@:@");
2908	assert(resultAddMethod);
2909	id dgAlloc = objc_msgSend_id((id)appDelegateClass, sel_registerName("alloc"));
2910	id dg = objc_msgSend_id(dgAlloc, sel_registerName("init"));
2911	#ifndef ARC_AVAILABLE
2912	objc_msgSend_void(dg, sel_registerName("autorelease"));
2913	#endif
2914
2915	objc_msgSend_void_id(NSApp, sel_registerName("setDelegate:"), dg);
2916	objc_msgSend_void(NSApp, sel_registerName("finishLaunching"));
2917
2918	id menubarAlloc = objc_msgSend_id((id)objc_getClass("NSMenu"), sel_registerName("alloc"));
2919	id menubar = objc_msgSend_id(menubarAlloc, sel_registerName("init"));
2920	#ifndef ARC_AVAILABLE
2921	objc_msgSend_void(menubar, sel_registerName("autorelease"));
2922	#endif
2923
2924	id appMenuItemAlloc = objc_msgSend_id((id)objc_getClass("NSMenuItem"), sel_registerName("alloc"));
2925	id appMenuItem = objc_msgSend_id(appMenuItemAlloc, sel_registerName("init"));
2926	#ifndef ARC_AVAILABLE
2927	objc_msgSend_void(appMenuItem, sel_registerName("autorelease"));
2928	#endif
2929
2930	objc_msgSend_void_id(menubar, sel_registerName("addItem:"), appMenuItem);
2931	((id (*)(id, SEL, id))objc_msgSend)(NSApp, sel_registerName("setMainMenu:"), menubar);
2932
2933	id appMenuAlloc = objc_msgSend_id((id)objc_getClass("NSMenu"), sel_registerName("alloc"));
2934	id appMenu = objc_msgSend_id(appMenuAlloc, sel_registerName("init"));
2935	#ifndef ARC_AVAILABLE
2936	objc_msgSend_void(appMenu, sel_registerName("autorelease"));
2937	#endif
2938
2939	id processInfo = objc_msgSend_id((id)objc_getClass("NSProcessInfo"), sel_registerName("processInfo"));
2940	id appName = objc_msgSend_id(processInfo, sel_registerName("processName"));
2941
2942	id quitTitlePrefixString = objc_msgSend_id_const_char((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), "Quit ");
2943	id quitTitle = ((id (*)(id, SEL, id))objc_msgSend)(quitTitlePrefixString, sel_registerName("stringByAppendingString:"), appName);
2944
2945	id quitMenuItemKey = objc_msgSend_id_const_char((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), "q");
2946	id quitMenuItemAlloc = objc_msgSend_id((id)objc_getClass("NSMenuItem"), sel_registerName("alloc"));
2947	id quitMenuItem = ((id (*)(id, SEL, id, SEL, id))objc_msgSend)(quitMenuItemAlloc, sel_registerName("initWithTitle:action:keyEquivalent:"), quitTitle, sel_registerName("terminate:"), quitMenuItemKey);
2948	#ifndef ARC_AVAILABLE
2949	objc_msgSend_void(quitMenuItem, sel_registerName("autorelease"));
2950	#endif
2951
2952	objc_msgSend_void_id(appMenu, sel_registerName("addItem:"), quitMenuItem);
2953	objc_msgSend_void_id(appMenuItem, sel_registerName("setSubmenu:"), appMenu);
2954
2955	tigrOSXInited = true;
2956}
2957
2958void tigrError(Tigr *bmp, const char *message, ...)
2959{
2960	char tmp[1024];
2961
2962	va_list args;
2963	va_start(args, message);
2964	vsnprintf(tmp, sizeof(tmp), message, args);
2965	tmp[sizeof(tmp)-1] = 0;
2966	va_end(args);
2967
2968	CFStringRef header = CFStringCreateWithCString(NULL, "Error", kCFStringEncodingUTF8);
2969	CFStringRef msg = CFStringCreateWithCString(NULL, tmp, kCFStringEncodingUTF8);
2970	CFUserNotificationDisplayNotice(0.0, kCFUserNotificationStopAlertLevel, NULL, NULL, NULL, header, msg, NULL);
2971	CFRelease(header);
2972	CFRelease(msg);
2973	exit(1);
2974}
2975
2976NSSize _tigrCocoaWindowSize(id window)
2977{
2978	id contentView = objc_msgSend_id(window, sel_registerName("contentView"));
2979	NSRect rect = ((NSRect (*)(id, SEL))abi_objc_msgSend_stret)(contentView, sel_registerName("frame"));
2980	rect = ((NSRect (*)(id, SEL, NSRect))abi_objc_msgSend_stret)(contentView, sel_registerName("convertRectToBacking:"), rect);
2981
2982	return rect.size;
2983}
2984
2985TigrInternal * _tigrInternalCocoa(id window)
2986{
2987	if(!window)
2988		return NULL;
2989
2990	id wdg = objc_msgSend_id(window, sel_registerName("delegate"));
2991	if(!wdg)
2992		return NULL;
2993
2994	Tigr * bmp = 0;
2995	object_getInstanceVariable(wdg, "tigrHandle", (void**)&bmp);
2996	return bmp ? tigrInternal(bmp) : NULL;
2997}
2998
2999Tigr *tigrWindow(int w, int h, const char *title, int flags)
3000{
3001	int scale;
3002	Tigr *bmp;
3003	TigrInternal *win;
3004
3005	tigrInitOSX();
3006
3007	if (flags & TIGR_AUTO)
3008	{
3009		// Always use a 1:1 pixel size.
3010		scale = 1;
3011	} else {
3012		// See how big we can make it and still fit on-screen.
3013		CGRect mainMonitor = CGDisplayBounds(CGMainDisplayID());
3014		int maxW = CGRectGetHeight(mainMonitor) * 3/4;
3015		int maxH = CGRectGetWidth(mainMonitor) * 3/4;
3016		scale = tigrCalcScale(w, h, maxW, maxH);
3017	}
3018
3019	scale = tigrEnforceScale(scale, flags);
3020
3021	NSRect rect = {{0, 0}, {w * scale, h * scale}};
3022	id windowAlloc = objc_msgSend_id((id)objc_getClass("NSWindow"), sel_registerName("alloc"));
3023	id window = ((id (*)(id, SEL, NSRect, NSUInteger, NSUInteger, BOOL))objc_msgSend)(windowAlloc, sel_registerName("initWithContentRect:styleMask:backing:defer:"), rect, 15, 2, NO);
3024	#ifndef ARC_AVAILABLE
3025	objc_msgSend_void(window, sel_registerName("autorelease"));
3026	#endif
3027
3028	// when we are not using ARC, than window will be added to autorelease pool
3029	// so if we close it by hand (pressing red button), we don't want it to be released for us
3030	// so it will be released by autorelease pool later
3031	objc_msgSend_void_bool(window, sel_registerName("setReleasedWhenClosed:"), NO);
3032
3033	Class WindowDelegateClass = objc_allocateClassPair((Class)objc_getClass("NSObject"), "WindowDelegate", 0);
3034	bool resultAddProtoc = class_addProtocol(WindowDelegateClass, objc_getProtocol("NSWindowDelegate"));
3035	assert(resultAddProtoc);
3036	bool resultAddIvar = class_addIvar(WindowDelegateClass, "closed", sizeof(NSUInteger), rint(log2(sizeof(NSUInteger))), NSUIntegerEncoding);
3037	assert(resultAddIvar);
3038	resultAddIvar = class_addIvar(WindowDelegateClass, "tigrHandle", sizeof(void*), rint(log2(sizeof(void*))), "ˆv");
3039	assert(resultAddIvar);
3040	bool resultAddMethod = class_addMethod(WindowDelegateClass, sel_registerName("windowWillClose:"), (IMP)windowWillClose,  "v@:@");
3041	assert(resultAddMethod);
3042	resultAddMethod = class_addMethod(WindowDelegateClass, sel_registerName("windowDidBecomeKey:"), (IMP)windowDidBecomeKey,  "v@:@");
3043	assert(resultAddMethod);
3044	id wdgAlloc = objc_msgSend_id((id)WindowDelegateClass, sel_registerName("alloc"));
3045	id wdg = objc_msgSend_id(wdgAlloc, sel_registerName("init"));
3046	#ifndef ARC_AVAILABLE
3047	objc_msgSend_void(wdg, sel_registerName("autorelease"));
3048	#endif
3049
3050	objc_msgSend_void_id(window, sel_registerName("setDelegate:"), wdg);
3051
3052	id contentView = objc_msgSend_id(window, sel_registerName("contentView"));
3053
3054	if(flags & TIGR_RETINA)
3055		objc_msgSend_void_bool(contentView, sel_registerName("setWantsBestResolutionOpenGLSurface:"), YES);
3056
3057	NSPoint point = {20, 20};
3058	((void (*)(id, SEL, NSPoint))objc_msgSend)(window, sel_registerName("cascadeTopLeftFromPoint:"), point);
3059
3060	id titleString = objc_msgSend_id_const_char((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), title);
3061	objc_msgSend_void_id(window, sel_registerName("setTitle:"), titleString);
3062
3063	uint32_t glAttributes[] =
3064	{
3065		8, 24,			//	NSOpenGLPFAColorSize, 24,
3066		11, 8,			//	NSOpenGLPFAAlphaSize, 8,
3067		5,				//	NSOpenGLPFADoubleBuffer,
3068		73,				//	NSOpenGLPFAAccelerated,
3069		//72,			//	NSOpenGLPFANoRecovery,
3070		//55, 1,		//	NSOpenGLPFASampleBuffers, 1,
3071		//56, 4,		//	NSOpenGLPFASamples, 4,
3072		99, 0x3200,		//	NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
3073		0
3074	};
3075
3076	id pixelFormatAlloc = objc_msgSend_id((id)objc_getClass("NSOpenGLPixelFormat"), sel_registerName("alloc"));
3077	id pixelFormat = ((id (*)(id, SEL, const uint32_t*))objc_msgSend)(pixelFormatAlloc, sel_registerName("initWithAttributes:"), glAttributes);
3078	#ifndef ARC_AVAILABLE
3079	objc_msgSend_void(pixelFormat, sel_registerName("autorelease"));
3080	#endif
3081
3082	id openGLContextAlloc = objc_msgSend_id((id)objc_getClass("NSOpenGLContext"), sel_registerName("alloc"));
3083	id openGLContext = ((id (*)(id, SEL, id, id))objc_msgSend)(openGLContextAlloc, sel_registerName("initWithFormat:shareContext:"), pixelFormat, nil);
3084	#ifndef ARC_AVAILABLE
3085	objc_msgSend_void(openGLContext, sel_registerName("autorelease"));
3086	#endif
3087
3088	objc_msgSend_void_id(openGLContext, sel_registerName("setView:"), contentView);
3089	objc_msgSend_void_id(window, sel_registerName("makeKeyAndOrderFront:"), window);
3090	objc_msgSend_void_bool(window, sel_registerName("setAcceptsMouseMovedEvents:"), YES);
3091
3092	id blackColor = objc_msgSend_id((id)objc_getClass("NSColor"), sel_registerName("blackColor"));
3093	objc_msgSend_void_id(window, sel_registerName("setBackgroundColor:"), blackColor);
3094
3095	// TODO do we really need this? id:6 gh:7
3096	objc_msgSend_void_bool(NSApp, sel_registerName("activateIgnoringOtherApps:"), YES);
3097
3098	// Wrap a bitmap around it.
3099	NSSize windowSize = _tigrCocoaWindowSize(window);
3100	bmp = tigrBitmap2(windowSize.width / scale, windowSize.height / scale, sizeof(TigrInternal));
3101	bmp->handle = window;
3102
3103	// Set the handle
3104	object_setInstanceVariable(wdg, "tigrHandle", (void*)bmp);
3105
3106	// Set up the Windows parts.
3107	win = tigrInternal(bmp);
3108	win->glContext = openGLContext;
3109	win->shown = 0;
3110	win->closed = 0;
3111	win->scale = scale;
3112	win->lastChar = 0;
3113	win->flags = flags;
3114	win->hblur = win->vblur = 0;
3115	win->scanlines = 0.0f;
3116	win->contrast = 1.0f;
3117	win->widgetsWanted = 0;
3118	win->widgetAlpha = 0;
3119	win->widgetsScale = 0;
3120	win->widgets = tigrBitmap(40, 14);
3121	win->gl.gl_legacy = 0;
3122	win->mouseButtons = 0;
3123
3124	tigrPosition(bmp, win->scale, bmp->w, bmp->h, win->pos);
3125
3126	objc_msgSend_void(openGLContext, sel_registerName("makeCurrentContext"));
3127	tigrGAPICreate(bmp);
3128	tigrGAPIBegin(bmp);
3129	tigrGAPIResize(bmp, bmp->w, bmp->h);
3130
3131	return bmp;
3132}
3133
3134void tigrFree(Tigr *bmp)
3135{
3136	if(bmp->handle)
3137	{
3138		TigrInternal * win = tigrInternal(bmp);
3139		objc_msgSend_void((id)win->glContext, sel_registerName("makeCurrentContext"));
3140		tigrGAPIEnd(bmp);
3141		tigrGAPIDestroy(bmp);
3142		tigrFree(win->widgets);
3143
3144		id window = (id)bmp->handle;
3145		if(!_tigrCocoaIsWindowClosed(window) && !terminated)
3146			objc_msgSend_void(window, sel_registerName("close"));
3147	}
3148	free(bmp->pix);
3149	free(bmp);
3150}
3151
3152uint8_t _tigrKeyFromOSX(uint16_t key)
3153{
3154	// from Carbon HIToolbox/Events.h
3155	enum
3156	{
3157		kVK_ANSI_A                    = 0x00,
3158		kVK_ANSI_S                    = 0x01,
3159		kVK_ANSI_D                    = 0x02,
3160		kVK_ANSI_F                    = 0x03,
3161		kVK_ANSI_H                    = 0x04,
3162		kVK_ANSI_G                    = 0x05,
3163		kVK_ANSI_Z                    = 0x06,
3164		kVK_ANSI_X                    = 0x07,
3165		kVK_ANSI_C                    = 0x08,
3166		kVK_ANSI_V                    = 0x09,
3167		kVK_ANSI_B                    = 0x0B,
3168		kVK_ANSI_Q                    = 0x0C,
3169		kVK_ANSI_W                    = 0x0D,
3170		kVK_ANSI_E                    = 0x0E,
3171		kVK_ANSI_R                    = 0x0F,
3172		kVK_ANSI_Y                    = 0x10,
3173		kVK_ANSI_T                    = 0x11,
3174		kVK_ANSI_1                    = 0x12,
3175		kVK_ANSI_2                    = 0x13,
3176		kVK_ANSI_3                    = 0x14,
3177		kVK_ANSI_4                    = 0x15,
3178		kVK_ANSI_6                    = 0x16,
3179		kVK_ANSI_5                    = 0x17,
3180		kVK_ANSI_Equal                = 0x18,
3181		kVK_ANSI_9                    = 0x19,
3182		kVK_ANSI_7                    = 0x1A,
3183		kVK_ANSI_Minus                = 0x1B,
3184		kVK_ANSI_8                    = 0x1C,
3185		kVK_ANSI_0                    = 0x1D,
3186		kVK_ANSI_RightBracket         = 0x1E,
3187		kVK_ANSI_O                    = 0x1F,
3188		kVK_ANSI_U                    = 0x20,
3189		kVK_ANSI_LeftBracket          = 0x21,
3190		kVK_ANSI_I                    = 0x22,
3191		kVK_ANSI_P                    = 0x23,
3192		kVK_ANSI_L                    = 0x25,
3193		kVK_ANSI_J                    = 0x26,
3194		kVK_ANSI_Quote                = 0x27,
3195		kVK_ANSI_K                    = 0x28,
3196		kVK_ANSI_Semicolon            = 0x29,
3197		kVK_ANSI_Backslash            = 0x2A,
3198		kVK_ANSI_Comma                = 0x2B,
3199		kVK_ANSI_Slash                = 0x2C,
3200		kVK_ANSI_N                    = 0x2D,
3201		kVK_ANSI_M                    = 0x2E,
3202		kVK_ANSI_Period               = 0x2F,
3203		kVK_ANSI_Grave                = 0x32,
3204		kVK_ANSI_KeypadDecimal        = 0x41,
3205		kVK_ANSI_KeypadMultiply       = 0x43,
3206		kVK_ANSI_KeypadPlus           = 0x45,
3207		kVK_ANSI_KeypadClear          = 0x47,
3208		kVK_ANSI_KeypadDivide         = 0x4B,
3209		kVK_ANSI_KeypadEnter          = 0x4C,
3210		kVK_ANSI_KeypadMinus          = 0x4E,
3211		kVK_ANSI_KeypadEquals         = 0x51,
3212		kVK_ANSI_Keypad0              = 0x52,
3213		kVK_ANSI_Keypad1              = 0x53,
3214		kVK_ANSI_Keypad2              = 0x54,
3215		kVK_ANSI_Keypad3              = 0x55,
3216		kVK_ANSI_Keypad4              = 0x56,
3217		kVK_ANSI_Keypad5              = 0x57,
3218		kVK_ANSI_Keypad6              = 0x58,
3219		kVK_ANSI_Keypad7              = 0x59,
3220		kVK_ANSI_Keypad8              = 0x5B,
3221		kVK_ANSI_Keypad9              = 0x5C,
3222		kVK_Return                    = 0x24,
3223		kVK_Tab                       = 0x30,
3224		kVK_Space                     = 0x31,
3225		kVK_Delete                    = 0x33,
3226		kVK_Escape                    = 0x35,
3227		kVK_Command                   = 0x37,
3228		kVK_Shift                     = 0x38,
3229		kVK_CapsLock                  = 0x39,
3230		kVK_Option                    = 0x3A,
3231		kVK_Control                   = 0x3B,
3232		kVK_RightShift                = 0x3C,
3233		kVK_RightOption               = 0x3D,
3234		kVK_RightControl              = 0x3E,
3235		kVK_Function                  = 0x3F,
3236		kVK_F17                       = 0x40,
3237		kVK_VolumeUp                  = 0x48,
3238		kVK_VolumeDown                = 0x49,
3239		kVK_Mute                      = 0x4A,
3240		kVK_F18                       = 0x4F,
3241		kVK_F19                       = 0x50,
3242		kVK_F20                       = 0x5A,
3243		kVK_F5                        = 0x60,
3244		kVK_F6                        = 0x61,
3245		kVK_F7                        = 0x62,
3246		kVK_F3                        = 0x63,
3247		kVK_F8                        = 0x64,
3248		kVK_F9                        = 0x65,
3249		kVK_F11                       = 0x67,
3250		kVK_F13                       = 0x69,
3251		kVK_F16                       = 0x6A,
3252		kVK_F14                       = 0x6B,
3253		kVK_F10                       = 0x6D,
3254		kVK_F12                       = 0x6F,
3255		kVK_F15                       = 0x71,
3256		kVK_Help                      = 0x72,
3257		kVK_Home                      = 0x73,
3258		kVK_PageUp                    = 0x74,
3259		kVK_ForwardDelete             = 0x75,
3260		kVK_F4                        = 0x76,
3261		kVK_End                       = 0x77,
3262		kVK_F2                        = 0x78,
3263		kVK_PageDown                  = 0x79,
3264		kVK_F1                        = 0x7A,
3265		kVK_LeftArrow                 = 0x7B,
3266		kVK_RightArrow                = 0x7C,
3267		kVK_DownArrow                 = 0x7D,
3268		kVK_UpArrow                   = 0x7E
3269	};
3270
3271	switch(key)
3272	{
3273	case kVK_ANSI_Q: return 'Q';
3274	case kVK_ANSI_W: return 'W';
3275	case kVK_ANSI_E: return 'E';
3276	case kVK_ANSI_R: return 'R';
3277	case kVK_ANSI_T: return 'T';
3278	case kVK_ANSI_Y: return 'Y';
3279	case kVK_ANSI_U: return 'U';
3280	case kVK_ANSI_I: return 'I';
3281	case kVK_ANSI_O: return 'O';
3282	case kVK_ANSI_P: return 'P';
3283	case kVK_ANSI_A: return 'A';
3284	case kVK_ANSI_S: return 'S';
3285	case kVK_ANSI_D: return 'D';
3286	case kVK_ANSI_F: return 'F';
3287	case kVK_ANSI_G: return 'G';
3288	case kVK_ANSI_H: return 'H';
3289	case kVK_ANSI_J: return 'J';
3290	case kVK_ANSI_K: return 'K';
3291	case kVK_ANSI_L: return 'L';
3292	case kVK_ANSI_Z: return 'Z';
3293	case kVK_ANSI_X: return 'X';
3294	case kVK_ANSI_C: return 'C';
3295	case kVK_ANSI_V: return 'V';
3296	case kVK_ANSI_B: return 'B';
3297	case kVK_ANSI_N: return 'N';
3298	case kVK_ANSI_M: return 'M';
3299	case kVK_ANSI_0: return '0';
3300	case kVK_ANSI_1: return '1';
3301	case kVK_ANSI_2: return '2';
3302	case kVK_ANSI_3: return '3';
3303	case kVK_ANSI_4: return '4';
3304	case kVK_ANSI_5: return '5';
3305	case kVK_ANSI_6: return '6';
3306	case kVK_ANSI_7: return '7';
3307	case kVK_ANSI_8: return '8';
3308	case kVK_ANSI_9: return '9';
3309	case kVK_ANSI_Keypad0: return TK_PAD0;
3310	case kVK_ANSI_Keypad1: return TK_PAD1;
3311	case kVK_ANSI_Keypad2: return TK_PAD2;
3312	case kVK_ANSI_Keypad3: return TK_PAD3;
3313	case kVK_ANSI_Keypad4: return TK_PAD4;
3314	case kVK_ANSI_Keypad5: return TK_PAD5;
3315	case kVK_ANSI_Keypad6: return TK_PAD6;
3316	case kVK_ANSI_Keypad7: return TK_PAD7;
3317	case kVK_ANSI_Keypad8: return TK_PAD8;
3318	case kVK_ANSI_Keypad9: return TK_PAD9;
3319	case kVK_ANSI_KeypadMultiply: return TK_PADMUL;
3320	case kVK_ANSI_KeypadPlus: return TK_PADADD;
3321	case kVK_ANSI_KeypadEnter: return TK_PADENTER;
3322	case kVK_ANSI_KeypadMinus: return TK_PADSUB;
3323	case kVK_ANSI_KeypadDecimal: return TK_PADDOT;
3324	case kVK_ANSI_KeypadDivide: return TK_PADDIV;
3325	case kVK_F1: return TK_F1;
3326	case kVK_F2: return TK_F2;
3327	case kVK_F3: return TK_F3;
3328	case kVK_F4: return TK_F4;
3329	case kVK_F5: return TK_F5;
3330	case kVK_F6: return TK_F6;
3331	case kVK_F7: return TK_F7;
3332	case kVK_F8: return TK_F8;
3333	case kVK_F9: return TK_F9;
3334	case kVK_F10: return TK_F10;
3335	case kVK_F11: return TK_F11;
3336	case kVK_F12: return TK_F12;
3337	case kVK_Shift: return TK_LSHIFT;
3338	case kVK_Control: return TK_LCONTROL;
3339	case kVK_Option: return TK_LALT;
3340	case kVK_CapsLock: return TK_CAPSLOCK;
3341	case kVK_Command: return TK_LWIN;
3342	case kVK_Command - 1: return TK_RWIN;
3343	case kVK_RightShift: return TK_RSHIFT;
3344	case kVK_RightControl: return TK_RCONTROL;
3345	case kVK_RightOption: return TK_RALT;
3346	case kVK_Delete: return TK_BACKSPACE;
3347	case kVK_Tab: return TK_TAB;
3348	case kVK_Return: return TK_RETURN;
3349	case kVK_Escape: return TK_ESCAPE;
3350	case kVK_Space: return TK_SPACE;
3351	case kVK_PageUp: return TK_PAGEUP;
3352	case kVK_PageDown: return TK_PAGEDN;
3353	case kVK_End: return TK_END;
3354	case kVK_Home: return TK_HOME;
3355	case kVK_LeftArrow: return TK_LEFT;
3356	case kVK_UpArrow: return TK_UP;
3357	case kVK_RightArrow: return TK_RIGHT;
3358	case kVK_DownArrow: return TK_DOWN;
3359	case kVK_Help: return TK_INSERT;
3360	case kVK_ForwardDelete: return TK_DELETE;
3361	case kVK_F14: return TK_SCROLL;
3362	case kVK_F15: return TK_PAUSE;
3363	case kVK_ANSI_KeypadClear: return TK_NUMLOCK;
3364	case kVK_ANSI_Semicolon: return TK_SEMICOLON;
3365	case kVK_ANSI_Equal: return TK_EQUALS;
3366	case kVK_ANSI_Comma: return TK_COMMA;
3367	case kVK_ANSI_Minus: return TK_MINUS;
3368	case kVK_ANSI_Slash: return TK_SLASH;
3369	case kVK_ANSI_Backslash: return TK_BACKSLASH;
3370	case kVK_ANSI_Grave: return TK_BACKTICK;
3371	case kVK_ANSI_Quote: return TK_TICK;
3372	case kVK_ANSI_LeftBracket: return TK_LSQUARE;
3373	case kVK_ANSI_RightBracket: return TK_RSQUARE;
3374	case kVK_ANSI_Period: return TK_DOT;
3375	default: return 0;
3376	}
3377}
3378
3379void _tigrOnCocoaEvent(id event, id window)
3380{
3381	if(!event)
3382		return;
3383
3384	TigrInternal * win = _tigrInternalCocoa(window);
3385	if(!win) // just pipe the event
3386	{
3387		objc_msgSend_void_id(NSApp, sel_registerName("sendEvent:"), event);
3388		return;
3389	}
3390
3391	NSUInteger eventType = ((NSUInteger (*)(id, SEL))objc_msgSend)(event, sel_registerName("type"));
3392	switch(eventType)
3393	{
3394	case 1: // NSLeftMouseDown
3395		win->mouseButtons |= 1;
3396		break;
3397	case 2: // NSLeftMouseUp
3398		win->mouseButtons &= ~1;
3399		break;
3400	case 3: // NSRightMouseDown
3401		win->mouseButtons |= 2;
3402		break;
3403	case 4: // NSRightMouseUp
3404		win->mouseButtons &= ~2;
3405		break;
3406	case 25: // NSOtherMouseDown
3407	{
3408		// number == 2 is a middle button
3409		NSInteger number = ((NSInteger (*)(id, SEL))objc_msgSend)(event, sel_registerName("buttonNumber"));
3410		if(number == 2)
3411			win->mouseButtons |= 4;
3412		break;
3413	}
3414	case 26: // NSOtherMouseUp
3415	{
3416		NSInteger number = ((NSInteger (*)(id, SEL))objc_msgSend)(event, sel_registerName("buttonNumber"));
3417		if(number == 2)
3418			win->mouseButtons &= ~4;
3419		break;
3420	}
3421	//case 22: // NSScrollWheel
3422	//{
3423	//	CGFloat deltaX = ((CGFloat (*)(id, SEL))abi_objc_msgSend_fpret)(event, sel_registerName("scrollingDeltaX"));
3424	//	CGFloat deltaY = ((CGFloat (*)(id, SEL))abi_objc_msgSend_fpret)(event, sel_registerName("scrollingDeltaY"));
3425	//	BOOL precisionScrolling = ((BOOL (*)(id, SEL))objc_msgSend)(event, sel_registerName("hasPreciseScrollingDeltas"));
3426	//
3427	//	if(precisionScrolling)
3428	//	{
3429	//		deltaX *= 0.1f; // similar to glfw
3430	//		deltaY *= 0.1f;
3431	//	}
3432	//
3433	//	if(fabs(deltaX) > 0.0f || fabs(deltaY) > 0.0f)
3434	//		printf("mouse scroll wheel delta %f %f\n", deltaX, deltaY);
3435	//	break;
3436	//}
3437	case 12: // NSFlagsChanged
3438	{
3439		NSUInteger modifiers = ((NSUInteger (*)(id, SEL))objc_msgSend)(event, sel_registerName("modifierFlags"));
3440
3441		// based on NSEventModifierFlags and NSDeviceIndependentModifierFlagsMask
3442		struct
3443		{
3444			union
3445			{
3446				struct
3447				{
3448					uint8_t alpha_shift:1;
3449					uint8_t shift:1;
3450					uint8_t control:1;
3451					uint8_t alternate:1;
3452					uint8_t command:1;
3453					uint8_t numeric_pad:1;
3454					uint8_t help:1;
3455					uint8_t function:1;
3456				};
3457				uint8_t mask;
3458			};
3459		} keys;
3460
3461		keys.mask = (modifiers & 0xffff0000UL) >> 16;
3462
3463		// TODO L,R variation of keys? id:1 gh:2
3464		win->keys[TK_CONTROL] = keys.alpha_shift;
3465		win->keys[TK_SHIFT] = keys.shift;
3466		win->keys[TK_CONTROL] = keys.control;
3467		win->keys[TK_ALT] = keys.alternate;
3468		win->keys[TK_LWIN] = keys.command;
3469		win->keys[TK_RWIN] = keys.command;
3470		break;
3471	}
3472	case 10: // NSKeyDown
3473	{
3474		id inputText = objc_msgSend_id(event, sel_registerName("characters"));
3475		const char * inputTextUTF8 = ((const char* (*)(id, SEL))objc_msgSend)(inputText, sel_registerName("UTF8String"));
3476
3477		tigrDecodeUTF8(inputTextUTF8, &win->lastChar);
3478
3479		uint16_t keyCode = ((unsigned short (*)(id, SEL))objc_msgSend)(event, sel_registerName("keyCode"));
3480		win->keys[_tigrKeyFromOSX(keyCode)] = 1;
3481		break;
3482	}
3483	case 11: // NSKeyUp
3484	{
3485		uint16_t keyCode = ((unsigned short (*)(id, SEL))objc_msgSend)(event, sel_registerName("keyCode"));
3486		win->keys[_tigrKeyFromOSX(keyCode)] = 0;
3487		break;
3488	}
3489	default:
3490		break;
3491	}
3492
3493	objc_msgSend_void_id(NSApp, sel_registerName("sendEvent:"), event);
3494}
3495
3496void tigrUpdate(Tigr *bmp)
3497{
3498	TigrInternal *win;
3499	id openGLContext;
3500	id window;
3501	win = tigrInternal(bmp);
3502	window = (id)bmp->handle;
3503	openGLContext = (id)win->glContext;
3504
3505	id keyWindow = objc_msgSend_id(NSApp, sel_registerName("keyWindow"));
3506
3507	if(keyWindow == window)
3508		memcpy(win->prev, win->keys, 256);
3509
3510	id distantPast = objc_msgSend_id((id)objc_getClass("NSDate"), sel_registerName("distantPast"));
3511	id event = ((id (*)(id, SEL, NSUInteger, id, id, BOOL))objc_msgSend)(NSApp, sel_registerName("nextEventMatchingMask:untilDate:inMode:dequeue:"), NSUIntegerMax, distantPast, NSDefaultRunLoopMode, YES);
3512	_tigrOnCocoaEvent(event, keyWindow);
3513	if(terminated || _tigrCocoaIsWindowClosed(window))
3514		return;
3515
3516	// do runloop stuff
3517	objc_msgSend_void(NSApp, sel_registerName("updateWindows"));
3518	objc_msgSend_void(openGLContext, sel_registerName("update"));
3519	objc_msgSend_void(openGLContext, sel_registerName("makeCurrentContext"));
3520
3521	NSSize windowSize = _tigrCocoaWindowSize(window);
3522
3523	if (win->flags & TIGR_AUTO)
3524		tigrResize(bmp, windowSize.width / win->scale, windowSize.height / win->scale);
3525	else
3526		win->scale = tigrEnforceScale(tigrCalcScale(bmp->w, bmp->h, windowSize.width, windowSize.height), win->flags);
3527
3528	tigrPosition(bmp, win->scale, windowSize.width, windowSize.height, win->pos);
3529	tigrGAPIResize(bmp, windowSize.width, windowSize.height);
3530	tigrGAPIPresent(bmp, windowSize.width, windowSize.height);
3531	objc_msgSend_void(openGLContext, sel_registerName("flushBuffer"));
3532}
3533
3534int tigrClosed(Tigr *bmp)
3535{
3536	return (terminated || _tigrCocoaIsWindowClosed((id)bmp->handle)) ? 1 : 0;
3537}
3538
3539void tigrMouse(Tigr *bmp, int *x, int *y, int *buttons)
3540{
3541	TigrInternal *win;
3542	id window;
3543	win = tigrInternal(bmp);
3544	window = (id)bmp->handle;
3545
3546	id windowContentView = objc_msgSend_id(window, sel_registerName("contentView"));
3547	NSRect adjustFrame = ((NSRect (*)(id, SEL))abi_objc_msgSend_stret)(windowContentView, sel_registerName("frame"));
3548
3549	// NSPoint is small enough to fit a register, so no need for objc_msgSend_stret
3550	NSPoint p = ((NSPoint (*)(id, SEL))objc_msgSend)(window, sel_registerName("mouseLocationOutsideOfEventStream"));
3551
3552	// map input to content view rect
3553	if(p.x < 0) p.x = 0;
3554	else if(p.x > adjustFrame.size.width) p.x = adjustFrame.size.width;
3555	if(p.y < 0) p.y = 0;
3556	else if(p.y > adjustFrame.size.height) p.y = adjustFrame.size.height;
3557
3558	// map input to pixels
3559	NSRect r = {p.x, p.y, 0, 0};
3560	r = ((NSRect (*)(id, SEL, NSRect))abi_objc_msgSend_stret)(windowContentView, sel_registerName("convertRectToBacking:"), r);
3561	p = r.origin;
3562
3563	p.x = (p.x - win->pos[0]) / win->scale;
3564	p.y = (adjustFrame.size.height - p.y - win->pos[1]) / win->scale;
3565
3566	if(x)
3567		*x = p.x;
3568	if(y)
3569		*y = p.y;
3570
3571	if(buttons)
3572	{
3573		id keyWindow = objc_msgSend_id(NSApp, sel_registerName("keyWindow"));
3574		*buttons = keyWindow != bmp->handle ? 0 : win->mouseButtons;
3575	}
3576}
3577
3578int tigrKeyDown(Tigr *bmp, int key)
3579{
3580	TigrInternal *win;
3581	assert(key < 256);
3582	id keyWindow = objc_msgSend_id(NSApp, sel_registerName("keyWindow"));
3583	if(keyWindow != bmp->handle)
3584		return 0;
3585	win = tigrInternal(bmp);
3586	return win->keys[key] && !win->prev[key];
3587}
3588
3589int tigrKeyHeld(Tigr *bmp, int key)
3590{
3591	TigrInternal *win;
3592	assert(key < 256);
3593	id keyWindow = objc_msgSend_id(NSApp, sel_registerName("keyWindow"));
3594	if(keyWindow != bmp->handle)
3595		return 0;
3596	win = tigrInternal(bmp);
3597	return win->keys[key];
3598}
3599
3600int tigrReadChar(Tigr *bmp)
3601{
3602	TigrInternal *win = tigrInternal(bmp);
3603	int c = win->lastChar;
3604	win->lastChar = 0;
3605	return c;
3606}
3607
3608float tigrTime()
3609{
3610	static uint64_t time = 0;
3611	static mach_timebase_info_data_t timebaseInfo;
3612
3613	if(timebaseInfo.denom == 0)
3614	{
3615		mach_timebase_info(&timebaseInfo);
3616		time = mach_absolute_time();
3617		return 0.0f;
3618	}
3619
3620	uint64_t current_time = mach_absolute_time();
3621	double elapsed = (double)(current_time - time) * timebaseInfo.numer / (timebaseInfo.denom * 1000000000.0);
3622	time = current_time;
3623	return (float)elapsed;
3624}
3625
3626#endif
3627#endif
3628
3629//////// End of inlined file: tigr_osx.c ////////
3630
3631//////// Start of inlined file: tigr_gl.c ////////
3632
3633//#include "tigr_internal.h"
3634#include <stdio.h> // TODO can we remove this and printf's later? id:4 gh:5
3635
3636#ifdef TIGR_GAPI_GL
3637#ifndef __APPLE__
3638// please provide you own glext.h, you can download latest at https://www.opengl.org/registry/api/GL/glext.h
3639#include <glext.h>
3640#endif
3641#ifdef _WIN32
3642// please provide you own wglext.h, you can download latest at https://www.opengl.org/registry/api/GL/wglext.h
3643#include <wglext.h>
3644#endif
3645
3646extern const unsigned char tigr_upscale_gl_vs[], tigr_upscale_gl_fs[];
3647extern int tigr_upscale_gl_vs_size, tigr_upscale_gl_fs_size;
3648
3649#ifdef _WIN32
3650PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormat;
3651PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs;
3652PFNGLGENVERTEXARRAYSPROC glGenVertexArrays;
3653PFNGLGENBUFFERSARBPROC glGenBuffers;
3654PFNGLBINDBUFFERPROC glBindBuffer;
3655PFNGLBUFFERDATAPROC glBufferData;
3656PFNGLBINDVERTEXARRAYPROC glBindVertexArray;
3657PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
3658PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
3659PFNGLCREATESHADERPROC glCreateShader;
3660PFNGLSHADERSOURCEPROC glShaderSource;
3661PFNGLCOMPILESHADERPROC glCompileShader;
3662PFNGLCREATEPROGRAMPROC glCreateProgram;
3663PFNGLATTACHSHADERPROC glAttachShader;
3664PFNGLLINKPROGRAMPROC glLinkProgram;
3665PFNGLDELETESHADERPROC glDeleteShader;
3666PFNGLDELETEPROGRAMPROC glDeleteProgram;
3667PFNGLGETSHADERIVPROC glGetShaderiv;
3668PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
3669PFNGLGETPROGRAMIVPROC glGetProgramiv;
3670PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
3671PFNGLUSEPROGRAMPROC glUseProgram;
3672PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
3673PFNGLUNIFORM4FPROC glUniform4f;
3674PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv;
3675PFNGLACTIVETEXTUREPROC glActiveTexture;
3676int tigrGL11Init(Tigr *bmp)
3677{
3678	int pixel_format;
3679	TigrInternal *win = tigrInternal(bmp);
3680	GLStuff *gl= &win->gl;
3681	PIXELFORMATDESCRIPTOR pfd =
3682	{
3683		sizeof(PIXELFORMATDESCRIPTOR),
3684		1,
3685		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_SWAP_EXCHANGE,
3686		PFD_TYPE_RGBA,
3687		32, // color bits
3688		0, 0, 0, 0, 0, 0, 0, 0,
3689		0, 0, 0, 0, 0,
3690		24, // depth
3691		8,  // stencil
3692		0,
3693		PFD_MAIN_PLANE, // is it ignored ?
3694		0,
3695		0, 0, 0
3696	};
3697	if(!(gl->dc = GetDC((HWND)bmp->handle))) {tigrError(bmp, "Cannot create OpenGL device context.\n"); return -1;}
3698	if(!(pixel_format = ChoosePixelFormat(gl->dc, &pfd))) {tigrError(bmp, "Cannot choose OpenGL pixel format.\n"); return -1;}
3699	if(!SetPixelFormat(gl->dc, pixel_format, &pfd)) {tigrError(bmp, "Cannot set OpenGL pixel format.\n"); return -1;}
3700	if(!(gl->hglrc = wglCreateContext(gl->dc))) {tigrError(bmp, "Cannot create OpenGL context.\n"); return -1;}
3701	if(!wglMakeCurrent(gl->dc, gl->hglrc)) {tigrError(bmp, "Cannot activate OpenGL context.\n"); return -1;}
3702	gl->gl_legacy = 1;
3703	return 0;
3704}
3705int tigrGL33Init(Tigr *bmp)
3706{
3707	int pixel_format;
3708	UINT num_formats;
3709	TigrInternal *win = tigrInternal(bmp);
3710	GLStuff *gl= &win->gl;
3711
3712	wglChoosePixelFormat = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
3713	wglCreateContextAttribs = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
3714	glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)wglGetProcAddress("glGenVertexArrays");
3715	glGenBuffers = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffers");
3716	glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
3717	glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
3718	glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)wglGetProcAddress("glBindVertexArray");
3719	glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glEnableVertexAttribArray");
3720	glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)wglGetProcAddress("glVertexAttribPointer");
3721	glCreateShader = (PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader");
3722	glShaderSource = (PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource");
3723	glCompileShader = (PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader");
3724	glCreateProgram = (PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram");
3725	glAttachShader = (PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader");
3726	glLinkProgram = (PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram");
3727	glDeleteShader = (PFNGLDELETESHADERPROC)wglGetProcAddress("glDeleteShader");
3728	glDeleteProgram = (PFNGLDELETEPROGRAMPROC)wglGetProcAddress("glDeleteProgram");
3729	glGetShaderiv = (PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv");
3730	glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog");
3731	glGetProgramiv = (PFNGLGETPROGRAMIVPROC)wglGetProcAddress("glGetProgramiv");
3732	glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)wglGetProcAddress("glGetProgramInfoLog");
3733	glUseProgram = (PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram");
3734	glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation");
3735	glUniform4f = (PFNGLUNIFORM4FPROC)wglGetProcAddress("glUniform4f");
3736	glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)wglGetProcAddress("glUniformMatrix4fv");
3737	glActiveTexture = (PFNGLACTIVETEXTUREPROC)wglGetProcAddress("glActiveTexture");
3738
3739	if(!wglChoosePixelFormat || !wglCreateContextAttribs) {tigrError(bmp, "Cannot create OpenGL context.\n"); return -1;}
3740	const int attribList[] =
3741	{
3742		WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
3743		WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
3744		WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
3745		WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
3746		WGL_COLOR_BITS_ARB, 32,
3747		WGL_DEPTH_BITS_ARB, 24,
3748		WGL_STENCIL_BITS_ARB, 8,
3749		0
3750	};
3751	int attribs[] = {
3752		WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
3753		WGL_CONTEXT_MINOR_VERSION_ARB, 3,
3754		0
3755	};
3756	if(!wglChoosePixelFormat(gl->dc, attribList, NULL, 1, &pixel_format, &num_formats)) {tigrError(bmp, "Cannot choose OpenGL pixel format.\n"); return -1;}
3757	if(!(gl->hglrc = wglCreateContextAttribs(gl->dc, gl->hglrc, attribs))) {tigrError(bmp, "Cannot create OpenGL context attribs.\n"); return -1;}
3758	if(!wglMakeCurrent(gl->dc, gl->hglrc)) {tigrError(bmp, "Cannot activate OpenGL context.\n"); return -1;}
3759	gl->gl_legacy = 0;
3760	return 0;
3761}
3762#endif
3763
3764void tigrCheckGLError(const char *state)
3765{
3766	GLenum err = glGetError();
3767	if(err != GL_NO_ERROR)
3768		printf("got gl error %x when was doing %s\n", err, state);
3769}
3770
3771void tigrCheckShaderErrors(GLuint object)
3772{
3773	GLint success;
3774	GLchar info[2048];
3775	glGetShaderiv(object, GL_COMPILE_STATUS, &success);
3776	if(!success)
3777	{
3778		glGetShaderInfoLog(object, sizeof(info), NULL, info);
3779		printf("shader compile error : %s\n", info);
3780	}
3781}
3782
3783void tigrCheckProgramErrors(GLuint object)
3784{
3785	GLint success;
3786	GLchar info[2048];
3787	glGetProgramiv(object, GL_LINK_STATUS, &success);
3788	if(!success)
3789	{
3790		glGetProgramInfoLog(object, sizeof(info), NULL, info);
3791		printf("shader link error : %s\n", info);
3792	}
3793}
3794
3795void tigrGAPICreate(Tigr *bmp)
3796{
3797	GLuint vs, fs;
3798	TigrInternal *win = tigrInternal(bmp);
3799	GLStuff *gl= &win->gl;
3800	GLuint VBO;
3801	GLfloat vertices[] = {
3802		// pos      uv
3803		0.0f, 1.0f, 0.0f, 1.0f,
3804		1.0f, 0.0f, 1.0f, 0.0f,
3805		0.0f, 0.0f, 0.0f, 0.0f,
3806		0.0f, 1.0f, 0.0f, 1.0f,
3807		1.0f, 1.0f, 1.0f, 1.0f,
3808		1.0f, 0.0f, 1.0f, 0.0f
3809	};
3810
3811	#ifdef _WIN32
3812	if(tigrGL11Init(bmp))
3813		return;
3814	tigrGL33Init(bmp);
3815	#endif
3816
3817	//printf("ogl version %s\n", glGetString(GL_VERSION));
3818
3819	if(!gl->gl_legacy)
3820	{
3821		// create vao
3822		glGenVertexArrays(1, &gl->vao);
3823		glGenBuffers(1, &VBO);
3824		glBindBuffer(GL_ARRAY_BUFFER, VBO);
3825		glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
3826		glBindVertexArray(gl->vao);
3827		glEnableVertexAttribArray(0);
3828		glEnableVertexAttribArray(1);
3829		glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), NULL);
3830		glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), NULL);
3831
3832		// create program
3833		vs = glCreateShader(GL_VERTEX_SHADER);
3834		const char *vs_source = (const char*)&tigr_upscale_gl_vs;
3835		glShaderSource(vs, 1, &vs_source, &tigr_upscale_gl_vs_size);
3836		glCompileShader(vs);
3837		tigrCheckShaderErrors(vs);
3838		fs = glCreateShader(GL_FRAGMENT_SHADER);
3839		const char *fs_source = (const char*)&tigr_upscale_gl_fs;
3840		glShaderSource(fs, 1, &fs_source, &tigr_upscale_gl_fs_size);
3841		glCompileShader(fs);
3842		tigrCheckShaderErrors(fs);
3843		gl->program = glCreateProgram();
3844		glAttachShader(gl->program, vs);
3845		glAttachShader(gl->program, fs);
3846		glLinkProgram(gl->program);
3847		tigrCheckProgramErrors(gl->program);
3848		glDeleteShader(vs);
3849		glDeleteShader(fs);
3850		gl->uniform_projection = glGetUniformLocation(gl->program, "projection");
3851		gl->uniform_model = glGetUniformLocation(gl->program, "model");
3852		gl->uniform_parameters = glGetUniformLocation(gl->program, "parameters");
3853	}
3854
3855	// create textures
3856	if(gl->gl_legacy)
3857		glEnable(GL_TEXTURE_2D);
3858	glGenTextures(2, gl->tex);
3859	for(int i = 0; i < 2; ++i) {
3860		glBindTexture(GL_TEXTURE_2D, gl->tex[i]);
3861		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl->gl_legacy ? GL_NEAREST : GL_LINEAR);
3862		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl->gl_legacy ? GL_NEAREST : GL_LINEAR);
3863		glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3864		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3865	}
3866
3867	tigrCheckGLError("initialization");
3868}
3869
3870void tigrGAPIBegin(Tigr *bmp)
3871{
3872}
3873
3874void tigrGAPIEnd(Tigr *bmp)
3875{
3876}
3877
3878void tigrGAPIDestroy(Tigr *bmp)
3879{
3880	TigrInternal *win = tigrInternal(bmp);
3881	GLStuff *gl= &win->gl;
3882
3883	if(!gl->gl_legacy)
3884	{
3885		glDeleteTextures(2, gl->tex);
3886		glDeleteProgram(gl->program);
3887	}
3888
3889	tigrCheckGLError("destroy");
3890
3891	#ifdef _WIN32
3892	if(!wglMakeCurrent(NULL, NULL)) {tigrError(bmp, "Cannot deactivate OpenGL context.\n"); return;}
3893	if(gl->hglrc && !wglDeleteContext(gl->hglrc)) {tigrError(bmp, "Cannot delete OpenGL context.\n"); return;}
3894	gl->hglrc = NULL;
3895
3896	if(gl->dc && !ReleaseDC((HWND)bmp->handle, gl->dc)) {tigrError(bmp, "Cannot release OpenGL device context.\n"); return;}
3897	gl->dc = NULL;
3898	#endif
3899}
3900
3901void tigrGAPIResize(Tigr *bmp, int width, int height)
3902{
3903	// no-op
3904	(void)bmp;
3905	(void)width;
3906	(void)height;
3907}
3908
3909void tigrGAPIDraw(int legacy, GLuint uniform_model, GLuint tex, Tigr *bmp, int x1, int y1, int x2, int y2)
3910{
3911	glBindTexture(GL_TEXTURE_2D, tex);
3912	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, bmp->w, bmp->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, bmp->pix);
3913
3914	if(!legacy)
3915	{
3916		float sx = (float)(x2 - x1);
3917		float sy = (float)(y2 - y1);
3918		float tx = (float)x1;
3919		float ty = (float)y1;
3920
3921		float model[16] =
3922		{
3923			  sx, 0.0f, 0.0f, 0.0f,
3924			0.0f,   sy, 0.0f, 0.0f,
3925			0.0f, 0.0f, 1.0f, 0.0f,
3926			  tx,   ty, 0.0f, 1.0f
3927		};
3928
3929		glUniformMatrix4fv(uniform_model, 1, GL_FALSE, model);
3930		glDrawArrays(GL_TRIANGLES, 0, 6);
3931	}
3932	else
3933	{
3934		#ifndef __APPLE__
3935		glBegin(GL_QUADS);
3936		glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
3937		glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
3938		glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
3939		glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
3940		glEnd();
3941		#else
3942		assert(0);
3943		#endif
3944	}
3945}
3946
3947void tigrGAPIPresent(Tigr *bmp, int w, int h)
3948{
3949	TigrInternal *win = tigrInternal(bmp);
3950	GLStuff *gl= &win->gl;
3951
3952#ifdef _WIN32
3953	wglMakeCurrent(gl->dc, gl->hglrc);
3954#endif
3955	glViewport(0, 0, w, h);
3956	glClearColor(0, 0, 0, 1);
3957	glClear(GL_COLOR_BUFFER_BIT);
3958
3959	if(!gl->gl_legacy)
3960	{
3961		float projection[16] =
3962		{
3963			 2.0f / w,  0.0f    , 0.0f, 0.0f,
3964			 0.0f    , -2.0f / h, 0.0f, 0.0f,
3965			 0.0f    ,  0.0f    , 1.0f, 0.0f,
3966			-1.0f    ,  1.0f    , 0.0f, 1.0f
3967		};
3968
3969		glActiveTexture(GL_TEXTURE0);
3970		glBindVertexArray(gl->vao);
3971		glUseProgram(gl->program);
3972		glUniformMatrix4fv(gl->uniform_projection, 1, GL_FALSE, projection);
3973		glUniform4f(gl->uniform_parameters, win->hblur ? 1.0f : 0.0f, win->vblur ? 1.0f : 0.0f, win->scanlines, win->contrast);
3974	}
3975	else
3976	{
3977		#ifndef __APPLE__
3978		glMatrixMode(GL_PROJECTION);
3979		glLoadIdentity();
3980		glOrtho(0, w, h, 0, -1.0f, 1.0f);
3981		glEnable(GL_TEXTURE_2D);
3982		#else
3983		assert(0);
3984		#endif
3985	}
3986
3987	glDisable(GL_BLEND);
3988	tigrGAPIDraw(gl->gl_legacy, gl->uniform_model, gl->tex[0], bmp, win->pos[0], win->pos[1], win->pos[2], win->pos[3]);
3989
3990	if(win->widgetsScale > 0)
3991	{
3992		glEnable(GL_BLEND);
3993		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3994		tigrGAPIDraw(gl->gl_legacy, gl->uniform_model, gl->tex[1], win->widgets, 
3995			(int)(w - win->widgets->w * win->widgetsScale), 0, 
3996			w, (int)(win->widgets->h * win->widgetsScale));
3997	}
3998
3999	tigrCheckGLError("present");
4000
4001	#ifdef _WIN32
4002	if(!SwapBuffers(gl->dc)) {tigrError(bmp, "Cannot swap OpenGL buffers.\n"); return;}
4003	#endif
4004}
4005
4006#endif
4007
4008//////// End of inlined file: tigr_gl.c ////////
4009
4010
4011//////// End of inlined file: tigr_amalgamated.c ////////
4012