]> git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/graphics.cpp
fixed the error 'macro parameters must be comma-separated' reported by running gcc...
[wxWidgets.git] / tests / benchmarks / graphics.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: graphics.cpp
3 // Purpose: Some benchmarks for measuring graphics operations performance
4 // Author: Vadim Zeitlin
5 // Created: 2008-04-13
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/app.h"
12 #include "wx/frame.h"
13 #include "wx/cmdline.h"
14 #include "wx/dcclient.h"
15 #include "wx/dcmemory.h"
16 #include "wx/stopwatch.h"
17
18 struct GraphicsBenchmarkOptions
19 {
20 GraphicsBenchmarkOptions()
21 {
22 mapMode = 0;
23 penWidth = 0;
24
25 width = 800;
26 height = 600;
27
28 numLines = 10000;
29 }
30
31 long mapMode,
32 penWidth,
33 width,
34 height,
35 numLines;
36 } opts;
37
38 class GraphicsBenchmarkFrame : public wxFrame
39 {
40 public:
41 GraphicsBenchmarkFrame()
42 : wxFrame(NULL, wxID_ANY, "wxWidgets Graphics Benchmark")
43 {
44 Connect(wxEVT_PAINT,
45 wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint));
46
47 Show();
48 SetClientSize(opts.width, opts.height);
49 wxClientDC dc(this);
50 BenchmarkLines("client", dc);
51
52 wxBitmap bmp(opts.width, opts.height);
53 wxMemoryDC dc2(bmp);
54 BenchmarkLines("memory", dc2);
55 }
56
57 protected:
58 void OnPaint(wxPaintEvent& WXUNUSED(event))
59 {
60 wxPaintDC dc(this);
61
62 BenchmarkLines(" paint", dc);
63
64 wxTheApp->ExitMainLoop();
65 }
66
67 void BenchmarkLines(const char *msg, wxDC& dc)
68 {
69 if ( opts.mapMode != 0 )
70 dc.SetMapMode(opts.mapMode);
71 if ( opts.penWidth != 0 )
72 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
73
74 wxPrintf("Benchmarking %s DC: ", msg);
75
76 wxStopWatch sw;
77 int x = 0,
78 y = 0;
79 for ( int n = 0; n < opts.numLines; n++ )
80 {
81 int x1 = rand() % opts.width,
82 y1 = rand() % opts.height;
83
84 dc.DrawLine(x, y, x1, y1);
85
86 x = x1;
87 y = y1;
88 }
89
90 const long t = sw.Time();
91
92 wxPrintf("%d lines done in %lums = %gus/line\n",
93 opts.numLines, t, (1000. * t)/opts.numLines);
94 }
95 };
96
97 class GraphicsBenchmarkApp : public wxApp
98 {
99 public:
100 virtual void OnInitCmdLine(wxCmdLineParser& parser)
101 {
102 static const wxCmdLineEntryDesc desc[] =
103 {
104 { wxCMD_LINE_OPTION, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER },
105 { wxCMD_LINE_OPTION, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER },
106 { wxCMD_LINE_OPTION, "w", "width", "", wxCMD_LINE_VAL_NUMBER },
107 { wxCMD_LINE_OPTION, "h", "height", "", wxCMD_LINE_VAL_NUMBER },
108 { wxCMD_LINE_OPTION, "L", "lines", "", wxCMD_LINE_VAL_NUMBER },
109 };
110
111 parser.SetDesc(desc);
112 }
113
114 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
115 {
116 if ( parser.Found("m", &opts.mapMode) &&
117 (opts.mapMode < 1 || opts.mapMode > wxMM_METRIC) )
118 return false;
119 if ( parser.Found("p", &opts.penWidth) && opts.penWidth < 1 )
120 return false;
121 if ( parser.Found("w", &opts.width) && opts.width < 1 )
122 return false;
123 if ( parser.Found("h", &opts.height) && opts.height < 1 )
124 return false;
125 if ( parser.Found("L", &opts.numLines) && opts.numLines < 1 )
126 return false;
127
128 return true;
129 }
130
131 virtual bool OnInit()
132 {
133 if ( !wxApp::OnInit() )
134 return false;
135
136 new GraphicsBenchmarkFrame;
137
138 return true;
139 }
140 };
141
142 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp)