File: mod_sdl.c

package info (click to toggle)
spl 1.0~pre6-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,352 kB
  • ctags: 2,070
  • sloc: ansic: 16,614; yacc: 3,182; sh: 299; makefile: 165; xml: 156
file content (1087 lines) | stat: -rw-r--r-- 26,588 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  mod_sdl.c: The SPL SDL module
 */

/**
 * A module for doing multimedia using the SDL library.
 *
 * Note: Only one SDL window can be opened by one host program at a time.
 * This is a limitation from SDL.
 */

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#include <stdlib.h>

#ifdef ENABLE_PTHREAD_SUPPORT
#include <pthread.h>
#endif

#include "spl.h"
#include "compat.h"

extern void SPL_ABI(spl_mod_sdl_init)(struct spl_vm *vm, struct spl_module *mod, int restore);
extern void SPL_ABI(spl_mod_sdl_done)(struct spl_vm *vm, struct spl_module *mod);

#ifdef ENABLE_PTHREAD_SUPPORT
static pthread_mutex_t spl_sdl_init_lock = PTHREAD_MUTEX_INITIALIZER;
#endif

static int spl_sdl_initialized = 0;
static SDL_Surface *spl_sdl_root = 0;
static Uint32 spl_sdl_last_ticks = 0;
static int spl_sdl_last_ticks_init = 0;

static char *undupable_reason = "VM is active SDL user";

/*** SDL HNODE ***/

struct sdl_hnode_data {
	SDL_Surface *image;
	int revision_counter;
	int reference_counter;
	struct sdl_hnode_data *left, *right;
};

static struct sdl_hnode_data *spl_sdl_hnlist = 0;

static struct sdl_hnode_data *clib_get_hnd(struct spl_task *task, struct spl_node *n)
{
	struct sdl_hnode_data *hnd = n ? n->hnode_data : 0;

	if (!n || (!n->hnode_name && !n->value && !n->subs_counter &&
		   !n->code && !n->ctx && !n->cls)) {
		return 0;
	}

	if (!n->hnode_name || strcmp(n->hnode_name, "sdl") || !hnd || !hnd->image) {
		spl_clib_exception(task, "SdlEx", "description",
				SPL_NEW_PRINTF("Expected SDL image (surface) node"),
				NULL);
		return 0;
	}

	return hnd;
}

static SDL_Surface *clib_get_surface(struct spl_task *task, int isdest)
{
	struct spl_node *n = spl_cleanup(task, spl_clib_get_node(task));
	struct sdl_hnode_data *hnd = n ? n->hnode_data : 0;

	if (!n || (!n->hnode_name && !n->value && !n->subs_counter &&
		   !n->code && !n->ctx && !n->cls)) {
		return spl_sdl_root;
	}

	if (!n->hnode_name || strcmp(n->hnode_name, "sdl") || !hnd || !hnd->image) {
		spl_clib_exception(task, "SdlEx", "description",
				SPL_NEW_PRINTF("Expected SDL image (surface) node"),
				NULL);
		return 0;
	}

	if (isdest)
		hnd->revision_counter++;

	return hnd->image;
}

static struct spl_node *sdl_hnode_new()
{
	struct spl_node *n = SPL_NEW_STRING_DUP("SDL Node");
	struct sdl_hnode_data *hnd = calloc(1, sizeof(struct sdl_hnode_data));

	n->hnode_name = strdup("sdl");
	n->hnode_data = hnd;

	if (spl_sdl_hnlist) {
		hnd->right = spl_sdl_hnlist;
		hnd->right->left = hnd;
	}
	spl_sdl_hnlist = hnd;
	hnd->reference_counter = 1;

	return n;
}

static void sdl_hnode_data_free(struct sdl_hnode_data *hnd)
{
	if (hnd->image) {
		SDL_FreeSurface(hnd->image);
		hnd->image = 0;
	}
}

static void handler_sdl_node(struct spl_task *task UNUSED, struct spl_vm *vm UNUSED,
		struct spl_node *node, struct spl_hnode_args *args, void *data UNUSED)
{
	if (args->action == SPL_HNODE_ACTION_PUT)
	{
		struct sdl_hnode_data *hnd = node->hnode_data;
		if (hnd && --hnd->reference_counter == 0) {
			sdl_hnode_data_free(hnd);
			*(hnd->left ? &hnd->left->right : &spl_sdl_hnlist) = hnd->right;
			if (hnd->right) hnd->right->left = hnd->left;
			free(hnd);
		}
		return;
	}

	if (args->action == SPL_HNODE_ACTION_LOOKUP)
	{
		struct sdl_hnode_data *hnd = node->hnode_data;
		char *mode = spl_hash_decode(args->key);

		if (hnd && hnd->image && !strcmp(mode, "w"))
			args->value = SPL_NEW_INT(hnd->image->w);

		if (hnd && hnd->image && !strcmp(mode, "h"))
			args->value = SPL_NEW_INT(hnd->image->h);

		free(mode);
	}

	return;
}


/*** SDL Functions ***/

/**
 * Initialize SDL and open a window with the specified size.
 *
 * The window is opened in fullscreen when the named parameter 'fullscreen'
 * is passed and has a 'true' (non-zero) value.
 *
 * The window is double-buffered when the named parameter 'doublebf'
 * is passed and has a 'true' (non-zero) value.
 */
// builtin sdl_init(width, height)
static struct spl_node *handler_sdl_init(struct spl_task *task, void *data UNUSED)
{
	struct spl_node *hargs = spl_cleanup(task, spl_clib_get_hargs(task));
	struct spl_node *hnode;

	int width = spl_clib_get_int(task);
	int height = spl_clib_get_int(task);

#ifdef ENABLE_PTHREAD_SUPPORT
	pthread_mutex_lock(&spl_sdl_init_lock);
#endif

	if (spl_sdl_initialized || task->vm->sdl_initialized) {
		spl_clib_exception(task, "SdlEx", "description",
				SPL_NEW_PRINTF("SDL already in use"),
				NULL);
		goto fin;
	}

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		spl_clib_exception(task, "SdlEx", "description",
				SPL_NEW_PRINTF("Unable to init SDL: %s", SDL_GetError()),
				NULL);
		goto fin;
	}

	int videoflags = SDL_ANYFORMAT | SDL_HWSURFACE;

	hnode = spl_lookup(task, hargs, "fullscreen", SPL_LOOKUP_TEST);
	if (hnode && spl_get_int(hnode)) videoflags |= SDL_FULLSCREEN;

	hnode = spl_lookup(task, hargs, "doublebuf", SPL_LOOKUP_TEST);
	if (hnode && spl_get_int(hnode)) videoflags |= SDL_DOUBLEBUF;

	spl_sdl_root = SDL_SetVideoMode(width, height, 32, videoflags);
	if (!spl_sdl_root) {
		spl_clib_exception(task, "SdlEx", "description",
				SPL_NEW_PRINTF("Unable to init SDL: %s", SDL_GetError()),
				NULL);
		SDL_Quit();
		goto fin;
	}

	SDL_ShowCursor(0);

	spl_sdl_last_ticks = 0;
	spl_sdl_last_ticks_init = 1;

	spl_sdl_initialized = 1;
	task->vm->sdl_initialized = 1;
	spl_undumpable_inc(task->vm, undupable_reason);

fin:
#ifdef ENABLE_PTHREAD_SUPPORT
	pthread_mutex_unlock(&spl_sdl_init_lock);
#endif
	return 0;
}

#define CHECK_INIT							\
	if (!spl_sdl_initialized || !task->vm->sdl_initialized) {	\
		spl_clib_exception(task, "SdlEx", "description",	\
				SPL_NEW_PRINTF("SDL not initialized"),	\
				NULL);					\
		return 0;						\
	}

/**
 * Terminate the SDL context and close the window.
 */
// builtin sdl_quit()
static struct spl_node *handler_sdl_quit(struct spl_task *task, void *data UNUSED)
{
	// task is a NULL pointer when we
	// are called from SPL_ABI(spl_mod_sdl_done)()
	if (task) {
		CHECK_INIT
	}

	for (struct sdl_hnode_data *i = spl_sdl_hnlist; i; i = i->right)
		sdl_hnode_data_free(i);

	SDL_Quit();
	spl_sdl_root = 0;
	spl_sdl_last_ticks = 0;
	spl_sdl_initialized = 0;

	if (task) {
		task->vm->sdl_initialized = 0;
		spl_undumpable_inc(task->vm, undupable_reason);
	}

	return 0;
}

/**
 * Set the window and icon name.
 */
// builtin sdl_title(title, icon)
static struct spl_node *handler_sdl_title(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	const char *title = spl_clib_get_string(task);
	const char *icon = spl_clib_get_string(task);
	SDL_WM_SetCaption(title, icon);
	return 0;
}

/**
 * Delay program execution for the specified number of milliseconds.
 *
 * Note that the given number of ms is relative to the end of the last call
 * to [[sdl_delay()]]. The return code is the effective number of ms the
 * program execution has been delayed.
 *
 * A zero or negative return code means that the function did not sleep and
 * you are likely to have a timing/performance problem in your application.
 */
// builtin sdl_delay(ms)
static struct spl_node *handler_sdl_delay(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	int delay = spl_clib_get_int(task);
	int ticks = SDL_GetTicks();

	if (spl_sdl_last_ticks_init) {
		spl_sdl_last_ticks = ticks;
		spl_sdl_last_ticks_init = 0;
	}

	int returncode = (spl_sdl_last_ticks + delay) - ticks;

	if (returncode > 0)
		SDL_Delay(returncode);

	spl_sdl_last_ticks = SDL_GetTicks();
	return SPL_NEW_INT(returncode);
}

/**
 * Update the window (switch backend framebuffers if the window is created
 * in doublebuffering mode).
 */
// builtin sdl_flip()
static struct spl_node *handler_sdl_flip(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT
	SDL_Flip(spl_sdl_root);
	return 0;
}

/**
 * Update the specified area of the window.
 */
// builtin sdl_update(x, y, w, h)
static struct spl_node *handler_sdl_update(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	int x = spl_clib_get_int(task);
	int y = spl_clib_get_int(task);
	int w = spl_clib_get_int(task);
	int h = spl_clib_get_int(task);

	SDL_UpdateRect(spl_sdl_root, x, y, w, h);
	return 0;
}

/**
 * Load the image from the specified filename and return the image handler.
 */
// builtin sdl_image_load(filename)
static struct spl_node *handler_sdl_image_load(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	const char *filename = spl_clib_get_string(task);

	struct spl_node *n = sdl_hnode_new();
	struct sdl_hnode_data *hnd = n->hnode_data;

	SDL_Surface *imgbuffer = IMG_Load(filename);
	if (!imgbuffer) {
		spl_clib_exception(task, "SdlEx", "description",
				SPL_NEW_PRINTF("Can't load image '%s': %s",
					filename, IMG_GetError()),
				NULL);
		spl_put(task->vm, n);
		return 0;
	}

	hnd->image = SDL_DisplayFormatAlpha(imgbuffer);
	SDL_FreeSurface(imgbuffer);

	return n;
}

/**
 * Create an empty image with the specified size.
 */
// builtin sdl_image_create(w, h)
static struct spl_node *handler_sdl_image_create(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	int w = spl_clib_get_int(task);
	int h = spl_clib_get_int(task);

	struct spl_node *n = sdl_hnode_new();
	struct sdl_hnode_data *hnd = n->hnode_data;

	Uint32 rmask, gmask, bmask, amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	rmask = 0xff000000;
	gmask = 0x00ff0000;
	bmask = 0x0000ff00;
	amask = 0x000000ff;
#else
	rmask = 0x000000ff;
	gmask = 0x0000ff00;
	bmask = 0x00ff0000;
	amask = 0xff000000;
#endif

	// It seems like there is no API for allocating a
	// surface in DisplayFormatAlpha format directly
	SDL_Surface *imgbuffer = SDL_CreateRGBSurface(0, w, h, 32,
			spl_sdl_root->format->Rmask,
			spl_sdl_root->format->Gmask,
			spl_sdl_root->format->Bmask,
			spl_sdl_root->format->Amask);

	hnd->image = SDL_DisplayFormatAlpha(imgbuffer);
	SDL_FreeSurface(imgbuffer);

	return n;
}

/**
 * Blit the source image to the specified coordinates in the destination
 * image. If 'undef' is passed as destination image, the image is blitted to
 * the window and will be displayed after the next call to [[sdl_flip()]].
 */
// builtin sdl_blit(dstimg, srcimg, x, y)
static struct spl_node *handler_sdl_blit(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	SDL_Surface *dst = clib_get_surface(task, 1);
	SDL_Surface *src = clib_get_surface(task, 0);

	if (dst && src) {
		SDL_Rect srcrect;
		SDL_Rect dstrect;

		srcrect.x = 0;
		srcrect.y = 0;
		srcrect.w = src->w;
		srcrect.h = src->h;

		dstrect.x = spl_clib_get_int(task);
		dstrect.y = spl_clib_get_int(task);
		dstrect.w = srcrect.w;
		dstrect.h = srcrect.h;

		SDL_BlitSurface(src, &srcrect, dst, &dstrect);
	}

	return 0;
}

/**
 * Like [[sdl_blit()]], but only blit the specified rectangle.
 */
// builtin sdl_blitrect(dstimg, srcimg, dest_x, dest_y, src_x, src_y, w, h)
static struct spl_node *handler_sdl_blitrect(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	SDL_Surface *dst = clib_get_surface(task, 1);
	SDL_Surface *src = clib_get_surface(task, 0);

	if (dst && src) {
		SDL_Rect srcrect;
		SDL_Rect dstrect;

		dstrect.x = spl_clib_get_int(task);
		dstrect.y = spl_clib_get_int(task);
		srcrect.x = spl_clib_get_int(task);
		srcrect.y = spl_clib_get_int(task);
		dstrect.w = srcrect.w = spl_clib_get_int(task);
		dstrect.h = srcrect.h = spl_clib_get_int(task);

		SDL_BlitSurface(src, &srcrect, dst, &dstrect);
	}

	return 0;
}

/**
 * Copy the the specified rectangle to a new image. The new image is returned.
 */
// builtin sdl_copy(srcimg, x, y, w, h)
static struct spl_node *handler_sdl_copy(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	SDL_Surface *src = clib_get_surface(task, 0);
	int x = spl_clib_get_int(task);
	int y = spl_clib_get_int(task);
	int w = spl_clib_get_int(task);
	int h = spl_clib_get_int(task);

	if (src) {
		struct spl_node *n = sdl_hnode_new();
		struct sdl_hnode_data *hnd = n->hnode_data;

		Uint32 rmask, gmask, bmask, amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
		rmask = 0xff000000;
		gmask = 0x00ff0000;
		bmask = 0x0000ff00;
		amask = 0x000000ff;
#else
		rmask = 0x000000ff;
		gmask = 0x0000ff00;
		bmask = 0x00ff0000;
		amask = 0xff000000;
#endif

		// It seems like there is no API for allocating a
		// surface in DisplayFormatAlpha format directly
		SDL_Surface *imgbuffer = SDL_CreateRGBSurface(0, w, h, 32,
				spl_sdl_root->format->Rmask,
				spl_sdl_root->format->Gmask,
				spl_sdl_root->format->Bmask,
				spl_sdl_root->format->Amask);

		hnd->image = SDL_DisplayFormatAlpha(imgbuffer);
		SDL_FreeSurface(imgbuffer);

		SDL_Rect srcrect;
		SDL_Rect dstrect;

		srcrect.x = x;
		srcrect.y = y;
		srcrect.w = w;
		srcrect.h = h;

		dstrect.x = 0;
		dstrect.y = 0;
		dstrect.w = w;
		dstrect.h = h;

		SDL_BlitSurface(src, &srcrect, hnd->image, &dstrect);

		return n;
	}

	return 0;
}

/**
 * Fill the specified rectangle in the destination image with the specified
 * RGBA values. (The RGBA values are in the range from 0 to 255.)
 */
// builtin sdl_fill(dstimg, x, y, w, h, r, g, b, a)
static struct spl_node *handler_sdl_fill(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	SDL_Surface *dst = clib_get_surface(task, 1);

	if (dst) {
		SDL_Rect dstrect;

		dstrect.x = spl_clib_get_int(task);
		dstrect.y = spl_clib_get_int(task);
		dstrect.w = spl_clib_get_int(task);
		dstrect.h = spl_clib_get_int(task);

		int r = spl_clib_get_int(task);
		int g = spl_clib_get_int(task);
		int b = spl_clib_get_int(task);
		int a = spl_clib_get_int(task);

		Uint32 color = SDL_MapRGBA(dst->format, r, g, b, a);

		SDL_FillRect(dst, &dstrect, color);
	}

	return 0;
}

/**
 * Fill the specified rectangle in the destination image with a
 * pattern.
 */
// builtin sdl_fill_pattern(dstimg, x, y, w, h, patternimg)
static struct spl_node *handler_sdl_fill_pattern(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	SDL_Surface *dst = clib_get_surface(task, 1);

	if (dst) {
		int dx = spl_clib_get_int(task);
		int dy = spl_clib_get_int(task);
		int dw = spl_clib_get_int(task);
		int dh = spl_clib_get_int(task);

		SDL_Surface *src = clib_get_surface(task, 0);

		SDL_Rect srcrect;

		srcrect.x = 0;
		srcrect.y = 0;
		srcrect.w = src->w;
		srcrect.h = src->h;

		for (int x=0; x<dw; x+=src->w)
		for (int y=0; y<dh; y+=src->h)
		{
			SDL_Rect dstrect;

			dstrect.x = dx + x;
			dstrect.y = dy + y;
			dstrect.w = src->w;
			dstrect.h = src->h;

			if (dstrect.x + dstrect.w > dx + dw)
				dstrect.w = dx + dw - dstrect.x;
			if (dstrect.y + dstrect.h > dy + dh)
				dstrect.h = dy + dh - dstrect.y;

			SDL_BlitSurface(src, &srcrect, dst, &dstrect);
		}
	}

	return 0;
}

/**
 * Return '1' if the specified key is pressed and '0' if it isn't. The keynames
 * are listed at:
 *
 *	http://www.libsdl.org/cgi/docwiki.cgi/SDLKey
 *
 * (lowercase versions of the SDLK_* strings without the SDLK_ prefix and with
 * blanks instead of underscores)
 */
// builtin sdl_keystat(keyname)
static struct spl_node *handler_sdl_keystate(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	const char *keyname = spl_clib_get_string(task);

	SDL_PumpEvents();

	int numkeys;
	Uint8 *keystates = SDL_GetKeyState(&numkeys);

	for (int i=0; i<numkeys; i++) {
		if (!strcmp(SDL_GetKeyName(i), keyname))
			return SPL_NEW_INT(keystates[i]);
	}

	spl_clib_exception(task, "SdlEx", "description",
			SPL_NEW_PRINTF("Unknown SDL keyname: %s", keyname),
			NULL);
	return 0;
}


/*** SDL Sprite HNODE ***/

struct sdl_sprite_hnode_data {
	struct sdl_hnode_data *image;
	int ox, oy, ow, oh, orev, oactive;
	int x, y, z, idx, updated;
	int schedule_destroy;
};

static struct sdl_sprite_hnode_data **sprite_list;
static int sprite_list_resort;
static int sprite_list_nextfree;
static int sprite_list_roof;

static void destroy_sprite_data(int idx)
{
	struct sdl_sprite_hnode_data *hnd = sprite_list[idx];
	struct sdl_hnode_data *ihnd = hnd->image;

	if (ihnd && --ihnd->reference_counter == 0) {
		sdl_hnode_data_free(ihnd);
		*(ihnd->left ? &ihnd->left->right : &spl_sdl_hnlist) = ihnd->right;
		if (ihnd->right) ihnd->right->left = ihnd->left;
		free(ihnd);
	}

	sprite_list[idx] = 0;
	sprite_list_resort = 1;
	free(hnd);
}

static void handler_sdl_sprite_node(struct spl_task *task, struct spl_vm *vm UNUSED,
		struct spl_node *node, struct spl_hnode_args *args, void *data UNUSED)
{
	if (args->action == SPL_HNODE_ACTION_PUT)
	{
		struct sdl_sprite_hnode_data *hnd = node->hnode_data;

		if (!hnd)
			return;

		if (hnd->oactive)
			hnd->schedule_destroy = 1;
		else
			destroy_sprite_data(hnd->idx);

		node->hnode_data = 0;
		return;
	}

	if (args->action == SPL_HNODE_ACTION_LOOKUP ||
	    args->action == SPL_HNODE_ACTION_CREATE)
	{
		struct sdl_sprite_hnode_data *hnd = node->hnode_data;
		char *mode = spl_hash_decode(args->key);

		if (!strcmp(mode, "x") || !strcmp(mode, "y") || !strcmp(mode, "z"))
		{
			int *value = 0;

			switch (*mode)
			{
			case 'x':
				value = &hnd->x;
				break;
			case 'y':
				value = &hnd->y;
				break;
			case 'z':
				value = &hnd->z;
				break;
			}

			if (args->action == SPL_HNODE_ACTION_LOOKUP)
				args->value = SPL_NEW_INT(*value);

			if (args->action == SPL_HNODE_ACTION_CREATE) {
				int new_value = spl_get_int(args->value);
				if (new_value != *value) {
					*value = spl_get_int(args->value);
					if (*mode == 'z')
						sprite_list_resort = 1;
					hnd->updated = 1;
				}
			}
		}

		if (!strcmp(mode, "image") && args->action == SPL_HNODE_ACTION_CREATE)
		{
			if (hnd->image) {
				struct sdl_hnode_data *ihnd = hnd->image;
				if (ihnd && --ihnd->reference_counter == 0) {
					sdl_hnode_data_free(ihnd);
					*(ihnd->left ? &ihnd->left->right : &spl_sdl_hnlist) = ihnd->right;
					if (ihnd->right) ihnd->right->left = ihnd->left;
					free(ihnd);
				}
				hnd->image = 0;
			}

			struct sdl_hnode_data *ihnd = clib_get_hnd(task, args->value);
			hnd->image = ihnd;
			if (hnd->image) {
				hnd->image->reference_counter++;
				hnd->orev = hnd->image->revision_counter;
			}
			hnd->updated = 1;
		}

		free(mode);
	}
}


static int sort_sprite_list_compar(const void *va, const void *vb)
{
	struct sdl_sprite_hnode_data **pa = (void*)va;
	struct sdl_sprite_hnode_data **pb = (void*)vb;
	struct sdl_sprite_hnode_data *a = *pa;
	struct sdl_sprite_hnode_data *b = *pb;

	if (!a && !b)
		return 0;

	if (!a) return +1;
	if (!b) return -1;

	if (a->z < b->z) return -1;
	if (a->z > b->z) return +1;

	return 0;
}

static void sort_sprite_list()
{
	if (!sprite_list_resort)
		return;

	qsort(sprite_list, sprite_list_nextfree,
		sizeof(struct sdl_sprite_hnode_data *),
		sort_sprite_list_compar);

	int i;
	for (i = 0; sprite_list[i] && i < sprite_list_nextfree; i++)
		sprite_list[i]->idx = i;
	sprite_list_nextfree = i;

	sprite_list_resort = 0;
}


/*** SDL Sprite Functions ***/

/**
 * Usually, when doing SDL programming in C, everyone is creating his own SDL
 * sprite library. This is possible when doing SDL programming with SDL to. But
 * for performance reasons I recommend to use the built-in sprites of this
 * module.
 *
 * This function returns a new sprite object. Such a sprite object has the
 * following attributes:
 *
 *	.x
 *		x-coordinate of the sprite (upper left corner)
 *	.y
 *		y-coordinate of the sprite (upper left corner)
 *
 *	.z
 *		z-coordinate of the sprite.
 *		sprites with higher values overlap sprites with lower values
 *
 *	.image
 *		the image data for the sprite.
 *		this must be set to one of SDL Image the object such as
 *		returned by [[sdl_image_create()]].
 */
// builtin sdl_sprite_create()
static struct spl_node *handler_sdl_sprite_create(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	struct sdl_sprite_hnode_data *hnd = calloc(1, sizeof(struct sdl_sprite_hnode_data));

	if (sprite_list_nextfree >= sprite_list_roof) {
		if (sprite_list_roof < 32)
			sprite_list_roof = 64;
		else
			sprite_list_roof = sprite_list_roof * 2;
		sprite_list = realloc(sprite_list, sprite_list_roof * sizeof(struct sdl_sprite_hnode_data*));
	}

	hnd->idx = sprite_list_nextfree++;
	sprite_list[hnd->idx] = hnd;
	sprite_list_resort = 1;

	struct spl_node *n = SPL_NEW_STRING_DUP("SDL Sprite");
	n->hnode_name = strdup("sdl_sprite");
	n->hnode_data = hnd;

	return n;
}

static void sprite_update_backend(int x, int y, int w, int h, int usexywh)
{
	for (int i = 0; i < sprite_list_nextfree; i++)
	{
		if (!usexywh) {
			if (sprite_list[i]->schedule_destroy) {
				destroy_sprite_data(i);
				continue;
			}
			if (sprite_list[i]->image)
				sprite_list[i]->orev = sprite_list[i]->image->revision_counter;
			sprite_list[i]->updated = 0;
		}

		if (!sprite_list[i] || !sprite_list[i]->image || !sprite_list[i]->image->image)
			continue;

		struct sdl_sprite_hnode_data *hnd = sprite_list[i];
		SDL_Surface *src = hnd->image->image;

		if (usexywh) {
			if (hnd->x + src->w < x) continue;
			if (hnd->y + src->h < y) continue;
			if (hnd->x > x + w) continue;
			if (hnd->y > y + h) continue;
		}

		SDL_Rect srcrect;
		SDL_Rect dstrect;

		if (usexywh) {
			if (hnd->x < x) {
				srcrect.x = x - hnd->x;
				dstrect.x = x;
			} else {
				srcrect.x = 0;
				dstrect.x = hnd->x;
			}

			if (hnd->y < y) {
				srcrect.y = y - hnd->y;
				dstrect.y = y;
			} else {
				srcrect.y = 0;
				dstrect.y = hnd->y;
			}

			srcrect.w = w;
			srcrect.h = h;
			dstrect.w = w;
			dstrect.h = h;
		} else {
			srcrect.x = 0;
			srcrect.y = 0;
			srcrect.w = src->w;
			srcrect.h = src->h;

			dstrect.x = sprite_list[i]->x;
			dstrect.y = sprite_list[i]->y;
			dstrect.w = srcrect.w;
			dstrect.h = srcrect.h;
		}

		SDL_BlitSurface(src, &srcrect, spl_sdl_root, &dstrect);
	}

	if (usexywh) {
		if (x < 0) x = 0;
		if (y < 0) y = 0;
		if (x + w > spl_sdl_root->w) w = spl_sdl_root->w - x;
		if (y + h > spl_sdl_root->h) h = spl_sdl_root->h - y;

		if (w > 0 && h > 0)
			SDL_UpdateRect(spl_sdl_root, x, y, w, h);
	}
}

/**
 * This redraws all sprites, also those which haven't been updated.
 *
 * This function is only needed if you are mixing sprites with direct blitting
 * to the window. Usually [[sdl_sprite_update()]] is used instead.
 *
 * In some corner cases (when many sprites have been updated) this function
 * might be faster than [[sdl_sprite_update()]]. But this is a very seldom
 * case.
 */
// builtin sdl_sprite_redraw()
static struct spl_node *handler_sdl_sprite_redraw(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	sort_sprite_list();
	sprite_update_backend(0, 0, 0, 0, 0);
	SDL_UpdateRect(spl_sdl_root, 0, 0, 0, 0);

	return 0;
}

/**
 * This redraws all the updated sprites.
 */
// builtin sdl_sprite_update()
static struct spl_node *handler_sdl_sprite_update(struct spl_task *task, void *data UNUSED)
{
	CHECK_INIT

	sort_sprite_list();

	for (int i = 0; i < sprite_list_nextfree; i++)
	{
		struct sdl_sprite_hnode_data *hnd = sprite_list[i];

		if (hnd->image && hnd->orev != hnd->image->revision_counter) {
			hnd->orev = hnd->image->revision_counter;
			hnd->updated = 1;
		}

		if (!hnd->updated)
			continue;

		if (hnd->oactive && (hnd->x != hnd->ox || hnd->y != hnd->oy ||
		                     !hnd->image || !hnd->image->image ||
		                     hnd->image->image->w != hnd->ow ||
		                     hnd->image->image->h != hnd->oh)) {
			sprite_update_backend(hnd->ox, hnd->oy, hnd->ow, hnd->oh, 1);
		}

		if (hnd->schedule_destroy) {
			destroy_sprite_data(hnd->idx);
			continue;
		}

		if (!hnd->image || !hnd->image->image) {
			hnd->updated = 0;
			hnd->oactive = 0;
			continue;
		}

		sprite_update_backend(hnd->x, hnd->y,
				hnd->image->image->w, hnd->image->image->h, 1);

		hnd->ox = hnd->x;
		hnd->oy = hnd->y;
		hnd->ow = hnd->image->image->w;
		hnd->oh = hnd->image->image->h;

		hnd->updated = 0;
		hnd->oactive = 1;
	}

	return 0;
}


/*** Initializations ***/

/**
 * An instance of this object is thrown on SDL errors.
 */
//object SdlEx

/**
 * A description text describing the error.
 */
// var description;

void SPL_ABI(spl_mod_sdl_init)(struct spl_vm *vm, struct spl_module *mod, int restore)
{
	if (!restore)
		spl_eval(vm, 0, strdup(mod->name), "object SdlEx { }");

	spl_hnode_reg(vm, "sdl", handler_sdl_node, 0);
	spl_hnode_reg(vm, "sdl_sprite", handler_sdl_sprite_node, 0);

	spl_clib_reg(vm, "sdl_init", handler_sdl_init, 0);
	spl_clib_reg(vm, "sdl_quit", handler_sdl_quit, 0);

	spl_clib_reg(vm, "sdl_title", handler_sdl_title, 0);
	spl_clib_reg(vm, "sdl_delay", handler_sdl_delay, 0);
	spl_clib_reg(vm, "sdl_flip", handler_sdl_flip, 0);
	spl_clib_reg(vm, "sdl_update", handler_sdl_update, 0);

	spl_clib_reg(vm, "sdl_image_load", handler_sdl_image_load, 0);
	spl_clib_reg(vm, "sdl_image_create", handler_sdl_image_create, 0);

	spl_clib_reg(vm, "sdl_blit", handler_sdl_blit, 0);
	spl_clib_reg(vm, "sdl_blitrect", handler_sdl_blitrect, 0);
	spl_clib_reg(vm, "sdl_copy", handler_sdl_copy, 0);

	spl_clib_reg(vm, "sdl_fill", handler_sdl_fill, 0);
	spl_clib_reg(vm, "sdl_fill_pattern", handler_sdl_fill_pattern, 0);

	spl_clib_reg(vm, "sdl_keystate", handler_sdl_keystate, 0);

	spl_clib_reg(vm, "sdl_sprite_create", handler_sdl_sprite_create, 0);
	spl_clib_reg(vm, "sdl_sprite_redraw", handler_sdl_sprite_redraw, 0);
	spl_clib_reg(vm, "sdl_sprite_update", handler_sdl_sprite_update, 0);
}

void SPL_ABI(spl_mod_sdl_done)(struct spl_vm *vm, struct spl_module *mod UNUSED)
{
	if (vm->sdl_initialized) {
		spl_report(SPL_REPORT_HOST, vm, "Missing call to sdl_quit()!\n");
		handler_sdl_quit(0, 0);
		vm->sdl_initialized = 0;
	}

	for (int i = 0; i < sprite_list_nextfree; i++) {
		if (sprite_list[i]) {
			if (sprite_list[i]->schedule_destroy) {
				destroy_sprite_data(i);
			} else {
				spl_report(SPL_REPORT_HOST, vm, "Found active non-null entry in SDL sprite list on module unload!\n");
				goto skip_free_sprite_list;
			}
		}
	}

	free(sprite_list);
	sprite_list_resort = 0;
	sprite_list_nextfree = 0;
	sprite_list_roof = 0;

skip_free_sprite_list:
	return;
}