1 /*
2  * GOATTRACKER "console" output routines
3  */
4 
5 #define GCONSOLE_C
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "goattrk2.h"
11 
12 int gfxinitted = 0;
13 unsigned *scrbuffer = NULL;
14 unsigned *prevscrbuffer = NULL;
15 unsigned char *chardata = NULL;
16 int key = 0;
17 int rawkey = 0;
18 int shiftpressed = 0;
19 int cursorflashdelay = 0;
20 int mouseb = 0;
21 int prevmouseb = 0;
22 unsigned mousex = 0;
23 unsigned mousey = 0;
24 unsigned mousepixelx = 0;
25 unsigned mousepixely = 0;
26 unsigned oldmousepixelx = 0xffffffff;
27 unsigned oldmousepixely = 0xffffffff;
28 int mouseheld = 0;
29 int region[MAX_ROWS];
30 int fontwidth = 8;
31 int fontheight = 14;
32 int mousesizex = 11;
33 int mousesizey = 20;
34 unsigned bigwindow = 1;
35 
36 void loadexternalpalette(void);
37 void initicon(void);
38 
setcharcolor(unsigned * dptr,short ch,short color)39 inline void setcharcolor(unsigned *dptr, short ch, short color)
40 {
41   *dptr = (ch & 0xff) | (color << 16);
42 }
43 
setcolor(unsigned * dptr,short color)44 inline void setcolor(unsigned *dptr, short color)
45 {
46   *dptr = (*dptr & 0xffff) | (color << 16);
47 }
48 
initscreen(void)49 int initscreen(void)
50 {
51   int handle;
52 
53   if (bigwindow > 1)
54   {
55     fontwidth *= bigwindow;
56     fontheight *= bigwindow;
57     mousesizex *= 2;
58     mousesizey *= 2;
59   }
60 
61   if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
62     return 0;
63   win_openwindow("GoatTracker", NULL);
64   win_setmousemode(MOUSE_ALWAYS_HIDDEN);
65   initicon();
66 
67   if (!gfx_init(MAX_COLUMNS * fontwidth, MAX_ROWS * fontheight, 60, 0))
68   {
69     win_fullscreen = 0;
70     if (!gfx_init(MAX_COLUMNS * fontwidth, MAX_ROWS * fontheight, 60, 0))
71       return 0;
72   }
73 
74   scrbuffer = (unsigned*)malloc(MAX_COLUMNS * MAX_ROWS * sizeof(unsigned));
75   prevscrbuffer = (unsigned*)malloc(MAX_COLUMNS * MAX_ROWS * sizeof(unsigned));
76   if ((!scrbuffer) || (!prevscrbuffer)) return 0;
77 
78   memset(region, 0, sizeof region);
79 
80   chardata = (unsigned char*)malloc(4096);
81   if (!chardata) return 0;
82   handle = io_open("chargen.bin");
83   if (handle == -1) return 0;
84   io_read(handle, &chardata[0], 4096);
85   io_close(handle);
86 
87   gfx_loadpalette("palette.bin");
88   loadexternalpalette();
89   gfx_setpalette();
90 
91   if (bigwindow - 1) gfx_loadsprites(0, "bcursor.bin");
92   else gfx_loadsprites(0, "cursor.bin");
93 
94   gfxinitted = 1;
95   clearscreen();
96   atexit(closescreen);
97   return 1;
98 }
99 
loadexternalpalette(void)100 void loadexternalpalette(void)
101 {
102   FILE *ext_f;
103   if ((ext_f = fopen("custom.pal", "rt")))
104   {
105     int p = 0;
106     char ln[100];
107     strcpy(ln, "");
108     fgets(ln, sizeof(ln), ext_f);
109 
110     if (strncmp("JASC-PAL", ln, 8) == 0)
111     {
112       int colors;
113       fgets(ln, sizeof(ln), ext_f);
114       fgets(ln, sizeof(ln), ext_f);
115       if (sscanf(ln, "%d", &colors) == 1 && colors == 256)
116       {
117         while (!feof(ext_f))
118         {
119           int r, g, b;
120           if (!fgets(ln, sizeof(ln), ext_f)) break;
121           if (sscanf(ln, "%d %d %d", &r, &g, &b) == 3)
122           {
123             // JASC palette is 8-bit and goat palette is 6-bit
124             gfx_palette[p++] = r / 4;
125             gfx_palette[p++] = g / 4;
126             gfx_palette[p++] = b / 4;
127           }
128 
129           if (p >= 768) break;
130         }
131         gfx_calcpalette(64, 0, 0, 0);
132       }
133     }
134 
135     fclose(ext_f);
136   }
137 }
138 
initicon(void)139 void initicon(void)
140 {
141   int handle = io_open("goattrk2.bmp");
142   if (handle != -1)
143   {
144     SDL_RWops *rw;
145     SDL_Surface *icon;
146     char *iconbuffer;
147     int size;
148 
149     size = io_lseek(handle, 0, SEEK_END);
150     io_lseek(handle, 0, SEEK_SET);
151     iconbuffer = (char*)malloc(size);
152     if (iconbuffer)
153     {
154       io_read(handle, iconbuffer, size);
155       io_close(handle);
156       rw = SDL_RWFromMem(iconbuffer, size);
157       icon = SDL_LoadBMP_RW(rw, 0);
158       SDL_WM_SetIcon(icon, 0);
159       free(iconbuffer);
160     }
161   }
162 }
closescreen(void)163 void closescreen(void)
164 {
165   if (scrbuffer)
166   {
167     free(scrbuffer);
168     scrbuffer = NULL;
169   }
170   if (prevscrbuffer)
171   {
172     free(prevscrbuffer);
173     prevscrbuffer = NULL;
174   }
175   if (chardata)
176   {
177     free(chardata);
178     chardata = NULL;
179   }
180 
181   gfxinitted = 0;
182 }
183 
clearscreen(void)184 void clearscreen(void)
185 {
186   int c;
187   unsigned *dptr = scrbuffer;
188 
189   if (!gfxinitted) return;
190 
191   for (c = 0; c < MAX_ROWS * MAX_COLUMNS; c++)
192   {
193     setcharcolor(dptr, 0x20, 0x7);
194     dptr++;
195   }
196 }
197 
printtext(int x,int y,int color,const char * text)198 void printtext(int x, int y, int color, const char *text)
199 {
200   unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS);
201 
202   if (!gfxinitted) return;
203   if (y < 0) return;
204   if (y >= MAX_ROWS) return;
205   while (*text)
206   {
207     setcharcolor(dptr, *text, color);
208     dptr++;
209     text++;
210   }
211 }
212 
printtextc(int y,int color,const char * text)213 void printtextc(int y, int color, const char *text)
214 {
215   int x = (MAX_COLUMNS - strlen(text)) / 2;
216 
217   printtext(x, y, color, text);
218 }
219 
printtextcp(int cp,int y,int color,const char * text)220 void printtextcp(int cp, int y, int color, const char *text)
221 {
222   int x = cp - (strlen(text) / 2);
223 
224   printtext(x, y, color, text);
225 }
226 
227 
printblank(int x,int y,int length)228 void printblank(int x, int y, int length)
229 {
230   unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS);
231 
232   if (!gfxinitted) return;
233   if (y < 0) return;
234   if (y >= MAX_ROWS) return;
235   while (length--)
236   {
237     setcharcolor(dptr, 0x20, 0x7);
238     dptr++;
239   }
240 }
241 
printblankc(int x,int y,int color,int length)242 void printblankc(int x, int y, int color, int length)
243 {
244   unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS);
245 
246   if (!gfxinitted) return;
247   if (y < 0) return;
248   if (y >= MAX_ROWS) return;
249   while (length--)
250   {
251     setcharcolor(dptr, 0x20, color);
252     dptr++;
253   }
254 }
255 
drawbox(int x,int y,int color,int sx,int sy)256 void drawbox(int x, int y, int color, int sx, int sy)
257 {
258   unsigned *dptr;
259   unsigned *dptr2;
260   int counter;
261 
262   if (!gfxinitted) return;
263   if (y < 0) return;
264   if (y >= MAX_ROWS) return;
265   if (y+sy > MAX_ROWS) return;
266   if ((!sx) || (!sy)) return;
267 
268   dptr = scrbuffer + (x + y * MAX_COLUMNS);
269   dptr2 = scrbuffer + ((x+sx-1) + y * MAX_COLUMNS);
270   counter = sy;
271 
272   while (counter--)
273   {
274     setcharcolor(dptr, '|', color);
275     setcharcolor(dptr2, '|', color);
276     dptr += MAX_COLUMNS;
277     dptr2 += MAX_COLUMNS;
278   }
279 
280   dptr = scrbuffer + (x + y * MAX_COLUMNS);
281   dptr2 = scrbuffer + (x + (y+sy-1) * MAX_COLUMNS);
282   counter = sx;
283 
284   while (counter--)
285   {
286     setcharcolor(dptr, '-', color);
287     setcharcolor(dptr2, '-', color);
288     dptr++;
289     dptr2++;
290   }
291 
292   dptr = scrbuffer + (x + y * MAX_COLUMNS);
293   setcharcolor(dptr, '+', color);
294 
295   dptr = scrbuffer + ((x+sx-1) + y * MAX_COLUMNS);
296   setcharcolor(dptr, '+', color);
297 
298   dptr = scrbuffer + (x + (y+sy-1) * MAX_COLUMNS);
299   setcharcolor(dptr, '+', color);
300 
301   dptr = scrbuffer + ((x+sx-1) + (y+sy-1) * MAX_COLUMNS);
302   setcharcolor(dptr, '+', color);
303 }
304 
printbg(int x,int y,int color,int length)305 void printbg(int x, int y, int color, int length)
306 {
307   unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS);
308 
309   if (!gfxinitted) return;
310   if (y < 0) return;
311   if (y >= MAX_ROWS) return;
312   while (length--)
313   {
314     setcolor(dptr, 15 | (color << 4));
315     dptr++;
316   }
317 }
318 
fliptoscreen(void)319 void fliptoscreen(void)
320 {
321   unsigned *sptr = scrbuffer;
322   unsigned *cmpptr = prevscrbuffer;
323   int x,y;
324   int regionschanged = 0;
325 
326   if (!gfxinitted) return;
327 
328   // Mark previous mousecursor area changed if mouse moved
329   if ((mousepixelx != oldmousepixelx) || (mousepixely != oldmousepixely))
330   {
331     if ((oldmousepixelx >= 0) && (oldmousepixely >= 0))
332     {
333       int sy = oldmousepixely / fontheight;
334       int ey = (oldmousepixely + mousesizey - 1) / fontheight;
335       int sx = oldmousepixelx / fontwidth;
336       int ex = (oldmousepixelx + mousesizex - 1) / fontwidth;
337 
338       if (ey >= MAX_ROWS) ey = MAX_ROWS - 1;
339       if (ex >= MAX_COLUMNS) ex = MAX_COLUMNS - 1;
340 
341       for (y = sy; y <= ey; y++)
342       {
343         for (x = sx; x <= ex; x++)
344           prevscrbuffer[y*MAX_COLUMNS+x] = 0xffffffff;
345       }
346     }
347   }
348 
349   // If redraw requested, mark whole screen changed
350   if (gfx_redraw)
351   {
352     gfx_redraw = 0;
353     memset(prevscrbuffer, 0xff, MAX_COLUMNS*MAX_ROWS*sizeof(unsigned));
354   }
355 
356   if (!gfx_lock()) return;
357 
358   // Now redraw text on changed areas
359   for (y = 0; y < MAX_ROWS; y++)
360   {
361     for (x = 0; x < MAX_COLUMNS; x++)
362     {
363       // Check if char changed
364       if (*sptr != *cmpptr)
365       {
366         *cmpptr = *sptr;
367         region[y] = 1;
368         regionschanged = 1;
369 
370         {
371           unsigned char *chptr = &chardata[(*sptr & 0xffff)*16];
372           unsigned char *dptr = (unsigned char*)gfx_screen->pixels + y*fontheight * gfx_screen->pitch + x*fontwidth;
373           unsigned char bgcolor = (*sptr) >> 20;
374           unsigned char fgcolor = ((*sptr) >> 16) & 0xf;
375           int c;
376           unsigned char e = *chptr++;
377 
378           for (c = 0; c < 14; c++)
379           {
380             e = *chptr++;
381 
382             switch (bigwindow)
383             {
384               case 1:
385                 if (e & 128) *dptr++ = fgcolor;
386                 else *dptr++ = bgcolor;
387                 if (e & 64) *dptr++ = fgcolor;
388                 else *dptr++ = bgcolor;
389                 if (e & 32) *dptr++ = fgcolor;
390                 else *dptr++ = bgcolor;
391                 if (e & 16) *dptr++ = fgcolor;
392                 else *dptr++ = bgcolor;
393                 if (e & 8) *dptr++ = fgcolor;
394                 else *dptr++ = bgcolor;
395                 if (e & 4) *dptr++ = fgcolor;
396                 else *dptr++ = bgcolor;
397                 if (e & 2) *dptr++ = fgcolor;
398                 else *dptr++ = bgcolor;
399                 if (e & 1) *dptr++ = fgcolor;
400                 else *dptr++ = bgcolor;
401                 dptr += gfx_screen->pitch - fontwidth;
402               break;
403               case 2:
404                 if (e & 128) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
405                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
406                 if (e & 64) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
407                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
408                 if (e & 32) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
409                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
410                 if (e & 16) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
411                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
412                 if (e & 8) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
413                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
414                 if (e & 4) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
415                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
416                 if (e & 2) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
417                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
418                 if (e & 1) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
419                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
420                 dptr += gfx_screen->pitch - fontwidth;
421                 if (e & 128) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
422                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
423                 if (e & 64) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
424                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
425                 if (e & 32) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
426                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
427                 if (e & 16) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
428                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
429                 if (e & 8) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
430                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
431                 if (e & 4) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
432                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
433                 if (e & 2) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
434                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
435                 if (e & 1) {*dptr++ = fgcolor; *dptr++ = fgcolor;}
436                 else {*dptr++ = bgcolor; *dptr++ = bgcolor;}
437                 dptr += gfx_screen->pitch - fontwidth;
438               break;
439               case 3:
440                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
441                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
442                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
443                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
444                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
445                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
446                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
447                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
448                 dptr += gfx_screen->pitch - fontwidth;
449                 if (e & 128) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
450                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
451                 if (e & 64) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
452                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
453                 if (e & 32) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
454                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
455                 if (e & 16) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
456                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
457                 if (e & 8) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
458                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
459                 if (e & 4) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
460                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
461                 if (e & 2) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
462                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
463                 if (e & 1) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
464                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
465                 dptr += gfx_screen->pitch - fontwidth;
466                 if (e & 128) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
467                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
468                 if (e & 64) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
469                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
470                 if (e & 32) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
471                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
472                 if (e & 16) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
473                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
474                 if (e & 8) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
475                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
476                 if (e & 4) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
477                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
478                 if (e & 2) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
479                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
480                 if (e & 1) {*dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
481                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
482                 dptr += gfx_screen->pitch - fontwidth;
483               break;
484               case 4:
485                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
486                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
487                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
488                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
489                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
490                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
491                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
492                 *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;
493                 dptr += gfx_screen->pitch - fontwidth;
494                 if (e & 128) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
495                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
496                 if (e & 64) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
497                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
498                 if (e & 32) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
499                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
500                 if (e & 16) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
501                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
502                 if (e & 8) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
503                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
504                 if (e & 4) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
505                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
506                 if (e & 2) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
507                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
508                 if (e & 1) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
509                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
510                 dptr += gfx_screen->pitch - fontwidth;
511                 if (e & 128) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
512                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
513                 if (e & 64) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
514                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
515                 if (e & 32) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
516                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
517                 if (e & 16) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
518                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
519                 if (e & 8) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
520                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
521                 if (e & 4) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
522                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
523                 if (e & 2) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
524                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
525                 if (e & 1) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
526                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
527                 dptr += gfx_screen->pitch - fontwidth;
528                 if (e & 128) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
529                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
530                 if (e & 64) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
531                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
532                 if (e & 32) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
533                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
534                 if (e & 16) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
535                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
536                 if (e & 8) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
537                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
538                 if (e & 4) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
539                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
540                 if (e & 2) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
541                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
542                 if (e & 1) {*dptr++ = bgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor; *dptr++ = fgcolor;}
543                 else {*dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor; *dptr++ = bgcolor;}
544                 dptr += gfx_screen->pitch - fontwidth;
545               break;
546             }
547           }
548         }
549       }
550       sptr++;
551       cmpptr++;
552     }
553   }
554 
555 
556   // Redraw mouse if text was redrawn
557   if (regionschanged)
558   {
559     int sy = mousepixely / fontheight;
560     int ey = (mousepixely + mousesizey - 1) / fontheight;
561     if (ey >= MAX_ROWS) ey = MAX_ROWS - 1;
562 
563     gfx_drawsprite(mousepixelx, mousepixely, 0x1);
564     for (y = sy; y <= ey; y++)
565       region[y] = 1;
566   }
567 
568   // Store current mouse position as old
569   oldmousepixelx = mousepixelx;
570   oldmousepixely = mousepixely;
571 
572   // Redraw changed screen regions
573   gfx_unlock();
574   for (y = 0; y < MAX_ROWS; y++)
575   {
576     if (region[y])
577     {
578       SDL_UpdateRect(gfx_screen, 0, y*fontheight, MAX_COLUMNS*fontwidth, fontheight);
579       region[y] = 0;
580     }
581   }
582 }
583 
getkey(void)584 void getkey(void)
585 {
586   int c;
587   win_asciikey = 0;
588   cursorflashdelay += win_getspeed(50);
589 
590   prevmouseb = mouseb;
591 
592   mou_getpos(&mousepixelx, &mousepixely);
593   mouseb = mou_getbuttons();
594   mousex = mousepixelx / fontwidth;
595   mousey = mousepixely / fontheight;
596 
597   if (mouseb) mouseheld++;
598   else mouseheld = 0;
599 
600   key = win_asciikey;
601   rawkey = 0;
602   for (c = 0; c < MAX_KEYS; c++)
603   {
604     if (win_keytable[c])
605     {
606       if ((c != KEY_LEFTSHIFT) && (c != KEY_RIGHTSHIFT) &&
607           (c != KEY_CTRL) && (c != KEY_RIGHTCTRL))
608       {
609         rawkey = c;
610         win_keytable[c] = 0;
611         break;
612       }
613     }
614   }
615 
616   shiftpressed = 0;
617   if ((win_keystate[KEY_LEFTSHIFT])||(win_keystate[KEY_RIGHTSHIFT])||
618       (win_keystate[KEY_CTRL])||(win_keystate[KEY_RIGHTCTRL]))
619     shiftpressed = 1;
620 
621   if (rawkey == SDLK_KP_ENTER)
622   {
623     key = KEY_ENTER;
624     rawkey = SDLK_RETURN;
625   }
626 
627   if (rawkey == SDLK_KP0) key = '0';
628   if (rawkey == SDLK_KP1) key = '1';
629   if (rawkey == SDLK_KP2) key = '2';
630   if (rawkey == SDLK_KP3) key = '3';
631   if (rawkey == SDLK_KP4) key = '4';
632   if (rawkey == SDLK_KP5) key = '5';
633   if (rawkey == SDLK_KP6) key = '6';
634   if (rawkey == SDLK_KP7) key = '7';
635   if (rawkey == SDLK_KP8) key = '8';
636   if (rawkey == SDLK_KP9) key = '9';
637 }
638