[go: up one dir, main page]

Menu

[r52]: / engine / media.c  Maximize  Restore  History

Download this file

800 lines (649 with data), 12.2 kB

  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
/*
* Media System Initialisation
*
* Start up multimedia devices and input
*/
#include <allegro.h>
#include <string.h>
#include "ithelib.h"
#include "loadfile.h"
#include "console.h"
#include "oscli.h"
#include "media.h"
#include "mouse.h"
#include "sound.h"
// Defines
#define CURSOR_CHAR "_"
#define KBSIZE 2 // Size of keyboard buffer
#ifdef _WIN32
#define WINDOWS_IS_BROKEN
#endif
// Variables
#ifdef WINDOWS_IS_BROKEN
BITMAP *screen=NULL;
#endif
BITMAP *swapscreen;
int ire_transparent,ire_nontransparent,ire_bpp,ire_black,ire_bytespp;
unsigned char ire_transparent_r,ire_transparent_g,ire_transparent_b;
static BITMAP *pic;
static BITMAP *oldpic;
//static BITMAP *load_cel(char *filename, RGB *pal);
static char media_on=0,media_clock=0;
static RGB pal[256];
COLOR_MAP light_table;
static RGB_MAP rgb_table;
static int ikeybuf_rptr=0; // Key buffer code
static int ikeybuf_wptr=0;
static int ikeybuf[KBSIZE+1];
static int imousebuf_rptr=0; // Mouse button buffer code
static int imousebuf_wptr=0;
static int imousebuf[KBSIZE+1];
// Functions
extern int gfx_mode_select_ire(int *card, int *bpp);
extern void ire_alert_mode();
extern void RFS_getescape();
static void ChooseMode();
static int rkbuf();
static int rmbuf();
// Code
/*
* Timer code and it's volatile data
*/
static volatile int ready_for_tick=0; // Timer clock
static volatile unsigned int ire_uticks=0;
static volatile unsigned int ire_animticks=0;
static volatile unsigned int ire_divisor=0;
static void ire_animation_clock(void)
{
ire_uticks++;
ire_divisor++;
// Divide the animation clock by four
if(ire_divisor >= 4)
{
ire_animticks++;
ready_for_tick=1;
ire_divisor=0;
}
}
END_OF_STATIC_FUNCTION(ire_animation_clock)
/*
* UpdateAnim() - returns true if it's time to update the animations
*/
int UpdateAnim()
{
if(ready_for_tick)
{
ready_for_tick = 0;
return 1;
}
return 0;
}
END_OF_STATIC_FUNCTION(UpdateAnim)
unsigned int GetIREClock()
{
return ire_uticks;
}
void ResetIREClock()
{
ire_uticks=0;
if(in_editor)
return;
// Sync animation clock
ready_for_tick=0;
ire_divisor=0;
ire_animticks=0;
}
unsigned int GetAnimClock()
{
return ire_animticks;
}
//
// Core media initialisation
//
void init_media(int game)
{
int r;
if(media_on)
return;
media_on=1;
ilog_printf("Starting up:\n");
ilog_printf(" Keyboard: ");
if(!install_keyboard())
ilog_printf("OK\n");
else
{
ilog_printf("FAILED\n");
ilog_printf("%s\n",allegro_error);
exit(1);
}
if(game && !debug_nosound)
{
ilog_printf(" Sound: ");
if(S_Init())
ilog_printf("OK\n");
else
ilog_printf("FAILED\n");
}
// Set allegro panic button
if(allow_breakout)
three_finger_flag=1;
else
three_finger_flag=0;
ilog_printf(" Timer: ");
if(install_timer() != -1)
ilog_printf("OK\n");
else
{
ilog_printf("FAILED\n");
ilog_printf("%s\n",allegro_error);
exit(1);
}
ilog_printf(" Mouse: ");
if(install_mouse() != -1)
ilog_printf("OK\n");
else
{
ilog_printf("FAILED\n");
ilog_printf("%s\n",allegro_error);
exit(1);
}
ilog_printf(" Video:\n");
switch(VideoMode)
{
// Windowed mode
case -2:
// Get colour depth
ire_bpp = desktop_color_depth();
// Work out bytes per pixel
ire_bytespp=ire_bpp/8;
if(ire_bpp==15)
ire_bytespp=2; // hack for 15bpp
// Set it up..
set_color_depth(ire_bpp);
set_color_conversion(COLORCONV_TOTAL);
if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0))
ChooseMode();
break;
// Supported video depths (fullscreen)
case 8:
case 15:
case 16:
case 24:
case 32:
ire_bpp=VideoMode;
// Work out bytes per pixel
ire_bytespp=ire_bpp/8;
if(ire_bpp==15)
ire_bytespp=2; // hack for 15bpp
set_color_depth(ire_bpp);
set_color_conversion(COLORCONV_TOTAL);
if(set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0))
ChooseMode();
break;
// Anything we don't understand
default:
ChooseMode();
break;
}
#ifdef WINDOWS_IS_BROKEN
ilog_quiet(" Attempting to fix windows bug\n");
screen = create_video_bitmap(640,480);
#endif
ilog_quiet(" Palette\n");
if(ire_bpp == 8)
{
for(r=0;r<256;r++)
{
pal[r].r = r>>2;
pal[r].g = r>>2;
pal[r].b = r>>2;
// pal[r].r|=4; // stop it being 0
}
// Purple transparent
pal[0].r = 0;
pal[0].g = 0;
pal[0].b = 0;
}
else
generate_332_palette(pal);
set_palette(pal);
ilog_quiet("Primary media init succesful\n");
ilog_printf("\n");
}
//
// Secondary media initialisation
//
void init_enginemedia()
{
int r,g,b,sum;
char picpath[1024];
ilog_quiet(" Video:\n");
ilog_quiet(" RGBtab\n");
ilog_quiet(" RGBtab\n");
if(ire_bpp == 8)
{
// Make a greyscale translation table for 8bpp mode
for(r=0;r<32;r++)
for(g=0;g<32;g++)
for(b=0;b<32;b++)
{
sum=((r<<3)+(g<<3)+(b<<3))/3;
/*
if(sum == 0 && r+g+b>0)
sum = 1;
*/
rgb_table.data[r][g][b]=sum;
}
}
else
create_rgb_table(&rgb_table,pal,NULL); // proper translation table
// Use it, whichever it is
rgb_map=&rgb_table;
ilog_quiet(" Light tab\n");
create_light_table(&light_table,pal,0,0,0,NULL);
color_map=&light_table;
ilog_quiet(" Transparency\n");
if(ire_bpp == 8)
{
ire_transparent = 0;
ire_black = 1;
}
else
{
ire_transparent = makecol(255,0,255);
ire_black = makecol(0,0,0);
}
ire_transparent_r = getr(ire_transparent);
ire_transparent_g = getg(ire_transparent);
ire_transparent_b = getb(ire_transparent);
ilog_quiet(" Text mode\n");
clear_to_color(screen,ire_black);
swapscreen=MakeScreen(640,480);
if(!swapscreen)
{
allegro_exit();
ilog_quiet("Can't create swapscreen\n");
printf("Can't create swapscreen\n");
exit(1);
}
oldpic=MakeScreen(640,480);
if(!oldpic)
{
allegro_exit();
ilog_quiet("Can't create save/restore bitmap\n");
printf("Can't create save/restore bitmap\n");
exit(1);
}
if(loadingpic[0] == 0)
{
// None specified
clear_to_color(swapscreen,ire_black);
Show();
}
else
{
loadfile(loadingpic,picpath);
pic = iload_bitmap(picpath);
if(!pic)
{
ilog_quiet(" Oh dear\n");
allegro_exit();
ilog_quiet("Can't load backing picture '%s'\n",picpath);
printf("Can't load backing picture '%s'\n",picpath);
exit(1);
}
clear_to_color(swapscreen,ire_black);
masked_blit(pic,swapscreen,0,0,0,0,640,480);
Show();
destroy_bitmap(pic);
}
ilog_quiet(" Start console\n");
irecon_init(logx,logy,80,loglen);
set_trans_blender(0, 0, 0, 128);
ilog_quiet(" Locks\n");
// Ok, let's do the timer stuff now
// Lock these when in DOS to prevent a horrible crash
LOCK_FUNCTION(ire_animation_clock);
LOCK_FUNCTION(UpdateAnim); // Not strictly necessary
LOCK_VARIABLE(ready_for_tick);
LOCK_VARIABLE(ire_uticks);
media_clock=0;
// Ok, let's do it
ilog_quiet(" Clock: ");
if(!install_int_ex(ire_animation_clock,BPS_TO_TIMER(140))) // 35
ilog_quiet("Ok\n");
else
{
ilog_quiet("Failed: %s\n",allegro_error);
readkey();
exit(1);
}
media_clock=1;
ilog_quiet("Media INIT successful\n");
}
void term_media()
{
if(!media_on)
{
ilog_quiet("Media not active, can't shut down\n");
return;
}
media_on=0;
ilog_quiet("Shut down Media:\n");
ilog_quiet(" Clock:\n");
if(media_clock)
{
remove_int(ire_animation_clock);
media_clock=0;
}
ilog_quiet(" Gfx:\n");
KillGFX();
ilog_quiet(" Keyboard:\n");
remove_keyboard();
ilog_quiet(" Allegro:\n");
//allegro_exit();
S_Term();
ilog_quiet("Media shutdown completed\n");
}
int sizeof_sprite(BITMAP *bmp)
{
int cd=0;
switch(bitmap_color_depth(bmp))
{
case 8:
cd=1;
break;
case 15:
case 16:
cd=2;
break;
case 24:
cd=3;
break;
case 32:
cd=4;
break;
default:
ithe_panic("Unknown colour depth in object",NULL);
break;
};
return bmp->w*bmp->h*cd;
}
/*
* Get a key, if one is pending, else return 0
*/
int GetKey()
{
return rkbuf()>>8;
}
int GetMouseButton()
{
return rmbuf();
}
/*
* Wait for a key
*/
unsigned char WaitForKey()
{
int k;
do
{
k = rkbuf();
// S_PollMusic();
// look for a mouse click?
poll_mouse();
CheckMouseRanges();
if(MouseID != -1)
{
return KEY_MAX+1; // Not a valid key, a mouse click
}
} while(!k);
return k>>8;
}
unsigned char WaitForAscii()
{
int k;
do
{
k = rkbuf();
// S_PollMusic();
} while(!k);
return k&0xff;
}
void FlushKeys()
{
clear_keybuf();
ikeybuf_rptr=0;
ikeybuf_wptr=0;
memset(ikeybuf,0,sizeof(ikeybuf));
}
// This is a ring buffer, not a stack
int rkbuf()
{
int k;
// Take key from buffer if necessary
rest(0);
poll_keyboard();
if(!keypressed())
{
k = ikeybuf[ikeybuf_rptr];
if(!k)
{
return k; // Abort if nothing is there
}
ikeybuf[ikeybuf_rptr++]=0;
// Wrap the buffer around if we go too far
if(ikeybuf_rptr>=KBSIZE)
ikeybuf_rptr=0;
return k;
}
// Fill the buffer
while(keypressed())
{
poll_keyboard();
if(ikeybuf[ikeybuf_wptr]) // It's full.
{
clear_keybuf();
}
else
{
ikeybuf[ikeybuf_wptr]=readkey();
if(key_shifts & KEY_LSHIFT)
ikeybuf[ikeybuf_wptr] |= 0x8000; // High bit is for SHIFT
ikeybuf_wptr++;
if(ikeybuf_wptr>=KBSIZE)
{
ikeybuf_wptr=0;
}
}
};
k = ikeybuf[ikeybuf_rptr];
ikeybuf[ikeybuf_rptr++]=0;
if(ikeybuf_rptr>=KBSIZE)
{
ikeybuf_rptr=0;
}
return k;
}
// Buffered mouse button code..
int rmbuf()
{
int k;
// Take key from buffer if necessary
poll_mouse();
if(!mouse_b)
{
k = imousebuf[imousebuf_rptr];
if(!k)
{
return k; // Abort if nothing is there
}
imousebuf[imousebuf_rptr++]=0;
// Wrap the buffer around if we go too far
if(imousebuf_rptr>=KBSIZE)
{
imousebuf_rptr=0;
}
return k;
}
// Fill the buffer
while(mouse_b)
{
poll_mouse();
if(imousebuf[imousebuf_wptr]) // It's full.
{
mouse_b=0; // Smash The Man
}
else
{
imousebuf[imousebuf_wptr]=mouse_b;
imousebuf_wptr++;
if(imousebuf_wptr>=KBSIZE)
{
imousebuf_wptr=0;
}
}
};
k = imousebuf[imousebuf_rptr];
imousebuf[imousebuf_rptr++]=0;
if(imousebuf_rptr>=KBSIZE)
{
imousebuf_rptr=0;
}
return k;
}
/*
* Plot - draw the little row of dots
*
* Call Plot(number); to set the maximum length of the row
* Then call Plot(0); each cycle of the loop to draw a dot
*/
void Plot(int max)
{
static int mpos,ctr=0;
ctr++;
if(max)
{
mpos=max/40;
if(mpos<1)
mpos=1;
ctr=0;
}
if(ctr>mpos)
{
ilog_printf(".");
ctr=0;
}
}
/*
* BMPShot - Take a screenshot in .BMP format
*/
void BMPshot()
{
char name[256];
int ctr=0;
do
{
sprintf(name,"ire%05d.bmp",ctr++);
} while(!access(name,F_OK));
// Create file or bail out
save_bitmap(name,swapscreen,pal);
}
void ChooseMode()
{
int card,ok;
// Choose sensible default BPP
#ifdef __DJGPP__
ire_bpp = 16;
#else
ire_bpp = desktop_color_depth();
#endif
set_gfx_mode(GFX_SAFE, 320, 200, 0, 0);
generate_332_palette(pal);
set_palette(pal);
do {
ok=0;
if (!gfx_mode_select_ire(&card, &ire_bpp))
{
allegro_exit();
exit(1);
}
set_color_depth(ire_bpp);
set_color_conversion(COLORCONV_TOTAL);
if(!set_gfx_mode(card, 640, 480, 0, 0))
ok=1;
else
{
set_color_depth(8);
set_gfx_mode(GFX_SAFE, 320, 200, 0, 0);
generate_332_palette(pal);
set_palette(pal);
ire_alert_mode();
}
} while(!ok);
}
/*
* Get a string
*/
int GetStringInput(char *ptr,int len)
{
int input=0;
int bufpos=0;
char buf[128];
strcpy(buf,ptr);
bufpos=strlen(buf);
buf[bufpos]=0;
do {
irecon_clearline();
if(buf[0] != 0)
irecon_print(buf); // Printing an empty line makes a newline
irecon_print(CURSOR_CHAR);
irecon_update();
Show();
if(keypressed())
{
input = readkey();
if((input>>8) == KEY_F10)
BMPshot();
if((input&0xff) >= ' ' && (input&0xff) <= 'z')
{
if(bufpos<conwid && bufpos<len)
{
buf[bufpos++]=input&0xff;
buf[bufpos]=0;
}
}
if((input>>8) == KEY_DEL || (input>>8) == KEY_BACKSPACE)
if(bufpos>0)
{
bufpos--;
buf[bufpos]=0;
}
}
else
input=0;
} while((input>>8) != KEY_ESC && (input>>8) != KEY_ENTER);
irecon_clearline();
irecon_print(buf);
irecon_print("\n");
if((input>>8) == KEY_ESC)
return 0;
strcpy(ptr,buf);
return 1;
}
BITMAP *MakeScreen(int w, int h)
{
if(use_hw_video)
if(gfx_capabilities&GFX_HW_VRAM_BLIT_MASKED)
return create_video_bitmap(w, h);
return create_bitmap(w,h);
//return screen;
}
void SaveScreen()
{
blit(swapscreen,oldpic,0,0,0,0,640,480);
}
void RestoreScreen()
{
blit(oldpic,swapscreen,0,0,0,0,640,480);
}