_emx.c
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 11k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  _emx.c
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. /* basic graphic routines for MSDOS
  12.  * implemented using the emx graphics library (jmgraph.a)
  13.  */
  14. #include <jmgraph.h>
  15. #include <stdio.h>
  16. static int MODE;
  17. static int STYLE;
  18. static int WIDTH;
  19. static int COLOR;
  20. static int win_xmin;  // coordinates of current window
  21. static int win_ymin;
  22. static int win_xmax; 
  23. static int win_ymax;
  24. void init_graphics(int mode, int col)
  25. {
  26.   if (mode == 1) // graphics mode
  27.   { 
  28.      if (!g_mode(G640x480x16))
  29.      { fprintf (stderr, "Cannot switch to graphics mode 640x480x16n");
  30.        exit(1);
  31.       }
  32.     char pal[48];
  33.     char* p = pal;
  34.     for(int i=0; i<16; i++) 
  35.       { *p++ = _R_[i];
  36.         *p++ = _G_[i];
  37.         *p++ = _B_[i];
  38.        }
  39.       g_vgapal(pal,0,16,1);
  40.     }
  41.   else // text mode
  42.     g_mode(GTEXT);
  43.   DISP_WIDTH  = g_xsize;
  44.   DISP_HEIGHT = g_ysize;
  45.   DISP_DEPTH  = g_colors;
  46.   DISP_MAX_X  = DISP_WIDTH-1;
  47.   DISP_MAX_Y  = DISP_HEIGHT-1;
  48.   MODE  = 0;
  49.   STYLE = 0;
  50.   WIDTH = 1;
  51.   COLOR = 1;
  52.   win_xmin = 0;
  53.   win_ymin = 0;
  54.   win_xmax = DISP_MAX_X;
  55.   win_ymax = DISP_MAX_Y;
  56.   
  57. }
  58. static void set_draw_window(Window w)
  59. { DosWindow win = win_stack[w];
  60.   win_xmin = win->xpos;
  61.   win_ymin = win->ypos;
  62.   win_xmax = win_xmin + win->width  - 1;
  63.   win_ymax = win_ymin + win->height - 1;
  64.   g_clip(win_xmin,win_ymin,win_xmax,win_ymax);
  65.  }
  66. void flush_display() {}
  67. int  new_color(const char*) { return 1; }
  68. int  text_height(const char*)  { return FONT_HEIGHT; }
  69. int  text_width(const char* s) { return FONT_WIDTH*strlen(s); }
  70. int  load_text_font(const char*) { return 0;}
  71. int  load_bold_font(const char*) { return 0;}
  72. int  load_message_font(const char*) { return 0;}
  73. int  set_font(const char*) { return 0;}
  74. void set_text_font() {}
  75. void set_bold_font() {}
  76. void set_message_font() {}
  77. int set_line_width(int width) 
  78. { int save = WIDTH;
  79.   WIDTH = width;
  80.   return save; 
  81.  }
  82. int set_line_style(int style) 
  83. { int save = STYLE;
  84.   STYLE = style;
  85.   switch(style) {
  86.   case solid : //_setlinestyle(0xFFFF);
  87.                break;
  88.   case dotted: //_setlinestyle(0x3333);
  89.                break;
  90.   case dashed: //_setlinestyle(0x0F0F);
  91.                break;
  92.   }
  93.   return save;
  94.  }
  95. int set_color(int col) 
  96. { int c = COLOR;
  97.   COLOR = col;
  98.   return c; 
  99.  }
  100. int set_mode(int mode)
  101. { int save = MODE;
  102.   MODE = mode;
  103.   switch(mode) {
  104.    case 0: g_wmode(G_NORM);
  105.            break;
  106.    case 1: g_wmode(G_XOR);
  107.            break;
  108.    case 2: g_wmode(G_OR);
  109.            break;
  110.    case 3: g_wmode(G_AND);
  111.            break;
  112.   }
  113.   return save;
  114. }
  115. void set_redraw(Window w, void (*f)())
  116. { win_stack[w]->redraw = f; }
  117. void line(Window w, int x1, int y1, int x2, int y2)
  118. { set_draw_window(w);
  119.   x1 += win_xmin;
  120.   x2 += win_xmin;
  121.   y1 += win_ymin;
  122.   y2 += win_ymin;
  123.   g_line(x1,y1,x2,y2,COLOR);
  124.   for(int i = 2; i <= WIDTH; i++)
  125.   { int dx = (x1 > x2) ? x1-x2 : x2-x1;
  126.     int dy = (y1 > y2) ? y1-y2 : y2-y1;
  127.     int d = (i&1) ? i/2 : -i/2;
  128.     if (dx < dy)
  129.        g_line(x1+d,y1,x2+d,y2,COLOR);
  130.     else
  131.        g_line(x1,y1+d,x2,y2+d,COLOR);
  132.    }
  133. }
  134. void fill_polygon(Window w, int n, int* xc, int* yc)
  135. { set_draw_window(w);
  136.   for (int i = 0; i<n; i++)
  137.   { xc[i] += win_xmin;
  138.     yc[i] += win_ymin;
  139.    }
  140.   g_polygon(xc,yc,n,COLOR,G_FILL);
  141.  }
  142. void box(Window w, int x0, int y0, int x1, int y1)
  143. { set_draw_window(w);
  144.   x0 += win_xmin;
  145.   x1 += win_xmin;
  146.   y0 += win_ymin;
  147.   y1 += win_ymin;
  148.   g_box(x0,y0,x1,y1,COLOR,G_FILL);
  149.  }
  150. void  rectangle(Window w, int x0, int y0, int x1, int y1)
  151. { set_draw_window(w);
  152.   x0 += win_xmin;
  153.   x1 += win_xmin;
  154.   y0 += win_ymin;
  155.   y1 += win_ymin;
  156.   g_box(x0,y0,x1,y1,COLOR,G_OUTLINE);
  157.  }
  158. void circle(Window w, int x0,int y0,int r)
  159. { set_draw_window(w);
  160.   x0 += win_xmin;
  161.   y0 += win_ymin;
  162.   g_ellipse(x0,y0,r,r,COLOR,G_OUTLINE);
  163.  }
  164. void fill_circle(Window w, int x0, int y0, int r)
  165. { set_draw_window(w);
  166.   x0 += win_xmin;
  167.   y0 += win_ymin;
  168.   g_ellipse(x0,y0,r,r,COLOR,G_FILL);
  169.  }
  170. void put_text(Window w, int x, int y0, const char *text, int opaque)
  171.   set_draw_window(w);
  172.   x  += win_xmin;
  173.   y0 += win_ymin;
  174.   int y1 = y0 + FONT_HEIGHT;
  175.   if (opaque)
  176.   { int bg_col = win_stack[w]->bg_col;
  177.     int save = set_mode(0);
  178.     g_box(x,y0,x+strlen(text)*FONT_WIDTH-1,y0+FONT_HEIGHT,bg_col,G_FILL);
  179.     set_mode(save);
  180.    }
  181.   const char* stop = text + strlen(text);
  182.   for (const char* p = text; p < stop; p++)
  183.   { unsigned char* q =  FONT + (*p & 127) * FONT_HEIGHT;
  184.     for(int y=y0; y<y1; y++)
  185.     { unsigned char pat = *q++;
  186.       if (pat & 128) g_set(x  ,y,COLOR);
  187.       if (pat &  64) g_set(x+1,y,COLOR);
  188.       if (pat &  32) g_set(x+2,y,COLOR);
  189.       if (pat &  16) g_set(x+3,y,COLOR);
  190.       if (pat &   8) g_set(x+4,y,COLOR);
  191.       if (pat &   4) g_set(x+5,y,COLOR);
  192.       if (pat &   2) g_set(x+6,y,COLOR);
  193.       if (pat &   1) g_set(x+7,y,COLOR);
  194.      }
  195.     x += 8;
  196.    }
  197. }
  198. void put_text(Window w, int x, int y, const char *text, int l, int opaque)
  199. { char* str = new char[strlen(text)+1];
  200.   strcpy(str,text);
  201.   str[l] = '';
  202.   put_text(w,x,y,text,l,opaque);
  203.   delete[] str;
  204. }
  205. void put_ctext(Window w, int x, int y, const char* str, int opaque)
  206. { put_text(w,x-(text_width(str)-1)/2, y-(text_height(str)-1)/2, str, opaque); }
  207. void show_coordinates(Window w, const char* s)
  208. { DosWindow win = win_stack[w];
  209.   set_draw_window(w);
  210.   int save_mode = set_mode(1);
  211.   int save_col = set_color(blue);
  212.   put_text(w,win->width-138,1,s,1); 
  213.   set_color(save_col);
  214.   set_mode(save_mode);
  215. }
  216. void pixel(Window w, int x, int y) 
  217. { set_draw_window(w);
  218.   x += win_xmin;
  219.   y += win_ymin;
  220.   g_set(x,y,COLOR);
  221. }
  222. void point(Window w, int x, int y) 
  223. { set_draw_window(w);
  224.   x += win_xmin;
  225.   y += win_ymin;
  226.   g_set(x,y,COLOR);
  227.   g_set(x-2,y-2,COLOR);
  228.   g_set(x-1,y-1,COLOR);
  229.   g_set(x+1,y+1,COLOR);
  230.   g_set(x+2,y+2,COLOR);
  231.   g_set(x-2,y+2,COLOR);
  232.   g_set(x-1,y+1,COLOR);
  233.   g_set(x+1,y-1,COLOR);
  234.   g_set(x+2,y-2,COLOR);
  235. }
  236. void pixels(Window w, int n, int* x, int* y)
  237. { while(n--) pixel(w,x[n],y[n]); }
  238. void ellipse(Window w, int x0, int y0, int a, int b)
  239. { set_draw_window(w);
  240.   x0 += win_xmin;
  241.   y0 += win_ymin;
  242.   g_ellipse(x0,y0,a,b,COLOR,G_OUTLINE);
  243. }
  244. void fill_ellipse(Window w, int x0, int y0, int a, int b)
  245. { set_draw_window(w);
  246.   x0 += win_xmin;
  247.   y0 += win_ymin;
  248.   g_ellipse(x0,y0,a,b,COLOR,G_FILL);
  249. }
  250. void arc(Window w, int x0, int y0, int a, int b, double start, double angle)
  251. { }
  252. void fill_arc(Window,int,int,int,int,double,double)
  253. { }
  254. void copy_pixrect(Window win, int x1, int y1, int x2, int y2, int x, int y)
  255. { set_draw_window(win);
  256.   x1 += win_xmin;
  257.   x2 += win_xmin;
  258.   x  += win_xmin;
  259.   y1 += win_ymin;
  260.   y2 += win_ymin;
  261.   y  += win_ymin;
  262.   int w = x2 - x1 + 1;
  263.   int h = y2 - y1 + 1;
  264.   int sz = g_imagesize(w,h);
  265.   char* image = new char[sz];
  266.   g_getimage(x1,y1,x2,y2,image);
  267.   g_putimage(x,y,x+w-1,y+h-1,image);
  268.   delete[] image;
  269.  }
  270. void insert_bitmap(Window w, int width, int height, char* data)
  271. { set_draw_window(w);
  272.   char* p = data;
  273.   for(int y=0; y<height; y++)
  274.     for(int x=0; x<width; x+=8)
  275.     { char pat = *p++;
  276.       if (pat & 0x01) g_set(x,   y,black);
  277.       if (pat & 0x02) g_set(x+1, y,black);
  278.       if (pat & 0x04) g_set(x+2, y,black);
  279.       if (pat & 0x08) g_set(x+3, y,black);
  280.       if (pat & 0x10) g_set(x+4, y,black);
  281.       if (pat & 0x20) g_set(x+5, y,black);
  282.       if (pat & 0x40) g_set(x+6, y,black);
  283.       if (pat & 0x80) g_set(x+7, y,black);
  284.      }
  285.  }
  286. void set_palette(int index, int red, int green, int blue)
  287. { if (red < 0)     red = _R_[index]; else _R_[index] = red;
  288.   if (green < 0) green = _G_[index]; else _G_[index] = green;
  289.   if (blue < 0)   blue = _B_[index]; else _B_[index] = blue;
  290.   char pal[3];
  291.   pal[0] = red;
  292.   pal[1] = green;
  293.   pal[2] = blue;
  294.   g_vgapal(pal,index,1,1);
  295. }
  296. //------------------------------------------------------------------------------
  297. // pixrects
  298. //------------------------------------------------------------------------------
  299. char* create_pixrect(Window win, int left, int top, int right, int bottom)
  300. { set_draw_window(win);
  301.   left   += win_xmin;
  302.   right  += win_xmin;
  303.   top    += win_ymin;
  304.   bottom += win_ymin;
  305.   left  &= 0xFFF8;
  306.   right |= 7;
  307.   int w = right-left+1;
  308.   int h = bottom-top+1;
  309.   char* bp = new char[g_imagesize(w,h)+8];
  310.   g_getimage(left,top,right,bottom,bp+8);
  311.   *(int*)bp = w;
  312.   *(int*)(bp+4) = h;
  313.   return bp;
  314.  }
  315. void insert_pixrect(Window win, int left, int top, char* rect)
  316. { set_draw_window(win);
  317.   left   += win_xmin;
  318.   top    += win_ymin;
  319.   left  &= 0xFFF8;
  320.   if (left < 0) left = 0;
  321.   if (top < 0) top = 0;
  322.   int w = *(int*)rect;
  323.   int h = *(int*)(rect+4);
  324.   g_putimage(left,top,left+w-1,top+h-1,rect+8);
  325.  }
  326. void delete_pixrect(char* rect)   { delete rect; }
  327. //------------------------------------------------------------------------------
  328. // mouse cursor
  329. //------------------------------------------------------------------------------
  330. static unsigned short p_mask1[14] = 
  331. {0xc000,0xf000,0x7c00,0x7f00,0x3fc0,0x3fc0,0x1f00,
  332.  0x1f80,0x0dc0,0x0ce0,0x0070,0x0038,0x001c,0x000c};
  333. static unsigned short p_mask2[14] = 
  334. {0x0003,0x000f,0x003e,0x00fe,0x03fc,0x03fc,0x00f8,
  335.  0x01f8,0x03b0,0x0730,0x0e00,0x1c00,0x3800,0x3000};
  336. void draw_pointer(int mouse_x, int mouse_y, int shape)
  337. {
  338.   g_clip(0,0,DISP_MAX_X,DISP_MAX_Y);
  339.   set_color(black);
  340.   if (shape == 1)
  341.   { g_set(mouse_x-2,mouse_y,black);
  342.     g_set(mouse_x-1,mouse_y,black);
  343.     g_set(mouse_x,mouse_y,black);
  344.     g_set(mouse_x+1,mouse_y,black);
  345.     g_set(mouse_x+3,mouse_y,black);
  346.     g_set(mouse_x,mouse_y-2,black);
  347.     g_set(mouse_x,mouse_y-1,black);
  348.     g_set(mouse_x,mouse_y+1,black);
  349.     g_set(mouse_x,mouse_y+2,black);
  350.     g_ellipse(mouse_x,mouse_y,6,6,black,G_OUTLINE);
  351.     return;
  352.    }
  353.   else
  354.   { int x = mouse_x;
  355.     int y = mouse_y;
  356.     unsigned short* p = p_mask1;
  357.     if (x > DISP_MAX_X - 16) 
  358.     { x -= 16;
  359.       p = p_mask2;
  360.      }
  361.     unsigned short* p_stop = p + 14;
  362.     int d  = 1;
  363.     if (y > DISP_MAX_Y - 14) 
  364.     { y -= 14;
  365.       d = -1;
  366.       p_stop = p-1;
  367.       p += 14;
  368.      }
  369.      
  370.     while (p != p_stop)
  371.     { unsigned short pat = *p;
  372.       if (pat & 0x8000) g_set(x,   y,black);
  373.       if (pat & 0x4000) g_set(x+1, y,black);
  374.       if (pat & 0x2000) g_set(x+2, y,black);
  375.       if (pat & 0x1000) g_set(x+3, y,black);
  376.       if (pat & 0x0800) g_set(x+4, y,black);
  377.       if (pat & 0x0400) g_set(x+5, y,black);
  378.       if (pat & 0x0200) g_set(x+6, y,black);
  379.       if (pat & 0x0100) g_set(x+7, y,black);
  380.       if (pat & 0x0080) g_set(x+8, y,black);
  381.       if (pat & 0x0040) g_set(x+9, y,black);
  382.       if (pat & 0x0020) g_set(x+10,y,black);
  383.       if (pat & 0x0010) g_set(x+11,y,black);
  384.       if (pat & 0x0008) g_set(x+12,y,black);
  385.       if (pat & 0x0004) g_set(x+13,y,black);
  386.       if (pat & 0x0002) g_set(x+14,y,black);
  387.       if (pat & 0x0001) g_set(x+15,y,black);
  388.       p+=d;
  389.       y++;
  390.      }
  391.    }
  392. }