| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/2dlib/pen.cpp $ |
| 21 | * $Revision: 5 $ |
| 22 | * $Date: 12/19/97 5:28p $ |
| 23 | * $Author: Samir $ |
| 24 | * |
| 25 | * line and rectangle drawing |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/2dlib/pen.cpp $ |
| 28 | * |
| 29 | * 5 12/19/97 5:28p Samir |
| 30 | * Took out old rasterization code. Moved to renderer library. |
| 31 | * |
| 32 | * 4 12/19/97 5:23p Samir |
| 33 | * Cleaned up a lot of code to call renderer code. |
| 34 | * |
| 35 | * 3 12/19/97 12:31p Samir |
| 36 | * Better renderer support. |
| 37 | * |
| 38 | * 21 6/16/97 4:45p Samir |
| 39 | * Added rendering line hooks. |
| 40 | * |
| 41 | * 20 6/12/97 6:29p Samir |
| 42 | * |
| 43 | * 19 6/06/97 12:18p Samir |
| 44 | * OpenGL HACKS to call renderer for some drawing functions. Must fix. |
| 45 | * |
| 46 | * 18 5/29/97 5:59p Samir |
| 47 | * Convert clipping from fix math to float math. |
| 48 | * |
| 49 | * 17 4/11/97 3:56 PM Jeremy |
| 50 | * replaced calls of ddgr_H/Vline to gr_h/vline |
| 51 | * |
| 52 | * 16 4/08/97 12:53p Samir |
| 53 | * Fixed hline clipping fuckup. |
| 54 | * |
| 55 | * 15 4/08/97 11:30a Jason |
| 56 | * made circles not draw if they are touching the edge ofthe viewport |
| 57 | * |
| 58 | * 14 4/08/97 10:42a Jason |
| 59 | * fixed bug with hline |
| 60 | * |
| 61 | * 13 4/01/97 4:37p Samir |
| 62 | * Simply better circle functions using viewport line and pixel functions. |
| 63 | * made an hline function that clips and a setpixel function that clips. |
| 64 | * |
| 65 | * 12 4/01/97 3:56p Samir |
| 66 | * Cheap clipping for scanlines. Should fix this so it's cleaner. |
| 67 | * |
| 68 | * 11 4/01/97 1:51p Samir |
| 69 | * Fixed clipping of circles. |
| 70 | * |
| 71 | * 10 4/01/97 1:40p Samir |
| 72 | * Circle functions |
| 73 | * |
| 74 | * 9 3/27/97 11:11a Samir |
| 75 | * Moved code from ddgr library to 2dlib because it was ANSI compliant and |
| 76 | * should work on all systems |
| 77 | * |
| 78 | * 8 2/28/97 2:52 PM Jeremy |
| 79 | * removed inline declaration of grViewport::clip_line |
| 80 | * |
| 81 | * 7 2/27/97 6:14p Samir |
| 82 | * moved higher level ddgr pen code to this library. |
| 83 | * |
| 84 | * 6 2/07/97 6:04p Matt |
| 85 | * Renamed vm_FixMulDiv() to FixMulDiv() |
| 86 | * |
| 87 | * 5 2/04/97 11:14a Samir |
| 88 | * Improved clipping system by adding a separate clipport based off of the |
| 89 | * viewport's rectangle in it's parent surface |
| 90 | * |
| 91 | * $NoKeywords: $ |
| 92 | */ |
| 93 | |
| 94 | #include "lib2d.h" |
| 95 | #include "pserror.h" |
| 96 | |
| 97 | #define CLIP_LEFT (vp_InitLeft + vp_Left) |
| 98 | #define CLIP_TOP (vp_InitTop + vp_Top) |
| 99 | #define CLIP_RIGHT (vp_InitLeft + vp_Right) |
| 100 | #define CLIP_BOTTOM (vp_InitTop + vp_Bottom) |
| 101 | |
| 102 | void grViewport::hline(ddgr_color col, int x1, int x2, int y1) { |
| 103 | ASSERT(vp_Locked); |
| 104 | |
| 105 | x1 = global_x(x1); |
| 106 | y1 = global_y(y1); |
| 107 | x2 = global_x(x2); |
| 108 | |
| 109 | if (x1 > x2) { |
| 110 | int t = x2; |
| 111 | x2 = x1; |
| 112 | x1 = t; |
| 113 | } |
| 114 | |
| 115 | if (y1 < CLIP_TOP) |
| 116 | return; |
| 117 | if (y1 > CLIP_BOTTOM) |
| 118 | return; |
| 119 | if (x1 > CLIP_RIGHT) |
| 120 | return; |
| 121 | if (x2 < CLIP_LEFT) |
| 122 | return; |
| 123 | if (x1 < CLIP_LEFT) |
| 124 | x1 = CLIP_LEFT; |
| 125 | if (x2 > CLIP_RIGHT) |
| 126 | x2 = CLIP_RIGHT; |
| 127 | |
| 128 | rend_SetFlatColor(col); |
| 129 | rend_DrawLine(x1, y1, x2, y1); |
| 130 | } |
| 131 | |
| 132 | // grViewport::line |
| 133 | // draws a clipped line |
| 134 | |
| 135 | void grViewport::line(ddgr_color color, int x1, int y1, int x2, int y2) { |
| 136 | int xa, ya, xb, yb; |
| 137 | ASSERT(vp_Locked); |
| 138 | |
| 139 | xa = global_x(x1); |
| 140 | ya = global_y(y1); |
| 141 | xb = global_x(x2); |
| 142 | yb = global_y(y2); |
| 143 | |
| 144 | rend_SetFlatColor(color); |
| 145 | rend_DrawLine(x1, y1, x2, y2); |
| 146 | } |
| 147 | |
| 148 | void grViewport::rect(ddgr_color color, int l, int t, int r, int b) { |
| 149 | ASSERT(vp_Locked); |
| 150 | |
| 151 | l = global_x(l); |
| 152 | t = global_y(t); |
| 153 | r = global_x(r); |
| 154 | b = global_y(b); |
| 155 | |
| 156 | rend_SetFlatColor(color); |
| 157 | rend_DrawLine(l, t, r, t); |
| 158 | rend_DrawLine(r, t, r, b); |
| 159 | rend_DrawLine(l, b, r, b); |
| 160 | rend_DrawLine(l, t, l, b); |
| 161 | } |
| 162 | |
| 163 | void grViewport::fillrect(ddgr_color color, int l, int t, int r, int b) { |
| 164 | ASSERT(vp_Locked); |
| 165 | |
| 166 | l = global_x(l); |
| 167 | t = global_y(t); |
| 168 | r = global_x(r); |
| 169 | b = global_y(b); |
| 170 | |
| 171 | rend_FillRect(color, l, t, r, b); |
| 172 | } |
| 173 | |
| 174 | void grViewport::circle(ddgr_color col, int xc, int yc, int r) { |
| 175 | ASSERT(vp_Locked); |
| 176 | |
| 177 | xc = global_x(xc); |
| 178 | yc = global_y(yc); |
| 179 | |
| 180 | rend_SetFlatColor(col); |
| 181 | rend_DrawCircle(xc, yc, r); |
| 182 | } |
| 183 | |
| 184 | void grViewport::fillcircle(ddgr_color col, int xc, int yc, int r) { |
| 185 | ASSERT(vp_Locked); |
| 186 | |
| 187 | xc = global_x(xc); |
| 188 | yc = global_y(yc); |
| 189 | |
| 190 | rend_FillCircle(col, xc, yc, r); |
| 191 | } |