1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
#if !defined (PLAYPEN_H)
#define PLAYPEN_H
#include "fgw_text.h"
#include <bitset>
#include <iostream>
#include <string>
namespace studentgraphics {
class playpen;
// Frequently used elements of the standard library.
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::istream;
using std::ostream;
using std::bitset;
// Wrapper for OS-specific sleep function.
void Wait(unsigned ms);
namespace detail {
// Forward declare the class that provides the OS specific code
class SingletonWindow;
}
class hue{
public:
hue(unsigned char code = 0):code_(code){}
hue(int code):code_(code%256){}
operator unsigned char()const{
return code_;}
bool operator[](int bit)const {
if(bit <0 or bit >7)
return false;
return bitset<8>(code_)[bit];}
class ref;
friend class hue::ref;
class ref{
public:
ref(hue & h, int n):h_(h),bit(n){};
operator bool(){
if (bit <0 or bit >7)
return false;
return bitset<8>(h_)[bit];}
void operator=(bool val){
if(bit <0 or bit >7)
return; bitset<8> v(h_); v[bit] = val; h_ = (unsigned char)v.to_ulong();}
private:
hue & h_;
int bit;
};
ref operator[](int bit){
return ref(*this, bit);}
unsigned char value()const{
return code_;}
hue operator +=(hue h){code_ |= h.code_;
return code_;}
hue operator -=(hue h){code_ &= ~h.code_;
return code_;}
private:
unsigned char code_;
};
inline hue operator+(hue code1, hue code2){
return (code1.value() | code2.value());}
inline hue operator+(unsigned char code1, hue code2){
return (code1 | code2.value());}
inline hue operator+(hue code1, unsigned char code2){
return (code1.value() | code2);}
inline hue operator-(hue code1, hue code2){
return (code1.value() & ~(code2.value()));}
inline hue operator-(unsigned char code1, hue code2){
return (code1 & ~(code2.value()));}
inline hue operator-(hue code1, unsigned char code2){
return (code1.value() & ~(code2));}
hue operator*(hue, hue);
hue operator/(hue, hue);
hue const white(255);
hue const black(0);
hue const red4(128);
hue const red2(64);
hue const red1(32);
hue const green4(16);
hue const green2(8);
hue const green1(1);
hue const blue4(4);
hue const blue2(2);
hue const blue1(1);
hue const torquoise(1);
inline istream & operator >> (istream & in , hue & shade){
shade = (std::cin == in ? fgw::read<int>() : fgw::read<int>(in));
return in;
}
typedef unsigned char palettecode;
struct HueRGB {
unsigned char r;
unsigned char g;
unsigned char b;
HueRGB() : r(0), g(0), b(0) {}
HueRGB(unsigned char red, unsigned char green, unsigned char blue) :
r(red), g(green), b(blue) {}
};
int const colours = 0x100; // number of colours in palette
int const Xpixels = 512; // pixels across
int const Ypixels = 512;
enum plotmode { direct, additive, filter, disjoint};
class pixelsize {
public:
pixelsize(int size =1);
int size()const {
return dim;}
bool size(int i){
if(i<1 || i >64)
return false; dim = i;
return true;}
private:
int dim;
};
inline pixelsize::pixelsize(int size):dim(size){
if(dim <1) dim = 1;}
// Front end.
class playpen {
public:
class exception{
public:
enum level{unknown, fatal, error, warning, info};
exception():lev_(unknown), message_("unknown problem"){};
exception(level l,char const * message):lev_(l), message_(message){};
void report()const {cout << message_ << endl;}
// Compiler generated copying and destruction are OK.
private:
level lev_;
string message_;
};
class raw_pixel_data {
int const x_, y_;
public:
explicit raw_pixel_data(int xval=0, int yval=0):x_(xval), y_(yval) {};
int x() const {
return x_;}
int y() const {
return y_;}
};
class origin_data: public raw_pixel_data {
public:
explicit origin_data(int xval=0, int yval = 0):raw_pixel_data(xval, yval){}
};
playpen(hue background = white);
~playpen();
playpen(playpen const & pp);
// Update the physical display.
playpen const& display() const;
// Plot a pixel. Changes are not visible until next display() call.
playpen& plot(int x, int y, hue h);
playpen& plot(double x, double y, hue h){
return plot(int(x+0.5), int(y+0.5), h);}
hue get_hue(int x, int y)const;
// Set the plotting mode for subsequent calls to plot().
plotmode setplotmode(plotmode pm);
playpen& origin(int xval, int yval){xorg = xval; yorg = yval;
return *this;}
origin_data origin()const {
return origin_data(xorg, yorg);}
bool scale(int i){
return pixsize.size(i);}
int scale()const {
return pixsize.size();}
raw_pixel_data get_raw_xy(int i, int j){
return raw_pixel_data(xorg+ i * pixsize.size(),
yorg - j * pixsize.size());}
// Clear all pixels to the specified hue. Changes is not visible
// until the next display() call.
playpen& clear(hue h = white);
playpen& rgbpalette();
playpen& setpalettentry(hue, HueRGB const & target);
// Get the RGB value mapped to a specified hue.
HueRGB getpalettentry(hue) const;
// Update the physical palette, possibly changing the display.
playpen const & updatepalette() const;
// Persistence (a.k.a serialization). Ensure stream is opened in
// binary (not text) mode (at least for MSVC6 - a bug, I think).
// Save all state to binary file.
ostream & save(ostream &)const;
// Restore all state from binary file. Automatically updates physical
// display to reflect changed state.
istream & restore(istream &);
hue getrawpixel(int x, int y) const;
void setrawpixel(int x, int y, hue h);
private:
plotmode pmode;
int xorg, yorg;
pixelsize pixsize;
static detail::SingletonWindow * graphicswindow;
};// class playpen
void LoadPlaypen(playpen& p, std::string filename);
void SavePlaypen(playpen const & p, std::string filename);
}
namespace fgw {
using namespace studentgraphics;
}
#endif
|