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