summaryrefslogtreecommitdiff
path: root/djgpp/gfx.c
blob: 8d8e849192edf11c6bf9cab9b90b53ff3b7f0aef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
/*

    viDOOM - level editor for DOOM

    Copyright (C) 2000  Ian Cowburn (ianc@noddybox.demon.co.uk)

    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

    -------------------------------------------------------------------------

    Graphics functions

*/
static const char rcs_id[]="$Id$";

#include "config.h"

#include <stdio.h>
#include <allegro.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include "gfx.h"
#include "gui.h"
#include "debug.h"


/* ---------------------------------------- VARS
*/
#define COLDEP		16
#define ACOL(c)		(makecol_depth(COLDEP,((c)&0xff0000)>>16, \
					      ((c)&0xff00)>>8, \
					      ((c)&0xff)))

static int		init=FALSE;

static BITMAP		*bm;
static int		width;
static int		height;

static int		mbuttons;

static int		dirty_min_x;
static int		dirty_max_x;
static int		dirty_min_y;
static int		dirty_max_y;

#define DIRTY(x,y)	do {					\
			    dirty_min_x=MIN(x,dirty_min_x);	\
			    dirty_min_y=MIN(y,dirty_min_y);	\
			    dirty_max_x=MAX((x)+1,dirty_max_x);	\
			    dirty_max_y=MAX((y)+1,dirty_max_y);	\
			} while(0)


/* ---------------------------------------- PRIVATE FUNCTIONS
*/

static void GetKey(GFXKey *key)
{
    static struct
    	{
	int alleg;
	int code;
	} keym[]=   {
			{KEY_F1,GFX_F1},
			{KEY_F2,GFX_F2},
			{KEY_F3,GFX_F3},
			{KEY_F4,GFX_F4},
			{KEY_F5,GFX_F5},
			{KEY_F6,GFX_F6},
			{KEY_F7,GFX_F7},
			{KEY_F8,GFX_F8},
			{KEY_F9,GFX_F9},
			{KEY_F10,GFX_F10},
			{KEY_F11,GFX_F11},
			{KEY_F12,GFX_F12},
			{KEY_ESC,GFX_ESC},
			{KEY_INSERT,GFX_INSERT},
			{KEY_HOME,GFX_HOME},
			{KEY_PGUP,GFX_PGUP},
			{KEY_DEL,GFX_DELETE},
			{KEY_END,GFX_END},
			{KEY_PGDN,GFX_PGDN},
			{KEY_UP,GFX_UP},
			{KEY_DOWN,GFX_DOWN},
			{KEY_LEFT,GFX_LEFT},
			{KEY_RIGHT,GFX_RIGHT},
			{KEY_ENTER,GFX_ENTER},
			{KEY_BACKSPACE,GFX_BACKSPACE},
			{KEY_TAB,GFX_TAB},
			{-1,-1},
		    };
    int f;
    int k;

    while(TRUE)
    	{
	k=readkey();

	key->shift=key_shifts&KB_SHIFT_FLAG;
	key->ctrl=key_shifts&KB_CTRL_FLAG;
	key->alt=key_shifts&KB_ALT_FLAG;

	f=0;
	while(keym[f].code!=-1)
	    {
	    if (keym[f].alleg==k>>8)
	    	{
		key->ascii=0;
		key->code=keym[f].code;
		return;
		}

	    f++;
	    }

	if (isprint(k&0xff))
	    {
	    key->ascii=(k&0xff);
	    key->code=GFX_ASCII;
	    return;
	    }
	}
}

/* ---------------------------------------- EXPORTED FUNCTIONS
*/
void GFX_init(void)
{
    if (!init)
    	{
	init=TRUE;
	allegro_init();

	install_keyboard();

	install_timer();

	if ((mbuttons=install_mouse())==-1)
	    GFX_exit(EXIT_FAILURE,"Failed to install mouse handler\n");

	set_color_depth(COLDEP);
	}
}


void GFX_close(void)
{
    if (init)
	{
	allegro_exit();
	init=FALSE;
	}
}


void GFX_open(int w,int h)
{
    height=h;
    width=w;

    if (set_gfx_mode(GFX_AUTODETECT,w,h,0,0)<0)
    	GFX_exit(EXIT_FAILURE,"Couldn't open screen of %dx%d\n",w,h);

    if (!(bm=create_bitmap(w,h)))
    	GFX_exit(EXIT_FAILURE,"Couldn't create bitmap of %dx%d\n",w,h);

    show_mouse(screen);
}


void GFX_clear(int col)
{
    clear_to_color(bm,ACOL(col));
    dirty_min_x=0;
    dirty_max_x=width;
    dirty_min_y=0;
    dirty_max_y=width;
}


void GFX_redraw(void)
{
    if (dirty_max_x<dirty_min_x)
    	return;

    dirty_min_x=MAX(0,dirty_min_x);
    dirty_min_y=MAX(0,dirty_min_y);
    dirty_max_x=MIN(width,dirty_max_x);
    dirty_max_y=MIN(height,dirty_max_y);

    show_mouse(NULL);

    blit(bm,screen,dirty_min_x,dirty_min_y,
		   dirty_min_x,dirty_min_y,
		   dirty_max_x-dirty_min_x+1,dirty_max_y-dirty_min_y+1);

    show_mouse(screen);

    dirty_max_x=0;
    dirty_min_x=width-1;
    dirty_max_y=0;
    dirty_min_y=width-1;
}


void GFX_FORCE_REDRAW(void)
{
    show_mouse(NULL);
    blit(bm,screen,0,0,0,0,width,height);
    show_mouse(screen);
    dirty_max_x=0;
    dirty_min_x=width-1;
    dirty_max_y=0;
    dirty_min_y=width-1;
}


void GFX_line(int x1,int y1,int x2,int y2,int col)
{
    DIRTY(x1,y1);
    DIRTY(x2,y2);
    line(bm,x1,y1,x2,y2,ACOL(col));
}


void GFX_plot(int x,int y,int col)
{
    DIRTY(x,y);
    putpixel(bm,x,y,ACOL(col));
}


void GFX_circle(int x,int y,int r,int col)
{
    DIRTY(x-r-1,y-r-1);
    DIRTY(x+r+1,y+r+1);
    circle(bm,x,y,r,ACOL(col));
}


void GFX_fcircle(int x,int y,int r,int col)
{
    DIRTY(x-r-1,y-r-1);
    DIRTY(x+r+1,y+r+1);
    circlefill(bm,x,y,r,ACOL(col));
}


void GFX_rect(int x,int y,int w,int h,int col)
{
    DIRTY(x,y);
    DIRTY(x+w,y+h);

    w-=SGN(w);
    h-=SGN(h);

    rect(bm,x,y,x+w,y+h,ACOL(col));
}


void GFX_frect(int x,int y,int w,int h,int col)
{
    DIRTY(x,y);
    DIRTY(x+w,y+h);

    w-=SGN(w);
    h-=SGN(h);

    rectfill(bm,x,y,x+w,y+h,ACOL(col));
}


void GFX_set_XOR_mode(void)
{
    drawing_mode(DRAW_MODE_XOR,NULL,0,0);
}


void GFX_clear_XOR_mode(void)
{
    drawing_mode(DRAW_MODE_SOLID,NULL,0,0);
}


void GFX_print(int x,int y,int col,char *fmt,...)
{
    char s[512];
    va_list va;

    va_start(va,fmt);
    vsprintf(s,fmt,va);
    va_end(va);

    DIRTY(x,y);
    DIRTY(x+text_length(font,s),y+text_height(font));

    text_mode(-1);
    textout(bm,font,s,x,y,ACOL(col));
}


int GFX_fh(void)
{
    return(text_height(font));
}


int GFX_fw(void)
{
    return(text_length(font," "));
}


int GFX_mouse_buttons(void)
{
    return(mbuttons);
}


int GFX_mouse(int *x,int *y)
{
    if (x)
	*x=mouse_x;

    if (y)
	*y=mouse_y;

    return(mouse_b);
}


void GFX_waitkey(GFXKey *key)
{
    GFXKey dummy;

    while(!keypressed());
    if (key)
	GetKey(key);
    else
	GetKey(&dummy);
}


int GFX_key(GFXKey *key)
{
    if(!keypressed())
    	return(FALSE);
    else
	{
	GetKey(key);
	return(TRUE);
	}
}


void GFX_bounce(void)
{
    while((keypressed())||(mouse_b))
    	if (keypressed())
	    readkey();
}


void GFX_await_input(GFXEvent *ev)
{
    static time_t last_time=0;
    static int mb;

    /* Assume that a gap of 1-2 seconds means that this is a new await
       input loop
    */
    if (time(NULL)-last_time>1)
	mb=0;

    while (TRUE)
    	{
	if (keypressed())
	    {
	    ev->type=GFX_KEY_EVENT;
	    GetKey(&ev->key);
	    return;
	    }

	if (mouse_b!=mb)
	    {
	    ev->mouse.shift=key_shifts&KB_SHIFT_FLAG;
	    ev->mouse.ctrl=key_shifts&KB_CTRL_FLAG;
	    ev->mouse.alt=key_shifts&KB_ALT_FLAG;
	    ev->mouse.x=mouse_x;
	    ev->mouse.y=mouse_y;
	    ev->mouse.b=mb=mouse_b;
	    ev->type=GFX_MOUSE_EVENT;
	    return;
	    }
	}
}


void GFX_await_input_full(GFXEvent *ev)
{
    static time_t last_time=0;
    static int mx,my,mb;

    /* Assume that a gap of 1-2 seconds means that this is a new await
       input loop
    */
    if (time(NULL)-last_time>1)
	{
        mx=-1;
	my=-1;
	mb=0;
	}

    while (TRUE)
    	{
	if (keypressed())
	    {
	    ev->type=GFX_KEY_EVENT;
	    GetKey(&ev->key);
	    last_time=time(NULL);
	    return;
	    }

	if ((mouse_b!=mb)||(mouse_x!=mx)||(mouse_y!=my))
	    {
	    ev->mouse.shift=key_shifts&KB_SHIFT_FLAG;
	    ev->mouse.ctrl=key_shifts&KB_CTRL_FLAG;
	    ev->mouse.alt=key_shifts&KB_ALT_FLAG;
	    ev->mouse.x=mx=mouse_x;
	    ev->mouse.y=my=mouse_y;
	    ev->mouse.b=mb=mouse_b;
	    ev->type=GFX_MOUSE_EVENT;
	    last_time=time(NULL);
	    return;
	    }
	}
}


void GFX_exit(int code,char *fmt,...)
{
    va_list va;

    if (init)
    	allegro_exit();

    va_start(va,fmt);
    vfprintf(stderr,fmt,va);
    va_end(va);

    exit(code);
}


GFX_IMAGE GFX_create_image(GFX_BITMAP *bm)
{
    BITMAP *b;
    RLE_SPRITE *ret;
    int x,y;

    if (!(b=create_bitmap(bm->w,bm->h)))
    	return(NULL);

    clear(b);

    for(x=0;x<bm->w;x++)
	for(y=0;y<bm->h;y++)
	    putpixel(b,x,y,ACOL(bm->pal[*(bm->data+(y*bm->w)+(x))]));

    ret=get_rle_sprite(b);
    destroy_bitmap(b);

    return((GFX_IMAGE)ret);
}


void GFX_destroy_image(GFX_IMAGE img)
{
    destroy_rle_sprite(img);
}


void GFX_draw_image(GFX_IMAGE i, int x, int y)
{
    RLE_SPRITE *s;

    s=i;
    draw_rle_sprite(bm,s,x,y);
    DIRTY(x,y);
    DIRTY(x+s->w,y+s->h);
}


void GFX_fill_screen(GFX_IMAGE i)
{
    BITMAP *b;
    RLE_SPRITE *s;

    s=i;
    b=create_bitmap(s->w,s->h);
    draw_rle_sprite(b,s,0,0);

    stretch_blit(b,bm,0,0,s->w,s->h,0,0,width,height);
    destroy_bitmap(b);

    DIRTY(0,0);
    DIRTY(width,height);
}


void GFX_save_screen(char *path)
{
    BITMAP *sub;

    sub=create_sub_bitmap(screen,0,0,width,height);
    save_bmp(path,sub,NULL);
    destroy_bitmap(sub);
}


/* END OF FILE */