Graphics: C/C++ Version


/****************************************************************************\
*                                                                            *
*  Graphics.c                                                                *
*                                                                            *
*  This program demonstrates some of the Fastgraph for Windows graphics      *
*  primitive functions.                                                      *
*                                                                            *
\****************************************************************************/
#include <fgwin.h>
#include "Graphics.h"
#define vbWidth  640
#define vbHeight 480
LRESULT CALLBACK WindowProc(HWND,UINT,WPARAM,LPARAM);
void Blit(void);
void CirclesClick(void);
void EllipsesClick(void);
void LinesClick(void);
void PaintClick(void);
void PointsClick(void);
void PolygonsClick(void);
void RectanglesClick(void);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdParam, int iCmdShow)
{
   static char szAppName[] = "FGgraphics";
   HWND        hWnd;
   MSG         msg;
   WNDCLASSEX  wndclass;
   wndclass.cbSize        = sizeof(wndclass);
   wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
   wndclass.lpfnWndProc   = WindowProc;
   wndclass.cbClsExtra    = 0;
   wndclass.cbWndExtra    = 0;
   wndclass.hInstance     = hInstance;
   wndclass.hIcon         = LoadIcon(NULL,IDI_APPLICATION);
   wndclass.hCursor       = LoadCursor(NULL,IDC_ARROW);
   wndclass.hbrBackground = NULL;
   wndclass.lpszMenuName  = szAppName;
   wndclass.lpszClassName = szAppName;
   wndclass.hIconSm       = LoadIcon(NULL,IDI_APPLICATION);
   RegisterClassEx(&wndclass);
   hWnd = CreateWindow(szAppName, // window class name
      "Graphics Primitives",   // window caption
      WS_OVERLAPPEDWINDOW,     // window style
      CW_USEDEFAULT,           // initial x position
      CW_USEDEFAULT,           // initial y position
      vbWidth,                 // initial x size
      vbHeight,                // initial y size
      NULL,                    // parent window handle
      NULL,                    // window menu handle
      hInstance,               // program instance handle
      NULL);                   // creation parameters
   ShowWindow(hWnd,iCmdShow);
   UpdateWindow(hWnd);
   while (GetMessage(&msg,NULL,0,0))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
   return msg.wParam;
}
/****************************************************************************\
*                                                                            *
*  WindowProc()                                                              *
*                                                                            *
\****************************************************************************/
HDC      hDC;
HPALETTE hPal;
int      hVB;
UINT     cxClient, cyClient;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
   PAINTSTRUCT ps;
   switch (iMsg)
   {
      case WM_CREATE:
         hDC = GetDC(hWnd);
         fg_setdc(hDC);
         hPal = fg_defpal();
         fg_realize(hPal);
         fg_vbinit();
         hVB = fg_vballoc(vbWidth,vbHeight);
         fg_vbopen(hVB);
         fg_vbcolors();
         fg_setcolor(25);
         fg_fillpage();
         return 0;
      case WM_COMMAND:
         switch (wParam)
         {
            case IDM_Points:
               PointsClick();
               return 0;
            case IDM_Lines:
               LinesClick();
               return 0;
            case IDM_Rectangles:
               RectanglesClick();
               return 0;
            case IDM_Circles:
               CirclesClick();
               return 0;
            case IDM_Ellipses:
               EllipsesClick();
               return 0;
            case IDM_Polygons:
               PolygonsClick();
               return 0;
            case IDM_Paint:
               PaintClick();
               return 0;
            case IDM_Exit:
               DestroyWindow(hWnd);
               return 0;
         }
         break;
      case WM_PAINT:
         BeginPaint(hWnd,&ps);
         Blit();
         EndPaint(hWnd,&ps);
         return 0;
      case WM_SETFOCUS:
         fg_realize(hPal);
         InvalidateRect(hWnd,NULL,TRUE);
         return 0;
      case WM_SIZE:
         cxClient = LOWORD(lParam);
         cyClient = HIWORD(lParam);
         return 0;
      case WM_DESTROY:
         fg_vbclose();
         fg_vbfree(hVB);
         fg_vbfin();
         DeleteObject(hPal);
         ReleaseDC(hWnd,hDC);
         PostQuitMessage(0);
         return 0;
   }
   return DefWindowProc(hWnd,iMsg,wParam,lParam);
}
/****************************************************************************\
*                                                                            *
*  Blit()                                                                    *
*                                                                            *
*  Use fg_vbpaste() or fg_vbscale() to display the virtual buffer contents   *
*  in the client area, depending on the size of the client window.           *
*                                                                            *
\****************************************************************************/
void Blit()
{
   if (cxClient > vbWidth || cyClient > vbHeight)   // window larger than 640x480
      fg_vbscale(0,vbWidth-1,0,vbHeight-1,0,cxClient-1,0,cyClient-1);
   else
      fg_vbpaste(0,vbWidth-1,0,vbHeight-1,0,cyClient-1);
}
/****************************************************************************\
*                                                                            *
*  CirclesClick()                                                            *
*                                                                            *
*  Draw a series of concentric circles.                                      *
*                                                                            *
\****************************************************************************/
void CirclesClick()
{
   register int i, Radius;
   fg_setcolor(11);
   fg_fillpage();
   // draw 25 concentric circles at the center of the virtual buffer
   fg_move(vbWidth/2,vbHeight/2);
   Radius = 4;
   fg_setcolor(25);
   for (i = 0; i < 25; i++)
   {
      fg_circle(Radius);
      Radius += 8;
   }
   Blit();
}
/****************************************************************************\
*                                                                            *
*  EllipsesClick()                                                           *
*                                                                            *
*  Draw a series of concentric ellipses.                                     *
*                                                                            *
\****************************************************************************/
void EllipsesClick()
{
   register int i;
   int Horiz, Vert;
   fg_setcolor(11);
   fg_fillpage();
   // draw 80 concentric ellipses at the center of the virtual buffer
   fg_move(vbWidth/2,vbHeight/2);
   Horiz = 4;
   Vert  = 1;
   fg_setcolor(25);
   for (i = 0; i < 80; i++)
   {
      fg_ellipse(Horiz,Vert);
      Horiz += 3;
      Vert++;
   }
   Blit();
}
/****************************************************************************\
*                                                                            *
*  LinesClick()                                                              *
*                                                                            *
*  Draw a pattern of solid lines.                                            *
*                                                                            *
\****************************************************************************/
void LinesClick()
{
   register int x, y;
   int i, x1, x2, y1;
   static int LineColor[] = {12,11,19,21,21,19,11,12};
   fg_setcolor(25);
   fg_fillpage();
   // draw horizontal lines
   for (y = 0; y < vbHeight; y+=40)
   {
      for (i = 0; i < 8; i++)
      {
         fg_setcolor(LineColor[i]);
         y1 = y + 3*i;
         fg_move(0,y1);
         fg_draw(vbWidth-1,y1);
      }
   }
   // draw vertical lines
   for (x = 0; x < vbWidth; x+=60)
   {
      for (i = 0; i < 8; i++)
      {
         fg_setcolor(LineColor[i]);
         x1 = x + 3*i;
         fg_move(x1,0);
         fg_draw(x1,vbHeight-1);
      }
   }
   // draw red diagonal lines
   fg_setcolor(22);
   for (x1 = -640; x1 < 640; x1+=60)
   {
      x2 = x1 + vbHeight;
      fg_move(x1,0);
      fg_draw(x2,vbHeight);
   }
   for (x1 = 0; x1 < 1280; x1+=60)
   {
      x2 = x1 - vbHeight;
      fg_move(x1,0);
      fg_draw(x2,vbHeight);
   }
   Blit();
}
/****************************************************************************\
*                                                                            *
*  PaintClick()                                                              *
*                                                                            *
*  Demonstrate region fill.                                                  *
*                                                                            *
\****************************************************************************/
void PaintClick()
{
   int x1, x2, y1, y2;
   fg_setcolor(25);
   fg_fillpage();
   // draw a rectangle
   x1 = 40;
   x2 = vbWidth - 40;
   y1 = 20;
   y2 = vbHeight - 20;
   fg_setcolor(21);
   fg_rect(x1,x2,y1,y2);
   // outline the rectangle
   fg_setcolor(10);
   fg_box(x1,x2,y1,y2);
   // draw the circle
   x1 = vbWidth / 2;
   y1 = vbHeight / 2;
   fg_move(x1,y1);
   fg_circle(80);
   // draw cross bars in the circle
   fg_move(x1,y1-80);
   fg_draw(x1,y1+80);
   fg_move(x1-80,y1);
   fg_draw(x1+80,y1);
   // paint each quarter of the circle
   fg_setcolor(11);
   fg_paint(x1-6,y1-6);
   fg_setcolor(12);
   fg_paint(x1+6,y1+6);
   fg_setcolor(13);
   fg_paint(x1+6,y1-6);
   fg_setcolor(14);
   fg_paint(x1-6,y1+6);
   // paint the area outside the box
   fg_setcolor(24);
   fg_paint(41,21);
   Blit();
}
/****************************************************************************\
*                                                                            *
*  PointsClick()                                                             *
*                                                                            *
*  Draw a pattern of points.                                                 *
*                                                                            *
\****************************************************************************/
void PointsClick()
{
   register int x, y;
   // fill the virtual buffer with yellow pixels
   fg_setcolor(24);
   fg_fillpage();
   // draw the patterns of points
   fg_setcolor(19);
   for (x = 7; x < vbWidth; x+=20)
      for (y = 0; y < vbHeight; y+=8)
         fg_point(x,y);
   fg_setcolor(22);
   for (x = 17; x < vbWidth; x+=20)
      for (y = 4; y < vbHeight; y+=8)
         fg_point(x,y);
   Blit();
}
/****************************************************************************\
*                                                                            *
*  PolygonsClick()                                                           *
*                                                                            *
*  Draw a grid of filled polygons.                                           *
*                                                                            *
\****************************************************************************/
void PolygonsClick()
{
   register int i, j;
   static int xyDarkBlue[]  = {0,16, 24,0, 24,40, 0,56};
   static int xyLightBlue[] = {24,0, 72,0, 72,40, 24,40};
   static int xyGreen[]     = {0,56, 24,40, 72,40, 48,56};
   fg_setcolor(25);
   fg_fillpage();
   // draw 225 filled polygons (15 rows of 15)
   for (j = 0; j < 15; j++)
   {
      for (i = 0; i < 15; i++)
      {
         fg_polyoff(i*72-j*24,j*56-i*16);
         fg_setcolor(11);
         fg_polyfill(xyDarkBlue,NULL,4);
         fg_setcolor(19);
         fg_polyfill(xyLightBlue,NULL,4);
         fg_setcolor(20);
         fg_polyfill(xyGreen,NULL,4);
      }
   }
   Blit();
}
/****************************************************************************\
*                                                                            *
*  RectanglesClick()                                                         *
*                                                                            *
*  Draw a grid of filled rectangles.                                         *
*                                                                            *
\****************************************************************************/
void RectanglesClick()
{
   register int i, j;
   int Color;
   int x1, x2, y1, y2;
   int xInc, yInc;
   x1 = 0;
   xInc = vbWidth / 10;
   x2 = xInc - 1;
   y1 = 0;
   yInc = vbHeight / 10;
   y2 = yInc - 1;
   Color = 10;
   // draw 100 filled rectangles (10 rows of 10)
   for (i = 0; i < 10; i++)
   {
      for (j = 0; j < 10; j++)
      {
         fg_setcolor(Color);
         fg_rect(x1,x2,y1,y2);
         Color++;
         if (Color > 24) Color = 10;
         x1 += xInc;
         x2 += xInc;
      }
      x1 = 0;
      x2 = xInc - 1;
      y1 += yInc;
      y2 += yInc;
   }
   Blit();
}

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.