From 98108bde13e33712dfa136423e62a8b4b277edac Mon Sep 17 00:00:00 2001 From: cxl Date: Mon, 9 Feb 2009 08:22:30 +0000 Subject: [PATCH] PainterExamples git-svn-id: svn://ultimatepp.org/upp/trunk@844 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- examples/PainterExamples/Curves.cpp | 4 +- examples/PainterExamples/Lion.cpp | 98 +++++++++----------- examples/PainterExamples/PainterExamples.upp | 2 + examples/PainterExamples/Path.cpp | 19 ++++ examples/PainterExamples/ThinLine.cpp | 29 ++++++ examples/PainterExamples/main.cpp | 4 +- 6 files changed, 100 insertions(+), 56 deletions(-) create mode 100644 examples/PainterExamples/Path.cpp create mode 100644 examples/PainterExamples/ThinLine.cpp diff --git a/examples/PainterExamples/Curves.cpp b/examples/PainterExamples/Curves.cpp index b85e3a68e..ee452bf78 100644 --- a/examples/PainterExamples/Curves.cpp +++ b/examples/PainterExamples/Curves.cpp @@ -2,7 +2,9 @@ void Triangle(Painter& sw) { - sw.Move(100, 100).Line(300, 100).Line(200, 300).Fill(Red()).Stroke(3, Blue()); + sw.Move(100, 100).Line(300, 102).Line(200, 300).Fill(Red()).Stroke(3, Blue()); + sw.Translate(500, 0); + sw.Move(100, 100).Line(300, 102).Line(200, 300).Fill(Red()).Dash("10").Stroke(5, Blue()); } void Cubic(Painter& sw) diff --git a/examples/PainterExamples/Lion.cpp b/examples/PainterExamples/Lion.cpp index c8f85487a..5cf83000b 100644 --- a/examples/PainterExamples/Lion.cpp +++ b/examples/PainterExamples/Lion.cpp @@ -145,64 +145,53 @@ static char g_lion[] = "M 147,338 L 142,341 L 143,345 L 141,354 L 147,343 L 147,338 L 147,338 L 147,338\n" "M 157,342 L 156,349 L 150,356 L 157,353 L 163,346 L 162,342 L 157,342 L 157,342 L 157,342\n" "M 99,265 L 96,284 L 92,299 L 73,339 L 73,333 L 87,300 L 99,265 L 99,265 L 99,265\n"; - - -void PaintLion(Painter& sw) + +struct ColorPolygon : Moveable { + Vector point; + Color color; +}; + +Vector Lion() { const char* ptr = g_lion; - unsigned npaths = 0; - Color color = Null; - Vector p; - - while (*ptr) { - if (*ptr != 'M' && isalnum(*ptr)) { - if(!IsNull(color)) { - sw.Fill(color); - p.Clear(); - } - unsigned c = 0; - sscanf(ptr, "%x", &c); - color = Color(c >> 16, (c >> 8) & 255, c & 255); - while (*ptr && *ptr != '\n') - ptr++; - if (*ptr == '\n') - ptr++; + Vector data; + Color color; + CParser p(g_lion); + while(!p.IsEof()) + if(p.Char('M')) { + data.Add().color = color; + goto line; + } + else + if(p.Char('L')) { + line: + double x = p.ReadDouble(); + p.Char(','); + double y = p.ReadDouble(); + data.Top().point.Add(Pointf(x, y)); } else { - double x = 0.0; - double y = 0.0; - - while (*ptr && *ptr != '\n') { - int c = *ptr; - - while (*ptr && !isdigit(*ptr)) - ptr++; - - x = ScanDouble(ptr); - - while (*ptr && isdigit(*ptr)) - ptr++; - - while (*ptr && !isdigit(*ptr)) - ptr++; - - y = ScanDouble(ptr); - - if(c == 'M') - sw.Move(x, y); - else - sw.Line(x, y); - while (*ptr && isdigit(*ptr)) - ptr++; - while (*ptr && *ptr != '\n' && !isalpha(*ptr)) - ptr++; - } - if (*ptr == '\n') - ptr++; + dword c = p.ReadNumber(16); + color = Color(c >> 16, (c >> 8) & 255, c & 255); } + return data; +} + +void PaintLion(Painter& sw) +{ + static Vector l = Lion(); + for(int i = 0; i < l.GetCount(); i++) { + Vector& p = l[i].point; + sw.Move(p[0].x, p[0].y); + for(int j = 1; j < p.GetCount(); j++) + sw.Line(p[j].x, p[j].y); + sw.Fill(l[i].color); } - sw.Fill(color); - +} + +void Demo(Painter& sw) +{ + PaintLion(sw); String txt = "U++ Painter"; Font fnt = Roman(180).Bold(); Size tsz = GetTextSize(txt, fnt); @@ -210,7 +199,7 @@ void PaintLion(Painter& sw) .Fill(TestImg::test(), 0, 400, 500, 400, FILL_REFLECT) .Stroke(5, 0, 500, Blue(), tsz.cx, 500, LtRed()) .Stroke(1, White()); - + for(int i = 0; i < 10; i++) { sw.Begin(); sw.Opacity(0.5); @@ -223,5 +212,6 @@ void PaintLion(Painter& sw) } INITBLOCK { - RegisterExample("U++ Painter", PaintLion); + RegisterExample("Lion", PaintLion); + RegisterExample("U++ Painter", Demo); } diff --git a/examples/PainterExamples/PainterExamples.upp b/examples/PainterExamples/PainterExamples.upp index ae19848d1..ea45bb7d7 100644 --- a/examples/PainterExamples/PainterExamples.upp +++ b/examples/PainterExamples/PainterExamples.upp @@ -24,6 +24,8 @@ file RadialMaskBlending.cpp, RichText.cpp, Stroke.cpp, + Path.cpp, + ThinLine.cpp, Examples.lay, Test.iml; diff --git a/examples/PainterExamples/Path.cpp b/examples/PainterExamples/Path.cpp new file mode 100644 index 000000000..f3c23f1ab --- /dev/null +++ b/examples/PainterExamples/Path.cpp @@ -0,0 +1,19 @@ +#include "Examples.h" + +void Path(Painter& sw) +{ + sw.Translate(52, 52); + for(int i = 0; i < 2; i++) { + sw.Rectangle(20, 20, 60, 60); + sw.Move(0, 0); + sw.Cubic(99, 0, 50, 50, 99, 99); + sw.Cubic(0, 99, 50, 50, 0, 0); + sw.EvenOdd(i).Fill(Green()); + sw.Stroke(1, Black()); + sw.Translate(120, 0); + } +} + +INITBLOCK { + RegisterExample("Path example", Path); +} diff --git a/examples/PainterExamples/ThinLine.cpp b/examples/PainterExamples/ThinLine.cpp new file mode 100644 index 000000000..2fc3a2d14 --- /dev/null +++ b/examples/PainterExamples/ThinLine.cpp @@ -0,0 +1,29 @@ +#include "Examples.h" + + +void ThinPolygon(Painter& sw) +{ + sw.Translate(52, 52); + for(int i = 0; i < 2; i++) { + sw.Move(36.000000, 142.000000); + sw.Line(480.000000, 148.000000); + sw.Line(429.000000, 148.000000); + sw.Line(36.000000, 142.000000); + sw.Fill(Black()); +/* + sw.Rectangle(20, 20, 60, 60); + + + sw.Move(0, 0); + sw.Cubic(99, 0, 50, 50, 99, 99); + sw.Cubic(0, 99, 50, 50, 0, 0); + sw.EvenOdd(i).Fill(Green()); + sw.Stroke(1, Black()); + sw.Translate(120, 0); +*/ + } +} + +INITBLOCK { + RegisterExample("Thin polygon", ThinPolygon); +} diff --git a/examples/PainterExamples/main.cpp b/examples/PainterExamples/main.cpp index a5102da3e..821ceb525 100644 --- a/examples/PainterExamples/main.cpp +++ b/examples/PainterExamples/main.cpp @@ -30,7 +30,9 @@ void App::DoPaint0(Painter& sw) sw.Opacity(~ctrl.opacity); sw.LineCap(~ctrl.linecap); sw.LineJoin(~ctrl.linejoin); + { PAINTER_TIMING("FILL"); sw.Clear(White()); + } PAINTER_TIMING("Paint"); if(list.IsCursor()) Examples()[list.GetCursor()].example(sw); @@ -64,7 +66,7 @@ void App::Benchmark() for(;;) { time = GetTickCount(); if(time - time0 > 1000) break; - ImageBuffer ib(GetSize()); + ImageBuffer ib(800, 600); BufferPainter sw(ib); DoPaint(sw); n++;