| 1 | // RenderArea2D.hxx - a class to manage 2D polygon-based drawing |
| 2 | // for a complex instrument (eg. GPS). |
| 3 | // |
| 4 | // Written by David Luff, started 2005. |
| 5 | // |
| 6 | // Copyright (C) 2005 - David C Luff - daveluff AT ntlworld.com |
| 7 | // |
| 8 | // This program is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU General Public License as |
| 10 | // published by the Free Software Foundation; either version 2 of the |
| 11 | // License, or (at your option) any later version. |
| 12 | // |
| 13 | // This program is distributed in the hope that it will be useful, but |
| 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | // |
| 22 | // $Id$ |
| 23 | |
| 24 | #pragma once |
| 25 | |
| 26 | #include <osg/ref_ptr> |
| 27 | #include <osg/State> |
| 28 | #include <osg/StateSet> |
| 29 | |
| 30 | #include <simgear/compiler.h> |
| 31 | #include <simgear/math/SGMath.hxx> |
| 32 | |
| 33 | #include <vector> |
| 34 | |
| 35 | |
| 36 | enum RA2DDrawingType { |
| 37 | RA2D_LINE, |
| 38 | RA2D_QUAD, |
| 39 | RA2D_PIXEL |
| 40 | }; |
| 41 | |
| 42 | struct RA2DPrimitive { |
| 43 | RA2DPrimitive(); |
| 44 | |
| 45 | RA2DDrawingType type; |
| 46 | int x1, y1, x2, y2; |
| 47 | bool invert; |
| 48 | bool debug; |
| 49 | }; |
| 50 | |
| 51 | class RenderArea2D { |
| 52 | |
| 53 | public: |
| 54 | RenderArea2D(int logx, int logy, int sizex, int sizey, int posx, int posy); |
| 55 | ~RenderArea2D(); |
| 56 | |
| 57 | void SetPixelColor(const float* rgba); |
| 58 | void SetBackgroundColor(const float* rgba); |
| 59 | void SetPosition(int posx, int posy); |
| 60 | void SetLogicalSize(int logx, int logy); |
| 61 | void SetActualSize(int sizex, int sizey); |
| 62 | |
| 63 | // Set clipping region in logical units |
| 64 | void SetClipRegion(int x1, int y1, int x2, int y2); |
| 65 | // Set clip region to be the same as the rendered area (default) |
| 66 | void ResetClipRegion(); |
| 67 | |
| 68 | // The DrawXXX functions place the shapes in the buffer, specified |
| 69 | // in logical units, and clipped to the current clip region. |
| 70 | void DrawPixel(int x, int y, bool invert = false); |
| 71 | void DrawLine(int x1, int y1, int x2, int y2); |
| 72 | void DrawQuad(int x1, int y1, int x2, int y2, bool invert = false); |
| 73 | void DrawBackground(); |
| 74 | |
| 75 | // Call Draw to have the buffer contents drawn and then cleared. |
| 76 | void Draw(osg::State& state); |
| 77 | |
| 78 | // Clear the buffer contents |
| 79 | void Flush(); |
| 80 | |
| 81 | // Turn debugging on or off. |
| 82 | inline void SetDebugging(bool b) { _ra2d_debug = b; } |
| 83 | |
| 84 | private: |
| 85 | int _logx, _logy; // logical size of rendered area |
| 86 | int _sizex, _sizey; // Actual size of rendered area |
| 87 | int _posx, _posy; // Position of rendered area |
| 88 | int _clipx1, _clipx2, _clipy1, _clipy2; // Current clipping region in *logical* units |
| 89 | |
| 90 | float _backgroundColor[4]; |
| 91 | float _pixelColor[4]; |
| 92 | |
| 93 | // Drawing specified in logical units |
| 94 | void DoDrawPixel(int x, int y, bool invert = false); |
| 95 | void DoDrawLine(int x1, int y1, int x2, int y2); |
| 96 | void DoDrawQuad(int x1, int y1, int x2, int y2, bool invert = false); |
| 97 | void DoDrawBackground(); |
| 98 | |
| 99 | // Actual rendering routines copied from Atlas |
| 100 | void SetRenderColor( const float *rgb ); |
| 101 | void RenderQuad( const SGVec2f *p); |
| 102 | void RenderQuad( const SGVec2f *p, const SGVec4f *color ); |
| 103 | |
| 104 | std::vector<RA2DPrimitive> drawing_list; |
| 105 | |
| 106 | // Control whether to output debugging output |
| 107 | bool _ra2d_debug; |
| 108 | }; |