home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / MODEL / GRAPHWIN.CPP < prev    next >
C/C++ Source or Header  |  1996-06-05  |  11KB  |  501 lines

  1.  
  2. #include <owl\owlpch.h>
  3. #include <owl\applicat.h>
  4. #include <owl\framewin.h>
  5. #include <owl\dc.h>
  6. #include <color.h>
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <dos.h>
  11.  
  12. extern    "C" {
  13.     #include "graph.h"
  14.     #include "view.h"
  15. }
  16.  
  17. /*int        Cols, Lines ;*/
  18. int        Display_X, Display_Y;
  19. int        FontH, FontV ;
  20.  
  21. #include "message.h"
  22.  
  23. extern    TFrameWindow    *MainWindow ;
  24. extern    TApplication    *Application ;
  25. extern    TDC                *ModelDC ;
  26. extern    TPalette        *ModelPalet;
  27.  
  28. static    TColor    *col[17] ;
  29. int lastcolor = -1;
  30. int paletteowner = TRUE;
  31. static int palettemode = FALSE;
  32. //static PALETTEENTRY pal[17];
  33. static PALETTEENTRY pal[1];
  34.  
  35. #define BUFFERPOINTS 512
  36.  
  37. static TPoint bufferpoint[BUFFERPOINTS], *ppoint;
  38. static BYTE buffertype[BUFFERPOINTS], *ptype;
  39. static int buffersize;
  40. static int bufferflag;
  41. static int polydrawflag;
  42. static    void buffer_flush();
  43. static    void buffer_append(int x1, int y1, int x2, int y2);
  44.  
  45. void    graph_init()
  46. {
  47.     int        i ;
  48.  
  49.     // カラーコードの設定
  50.     for( i = 0 ; i < 16 ; i++ )
  51.     {
  52.         if ( col[i] != NULL )
  53.         {
  54.             delete col[i] ;
  55.         }
  56.     }
  57.     for( i = 0 ; i < 8 ; ++i )
  58.     {
  59.         col[i] = new TColor( (i&2) ? 255 : 0, (i&4) ? 255 : 0, (i&1) ? 255 : 0 );
  60.     }
  61.  
  62.     for( i = 8 ; i < 16 ; ++i )
  63.     {
  64.         col[i] = new TColor( 127, 255, 255 );
  65.     }
  66.     if (ModelDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE) {
  67.         col[16] = new TColor(128, 128, 0);
  68.         palettemode = TRUE;
  69.  
  70.         pal[0] = TPaletteEntry(255, 128, 128, PC_RESERVED);
  71.  
  72.         ModelPalet = new TPalette(pal, 1);
  73.         ModelDC->SelectObject(*ModelPalet);
  74.         ModelDC->RealizePalette();
  75.     } else {
  76.         col[16] = new TColor(255^100, 255^100, 0);
  77.         palettemode = FALSE;
  78.     }
  79.     bufferflag = FALSE;
  80.     ppoint = bufferpoint;
  81.     ptype = buffertype;
  82. }
  83.  
  84. void    graph_exit()
  85. {
  86. }
  87.  
  88. void    graph_line( int x1, int y1, int x2, int y2, int color )
  89. {
  90.     if ( color < 16 )
  91.     {
  92.         if (lastcolor != color) {
  93.             if (bufferflag && buffersize > 0) {
  94.                 buffer_flush();
  95.             }
  96.             if (palettemode && color == SELECT_COLOR) {
  97.                 ModelDC->SelectObject(TPen(PALETTEINDEX(0)));
  98.             } else {
  99.                 ModelDC->SelectObject( TPen( *(col[color]) ) );
  100.             }
  101.             lastcolor = color;
  102.         }
  103.         if (bufferflag) {
  104.             buffer_append(x1, y1, x2, y2);
  105.         } else {
  106.             ModelDC->MoveTo( x1, y1 );
  107.             ModelDC->LineTo( x2, y2 );
  108.         }
  109.     }
  110.     else
  111.     {
  112.         if (bufferflag && buffersize > 0) {
  113.             buffer_flush();
  114.         }
  115.         int        rmode ;
  116.         rmode = ModelDC->SetROP2( R2_XORPEN );
  117.         if (lastcolor != 16) {
  118.             ModelDC->SelectObject( TPen(*(col[16])) );
  119.             lastcolor = 16;
  120.         }
  121.         ModelDC->MoveTo( x1, y1 );
  122.         ModelDC->LineTo( x2, y2 );
  123.         ModelDC->SetROP2( rmode );
  124.     }
  125. }
  126.  
  127. void    graph_dashline( int x1, int y1, int x2, int y2, int color )
  128. {
  129.     if ( color < 16 )
  130.     {
  131.         if (bufferflag && buffersize > 0) {
  132.             buffer_flush();
  133.         }
  134.         ModelDC->MoveTo( x1, y1 );
  135.         if (lastcolor != color+32) {
  136.             ModelDC->SelectObject( TPen( *(col[color]), 1, PS_DOT ) );
  137.             lastcolor = color+32;
  138.         }
  139.         ModelDC->LineTo( x2, y2 );
  140.     }
  141. }
  142.  
  143. void    graph_cls( int color )
  144. {
  145. }
  146.  
  147. void    graph_fill( int x1, int y1, int x2, int y2, int color )
  148. {
  149.  
  150.     if ( color < 16 )
  151.     {
  152.         if (x2 == x1+2 && y2 == y1+2) {
  153.             if (lastcolor != color) {
  154.                 if (bufferflag && buffersize > 0) {
  155.                     buffer_flush();
  156.                 }
  157.                 if (palettemode && color == SELECT_COLOR) {
  158.                     ModelDC->SelectObject(TPen(PALETTEINDEX(0)));
  159.                 } else {
  160.                     ModelDC->SelectObject( TPen( *(col[color]) ) );
  161.                 }
  162.                 lastcolor = color;
  163.             }
  164.             if (bufferflag) {
  165.                 buffer_append(x1, y1, x1, y2);
  166.                 buffer_append(x1, y2, x2, y2);
  167.                 buffer_append(x2, y2, x2, y1);
  168.                 buffer_append(x2, y1, x1, y1);
  169.             } else {
  170.                 TPoint pp[5];
  171.                 pp[0].x = pp[1].x = pp[4].x = x1;
  172.                 pp[2].x = pp[3].x = x2;
  173.                 pp[0].y = pp[3].y = pp[4].y = y1;
  174.                 pp[1].y = pp[2].y = y2;
  175.                 ModelDC->Polyline(pp, 5);
  176.             }
  177.         } else if (palettemode && color == SELECT_COLOR) {
  178.             if (bufferflag && buffersize > 0) {
  179.                 buffer_flush();
  180.             }
  181.             if (lastcolor != color+64) {
  182.                 ModelDC->SelectObject(TPen(PALETTEINDEX(0)));
  183.                 ModelDC->SelectObject(TBrush(PALETTEINDEX(0)));
  184.                 lastcolor = color + 64;
  185.             }
  186.             ModelDC->Rectangle(x1, y1, x2+1, y2+1);
  187.         } else {
  188.             ModelDC->TextRect( x1, y1, x2+1, y2+1, *(col[color]) );
  189.         }
  190.     } else {
  191.         if (bufferflag && buffersize > 0) {
  192.             buffer_flush();
  193.         }
  194.         int        rmode ;
  195.         rmode = ModelDC->SetROP2( R2_XORPEN );
  196.         if (lastcolor != 16 + 64) {
  197.             ModelDC->SelectObject(TPen(*(col[16])));
  198.             ModelDC->SelectObject(TBrush(*(col[16])));
  199.             lastcolor = 16 + 64;
  200.         }
  201.         if (x2 == x1+2 && y2 == y1+2) {
  202.             TPoint pp[5];
  203.             pp[0].x = pp[1].x = pp[4].x = x1;
  204.             pp[2].x = pp[3].x = x2;
  205.             pp[0].y = pp[3].y = pp[4].y = y1;
  206.             pp[1].y = pp[2].y = y2;
  207.             ModelDC->Polyline(pp, 5);
  208.         } else {
  209.             ModelDC->Rectangle(x1, y1, x2+1, y2+1);
  210.         }
  211.         ModelDC->SetROP2( rmode );
  212.     }
  213. }
  214.  
  215. void    graph_palet( int code, int g, int r, int b )
  216. {
  217.     if (!palettemode || code != SELECT_COLOR) {
  218.         delete col[code] ;
  219.         col[code] = new TColor( r, g, b );
  220.     }
  221.     if (palettemode && code == SELECT_COLOR && paletteowner) {
  222.         pal[0] = TPaletteEntry(r, g, b, PC_RESERVED);
  223.         ModelPalet->AnimatePalette(0, 1, pal);
  224.     }
  225. }
  226.  
  227. void    graph_puts( char* str, int x, int y, int atr )
  228. {
  229.     int rmode;
  230.     ModelDC->SetTextColor( *(col[atr & 15]) );
  231.     ModelDC->SetBkColor( *(col[atr >> 4]) );
  232.     rmode = ModelDC->SetBkMode(OPAQUE);
  233.     ModelDC->TextOut( x, y, str );
  234.     ModelDC->SetBkMode(rmode);
  235. }
  236.  
  237. void    graph_pattern( int x, int y, int color, short* pat, int h, int v )
  238. {
  239. #if 0
  240.  
  241. //    const int PATTERN_MAXWIDTH = 64;
  242. //    const int PATTERN_MAXHEIGHT = 64;
  243. //    const int PATTERN_HBYTE = ((PATTERN_MAXWIDTH+3)/4)*4;
  244.     int rh = ((h-1)/4+1)*4;
  245. //    static TDib dib(PATTERN_MAXWIDTH, PATTERN_MAXHEIGHT, 256);
  246.     TDib dib(h, v, 256);
  247.     dib.SetColor(0, *col[color>>4]);
  248.     dib.SetColor(1, *col[color&15]);
  249.     unsigned char *p = (unsigned char *)dib.GetBits();
  250.     int i, j, k, hword, hh;
  251.     int a;
  252.     hword = ( h + 15 ) / 16 ;
  253.     p += rh*(v-1);
  254. //    p += PATTERN_HBYTE * (PATTERN_MAXHEIGHT-1);
  255.     for( i = 0 ; i < v ; i++ )
  256.     {
  257.         hh = h;
  258.         for( j = 0 ; j < hword ; j++ )
  259.         {
  260.             a = *pat++ & 0xffff ;
  261.             for( k = 0 ; k < 16 && k < hh ; k++ )
  262.             {
  263.                 if ( a & 0x8000 )
  264.                     *p++ = 1;
  265.                 else
  266.                     *p++ = 0 ;
  267.                 a += a ;
  268.             }
  269.             hh -= 16 ;
  270.         }
  271.         p += (rh-h) - 2 * rh;
  272. //        p +=  (PATTERN_HBYTE - h)  - 2 * PATTERN_HBYTE;
  273.     }
  274. //    dib.ChangeModeToPal(*ModelPalet);
  275.     ModelDC->SetDIBitsToDevice(TRect(x,y,x+h,y+v), TPoint(0,0),dib);
  276. #else
  277.     int i, j, k, hword, hh;
  278.     int a;
  279.     hword = ( h + 15 ) / 16 ;
  280.  
  281.     graph_fill(x, y, x+h-1, y+v-1, color & 15);
  282. //    ModelDC->SelectObject(TPen(*(col[color>>4])));
  283. //    ModelDC->SelectObject(TBrush(*(col[color>>4])));
  284. //    lastcolor = -1;
  285.     for( i = 0 ; i < v ; i++ )
  286.     {
  287.         hh = h;
  288.         for( j = 0 ; j < hword ; j++ )
  289.         {
  290.             a = *pat++ & 0xffff ;
  291.             for( k = 0 ; k < 16 && k < hh ; k++ )
  292.             {
  293.                 if ((a & 0x8000) == 0) {
  294. //                    ModelDC->Rectangle(x+j*16+k, y+i, x+j*16+k+1, y+i+1);
  295.                     ModelDC->TextRect(x+j*16+k, y+i, x+j*16+k+1, y+i+1, *(col[color>>4]));
  296.                 }
  297.                 a += a ;
  298.             }
  299.             hh -= 16 ;
  300.         }
  301.     }
  302. #endif
  303. }
  304.  
  305. void    graph_pattern2( int x, int y, int color, short* pat, int h, int v )
  306. {
  307. }
  308.  
  309. void    graph_pattern_xor( int x, int y, int color, short* pat, int h, int v )
  310. {
  311. }
  312.  
  313. /*    ボタン表示    */
  314. void    graph_box( int x1, int y1, int x2, int y2, int sw )
  315. {
  316.     if ( sw )
  317.     {
  318. #if 0
  319.         graph_line( x1, y1, x2, y1, 7 );
  320.         graph_fill( x1, y1, x1+1, y2, 7 );
  321.         graph_line( x1, y2, x2, y2, 0 );
  322.         graph_line( x2, y1, x2, y2, 0 );
  323. #else
  324.         graph_line( x1+1, y1, x2-1, y1, 7 );
  325.         graph_line( x1, y1+1, x1, y2-1, 7 );
  326.         graph_line( x1+1, y2, x2-1, y2, 0 );
  327.         graph_line( x2, y1+1, x2, y2-1, 0 );
  328. #endif
  329.     }
  330.     else
  331.     {
  332. #if 0
  333.         graph_line( x1, y1, x2, y1, 0 );
  334.         graph_line( x1, y1, x1, y2, 0 );
  335.         graph_line( x1, y2, x2, y2, 7 );
  336.         graph_fill( x2-1, y1, x2, y2, 7 );
  337. #else
  338.         graph_line( x1+1, y1, x2-1, y1, 0 );
  339.         graph_line( x1, y1+1, x1, y2-1, 0 );
  340.         graph_line( x1+1, y2, x2-1, y2, 7 );
  341.         graph_line( x2, y1+1, x2, y2-1, 7 );
  342. #endif
  343.     }
  344. }
  345.  
  346. int        graph_push(int, int, int, int )
  347. {
  348.     return FALSE;
  349. }
  350.  
  351. int        graph_pop(void)
  352. {
  353.     return FALSE;
  354. }
  355.  
  356. #if 0
  357.  
  358. TDC *backupDC = NULL;
  359. TBitmap *bufferbitmap;
  360. #endif
  361.  
  362. static void buffer_flush()
  363. {
  364.     if (buffersize > 0) {
  365. #if 0
  366. printf("flush %d\n", buffersize);
  367. int i;
  368. for (i = 0; i < buffersize; i++) {
  369.     if (buffertype[i] == PT_MOVETO) {
  370.         printf("MoveTo(%d,%d)\n", bufferpoint[i].x, bufferpoint[i].y);
  371.     } else {
  372.         printf("LineTo(%d,%d)\n", bufferpoint[i].x, bufferpoint[i].y);
  373.     }
  374. }
  375. printf("->\n");
  376. #endif
  377.         if (ModelDC->PolyDraw(bufferpoint, buffertype, buffersize) == FALSE) {
  378.             TPoint *pbegin, *pend;
  379.             BYTE *pt;
  380.             int i, size;;
  381.             pbegin = bufferpoint;
  382.             pend = pbegin+1;
  383.             size = 1;
  384.             pt = buffertype+1;
  385.             for (i = buffersize-1; i > 0; i--) {
  386.                 if (*pt == PT_MOVETO) {
  387.                     ModelDC->Polyline(pbegin, size);
  388. #if 0
  389. printf("MoveTo(%d,%d)\n", pbegin[0].x, pbegin[0].y);
  390. for (int j = 1; j < size; j++) {
  391.     printf("LineTo(%d,%d)\n", pbegin[j].x, pbegin[j].y);
  392. }
  393. #endif
  394.  
  395.                     pbegin = pend;
  396.                     size = 0;
  397.                 }
  398.                 pt++;
  399.                 pend++;
  400.                 size++;
  401.             }
  402.             if (size > 0) {
  403.                 ModelDC->Polyline(pbegin, size);
  404. #if 0
  405. printf("MoveTo(%d,%d)\n", pbegin[0].x, pbegin[0].y);
  406. for (int j = 1; j < size; j++) {
  407.     printf("LineTo(%d,%d)\n", pbegin[j].x, pbegin[j].y);
  408. }
  409. #endif
  410.             }
  411.         }
  412.         buffersize = 0;
  413.         ppoint = bufferpoint;
  414.         ptype = buffertype;
  415.     }
  416. }
  417.  
  418. static void buffer_append(int x1, int y1, int x2, int y2)
  419. {
  420.     if (buffersize > BUFFERPOINTS-2) {
  421.         buffer_flush();
  422.     }
  423.     if (buffersize > 0 && ppoint[-1].x == x1 && ppoint[-1].y == y1) {
  424.         ppoint->x = x2;
  425.         ppoint->y = y2;
  426.         ppoint++;
  427.         *ptype++ = PT_LINETO;
  428.         buffersize++;
  429.     } else if (buffersize > 0 && ppoint[-1].x == x2 && ppoint[-1].y == y2) {
  430.         ppoint->x = x1;
  431.         ppoint->y = y1;
  432.         ppoint++;
  433.         *ptype++ = PT_LINETO;
  434.         buffersize++;
  435.     } else {
  436.         ppoint->x = x1;
  437.         ppoint->y = y1;
  438.         ppoint++;
  439.         *ptype++ = PT_MOVETO;
  440.  
  441.         ppoint->x = x2;
  442.         ppoint->y = y2;
  443.         ppoint++;
  444.         *ptype++ = PT_LINETO;
  445.         buffersize+=2;
  446.     }
  447. }
  448.  
  449. int graph_buffer_start()
  450. {
  451. #if 0
  452.     if (backupDC == NULL) {
  453.         backupDC = ModelDC;
  454.         ModelDC = new TMemoryDC(*backupDC);
  455.         TRect rect=backupDC->GetClipBox();
  456. //printf("Alloc MemoryDC(%d,%d)\n", rect.right, rect.bottom);
  457. //        bufferbitmap = new TBitmap(*backupDC, rect.right, rect.bottom);
  458.         bufferbitmap = new TBitmap(*backupDC, x2-x1+1, y2-y1+1);
  459.         ModelDC->SetWindowOrg(TPoint(x1,y1));
  460. //        ModelDC->SetViewportOrg(TPoint(x1,y1));
  461.         ModelDC->SelectObject(*bufferbitmap);
  462.         if (ModelPalet != NULL) {
  463.             ModelDC->SelectObject(*ModelPalet);
  464.             ModelDC->RealizePalette();
  465.         }
  466.         ModelDC->SetBkMode(TRANSPARENT);
  467.         lastcolor = -1;
  468.     }
  469. #endif
  470.     bufferflag = TRUE;
  471.     return TRUE;
  472. }
  473.  
  474. #if 0
  475. void graph_buffer_draw(int x1, int y1, int x2, int y2)
  476. {
  477. printf("buffer_draw(%d,%d)-(%d,%d) %d\n", x1, y1, x2, y2, (int)backupDC);
  478.     if (backupDC != NULL) {
  479.         backupDC->BitBlt(x1, y1, x2-x1+1, y2-y1+1, *ModelDC, x1, y1, SRCCOPY);
  480.     }
  481. }
  482. #endif
  483.  
  484. void graph_buffer_end(void)
  485. {
  486.     if (bufferflag) {
  487.         buffer_flush();
  488.     }
  489.     bufferflag = FALSE;
  490. #if 0
  491.     if (backupDC != NULL) {
  492. printf("Free MemoryDC\n");
  493.         delete ModelDC;
  494.         ModelDC = backupDC;
  495.         backupDC = NULL;
  496.         lastcolor = -1;
  497.     }
  498. #endif
  499. }
  500.  
  501.