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