OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

base.cpp
Go to the documentation of this file.
1 /* $Id: base.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "../stdafx.h"
13 #include "base.hpp"
14 #include "../core/math_func.hpp"
15
16 #include "../safeguards.h"
17
18 void Blitter::DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash)
19 {
20 int dy;
21 int dx;
22 int stepx;
23 int stepy;
24
25 dy = (y2 - y) * 2;
26 if (dy < 0) {
27 dy = -dy;
28 stepy = -1;
29 } else {
30 stepy = 1;
31 }
32
33 dx = (x2 - x) * 2;
34 if (dx < 0) {
35 dx = -dx;
36 stepx = -1;
37 } else {
38 stepx = 1;
39 }
40
41 if (dx == 0 && dy == 0) {
42 /* The algorithm below cannot handle this special case; make it work at least for line width 1 */
43 if (x >= 0 && x < screen_width && y >= 0 && y < screen_height) this->SetPixel(video, x, y, colour);
44 return;
45 }
46
47 int frac_diff = width * max(dx, dy);
48 if (width > 1) {
49 /* compute frac_diff = width * sqrt(dx*dx + dy*dy)
50 * Start interval:
51 * max(dx, dy) <= sqrt(dx*dx + dy*dy) <= sqrt(2) * max(dx, dy) <= 3/2 * max(dx, dy) */
52 int frac_sq = width * width * (dx * dx + dy * dy);
53 int frac_max = 3 * frac_diff / 2;
54 while (frac_diff < frac_max) {
55 int frac_test = (frac_diff + frac_max) / 2;
56 if (frac_test * frac_test < frac_sq) {
57 frac_diff = frac_test + 1;
58 } else {
59 frac_max = frac_test - 1;
60 }
61 }
62 }
63
64 int gap = dash;
65 if (dash == 0) dash = 1;
66 int dash_count = 0;
67 if (dx > dy) {
68 int y_low = y;
69 int y_high = y;
70 int frac_low = dy - frac_diff / 2;
71 int frac_high = dy + frac_diff / 2;
72
73 while (frac_low + dx / 2 < 0) {
74 frac_low += dx;
75 y_low -= stepy;
76 }
77 while (frac_high - dx / 2 >= 0) {
78 frac_high -= dx;
79 y_high += stepy;
80 }
81 x2 += stepx;
82
83 while (x != x2) {
84 if (dash_count < dash && x >= 0 && x < screen_width) {
85 for (int y = y_low; y != y_high; y += stepy) {
86 if (y >= 0 && y < screen_height) this->SetPixel(video, x, y, colour);
87 }
88 }
89 if (frac_low >= 0) {
90 y_low += stepy;
91 frac_low -= dx;
92 }
93 if (frac_high >= 0) {
94 y_high += stepy;
95 frac_high -= dx;
96 }
97 x += stepx;
98 frac_low += dy;
99 frac_high += dy;
100 if (++dash_count >= dash + gap) dash_count = 0;
101 }
102 } else {
103 int x_low = x;
104 int x_high = x;
105 int frac_low = dx - frac_diff / 2;
106 int frac_high = dx + frac_diff / 2;
107
108 while (frac_low + dy / 2 < 0) {
109 frac_low += dy;
110 x_low -= stepx;
111 }
112 while (frac_high - dy / 2 >= 0) {
113 frac_high -= dy;
114 x_high += stepx;
115 }
116 y2 += stepy;
117
118 while (y != y2) {
119 if (dash_count < dash && y >= 0 && y < screen_height) {
120 for (int x = x_low; x != x_high; x += stepx) {
121 if (x >= 0 && x < screen_width) this->SetPixel(video, x, y, colour);
122 }
123 }
124 if (frac_low >= 0) {
125 x_low += stepx;
126 frac_low -= dy;
127 }
128 if (frac_high >= 0) {
129 x_high += stepx;
130 frac_high -= dy;
131 }
132 y += stepy;
133 frac_low += dx;
134 frac_high += dx;
135 if (++dash_count >= dash + gap) dash_count = 0;
136 }
137 }
138 }