| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Definitions for graphics lib. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#pragma once |
| 27 |
|
| 28 |
#include <cassert> |
| 29 |
#include "fwd-gr.h" |
| 30 |
#include "pstypes.h" |
| 31 |
#include "maths.h" |
| 32 |
#include "palette.h" |
| 33 |
#include "dxxsconf.h" |
| 34 |
#include "dsx-ns.h" |
| 35 |
#include "pack.h" |
| 36 |
#include <array> |
| 37 |
|
| 38 |
#if DXX_USE_SDLIMAGE || !DXX_USE_OGL |
| 39 |
#include <memory> |
| 40 |
#include <SDL_video.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
namespace dcx { |
| 44 |
|
| 45 |
enum class gr_fade_level : uint8_t |
| 46 |
{ |
| 47 |
off = GR_FADE_LEVELS // yes, max means OFF - don't screw that up |
| 48 |
}; |
| 49 |
|
| 50 |
struct grs_point |
| 51 |
{ |
| 52 |
fix x,y; |
| 53 |
}; |
| 54 |
|
| 55 |
struct grs_bitmap : prohibit_void_ptr<grs_bitmap> |
| 56 |
{ |
| 57 |
uint16_t bm_x,bm_y; // Offset from parent's origin |
| 58 |
uint16_t bm_w,bm_h; // width,height |
| 59 |
private: |
| 60 |
bm_mode bm_type; |
| 61 |
uint8_t bm_flags; |
| 62 |
public: |
| 63 |
bm_mode get_type() const |
| 64 |
{ |
| 65 |
return bm_type; |
| 66 |
} |
| 67 |
void set_type(bm_mode t) |
| 68 |
{ |
| 69 |
bm_type = t; |
| 70 |
} |
| 71 |
uint8_t get_flags() const |
| 72 |
{ |
| 73 |
return bm_flags; |
| 74 |
} |
| 75 |
uint8_t get_flag_mask(const uint8_t mask) const |
| 76 |
{ |
| 77 |
return get_flags() & mask; |
| 78 |
} |
| 79 |
void set_flags(const uint8_t f) |
| 80 |
{ |
| 81 |
bm_flags = f; |
| 82 |
} |
| 83 |
void clear_flags() |
| 84 |
{ |
| 85 |
set_flags(0); |
| 86 |
} |
| 87 |
void set_flag_mask(const bool set, const uint8_t mask) |
| 88 |
{ |
| 89 |
const auto f = get_flags(); |
| 90 |
set_flags(set ? f | mask : f & ~mask); |
| 91 |
} |
| 92 |
void add_flags(const uint8_t f) |
| 93 |
{ |
| 94 |
bm_flags |= f; |
| 95 |
} |
| 96 |
// bit 1 on means it has supertransparency |
| 97 |
// bit 2 on means it doesn't get passed through lighting. |
| 98 |
short bm_rowsize; // unsigned char offset to next row |
| 99 |
std::array<fix, 3> avg_color_rgb; // same as above but real rgb value to be used to textured objects that should emit light |
| 100 |
union { |
| 101 |
const color_palette_index *bm_data{}; // ptr to pixel data... |
| 102 |
// Linear = *parent+(rowsize*y+x) |
| 103 |
// ModeX = *parent+(rowsize*y+x/4) |
| 104 |
// SVGA = *parent+(rowsize*y+x) |
| 105 |
color_palette_index *bm_mdata; |
| 106 |
}; |
| 107 |
const color_palette_index *get_bitmap_data() const { return bm_data; } |
| 108 |
color_palette_index *get_bitmap_data() { return bm_mdata; } |
| 109 |
struct grs_bitmap *bm_parent{}; |
| 110 |
#if DXX_USE_OGL |
| 111 |
struct ogl_texture *gltexture{}; |
| 112 |
#else |
| 113 |
uint8_t avg_color; // Average color of all pixels in texture map. |
| 114 |
#endif /* def OGL */ |
| 115 |
}; |
| 116 |
|
| 117 |
// Free the bitmap and its pixel data |
| 118 |
class grs_main_bitmap : public grs_bitmap |
| 119 |
{ |
| 120 |
public: |
| 121 |
grs_main_bitmap(); |
| 122 |
grs_main_bitmap(const grs_main_bitmap &) = delete; |
| 123 |
grs_main_bitmap &operator=(const grs_main_bitmap &) = delete; |
| 124 |
grs_main_bitmap(grs_main_bitmap &&r) : |
| 125 |
grs_bitmap{std::move(static_cast<grs_bitmap &>(r))} |
| 126 |
{ |
| 127 |
r.bm_data = nullptr; |
| 128 |
#if DXX_USE_OGL |
| 129 |
r.gltexture = nullptr; |
| 130 |
#endif |
| 131 |
} |
| 132 |
grs_main_bitmap &operator=(grs_main_bitmap &&r) |
| 133 |
{ |
| 134 |
if (grs_bitmap *const that = &r; this == that) |
| 135 |
return *this; |
| 136 |
reset(); |
| 137 |
grs_bitmap::operator=(std::move(static_cast<grs_bitmap &>(r))); |
| 138 |
r.bm_data = nullptr; |
| 139 |
#if DXX_USE_OGL |
| 140 |
r.gltexture = nullptr; |
| 141 |
#endif |
| 142 |
return *this; |
| 143 |
} |
| 144 |
~grs_main_bitmap() |
| 145 |
{ |
| 146 |
reset(); |
| 147 |
} |
| 148 |
void reset() |
| 149 |
{ |
| 150 |
gr_free_bitmap_data(*this); |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
struct grs_canvas : prohibit_void_ptr<grs_canvas> |
| 155 |
{ |
| 156 |
grs_canvas(const grs_canvas &) = delete; |
| 157 |
grs_canvas &operator=(const grs_canvas &) = delete; |
| 158 |
grs_canvas(grs_canvas &&) = default; |
| 159 |
grs_canvas &operator=(grs_canvas &&) = default; |
| 160 |
~grs_canvas() |
| 161 |
{ |
| 162 |
/* `grd_curcanv` points to the currently active canvas. If it |
| 163 |
* points to `this` when the destructor runs, then any further |
| 164 |
* attempts to use the referenced canvas will access an |
| 165 |
* out-of-scope variable, leading to undefined behavior. Assert |
| 166 |
* that `grd_curcanv` does not point to the expiring variable so |
| 167 |
* that potential misuses are detected before they manifest as |
| 168 |
* undefined behavior. |
| 169 |
* |
| 170 |
* If you get an assertion failure here, enable |
| 171 |
* `DXX_DEBUG_CURRENT_CANVAS_ORIGIN` to trace where the canvas |
| 172 |
* was most recently set. |
| 173 |
* |
| 174 |
* Eventually, `grd_curcanv` will be removed and this test will |
| 175 |
* become obsolete. |
| 176 |
*/ |
| 177 |
assert(this != grd_curcanv); |
| 178 |
/* If the canvas is reset before next use, then no crash. |
| 179 |
* If the canvas is not reset, then crash instead of accessing |
| 180 |
* freed memory. |
| 181 |
*/ |
| 182 |
if (grd_curcanv == this) |
| 183 |
grd_curcanv = nullptr; |
| 184 |
} |
| 185 |
grs_bitmap cv_bitmap; // the bitmap for this canvas |
| 186 |
const grs_font * cv_font; // the currently selected font |
| 187 |
color_palette_index cv_font_fg_color; // current font foreground color (255==Invisible) |
| 188 |
color_palette_index cv_font_bg_color; // current font background color (255==Invisible) |
| 189 |
gr_fade_level cv_fade_level; // transparency level |
| 190 |
protected: |
| 191 |
grs_canvas() = default; |
| 192 |
}; |
| 193 |
|
| 194 |
//========================================================================= |
| 195 |
// System functions: |
| 196 |
// setup and set mode. this creates a grs_screen structure and sets |
| 197 |
// grd_curscreen to point to it. grs_curcanv points to this screen's |
| 198 |
// canvas. Saves the current VGA state and screen mode. |
| 199 |
|
| 200 |
union screen_mode |
| 201 |
{ |
| 202 |
private: |
| 203 |
uint32_t wh; |
| 204 |
public: |
| 205 |
struct { |
| 206 |
uint16_t width, height; |
| 207 |
}; |
| 208 |
constexpr bool operator==(const screen_mode &rhs) const |
| 209 |
{ |
| 210 |
return wh == rhs.wh; |
| 211 |
} |
| 212 |
screen_mode() = default; |
| 213 |
constexpr screen_mode(uint16_t &&w, uint16_t &&h) : |
| 214 |
width{w}, height{h} |
| 215 |
{ |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
static inline uint16_t SM_W(const screen_mode &s) |
| 220 |
{ |
| 221 |
return s.width; |
| 222 |
} |
| 223 |
|
| 224 |
static inline uint16_t SM_H(const screen_mode &s) |
| 225 |
{ |
| 226 |
return s.height; |
| 227 |
} |
| 228 |
|
| 229 |
// Makes a new canvas. allocates memory for the canvas and its bitmap, |
| 230 |
// including the raw pixel buffer. |
| 231 |
|
| 232 |
struct grs_main_canvas : grs_canvas, prohibit_void_ptr<grs_main_canvas> |
| 233 |
{ |
| 234 |
using prohibit_void_ptr<grs_main_canvas>::operator &; |
| 235 |
grs_main_canvas &operator=(grs_main_canvas &) = delete; |
| 236 |
grs_main_canvas &operator=(grs_main_canvas &&) = default; |
| 237 |
~grs_main_canvas(); |
| 238 |
}; |
| 239 |
|
| 240 |
// Creates a canvas that is part of another canvas. This can be used to make |
| 241 |
// a window on the screen. The address of the raw pixel data is inherited from |
| 242 |
// the parent canvas. |
| 243 |
struct grs_subcanvas : grs_canvas, prohibit_void_ptr<grs_subcanvas> |
| 244 |
{ |
| 245 |
using prohibit_void_ptr<grs_subcanvas>::operator &; |
| 246 |
}; |
| 247 |
|
| 248 |
#if DXX_USE_SDLIMAGE || !DXX_USE_OGL |
| 249 |
struct RAII_SDL_Surface |
| 250 |
{ |
| 251 |
struct deleter |
| 252 |
{ |
| 253 |
void operator()(SDL_Surface *s) const |
| 254 |
{ |
| 255 |
SDL_FreeSurface(s); |
| 256 |
} |
| 257 |
}; |
| 258 |
std::unique_ptr<SDL_Surface, deleter> surface; |
| 259 |
constexpr RAII_SDL_Surface() = default; |
| 260 |
RAII_SDL_Surface(RAII_SDL_Surface &&) = default; |
| 261 |
RAII_SDL_Surface &operator=(RAII_SDL_Surface &&) = default; |
| 262 |
explicit RAII_SDL_Surface(SDL_Surface *const s) : |
| 263 |
surface{s} |
| 264 |
{ |
| 265 |
} |
| 266 |
}; |
| 267 |
#endif |
| 268 |
|
| 269 |
class grs_screen : prohibit_void_ptr<grs_screen> |
| 270 |
{ // This is a video screen |
| 271 |
screen_mode sc_mode; |
| 272 |
public: |
| 273 |
grs_screen &operator=(grs_screen &) = delete; |
| 274 |
grs_screen &operator=(grs_screen &&) = default; |
| 275 |
#if DXX_USE_OGL |
| 276 |
/* OpenGL builds allocate the backing data for the canvas, so use |
| 277 |
* grs_main_canvas to ensure it is freed when appropriate. |
| 278 |
*/ |
| 279 |
grs_main_canvas sc_canvas; // Represents the entire screen |
| 280 |
#else |
| 281 |
/* SDL builds borrow the backing data from the SDL_Surface, so use |
| 282 |
* grs_subcanvas for the canvas because the memory is owned by SDL and will |
| 283 |
* be freed by SDL_FreeSurface. Store the associated SDL_Surface alongside |
| 284 |
* the canvas, so that it is freed at the same time. |
| 285 |
*/ |
| 286 |
RAII_SDL_Surface sdl_surface; |
| 287 |
grs_subcanvas sc_canvas; |
| 288 |
#endif |
| 289 |
fix sc_aspect; //aspect ratio (w/h) for this screen |
| 290 |
uint16_t get_screen_width() const |
| 291 |
{ |
| 292 |
return SM_W(sc_mode); |
| 293 |
} |
| 294 |
uint16_t get_screen_height() const |
| 295 |
{ |
| 296 |
return SM_H(sc_mode); |
| 297 |
} |
| 298 |
screen_mode get_screen_mode() const |
| 299 |
{ |
| 300 |
return sc_mode; |
| 301 |
} |
| 302 |
void set_screen_width_height(uint16_t w, uint16_t h) |
| 303 |
{ |
| 304 |
sc_mode.width = w; |
| 305 |
sc_mode.height = h; |
| 306 |
} |
| 307 |
}; |
| 308 |
|
| 309 |
//========================================================================= |
| 310 |
// Canvas functions: |
| 311 |
|
| 312 |
// Free the bitmap, but not the pixel data buffer |
| 313 |
class grs_subbitmap : public grs_bitmap |
| 314 |
{ |
| 315 |
}; |
| 316 |
|
| 317 |
//font structure |
| 318 |
struct grs_font : public prohibit_void_ptr<grs_font> |
| 319 |
{ |
| 320 |
uint16_t ft_w; // Width in pixels |
| 321 |
uint16_t ft_h; // Height in pixels |
| 322 |
int16_t ft_flags; // Proportional? |
| 323 |
int16_t ft_baseline; // |
| 324 |
uint8_t ft_minchar; // First char defined by this font |
| 325 |
uint8_t ft_maxchar; // Last char defined by this font |
| 326 |
std::array<char, 13> ft_filename{}; |
| 327 |
const uint8_t *ft_data = nullptr; // Ptr to raw data. |
| 328 |
const uint8_t *const *ft_chars = nullptr; // Ptrs to data for each char (required for prop font) |
| 329 |
const uint16_t *ft_widths = nullptr; // Array of widths (required for prop font) |
| 330 |
const uint8_t *ft_kerndata = nullptr; // Array of kerning triplet data |
| 331 |
std::unique_ptr<uint8_t[]> ft_allocdata; |
| 332 |
#if DXX_USE_OGL |
| 333 |
// These fields do not participate in disk i/o! |
| 334 |
std::unique_ptr<grs_bitmap[]> ft_bitmaps; |
| 335 |
grs_main_bitmap ft_parent_bitmap; |
| 336 |
#endif /* def OGL */ |
| 337 |
}; |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
static inline void gr_set_bitmap_flags(grs_bitmap &bm, uint8_t flags) |
| 342 |
{ |
| 343 |
bm.set_flags(flags); |
| 344 |
} |
| 345 |
|
| 346 |
static inline void gr_set_transparent(grs_bitmap &bm, bool bTransparent) |
| 347 |
{ |
| 348 |
bm.set_flag_mask(bTransparent, BM_FLAG_TRANSPARENT); |
| 349 |
} |
| 350 |
|
| 351 |
namespace dcx { |
| 352 |
|
| 353 |
static inline void gr_set_font_fg_color(grs_canvas &canvas, color_palette_index fg_color) |
| 354 |
{ |
| 355 |
canvas.cv_font_fg_color = fg_color; |
| 356 |
} |
| 357 |
|
| 358 |
static inline void gr_set_font_bg_color(grs_canvas &canvas, color_palette_index bg_color) |
| 359 |
{ |
| 360 |
canvas.cv_font_bg_color = bg_color; |
| 361 |
} |
| 362 |
|
| 363 |
#define gr_set_fontcolor(C,F,B) \ |
| 364 |
( DXX_BEGIN_COMPOUND_STATEMENT { \ |
| 365 |
auto &gr_set_fontcolor = C; \ |
| 366 |
gr_set_font_fg_color(gr_set_fontcolor, F); \ |
| 367 |
gr_set_font_bg_color(gr_set_fontcolor, B); \ |
| 368 |
} DXX_END_COMPOUND_STATEMENT ) |
| 369 |
|
| 370 |
} |
| 371 |
|
| 372 |
static inline void (gr_set_current_canvas)(grs_canvas_ptr &canv DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS) |
| 373 |
{ |
| 374 |
(gr_set_current_canvas)(canv.get() DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_PASS_VARS); |
| 375 |
} |
| 376 |
|
| 377 |
static inline void (gr_set_current_canvas)(grs_subcanvas_ptr &canv DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS) |
| 378 |
{ |
| 379 |
(gr_set_current_canvas)(canv.get() DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_PASS_VARS); |
| 380 |
} |