home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / oct93 / graphics / graphtal.lha / Graphtal / BaseWindow.C < prev    next >
C/C++ Source or Header  |  1992-11-17  |  5KB  |  225 lines

  1. /*
  2.  * BaseWindow.C - abstract base class for window support.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  *                     University of Berne, Switzerland
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified, and redistributed
  9.  * provided that this copyright notice is preserved on all copies.
  10.  *
  11.  * You may not distribute this software, in whole or in part, as part of
  12.  * any commercial product without the express consent of the authors.
  13.  *
  14.  * There is no warranty or other guarantee of fitness of this software
  15.  * for any purpose.  It is provided solely "as is".
  16.  *
  17.  */
  18.  
  19. #include "BaseWindow.h"
  20. #include "list.h"
  21. #include "Error.h"
  22. #include "Options.h"
  23.  
  24. //___________________________________________________________ LineSegment
  25.  
  26. struct LineSegment
  27. {
  28.   LineSegment(const Vector& p1, const Vector& p2)
  29.   : from(p1), to(p2) {}
  30.  
  31.   Vector from, to;
  32. };
  33.  
  34. typedef LineSegment* LineSegmentPtr;
  35. declareList(LineList, LineSegmentPtr);
  36. implementList(LineList, LineSegmentPtr);
  37.  
  38. //___________________________________________________________ BaseWindow
  39.  
  40. BaseWindow::BaseWindow()
  41. : view(NULL), resX(400), resY(400), 
  42.   lines(NULL), polys(NULL),
  43.   windowIsOpen(0), buffering(1)
  44. {}
  45.  
  46. BaseWindow::~BaseWindow()
  47. {
  48.   if (view != NULL)  
  49.     delete view;
  50.   clearBuffer();
  51.   if (lines != NULL)
  52.     delete lines;
  53.   if (polys != NULL)
  54.     delete polys;
  55. }
  56.  
  57. void BaseWindow::open(int width, int height, const rcString& windowName)
  58. {
  59.   if (windowIsOpen)
  60.     Error(ERR_PANIC, "Window is already open");
  61.  
  62.   windowIsOpen = 1;
  63.   resX = width;
  64.   resY = height;
  65.   name = windowName;
  66. }
  67.  
  68. void BaseWindow::close()
  69. {
  70.   if (!windowIsOpen)
  71.     Error(ERR_PANIC, "close window: but window is not open");
  72.   windowIsOpen = 0;
  73. }
  74.  
  75. void BaseWindow::clear()
  76. {
  77.   if (!windowIsOpen)
  78.     Error(ERR_PANIC, "clear window: but window is not open");
  79. }
  80.  
  81. /*
  82.  * If buffering is enabled store the given line segment in the
  83.  * line list. Otherwise transform the segment to screen coordinates
  84.  * and show it on the screen.
  85.  */
  86.  
  87. void BaseWindow::line(const Vector& from, const Vector& to)
  88. {
  89.   if (buffering) {
  90.     if (lines == NULL)
  91.       lines = new LineList(1000);
  92.     lines->append(new LineSegment(from, to));
  93.   }
  94.   else if (view == NULL) {
  95.     Error(ERR_PANIC, "no view parameter set");
  96.   }
  97.  
  98.   /*
  99.    * Transform and draw the line without buffering.
  100.    */
  101.   else { 
  102.     Vector x = view->transformWorld2Screen(from);
  103.     Vector y = view->transformWorld2Screen(to);
  104.     drawLine((int)x[0], (int)x[1], (int)y[0], (int)y[1]);
  105.   }
  106. }
  107.  
  108. /*
  109.  * If buffering is enabled store the given polygon in the
  110.  * poly list. Otherwise transform the polygon to screen coordinates
  111.  * and show it on the screen.
  112.  */
  113.  
  114. void BaseWindow::polygon(Polygon* p)
  115. {
  116.   if (buffering) {
  117.     if (polys == NULL)
  118.       polys = new PolygonList(1000);
  119.     polys->append(p);
  120.   }
  121.   else if (view == NULL) {
  122.     Error(ERR_PANIC, "no view parameter set");
  123.   }
  124.   else if (p->numVertices() >= 3) { 
  125.     Vector x = view->transformWorld2Screen(p->vertex(0));
  126.     Vector y;
  127.  
  128.     for (register long i=1; i<p->numVertices(); i++) {
  129.       y = view->transformWorld2Screen(p->vertex(i));
  130.       drawLine((int)x[0], (int)x[1], (int)y[0], (int)y[1]);
  131.       x = y;
  132.     }
  133.     y = view->transformWorld2Screen(p->vertex(0));
  134.     drawLine((int)x[0], (int)x[1], (int)y[0], (int)y[1]);
  135.   }  
  136.  
  137.   if (!buffering)
  138.     delete p;
  139. }
  140.  
  141. /*
  142.  * For all the line segment in the line list and all the polys in the
  143.  * polygon list, transform and bring dem to screen.
  144.  */
  145.  
  146. void BaseWindow::flush()
  147. {
  148.   if (view == NULL)
  149.     Error(ERR_PANIC, "no view parameter set");
  150.  
  151.   Vector from, to;
  152.   if (lines != NULL) {
  153.     for (register long i=0; i<lines->count(); i++) {
  154.       from = view->transformWorld2Screen(lines->item(i)->from);
  155.       to   = view->transformWorld2Screen(lines->item(i)->to);
  156.       drawLine((int)from[0], (int)from[1], (int)to[0], (int)to[1]);
  157.     }
  158.   }
  159.   
  160.   if (polys != NULL) {
  161.     for (register long i=0; i<polys->count(); i++) {
  162.       Polygon* p = polys->item(i);
  163.       if (p->numVertices() >= 3) {
  164.     from = view->transformWorld2Screen(p->vertex(0));
  165.     for (register long j=1; j<p->numVertices(); j++) {
  166.       to   = view->transformWorld2Screen(p->vertex(j));
  167.       drawLine((int)from[0], (int)from[1], (int)to[0], (int)to[1]);
  168.       from = to;
  169.     }
  170.     to = view->transformWorld2Screen(p->vertex(0));
  171.     drawLine((int)from[0], (int)from[1], (int)to[0], (int)to[1]);
  172.       }
  173.     }
  174.   }
  175. }
  176.  
  177. void BaseWindow::disableBuffering()
  178.   buffering = 0; 
  179. }
  180.  
  181. void BaseWindow::enableBuffering()
  182. {
  183.   buffering = 1; 
  184. }
  185.  
  186. void BaseWindow::setView(const Vector& eye, const Vector& lookat, const Vector& up, 
  187.              real fov)
  188. {
  189.   if (view != NULL)
  190.     delete view;
  191.   view = new ViewTransform(eye, lookat, up, fov, resX, resY);
  192.  
  193.   if (theOptions.verbose)
  194.     cerr << *view << '\n';
  195. }
  196.  
  197. void BaseWindow::setView(const BoundingBox& bbox, const Vector& up, real fov)
  198. {
  199.   if (view != NULL)
  200.     delete view;
  201.   view = new ViewTransform(bbox, up, fov, resX, resY);
  202.  
  203.   if (theOptions.verbose)
  204.     cerr << *view << '\n';
  205. }
  206.  
  207. /*
  208.  * Remove all the line segment und polygon from the buffer.
  209.  */
  210.  
  211. void BaseWindow::clearBuffer()
  212. {
  213.   if (lines != NULL) {
  214.     for (register long i=0; i<lines->count(); i++)
  215.       delete lines->item(i);
  216.     lines->remove_all();
  217.   }
  218.   if (polys != NULL) {
  219.     for (register long i=0; i<polys->count(); i++)
  220.       delete polys->item(i);
  221.     polys->remove_all();
  222.   }
  223. }
  224.