Speech bubbles can point down right.
[scummvm-innocent.git] / graphics / primitives.cpp
blobb98b2cefa502ba763f6dbfb2f637ccc961495c28
1 /* ScummVM - Graphic Adventure Engine
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $URL$
22 * $Id$
25 #include "common/util.h"
27 namespace Graphics {
29 void drawLine(int x0, int y0, int x1, int y1, int color, void (*plotProc)(int, int, int, void *), void *data) {
30 // Bresenham's line algorithm, as described by Wikipedia
31 const bool steep = ABS(y1 - y0) > ABS(x1 - x0);
33 if (steep) {
34 SWAP(x0, y0);
35 SWAP(x1, y1);
38 const int delta_x = ABS(x1 - x0);
39 const int delta_y = ABS(y1 - y0);
40 const int delta_err = delta_y;
41 int x = x0;
42 int y = y0;
43 int err = 0;
45 const int x_step = (x0 < x1) ? 1 : -1;
46 const int y_step = (y0 < y1) ? 1 : -1;
48 if (steep)
49 (*plotProc)(y, x, color, data);
50 else
51 (*plotProc)(x, y, color, data);
53 while (x != x1) {
54 x += x_step;
55 err += delta_err;
56 if (2 * err > delta_x) {
57 y += y_step;
58 err -= delta_x;
60 if (steep)
61 (*plotProc)(y, x, color, data);
62 else
63 (*plotProc)(x, y, color, data);
68 // FIXME: This is a limited version of thick line drawing
69 // it draws striped lines at some angles. Better algorithm could
70 // be found here:
72 // http://homepages.enterprise.net/murphy/thickline/index.html
74 // Feel free to replace it with better implementation
75 void drawThickLine(int x0, int y0, int x1, int y1, int thickness, int color, void (*plotProc)(int, int, int, void *), void *data) {
76 const bool steep = ABS(y1 - y0) > ABS(x1 - x0);
78 if (steep) {
79 SWAP(x0, y0);
80 SWAP(x1, y1);
83 float dx = x1 - x0;
84 float dy = y1 - y0;
85 float d = (float)sqrt(dx * dx + dy * dy);
87 if (!d)
88 return;
90 int thickX = (int)((float)thickness * dy / d / 2);
91 int thickY = (int)((float)thickness * dx / d / 2);
93 const int delta_x = ABS(x1 - x0);
94 const int delta_y = ABS(y1 - y0);
95 const int delta_err = delta_y;
96 int x = x0;
97 int y = y0;
98 int err = 0;
100 const int x_step = (x0 < x1) ? 1 : -1;
101 const int y_step = (y0 < y1) ? 1 : -1;
103 if (steep)
104 drawLine(y - thickY, x + thickX, y + thickY, x - thickX, color, plotProc, data);
105 else
106 drawLine(x - thickX, y + thickY, x + thickX, y - thickY, color, plotProc, data);
108 while (x != x1) {
109 x += x_step;
110 err += delta_err;
111 if (2 * err > delta_x) {
112 y += y_step;
113 err -= delta_x;
115 if (steep)
116 drawLine(y - thickY, x + thickX, y + thickY, x - thickX, color, plotProc, data);
117 else
118 drawLine(x - thickX, y + thickY, x + thickX, y - thickY, color, plotProc, data);
122 } // End of namespace Graphics