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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
class vector2d : public std::complex<float>{
public:
static const std::complex<float> EX;
static const std::complex<float> EY;
vector2d(float x = 0, float y = 0);
vector2d(const std::complex<float>& copy);
~vector2d() = default;
float operator*(const vector2d& lhs) const;
float x() const;
float y() const;
vector2d& operator/(vector2d& d) = delete;
vector2d& operator*=(vector2d& d) = delete;
vector2d& operator/=(vector2d& d) = delete;
};
vector2d::vector2d(float x, float y) : std::complex<float>(x, y){ }
float vector2d::x() const{
return real();
}
float vector2d::y() const{
return imag();
}
float vector2d::operator*(const vector2d& lhs) const{
float dotx, doty;
dotx = real() * lhs.x();
doty = imag() * lhs.y();
return (dotx + doty);
}
const std::complex<float> vector2d::EX = std::complex<float>(1,0);
const std::complex<float> vector2d::EY = std::complex<float>(0,1);
class Object: public ICloneable{
public:
Object(float depth = 0);
virtual ~Object() = default;
float depth() const;
virtual void render() const;
using PointType = vector2d;
using VectorType = vector2d;
virtual Object* clone() const{return new Object(*this);}
private:
float _depth;
};
Object::Object(float depth) : _depth(depth){ }
float Object::depth() const{
return _depth;
}
void Object::render() const{ }
class Factory;
class IRenderingInformation
{
public:
virtual Factory& getFactory() const = 0;
virtual unsigned int getColor() const = 0;
virtual ~IRenderingInformation() = default;
};
template<template<typename, typename> class C, typename T, typename Alloc>
void render(C<T, Alloc>& container)
{
std::sort(container.begin(), container.end());
}
class ObjectDepthComparer
{
Object* DepthComparerx;
Object* DepthComparery;
public:
ObjectDepthComparer(Object* x, Object* y) : DepthComparerx(x),DepthComparery(y) {}
bool operator()(Object* x, Object* y) {
if(x<y)
{
return true;
}
else{
return false;
}
}
ObjectDepthComparer& operator-(const std::complex<float>& __x) const
{
}
};
class Factory final
{
private:
Factory() = delete;
Factory(const Factory&) = delete;
Factory(Factory&&) = delete;
Factory& operator=(const Factory&) = delete;
Factory& operator=(Factory&&) = delete;
public:
typedef Object::VectorType VectorType;
typedef Object::PointType PointType;
typedef IRenderingInformation RenderingInformation;
typedef uint8_t Byte;
Factory(unsigned int width, unsigned int height, const VectorType& extents);
~Factory();
Object* createPoint(const PointType& center,
unsigned color, float depth = 1);
Object* createCircle(const PointType& center, float radius,
unsigned color, float depth = 1);
Object* createSquare(const PointType& center, const VectorType& half_edge,
unsigned color, float depth = 1);
Object* createPolygon(const PointType* vertices, int size,
unsigned color, float depth = 1);
Object* createBackground(const char* pixelData, unsigned int width,
unsigned int height, float depth = 0);
void writeToBitmap(const char* name);
static char* readFromBitmap(unsigned int& width, unsigned int& height,
const char* name);
static unsigned getColor(Byte r, Byte g, Byte b);
POINT toDevice(const VectorType& v) const;
HDC getContext() const;
private:
HBITMAP _bitmap;
HDC _context;
unsigned int _width;
unsigned int _height;
void* _bitmapData;
float _xFactor;
float _yFactor;
};
class Random
{
public:
Random() :
A(32993),
B(66047),
_high(683),
_low(1087)
{
}
unsigned operator()()
{
_high = ((A * _high) % B) & 0xff;
_low = ((A * _low) % B) & 0xff;
return ((_high << 8) | _low);
}
unsigned operator()(int n)
{
return operator()() % n;
}
float operator()(float a, float b)
{
return a + (b - a)*float(operator()()) / float(1 << 16);
}
private:
const unsigned A;
const unsigned B;
unsigned _high;
unsigned _low;
} random;
class RandomFactory
{
Factory& _factory;
public:
RandomFactory(Factory& factory) :
_factory(factory)
{
}
static Object::PointType getPoint()
{
return Object::PointType(random(0, 1), random(0, 1));
}
static Object::VectorType getVector()
{
return Object::VectorType(random(0, 0.25f), random(0, 0.25f));
}
Object* operator()()
{
const float depth = float(random(1000));
const unsigned color = Factory::getColor(
static_cast<Factory::Byte>(random(256)),
static_cast<Factory::Byte>(random(256)),
static_cast<Factory::Byte>(random(256))
);
switch (random(4))
{
case 0:
return _factory.createPoint(getPoint(),
color, depth);
case 1:
return _factory.createCircle(getPoint(), random(0, 0.25f),
color, depth);
case 2:
return _factory.createSquare(getPoint(), getVector(),
color, depth);
default:
{
const int size = 1 + random(10);
std::vector<Object::PointType> vertices;
vertices.resize(size);
for (int i = 0; i < size; ++i)
{
vertices[i] = getPoint();
}
return _factory.createPolygon(vertices.data(), size,
color, depth);
}
}
}
};
class Deleter
{
public:
void operator()(Object* obj)
{
delete obj;
}
};
int main()
{
const int WIDTH = 502;
const int HEIGHT = 502;
Factory factory(WIDTH, HEIGHT, Object::VectorType(1, 1));
std::vector<Object*> objects;
std::list<Object*> objects2;
// You must self-study the following STL functions:
// * std::generate_n
// * back_inserter
std::generate_n(std::back_inserter(objects), 10, RandomFactory(factory));
std::generate_n(std::back_inserter(objects2), 10, RandomFactory(factory));
unsigned int width;
unsigned int height;
const char* const data = Factory::readFromBitmap(width, height, "cube.bmp");
if (data != nullptr)
{
objects.push_back(factory.createBackground(data, width, height));
}
render(objects);
std::for_each(objects.begin(), objects.end(), Deleter{});
render(objects2);
std::for_each(objects2.begin(), objects2.end(), Deleter{});
factory.writeToBitmap("render_test.bmp");
delete[] data;
}
|