| 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/viewport.cpp $ |
| 21 | * $Revision: 7 $ |
| 22 | * $Date: 12/19/97 5:23p $ |
| 23 | * $Author: Samir $ |
| 24 | * |
| 25 | * this class manages a specific viewing region of either a screen or bitmap. |
| 26 | * functions such as font setting and bitmap bltting are supported |
| 27 | * under this class. |
| 28 | * |
| 29 | * this class also spawns a pen object which effectively locks the screen or |
| 30 | * bitmap associated with it. |
| 31 | * |
| 32 | * $Log: /DescentIII/Main/2dlib/viewport.cpp $ |
| 33 | * |
| 34 | * 7 12/19/97 5:23p Samir |
| 35 | * Cleaned up a lot of code to call renderer code. |
| 36 | * |
| 37 | * 6 12/19/97 12:32p Samir |
| 38 | * Support alpha for text. Stubbed out clipping support. |
| 39 | * |
| 40 | * 5 11/18/97 12:32p Samir |
| 41 | * Took out useless lines. |
| 42 | * |
| 43 | * 4 11/14/97 12:05p Samir |
| 44 | * don't free font, just init it. |
| 45 | * |
| 46 | * 20 6/13/97 1:17p Samir |
| 47 | * Fucked up last checkin |
| 48 | * |
| 49 | * 19 6/13/97 1:00p Samir |
| 50 | * lock when called will always lock, so if the lock fails, then it's not |
| 51 | * viewport's job to fix it. |
| 52 | * |
| 53 | * 18 6/12/97 6:29p Samir |
| 54 | * DDGR v2.0 Changes in 2d system implemented. |
| 55 | * |
| 56 | * 17 6/06/97 12:18p Samir |
| 57 | * OpenGL HACKS to call renderer for some drawing functions. Must fix. |
| 58 | * |
| 59 | * 16 5/15/97 3:47p Samir |
| 60 | * Fixed lock_clear. |
| 61 | * |
| 62 | * 15 5/13/97 6:36p Samir |
| 63 | * added lock_clear functions. |
| 64 | * |
| 65 | * 14 4/01/97 4:37p Samir |
| 66 | * Simply better circle functions using viewport line and pixel functions. |
| 67 | * made an hline function that clips and a setpixel function that clips. |
| 68 | * |
| 69 | * 13 3/28/97 3:07p Samir |
| 70 | * Clear function now takes a color |
| 71 | * |
| 72 | * 12 3/28/97 2:50p Samir |
| 73 | * Fixed clip_rect fuckup. |
| 74 | * |
| 75 | * 11 2/18/97 1:04p Samir |
| 76 | * Changed prototype for setpixel |
| 77 | * |
| 78 | * 10 2/18/97 11:38a Samir |
| 79 | * Added getpixel and setpixel to grViewport. NOTE: you must lock the |
| 80 | * viewport when doing these ops. |
| 81 | * |
| 82 | * 9 2/04/97 4:56p Samir |
| 83 | * added 'hack' in viewport lock. see the function to see what I mean. |
| 84 | * |
| 85 | * 8 2/04/97 12:50p Samir |
| 86 | * Viewports now lock on CLIP_LEFT, CLIP_TOP |
| 87 | * |
| 88 | * 7 2/04/97 11:14a Samir |
| 89 | * Improved clipping system by adding a separate clipport based off of the |
| 90 | * viewport's rectangle in it's parent surface |
| 91 | * |
| 92 | * 6 2/03/97 7:56p Samir |
| 93 | * |
| 94 | * 5 1/31/97 11:02a Samir |
| 95 | * Adjusted clear function to use x,y,width,height parms. |
| 96 | * |
| 97 | * 4 1/30/97 6:28p Samir |
| 98 | * Stupid me, left some dummy text causing errors. |
| 99 | * |
| 100 | * 3 1/30/97 6:01p Samir |
| 101 | * The partition of memory and os surfaces as well as the change in |
| 102 | * ddgr_surface structure and all related changes. |
| 103 | * |
| 104 | * $NoKeywords: $ |
| 105 | */ |
| 106 | |
| 107 | #include <algorithm> |
| 108 | |
| 109 | #include "gr.h" |
| 110 | #include "lib2d.h" |
| 111 | #include "renderer.h" |
| 112 | |
| 113 | #include "pserror.h" |
| 114 | |
| 115 | #define CLIP_LEFT (vp_InitLeft + vp_Left) |
| 116 | #define CLIP_TOP (vp_InitTop + vp_Top) |
| 117 | #define CLIP_RIGHT (vp_InitLeft + vp_Right) |
| 118 | #define CLIP_BOTTOM (vp_InitTop + vp_Bottom) |
| 119 | |
| 120 | int grViewport::vp_Locked = 0; |
| 121 | grViewport *grViewport::vp_Current_VP = nullptr; |
| 122 | |
| 123 | // --------------------------------------------------------------------------- |
| 124 | // grViewport constructor and destructor |
| 125 | // --------------------------------------------------------------------------- |
| 126 | |
| 127 | grViewport::grViewport(grScreen *scr_parent) { |
| 128 | ASSERT(scr_parent != nullptr); |
| 129 | |
| 130 | sf_Parent = scr_parent; // screen is a child of grSurface, so we |
| 131 | // can do this. |
| 132 | vp_InitLeft = vp_Left = 0; // define bounds = entire screen |
| 133 | vp_InitTop = vp_Top = 0; |
| 134 | vp_InitRight = vp_Right = sf_Parent->width() - 1; |
| 135 | vp_InitBottom = vp_Bottom = sf_Parent->height() - 1; |
| 136 | |
| 137 | // initialize font object |
| 138 | // setup text settings. |
| 139 | char_Props.col[0] = GR_WHITE; |
| 140 | char_Props.col[1] = GR_WHITE; |
| 141 | char_Props.col[2] = GR_WHITE; |
| 142 | char_Props.col[3] = GR_WHITE; |
| 143 | char_Props.alpha = 255; |
| 144 | |
| 145 | text_Spacing = 1; |
| 146 | text_Line_spacing = 1; |
| 147 | text_Font.init(DEFAULT_FONT); |
| 148 | } |
| 149 | |
| 150 | grViewport::grViewport(grSurface *sf_parent) { |
| 151 | ASSERT(sf_parent != nullptr); |
| 152 | |
| 153 | sf_Parent = sf_parent; |
| 154 | |
| 155 | vp_InitLeft = vp_Left = 0; // define bounds = entire bitmap |
| 156 | vp_InitTop = vp_Top = 0; |
| 157 | vp_InitRight = vp_Right = sf_Parent->width() - 1; |
| 158 | vp_InitBottom = vp_Bottom = sf_Parent->height() - 1; |
| 159 | |
| 160 | // initialize font object |
| 161 | // setup text settings. |
| 162 | char_Props.col[0] = GR_WHITE; |
| 163 | char_Props.col[1] = GR_WHITE; |
| 164 | char_Props.col[2] = GR_WHITE; |
| 165 | char_Props.col[3] = GR_WHITE; |
| 166 | char_Props.alpha = 255; |
| 167 | |
| 168 | text_Spacing = 1; |
| 169 | text_Line_spacing = 1; |
| 170 | text_Font.init(DEFAULT_FONT); |
| 171 | } |
| 172 | |
| 173 | grViewport::~grViewport() { |
| 174 | ASSERT(!vp_Locked); // Viewport shouldn't be locked here! |
| 175 | |
| 176 | // text_Font.free(); |
| 177 | } |
| 178 | |
| 179 | void grViewport::set_clipping_rect(int left, int top, int right, int bottom) { |
| 180 | ASSERT(!vp_Locked); // YOU CANT DO THIS WHILE LOCKED! |
| 181 | |
| 182 | // these are local to the viewport. |
| 183 | // vp_Left = left; |
| 184 | // vp_Top = top; |
| 185 | // vp_Right = right; |
| 186 | // vp_Bottom = bottom; |
| 187 | } |
| 188 | |
| 189 | // these functions set and restore the state of the viewport (font, colors, etc.) |
| 190 | void grViewport::get_state(tVPState *state) { |
| 191 | state->font = text_Font; |
| 192 | state->text_spacing = text_Spacing; |
| 193 | state->text_line_spacing = text_Line_spacing; |
| 194 | get_char_properties(&state->chprops); |
| 195 | } |
| 196 | |
| 197 | void grViewport::set_state(const tVPState *state) { |
| 198 | set_char_properties(GR_VPCP_ALL, &state->chprops); |
| 199 | text_Font = state->font; |
| 200 | text_Spacing = state->text_spacing; |
| 201 | text_Line_spacing = state->text_line_spacing; |
| 202 | } |
| 203 | |
| 204 | // --------------------------------------------------------------------------- |
| 205 | // font functions |
| 206 | // --------------------------------------------------------------------------- |
| 207 | |
| 208 | // --------------------------------------------------------------------------- |
| 209 | // bltting functions |
| 210 | // --------------------------------------------------------------------------- |
| 211 | |
| 212 | void grViewport::clear(ddgr_color col) // clear with background color |
| 213 | { |
| 214 | sf_Parent->clear(CLIP_LEFT, CLIP_TOP, (vp_Right - vp_Left) + 1, (vp_Bottom - vp_Top) + 1, col); |
| 215 | } |
| 216 | |
| 217 | void grViewport::blt(int dx, int dy, grSurface *ssf) { |
| 218 | int x1, y1, x2, y2, sx, sy; |
| 219 | |
| 220 | // perform rectangular clipping |
| 221 | x1 = global_x(dx); |
| 222 | y1 = global_y(dy); |
| 223 | x2 = x1 + ssf->width() - 1; |
| 224 | y2 = y1 + ssf->height() - 1; |
| 225 | |
| 226 | if (x1 < CLIP_LEFT) |
| 227 | sx = CLIP_LEFT - x1; |
| 228 | else |
| 229 | sx = 0; |
| 230 | if (y1 < CLIP_TOP) |
| 231 | sy = CLIP_TOP - y1; |
| 232 | else |
| 233 | sy = 0; |
| 234 | |
| 235 | if (clip_rect(x1, y1, x2, y2) == 2) |
| 236 | return; |
| 237 | |
| 238 | sf_Parent->blt(x1, y1, ssf, sx, sy, x2 - x1 + 1, y2 - y1 + 1); |
| 239 | } |
| 240 | |
| 241 | void grViewport::blt(int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh) { |
| 242 | int x1, y1, x2, y2; |
| 243 | |
| 244 | x1 = global_x(dx); |
| 245 | y1 = global_y(dy); |
| 246 | x2 = x1 + sw - 1; |
| 247 | y2 = y1 + sh - 1; |
| 248 | if (clip_rect(x1, y1, x2, y2) == 2) |
| 249 | return; |
| 250 | |
| 251 | sf_Parent->blt(x1, y1, ssf, sx, sy, x2 - x1 + 1, y2 - y1 + 1); |
| 252 | } |
| 253 | |
| 254 | // --------------------------------------------------------------------------- |
| 255 | // general lock creation functions |
| 256 | // --------------------------------------------------------------------------- |
| 257 | |
| 258 | grSurface *grViewport::lock() { |
| 259 | int rowsize; |
| 260 | |
| 261 | ASSERT(!vp_Locked); |
| 262 | |
| 263 | sf_Parent->lock(CLIP_LEFT, CLIP_TOP, &rowsize); |
| 264 | vp_Locked = 1; |
| 265 | |
| 266 | if (vp_Locked) { |
| 267 | pen_Obj.sf = &sf_Parent->ddsfObj; |
| 268 | pen_Obj.rowsize = sf_Parent->rowsize(); |
| 269 | pen_Obj.rgbcolor = GR_RGB(255, 255, 255); |
| 270 | |
| 271 | // SUPERHACK. Pen functions don't use the adjusted pointer of grSurface, but use the |
| 272 | // base data pointer provided by locking the entire surface at (0,0). Pen functions |
| 273 | // use global coordinates, so they can calculate the adjusted pointer themselves. This |
| 274 | // makes global_x and global_y consisitent with the surface base pointer. This problem |
| 275 | // was caused by the clipping rect being a virtual viewport within a viewport to make |
| 276 | // the 3d system work better. |
| 277 | pen_Obj.data = sf_Parent->m_OrigDataPtr; |
| 278 | } |
| 279 | |
| 280 | grViewport::vp_Current_VP = this; |
| 281 | |
| 282 | return sf_Parent; |
| 283 | } |
| 284 | |
| 285 | void grViewport::unlock() { |
| 286 | ASSERT(vp_Locked); |
| 287 | |
| 288 | sf_Parent->unlock(); |
| 289 | vp_Locked = 0; |
| 290 | |
| 291 | grViewport::vp_Current_VP = nullptr; |
| 292 | |
| 293 | pen_Obj.data = nullptr; |
| 294 | pen_Obj.rowsize = 0; |
| 295 | pen_Obj.sf = nullptr; |
| 296 | } |
| 297 | |
| 298 | // --------------------------------------------------------------------------- |
| 299 | // miscellaneous viewport functions |
| 300 | // --------------------------------------------------------------------------- |
| 301 | |
| 302 | void grViewport::setpixel(ddgr_color col, int x, int y) { |
| 303 | ASSERT(vp_Locked); |
| 304 | |
| 305 | rend_SetPixel(col, x, y); |
| 306 | } |
| 307 | |
| 308 | void grViewport::setpixelc(ddgr_color col, int x, int y) { |
| 309 | ASSERT(vp_Locked); |
| 310 | |
| 311 | if (x < CLIP_LEFT || x > CLIP_RIGHT || y < CLIP_TOP || y > CLIP_BOTTOM) |
| 312 | return; |
| 313 | |
| 314 | rend_SetPixel(col, x, y); |
| 315 | } |
| 316 | |
| 317 | ddgr_color grViewport::getpixel(int x, int y) { |
| 318 | ASSERT(vp_Locked); |
| 319 | |
| 320 | return rend_GetPixel(x, y); |
| 321 | } |
| 322 | |
| 323 | ddgr_color grViewport::getpixelc(int x, int y) { |
| 324 | ASSERT(vp_Locked); |
| 325 | |
| 326 | if (x < CLIP_LEFT || x > CLIP_RIGHT || y < CLIP_TOP || y > CLIP_BOTTOM) |
| 327 | return 0; |
| 328 | |
| 329 | return rend_GetPixel(x, y); |
| 330 | } |
| 331 | |
| 332 | void grViewport::lock_clear(ddgr_color col) { |
| 333 | int l = CLIP_LEFT, t = CLIP_TOP, r = CLIP_RIGHT, b = CLIP_BOTTOM; |
| 334 | ddgr_surface *memsurf = sf_Parent->scratch_mem_surf; |
| 335 | |
| 336 | ASSERT(vp_Locked); |
| 337 | |
| 338 | if (sf_Parent->ddsfObj.flags & SURFFLAG_RENDERER) { |
| 339 | rend_FillRect(col, l, t, r, b); |
| 340 | } else { |
| 341 | memsurf->bpp = pen_Obj.sf->bpp; |
| 342 | |
| 343 | gr_mem_surf_Init(memsurf, pen_Obj.data, pen_Obj.rowsize); |
| 344 | gr_mem_surf_Clear(memsurf, col, l, t, r - l + 1, b - t + 1); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void grViewport::lock_clear(ddgr_color col, int l, int t, int r, int b) { |
| 349 | ddgr_surface *memsurf = sf_Parent->scratch_mem_surf; |
| 350 | |
| 351 | ASSERT(vp_Locked); |
| 352 | |
| 353 | l = global_x(l); |
| 354 | t = global_y(t); |
| 355 | r = global_x(r); |
| 356 | b = global_y(b); |
| 357 | |
| 358 | clip_rect(l, t, r, b); |
| 359 | |
| 360 | if (sf_Parent->ddsfObj.flags & SURFFLAG_RENDERER) { |
| 361 | rend_FillRect(col, l, t, r, b); |
| 362 | } else { |
| 363 | memsurf->bpp = pen_Obj.sf->bpp; |
| 364 | gr_mem_surf_Init(memsurf, pen_Obj.data, pen_Obj.rowsize); |
| 365 | gr_mem_surf_Clear(memsurf, col, l, t, r - l + 1, b - t + 1); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // --------------------------------------------------------------------------- |
| 370 | // private functions |
| 371 | // --------------------------------------------------------------------------- |
| 372 | |
| 373 | // grPen::clip_rect |
| 374 | // returns 2 if totally clipped |
| 375 | // returns 1 if partially clipped |
| 376 | // returns 0 if not clipped. |
| 377 | |
| 378 | int grViewport::clip_rect(int &l, int &t, int &r, int &b) { |
| 379 | int clipped = 0; |
| 380 | |
| 381 | if (l > r) |
| 382 | std::swap(l, r); |
| 383 | if (t > b) |
| 384 | std::swap(t, b); |
| 385 | |
| 386 | if (l > CLIP_RIGHT || t > CLIP_BOTTOM || r < CLIP_LEFT || b < CLIP_TOP) |
| 387 | return 2; |
| 388 | |
| 389 | if (l < CLIP_LEFT) { |
| 390 | l = CLIP_LEFT; |
| 391 | clipped = 1; |
| 392 | } |
| 393 | if (t < CLIP_TOP) { |
| 394 | t = CLIP_TOP; |
| 395 | clipped = 1; |
| 396 | } |
| 397 | if (r > CLIP_RIGHT) { |
| 398 | r = CLIP_RIGHT; |
| 399 | clipped = 1; |
| 400 | } |
| 401 | if (b > CLIP_BOTTOM) { |
| 402 | b = CLIP_BOTTOM; |
| 403 | clipped = 1; |
| 404 | } |
| 405 | |
| 406 | return clipped; |
| 407 | } |