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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
#include <swidget/SFrame.h>
#include <swindow/SGEngine.h>
#include <swindow/SRasterizer.h>
#include <swidget/SIconFactory.h>
#include <sys/time.h>
#include <stdlib.h>
#define YUU_COUNT 1000
#define TEST_YUU 0
#define TEST_LINE 0
#define TEST_CIRCLE 0
#define TEST_WINDINGRULE_EVENODD 1
#define TEST_WINDINGRULE_NONZERO 0
#define TEST_ICON 0
#if TEST_YUU
static SImage* renderNam (int x, int y,
unsigned int width, unsigned int height, const SPen& pen) {
y = y - height;
SGEngine* engine = new SGEngine();
engine->scale (1.0, -1.0);
#include "nam.cpp"
}
static SImage* renderYuu (int x, int y,
unsigned int width, unsigned int height, const SPen& pen) {
y = y - height;
SGEngine* engine = new SGEngine();
engine->scale (3.2, -3.2);
#include "yuu.cpp"
}
static SImage* renderNam2 (int x, int y,
unsigned int width, unsigned int height, const SPen& pen) {
y = y - height;
SRasterizer* engine = new SRasterizer();
engine->scale (1.0, -1.0);
#include "nam.cpp"
}
static SImage* renderYuu2 (int x, int y,
unsigned int width, unsigned int height, const SPen& pen) {
y = y - height;
x = x;
SRasterizer* engine = new SRasterizer(8);
engine->scale (3.2, -3.2);
#include "yuu.cpp"
}
static SImage* renderZ (int x, int y,
unsigned int width, unsigned int height, const SPen& pen) {
y = y - height;
x = x;
SRasterizer* engine = new SRasterizer();
engine->scale (3.5, -3.5);
#include "z.cpp"
}
static SImage* renderGasko (int x, int y,
unsigned int width, unsigned int height, const SPen& pen) {
y = y - height;
x = x;
SRasterizer* engine = new SRasterizer();
engine->scale (3.5, -3.5);
#include "gasko.cpp"
}
#endif
class MyPanel : public SFrame
{
public:
SImage* nam;
SImage* yuu;
SImage* nam2;
SImage* yuu2;
SImage* z;
SImage* gasko;
MyPanel () {
#if TEST_YUU
struct timeval stop, start;
gettimeofday(&start, 0);
unsigned int NUM = YUU_COUNT;
setBackground(SColor("Gray"));
SPen pen(SColor("black"), SColor("white"), 1.0);
int val = NUM;
gettimeofday(&start, 0);
while (val--) {
nam = renderNam (0, 0, 80, 80, pen);
if (val > 1) delete nam;
};
gettimeofday(&stop, 0);
fprintf (stderr, "%d nam took %lu ms\n", NUM ,
(stop.tv_sec - start.tv_sec) * 1000
+ (stop.tv_usec - start.tv_usec) / 1000);
gettimeofday(&start, 0);
val = NUM;
while (val--) {
yuu = renderYuu (0, 0, 80, 80, pen);
if (val > 1) delete yuu;
}
gettimeofday(&stop, 0);
fprintf (stderr, "%d yuu took %lu ms\n", NUM ,
(stop.tv_sec - start.tv_sec) * 1000
+ (stop.tv_usec - start.tv_usec) / 1000);
gettimeofday(&start, 0);
val = NUM;
while (val--) {
nam2 = renderNam2 (0, 0, 80, 80, pen);
if (val > 1) delete nam2;
};
gettimeofday(&stop, 0);
fprintf (stderr, "%d nam2 took %lu ms\n", NUM ,
(stop.tv_sec - start.tv_sec) * 1000
+ (stop.tv_usec - start.tv_usec) / 1000);
gettimeofday(&start, 0);
val = NUM;
while (val--) {
yuu2 = renderYuu2 (0, 0, 80, 80, pen);
if (val > 1) delete yuu2;
}
gettimeofday(&stop, 0);
fprintf (stderr, "%d yuu2 took %lu ms\n", NUM ,
(stop.tv_sec - start.tv_sec) * 1000
+ (stop.tv_usec - start.tv_usec) / 1000);
gettimeofday(&start, 0);
val = NUM;
while (val--) {
z = renderZ (0, 0, 80, 80, pen);
if (val > 1) delete z;
}
gettimeofday(&stop, 0);
fprintf (stderr, "%d z took %lu ms\n", NUM ,
(stop.tv_sec - start.tv_sec) * 1000
+ (stop.tv_usec - start.tv_usec) / 1000);
val = NUM;
while (val--) {
gasko = renderGasko (0, 0, 80, 80, pen);
if (val > 1) delete gasko;
}
gettimeofday(&stop, 0);
fprintf (stderr, "%d gasko took %lu ms\n", NUM ,
(stop.tv_sec - start.tv_sec) * 1000
+ (stop.tv_usec - start.tv_usec) / 1000);
#endif
setBackground (SColor("Grey"));
};
virtual ~MyPanel() {
};
void drawNamYuu(SCanvas* w, int x, int y,
unsigned int width ,unsigned int height) {
#if TEST_YUU
w->putImage (80, 80, *nam);
w->putImage (160, 80, *yuu);
w->putImage (80, 160, *nam2);
w->putImage (160, 160, *yuu2);
w->putImage (240, 160, *z);
w->putImage (240, 80, *gasko);
#endif
}
void drawLines (SCanvas* w, int x, int y,
unsigned int width ,unsigned int height) {
#if TEST_LINE
SRasterizer* engine = new SRasterizer(5);
// SGEngine crashes.
//SGEngine* engine = new SGEngine();
// old SGEngine only support translate and scale at beginning.
// remove it at end.
//engine->scale (300, 300);
//engine->translate(300, 300);
engine->beginImage(0, 0, SString());
engine->moveto(0, 0);
engine->lineto(160, 160);
engine->lineto(0, 160);
SPen pen(SColor("red"), SColor("black"), 20.0);
engine->stroke(x, y, width, height, pen);
engine->newpath();
engine->moveto(180, 0);
engine->lineto(340, 160);
engine->lineto(180, 160);
SPen pen2(SColor("black"), SColor("black"), 20.0);
engine->stroke(x, y, width, height, pen2);
engine->newpath();
engine->moveto(0, 80);
engine->lineto(340, 80);
SColor color ("DarkGreen", 0.8);
SPen pen3(color, SColor("black"), 40.0);
engine->stroke(x, y, width, height, pen3);
engine->newpath();
double p = 0.552284749831;
engine->scale (300, 300);
engine->translate(300, 300);
engine->moveto(1, 0);
engine->curveto(1, p, p, 1, 0, 1);
engine->curveto(-p, 1, -1, p, -1, 0);
engine->curveto(-1, -p, -p, -1, 0, -1);
engine->curveto(p, -1, 1, -p, 1, 0);
engine->closepath();
SPen penF(SColor("#4C6CB3", 0.9), SColor("Gray"), 1.0);
engine->fill(x, y, width, height, penF);
// SPen penS(SColor("Black", 0.5), SColor("Gray"), 2.0);
// engine->stroke(x, y, width, height, penS);
SImage* im = engine->endImage();
w->putImage (40, 40, *im);
delete im;
delete engine;
#endif
}
void drawIcons(SCanvas* w, int x, int y,
unsigned int width ,unsigned int height) {
#if TEST_ICON
SIcon* icon = SIconFactory::getIcon("Preview");
if (icon) {
SImage im = icon->getImage();
SVGBase * svgBase = im.getSVGBase();
if (svgBase) {
svgBase->setCanvas (w, SColor("Gray"));
svgBase->render(40, 40, 10.0);
}
}
icon = SIconFactory::getIcon("CheckFinished");
if (icon) {
SImage im = icon->getImage();
SVGBase * svgBase = im.getSVGBase();
if (svgBase) {
svgBase->setCanvas (w, SColor("Gray"));
svgBase->render(280, 40, 10.0);
}
}
#endif
}
// This is just an approximation of a circle.
void drawCircle (SCanvas* w, int x, int y,
unsigned int width ,unsigned int height) {
#if TEST_CIRCLE
//(4/3)*tan(pi/8) = 4*(sqrt(2)-1)/3 = 0.552284749831
SRasterizer* engine = new SRasterizer(5);
//SGEngine* engine = new SGEngine();
engine->scale (300, 300);
engine->translate (300, 300);
double p = 0.552284749831;
engine->beginImage(100, 100, SString());
engine->moveto(1, 0);
engine->curveto(1, p, p, 1, 0, 1);
engine->curveto(-p, 1, -1, p, -1, 0);
engine->curveto(-1, -p, -p, -1, 0, -1);
engine->curveto(p, -1, 1, -p, 1, 0);
engine->closepath();
SPen penF(SColor("#4C6CB3"), SColor("Gray"), 1.0);
engine->fill(0, 0, 620, 620, penF);
SPen penS(SColor("Black"), SColor("Gray"), 10.0);
engine->stroke(0, 0, 620, 620, penS);
SImage* im = 0;
im = engine->endImage();
if (im) {
fprintf (stderr, "PutImage offScreen=%d\n", im->offScreen);
w->putImage (100, 100, *im);
delete im;
}
fprintf (stderr, "PutImage finished.\n");
//for (int i=0; i<100000; i++) {
// fprintf (stdout, "%d\n", i);
//}
delete engine;
#endif
}
// This is just an approximation of a circle.
void drawWindingRule (SCanvas* w, int x, int y,
unsigned int width ,unsigned int height) {
#if TEST_WINDINGRULE_NONZERO || TEST_WINDINGRULE_EVENODD
//(4/3)*tan(pi/8) = 4*(sqrt(2)-1)/3 = 0.552284749831
#if TEST_WINDINGRULE_EVENODD
SRasterizer* engine = new SRasterizer(5, SRasterizer::EVENODD);
#else
SRasterizer* engine = new SRasterizer(5, SRasterizer::NONZERO);
#endif
//SGEngine* engine = new SGEngine();
engine->scale (150, 150);
engine->translate (150, 150);
double p = 0.552284749831;
engine->beginImage(100, 100, SString());
engine->moveto(1, 0);
engine->curveto(1, p, p, 1, 0, 1);
engine->curveto(-p, 1, -1, p, -1, 0);
engine->curveto(-1, -p, -p, -1, 0, -1);
engine->curveto(p, -1, 1, -p, 1, 0);
engine->closepath();
engine->translate (150, 0);
engine->moveto(1, 0);
engine->curveto(1, p, p, 1, 0, 1);
engine->curveto(-p, 1, -1, p, -1, 0);
engine->curveto(-1, -p, -p, -1, 0, -1);
engine->curveto(p, -1, 1, -p, 1, 0);
engine->closepath();
engine->translate (-75, 125);
engine->moveto(1, 0);
engine->curveto(1,-p, p,-1, 0,-1);
engine->curveto(-p,-1, -1,-p, -1, 0);
engine->curveto(-1, p, -p, 1, 0, 1);
engine->curveto(p, 1, 1, p, 1, 0);
engine->closepath();
SPen penF(SColor("#4C6CB3"), SColor("Gray"), 1.0);
engine->fill(0, 0, 620, 3620, penF);
SPen penS(SColor("Black"), SColor("Gray"), 10.0);
engine->stroke(0, 0, 620, 620, penS);
SImage* im = 0;
im = engine->endImage();
if (im) {
fprintf (stderr, "PutImage offScreen=%d\n", im->offScreen);
w->putImage (100, 100, *im);
delete im;
}
fprintf (stderr, "PutImage finished.\n");
//for (int i=0; i<100000; i++) {
// fprintf (stdout, "%d\n", i);
//}
delete engine;
#endif
}
void redraw(SCanvas* w, int x, int y,
unsigned int width ,unsigned int height) {
drawWindingRule (w,x,y,width, height);
drawCircle (w,x,y,width, height);
drawNamYuu (w,x,y,width, height);
drawLines (w,x,y,width, height);
drawIcons (w,x,y,width, height);
}
virtual bool windowClose (SWindow* w) {
exit(0);
//return true;
}
};
int
main (int argc, char* argv[])
{
SAwt awt;
//SPanel::setAllDoubleBuffered(true);
MyPanel * panel = new MyPanel();
panel->setThisDoubleBuffered(true);
//panel->setBackground(SColor("#7f7f7f"));
panel->resize (SDimension(800,800));
panel->show();
SEventHandler::start();
return (0);
}
|