home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.73.zip / src / gconsole.c < prev    next >
C/C++ Source or Header  |  2014-07-23  |  11KB  |  476 lines

  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.  
  31. void loadexternalpalette(void);
  32. void initicon(void);
  33.  
  34. inline void setcharcolor(unsigned *dptr, short ch, short color)
  35. {
  36.   *dptr = (ch & 0xff) | (color << 16);
  37. }
  38.  
  39. inline void setcolor(unsigned *dptr, short color)
  40. {
  41.   *dptr = (*dptr & 0xffff) | (color << 16);
  42. }
  43.  
  44. int initscreen(void)
  45. {
  46.   int handle;
  47.  
  48.   if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
  49.     return 0;
  50.   win_openwindow("GoatTracker", NULL);
  51.   win_setmousemode(MOUSE_ALWAYS_HIDDEN);
  52.   initicon();
  53.  
  54.   if (!gfx_init(MAX_COLUMNS * 8, MAX_ROWS * 16, 60, 0))
  55.   {
  56.     win_fullscreen = 0;
  57.     if (!gfx_init(MAX_COLUMNS * 8, MAX_ROWS * 16, 60, 0))
  58.       return 0;
  59.   }
  60.    
  61.   scrbuffer = malloc(MAX_COLUMNS * MAX_ROWS * sizeof(unsigned));
  62.   prevscrbuffer = malloc(MAX_COLUMNS * MAX_ROWS * sizeof(unsigned));
  63.   if ((!scrbuffer) || (!prevscrbuffer)) return 0;
  64.  
  65.   memset(region, 0, sizeof region);
  66.  
  67.   chardata = malloc(4096);
  68.   if (!chardata) return 0;
  69.   handle = io_open("chargen.bin");
  70.   if (handle == -1) return 0;
  71.   io_read(handle, &chardata[0], 4096);
  72.   io_close(handle);
  73.  
  74.   gfx_loadpalette("palette.bin");
  75.   loadexternalpalette();
  76.   gfx_setpalette();
  77.  
  78.   gfx_loadsprites(0, "cursor.bin");
  79.  
  80.   gfxinitted = 1;
  81.   clearscreen();
  82.   atexit(closescreen);
  83.   return 1;
  84. }
  85.  
  86. void loadexternalpalette(void)
  87. {
  88.   FILE *ext_f;
  89.   if ((ext_f = fopen("custom.pal", "rt")))
  90.   {
  91.     int p = 0;
  92.     char ln[100];
  93.     strcpy(ln, "");
  94.     fgets(ln, sizeof(ln), ext_f);
  95.  
  96.     if (strncmp("JASC-PAL", ln, 8) == 0)
  97.     {
  98.       int colors;
  99.       fgets(ln, sizeof(ln), ext_f);
  100.       fgets(ln, sizeof(ln), ext_f);
  101.       if (sscanf(ln, "%d", &colors) == 1 && colors == 256)
  102.       {
  103.         while (!feof(ext_f))
  104.         {
  105.           int r, g, b;
  106.           if (!fgets(ln, sizeof(ln), ext_f)) break;
  107.           if (sscanf(ln, "%d %d %d", &r, &g, &b) == 3)
  108.           {
  109.             // JASC palette is 8-bit and goat palette is 6-bit
  110.             gfx_palette[p++] = r / 4;
  111.             gfx_palette[p++] = g / 4;
  112.             gfx_palette[p++] = b / 4;
  113.           }
  114.  
  115.           if (p >= 768) break;
  116.         }
  117.         gfx_calcpalette(64, 0, 0, 0);
  118.       }
  119.     }
  120.  
  121.     fclose(ext_f);
  122.   }
  123. }
  124.  
  125. void initicon(void)
  126. {
  127.   int handle = io_open("goattrk2.bmp");
  128.   if (handle != -1)
  129.   {
  130.     SDL_RWops *rw;
  131.     SDL_Surface *icon;
  132.     char *iconbuffer;
  133.     int size;
  134.  
  135.     size = io_lseek(handle, 0, SEEK_END);
  136.     io_lseek(handle, 0, SEEK_SET);
  137.     iconbuffer = malloc(size);
  138.     if (iconbuffer)
  139.     {
  140.       io_read(handle, iconbuffer, size);
  141.       io_close(handle);
  142.       rw = SDL_RWFromMem(iconbuffer, size);
  143.       icon = SDL_LoadBMP_RW(rw, 0);
  144.       SDL_WM_SetIcon(icon, 0);
  145.       free(iconbuffer);
  146.     }
  147.   }
  148. }
  149. void closescreen(void)
  150. {
  151.   if (scrbuffer)
  152.   {
  153.     free(scrbuffer);
  154.     scrbuffer = NULL;
  155.   }
  156.   if (prevscrbuffer)
  157.   {
  158.     free(prevscrbuffer);
  159.     prevscrbuffer = NULL;
  160.   }
  161.   if (chardata)
  162.   {
  163.     free(chardata);
  164.     chardata = NULL;
  165.   }
  166.  
  167.   gfxinitted = 0;
  168. }
  169.  
  170. void clearscreen(void)
  171. {
  172.   int c;
  173.   unsigned *dptr = scrbuffer;
  174.  
  175.   if (!gfxinitted) return;
  176.  
  177.   for (c = 0; c < MAX_ROWS * MAX_COLUMNS; c++)
  178.   {
  179.     setcharcolor(dptr, 0x20, 0x7);
  180.     dptr++;
  181.   }
  182. }
  183.  
  184. void printtext(int x, int y, int color, const char *text)
  185. {
  186.   unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS);
  187.  
  188.   if (!gfxinitted) return;
  189.   if (y < 0) return;
  190.   if (y >= MAX_ROWS) return;
  191.   while (*text)
  192.   {
  193.     setcharcolor(dptr, *text, color);
  194.     dptr++;
  195.     text++;
  196.   }
  197. }
  198.  
  199. void printtextc(int y, int color, const char *text)
  200. {
  201.   int x = (MAX_COLUMNS - strlen(text)) / 2;
  202.  
  203.   printtext(x, y, color, text);
  204. }
  205.  
  206. void printtextcp(int cp, int y, int color, const char *text)
  207. {
  208.   int x = cp - (strlen(text) / 2);
  209.  
  210.   printtext(x, y, color, text);
  211. }
  212.  
  213.  
  214. void printblank(int x, int y, int length)
  215. {
  216.   unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS);
  217.  
  218.   if (!gfxinitted) return;
  219.   if (y < 0) return;
  220.   if (y >= MAX_ROWS) return;
  221.   while (length--)
  222.   {
  223.     setcharcolor(dptr, 0x20, 0x7);
  224.     dptr++;
  225.   }
  226. }
  227.  
  228. void printblankc(int x, int y, int color, 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, color);
  238.     dptr++;
  239.   }
  240. }
  241.  
  242. void drawbox(int x, int y, int color, int sx, int sy)
  243. {
  244.   unsigned *dptr;
  245.   unsigned *dptr2;
  246.   int counter;
  247.  
  248.   if (!gfxinitted) return;
  249.   if (y < 0) return;
  250.   if (y >= MAX_ROWS) return;
  251.   if (y+sy > MAX_ROWS) return;
  252.   if ((!sx) || (!sy)) return;
  253.  
  254.   dptr = scrbuffer + (x + y * MAX_COLUMNS);
  255.   dptr2 = scrbuffer + ((x+sx-1) + y * MAX_COLUMNS);
  256.   counter = sy;
  257.  
  258.   while (counter--)
  259.   {
  260.     setcharcolor(dptr, '|', color);
  261.     setcharcolor(dptr2, '|', color);
  262.     dptr += MAX_COLUMNS;
  263.     dptr2 += MAX_COLUMNS;
  264.   }
  265.  
  266.   dptr = scrbuffer + (x + y * MAX_COLUMNS);
  267.   dptr2 = scrbuffer + (x + (y+sy-1) * MAX_COLUMNS);
  268.   counter = sx;
  269.  
  270.   while (counter--)
  271.   {
  272.     setcharcolor(dptr, '-', color);
  273.     setcharcolor(dptr2, '-', color);
  274.     dptr++;
  275.     dptr2++;
  276.   }
  277.  
  278.   dptr = scrbuffer + (x + y * MAX_COLUMNS);
  279.   setcharcolor(dptr, '+', color);
  280.  
  281.   dptr = scrbuffer + ((x+sx-1) + y * MAX_COLUMNS);
  282.   setcharcolor(dptr, '+', color);
  283.  
  284.   dptr = scrbuffer + (x + (y+sy-1) * MAX_COLUMNS);
  285.   setcharcolor(dptr, '+', color);
  286.  
  287.   dptr = scrbuffer + ((x+sx-1) + (y+sy-1) * MAX_COLUMNS);
  288.   setcharcolor(dptr, '+', color);
  289. }
  290.  
  291. void printbg(int x, int y, int color, int length)
  292. {
  293.   unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS);
  294.  
  295.   if (!gfxinitted) return;
  296.   if (y < 0) return;
  297.   if (y >= MAX_ROWS) return;
  298.   while (length--)
  299.   {
  300.     setcolor(dptr, 15 | (color << 4));
  301.     dptr++;
  302.   }
  303. }
  304.  
  305. void fliptoscreen(void)
  306. {
  307.   unsigned *sptr = scrbuffer;
  308.   unsigned *cmpptr = prevscrbuffer;
  309.   int x,y;
  310.   int regionschanged = 0;
  311.  
  312.   if (!gfxinitted) return;
  313.  
  314.   // Mark previous mousecursor area changed if mouse moved
  315.   if ((mousepixelx != oldmousepixelx) || (mousepixely != oldmousepixely))
  316.   {
  317.     if ((oldmousepixelx >= 0) && (oldmousepixely >= 0))
  318.     {
  319.       int sy = oldmousepixely >> 4;
  320.       int ey = (oldmousepixely + MOUSESIZEY - 1) >> 4;
  321.       int sx = oldmousepixelx >> 3;
  322.       int ex = (oldmousepixelx + MOUSESIZEX - 1) >> 3;
  323.  
  324.       if (ey >= MAX_ROWS) ey = MAX_ROWS - 1;
  325.       if (ex >= MAX_COLUMNS) ex = MAX_COLUMNS - 1;
  326.  
  327.       for (y = sy; y <= ey; y++)
  328.       {
  329.         for (x = sx; x <= ex; x++)
  330.           prevscrbuffer[y*MAX_COLUMNS+x] = 0xffffffff;
  331.       }
  332.     }
  333.   }
  334.  
  335.   // If redraw requested, mark whole screen changed
  336.   if (gfx_redraw)
  337.   {
  338.     gfx_redraw = 0;
  339.     memset(prevscrbuffer, 0xff, MAX_COLUMNS*MAX_ROWS*sizeof(unsigned));
  340.   }
  341.  
  342.   if (!gfx_lock()) return;
  343.  
  344.   // Now redraw text on changed areas
  345.   for (y = 0; y < MAX_ROWS; y++)
  346.   {
  347.     for (x = 0; x < MAX_COLUMNS; x++)
  348.     {
  349.       // Check if char changed
  350.       if (*sptr != *cmpptr)
  351.       {
  352.         *cmpptr = *sptr;
  353.         region[y] = 1;
  354.         regionschanged = 1;
  355.  
  356.         {
  357.           unsigned char *chptr = &chardata[(*sptr & 0xffff)*16];
  358.           unsigned char *dptr = gfx_screen->pixels + y*16 * gfx_screen->pitch + x*8;
  359.           unsigned char bgcolor = (*sptr) >> 20;
  360.           unsigned char fgcolor = ((*sptr) >> 16) & 0xf;
  361.           int c;
  362.  
  363.           for (c = 0; c < 16; c++)
  364.           {
  365.             unsigned char e = *chptr++;
  366.  
  367.             if (e & 128) *dptr++ = fgcolor;
  368.             else *dptr++ = bgcolor;
  369.             if (e & 64) *dptr++ = fgcolor;
  370.             else *dptr++ = bgcolor;
  371.             if (e & 32) *dptr++ = fgcolor;
  372.             else *dptr++ = bgcolor;
  373.             if (e & 16) *dptr++ = fgcolor;
  374.             else *dptr++ = bgcolor;
  375.             if (e & 8) *dptr++ = fgcolor;
  376.             else *dptr++ = bgcolor;
  377.             if (e & 4) *dptr++ = fgcolor;
  378.             else *dptr++ = bgcolor;
  379.             if (e & 2) *dptr++ = fgcolor;
  380.             else *dptr++ = bgcolor;
  381.             if (e & 1) *dptr++ = fgcolor;
  382.             else *dptr++ = bgcolor;
  383.  
  384.             dptr += gfx_screen->pitch - 8;
  385.           }
  386.         }
  387.       }
  388.       sptr++;
  389.       cmpptr++;
  390.     }
  391.   }
  392.  
  393.  
  394.   // Redraw mouse if text was redrawn
  395.   if (regionschanged)
  396.   {
  397.     int sy = mousepixely >> 4;
  398.     int ey = (mousepixely + MOUSESIZEY - 1) >> 4;
  399.     if (ey >= MAX_ROWS) ey = MAX_ROWS - 1;
  400.  
  401.     gfx_drawsprite(mousepixelx, mousepixely, 0x1);
  402.     for (y = sy; y <= ey; y++)
  403.       region[y] = 1;
  404.   }
  405.  
  406.   // Store current mouse position as old
  407.   oldmousepixelx = mousepixelx;
  408.   oldmousepixely = mousepixely;
  409.  
  410.   // Redraw changed screen regions
  411.   gfx_unlock();
  412.   for (y = 0; y < MAX_ROWS; y++)
  413.   {
  414.     if (region[y])
  415.     {
  416.       SDL_UpdateRect(gfx_screen, 0, y*16, MAX_COLUMNS*8, 16);
  417.       region[y] = 0;
  418.     }
  419.   }
  420. }
  421.  
  422. void getkey(void)
  423. {
  424.   int c;
  425.   win_asciikey = 0;
  426.   cursorflashdelay += win_getspeed(50);
  427.  
  428.   prevmouseb = mouseb;
  429.  
  430.   mou_getpos(&mousepixelx, &mousepixely);
  431.   mouseb = mou_getbuttons();
  432.   mousex = mousepixelx / 8;
  433.   mousey = mousepixely / 16;
  434.  
  435.   if (mouseb) mouseheld++;
  436.   else mouseheld = 0;
  437.  
  438.   key = win_asciikey;
  439.   rawkey = 0;
  440.   for (c = 0; c < MAX_KEYS; c++)
  441.   {
  442.     if (win_keytable[c])
  443.     {
  444.       if ((c != KEY_LEFTSHIFT) && (c != KEY_RIGHTSHIFT) &&
  445.           (c != KEY_CTRL) && (c != KEY_RIGHTCTRL))
  446.       {
  447.         rawkey = c;
  448.         win_keytable[c] = 0;
  449.         break;
  450.       }
  451.     }
  452.   }
  453.  
  454.   shiftpressed = 0;
  455.   if ((win_keystate[KEY_LEFTSHIFT])||(win_keystate[KEY_RIGHTSHIFT])||
  456.       (win_keystate[KEY_CTRL])||(win_keystate[KEY_RIGHTCTRL]))
  457.     shiftpressed = 1;
  458.  
  459.   if (rawkey == SDLK_KP_ENTER)
  460.   {
  461.     key = KEY_ENTER;
  462.     rawkey = SDLK_RETURN;
  463.   }
  464.  
  465.   if (rawkey == SDLK_KP0) key = '0';
  466.   if (rawkey == SDLK_KP1) key = '1';
  467.   if (rawkey == SDLK_KP2) key = '2';
  468.   if (rawkey == SDLK_KP3) key = '3';
  469.   if (rawkey == SDLK_KP4) key = '4';
  470.   if (rawkey == SDLK_KP5) key = '5';
  471.   if (rawkey == SDLK_KP6) key = '6';
  472.   if (rawkey == SDLK_KP7) key = '7';
  473.   if (rawkey == SDLK_KP8) key = '8';
  474.   if (rawkey == SDLK_KP9) key = '9';
  475. }
  476.