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