Debian Codegraph @

common/include/fwd-gr.h

raw
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 #pragma once
21
22 #include <SDL_version.h>
23 #if SDL_MAJOR_VERSION == 2
24 #include <SDL_video.h>
25 #endif
26 #include <cstdint>
27 #include <memory>
28 #include <span>
29 #include <utility>
30 #include "d_array.h"
31 #include "palette.h"
32 #include "maths.h"
33 #include "u_mem.h"
34
35 namespace dcx {
36
37 enum class gr_fade_level : uint8_t;
38
39 // some defines for transparency and blending
40 constexpr auto TRANSPARENCY_COLOR = color_palette_index{255}; // palette entry of transparency color -- 255 on the PC
41 #define GR_FADE_LEVELS 34u
42 #define GR_FADE_OFF gr_fade_level::off
43 enum class gr_blend : uint8_t {
44 normal, // normal blending
45 additive_a, // additive alpha blending
46 additive_c, // additive color blending
47 };
48
49 /* rgb color packed into 3 units of 5 bits per color, with the sign bit left unused.
50 * r << 10
51 * g << 5
52 * b << 0
53 */
54 enum class packed_color_r5g5b5 : int16_t
55 {
56 };
57
58 }
59
60 #define SWIDTH (grd_curscreen->get_screen_width())
61 #define SHEIGHT (grd_curscreen->get_screen_height())
62
63 #ifdef DXX_BUILD_DESCENT
64 #if DXX_BUILD_DESCENT == 1
65 namespace dsx {
66 extern uint8_t HiresGFXAvailable;
67 }
68 #define HIRESMODE HiresGFXAvailable // descent.pig either contains hires or lowres graphics, not both
69 #elif DXX_BUILD_DESCENT == 2
70 #define HIRESMODE (SWIDTH >= 640 && SHEIGHT >= 480 && !GameArg.GfxSkipHiresGFX)
71 #endif
72 #endif
73 #define MAX_BMP_SIZE(width, height) (4 + ((width) + 2) * (height))
74
75 #define SCRNS_DIR "screenshots/"
76
77 //these are control characters that have special meaning in the font code
78
79 #define CC_COLOR 1 //next char is new foreground color
80 #define CC_LSPACING 2 //next char specifies line spacing
81 #define CC_UNDERLINE 3 //next char is underlined
82
83 //now have string versions of these control characters (can concat inside a string)
84
85 #define CC_COLOR_S "\x1" //next char is new foreground color
86 #define CC_LSPACING_S "\x2" //next char specifies line spacing
87 #define CC_UNDERLINE_S "\x3" //next char is underlined
88
89 namespace dcx {
90
91 enum class bm_mode : uint8_t
92 {
93 linear,
94 ilbm,
95 rgb15 = 3, //5 bits each r,g,b stored at 16 bits
96 #if DXX_USE_OGL
97 ogl = 5,
98 #endif /* def OGL */
99 };
100
101 }
102
103 #define BM_FLAG_TRANSPARENT 1
104 #define BM_FLAG_SUPER_TRANSPARENT 2
105 #define BM_FLAG_NO_LIGHTING 4
106 #define BM_FLAG_RLE 8 // A run-length encoded bitmap.
107 #define BM_FLAG_PAGED_OUT 16 // This bitmap's data is paged out.
108 #define BM_FLAG_RLE_BIG 32 // for bitmaps that RLE to > 255 per row (i.e. cockpits)
109
110 #include "dxxsconf.h"
111 #include "dsx-ns.h"
112 #include <array>
113
114 namespace dcx {
115
116 struct grs_bitmap;
117 struct grs_canvas;
118 #define GRS_FONT_SIZE 28 // how much space it takes up on disk
119 struct grs_point;
120
121 union screen_mode;
122
123 class grs_screen;
124
125 struct grs_main_canvas;
126 typedef std::unique_ptr<grs_main_canvas> grs_canvas_ptr;
127
128 struct grs_subcanvas;
129 typedef std::unique_ptr<grs_subcanvas> grs_subcanvas_ptr;
130
131 // Free the bitmap and its pixel data
132 class grs_main_bitmap;
133 typedef std::unique_ptr<grs_main_bitmap> grs_bitmap_ptr;
134
135 struct grs_font;
136
137 #if SDL_MAJOR_VERSION == 1
138 uint_fast32_t gr_list_modes(std::array<screen_mode, 50> &modes);
139 #elif SDL_MAJOR_VERSION == 2
140 extern SDL_Window *g_pRebirthSDLMainWindow;
141 #endif
142
143 }
144
145 #ifdef DXX_BUILD_DESCENT
146 namespace dsx {
147 int gr_set_mode(screen_mode mode);
148 void gr_set_mode_from_window_size();
149
150 int gr_init();
151 #if DXX_USE_OGL
152 void gr_set_attributes();
153 #endif
154 void gr_close();
155 }
156 #endif
157
158 namespace dcx {
159
160 grs_canvas_ptr gr_create_canvas(uint16_t w, uint16_t h);
161
162 grs_subcanvas_ptr gr_create_sub_canvas(grs_canvas &canv,uint16_t x,uint16_t y,uint16_t w, uint16_t h);
163
164 // Initialize the specified canvas. the raw pixel data buffer is passed as
165 // a parameter. no memory allocation is performed.
166
167 void gr_init_canvas(grs_canvas &canv,unsigned char *pixdata, bm_mode pixtype, uint16_t w, uint16_t h);
168
169 // Initialize the specified sub canvas. no memory allocation is performed.
170
171 void gr_init_sub_canvas(grs_subcanvas &n, grs_canvas &src, uint16_t x, uint16_t y, uint16_t w, uint16_t h);
172
173 // Clear the current canvas to the specified color
174 void gr_clear_canvas(grs_canvas &, color_t color);
175
176 //=========================================================================
177 // Bitmap functions:
178
179 // these are the two workhorses, the others just use these
180 void gr_init_bitmap(grs_bitmap &bm, bm_mode mode, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t bytesperline, color_palette_index *data) noexcept;
181 void gr_init_main_bitmap(grs_main_bitmap &bm, bm_mode mode, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t bytesperline, RAIIdmem<uint8_t[]> data);
182 void gr_init_sub_bitmap (grs_bitmap &bm, grs_bitmap &bmParent, uint16_t x, uint16_t y, uint16_t w, uint16_t h);
183
184 void gr_init_bitmap_alloc(grs_main_bitmap &bm, bm_mode mode, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t bytesperline);
185 void gr_free_bitmap_data(grs_bitmap &bm);
186
187 // Allocate a bitmap and its pixel data buffer.
188 grs_bitmap_ptr gr_create_bitmap(uint16_t w,uint16_t h);
189
190 // Free the bitmap, but not the pixel data buffer
191 class grs_subbitmap;
192 typedef std::unique_ptr<grs_subbitmap> grs_subbitmap_ptr;
193
194 // Creates a bitmap which is part of another bitmap
195 grs_subbitmap_ptr gr_create_sub_bitmap(grs_bitmap &bm, uint16_t x, uint16_t y, uint16_t w, uint16_t h);
196
197 // Free the bitmap's data
198
199 #if !DXX_USE_OGL
200 void gr_bm_ubitblt(grs_canvas &dest, unsigned w, unsigned h, int dx, int dy, int sx, int sy, const grs_bitmap &src);
201 void gr_bm_ubitbltm(grs_canvas &dest, unsigned w, unsigned h, unsigned dx, unsigned dy, unsigned sx, unsigned sy, const grs_bitmap &src);
202 #define gr_bm_pixel(C,B,X,Y,C2) gr_bm_pixel(B,X,Y,C2)
203 #define gr_settransblend(A,B,C) gr_settransblend(A,B)
204 #endif
205 void gr_bm_pixel(grs_canvas &, grs_bitmap &bm, uint_fast32_t x, uint_fast32_t y, uint8_t color);
206 void gr_set_bitmap_data(grs_bitmap &bm, const uint8_t *data);
207 }
208
209 #ifdef DXX_BUILD_DESCENT
210 namespace dsx {
211
212 //=========================================================================
213 // Color functions:
214
215 // When this function is called, the guns are set to gr_palette, and
216 // the palette stays the same until gr_close is called
217
218 void gr_use_palette_table(const char * filename);
219
220 }
221 #endif
222
223 //=========================================================================
224 // Drawing functions:
225
226 namespace dcx {
227
228 // Sets transparency and blending function
229 void gr_settransblend(grs_canvas &, gr_fade_level fade_level, gr_blend blend_func);
230
231 // Draws a point into the current canvas in the current color and drawmode.
232 void gr_pixel(grs_bitmap &, unsigned x, unsigned y, color_palette_index color);
233 void gr_upixel(grs_bitmap &, unsigned x, unsigned y, color_palette_index color);
234
235 // Gets a pixel;
236 color_palette_index gr_gpixel(const grs_bitmap &bitmap, unsigned x, unsigned y);
237 color_palette_index gr_ugpixel(const grs_bitmap &bitmap, int x, int y);
238
239 // Draws a line into the current canvas in the current color and drawmode.
240 void gr_line(grs_canvas &, fix x0,fix y0,fix x1,fix y1, color_palette_index color);
241 void gr_uline(grs_canvas &canvas, fix x0,fix y0,fix x1,fix y1, color_palette_index color);
242
243 // Draw the bitmap into the current canvas at the specified location.
244 void gr_bitmap(grs_canvas &, unsigned x,unsigned y,grs_bitmap &bm);
245 void gr_ubitmap(grs_canvas &, grs_bitmap &bm);
246 void show_fullscr(grs_canvas &, grs_bitmap &bm);
247
248 // Find transparent area in bitmap
249 void gr_bitblt_find_transparent_area(const grs_bitmap &bm, unsigned &minx, unsigned &miny, unsigned &maxx, unsigned &maxy);
250
251 // bitmap function with transparency
252 #if !DXX_USE_OGL
253 void gr_bitmapm(grs_canvas &, unsigned x, unsigned y, const grs_bitmap &bm);
254 void gr_ubitmapm(grs_canvas &, unsigned x, unsigned y, grs_bitmap &bm);
255 #endif
256
257 // Draw a rectangle into the current canvas.
258 void gr_rect(grs_canvas &, int left,int top,int right,int bot, color_palette_index color);
259 void gr_urect(grs_canvas &, int left,int top,int right,int bot, color_palette_index color);
260
261 // Draw a filled circle
262 int gr_disk(grs_canvas &, fix x,fix y,fix r, color_palette_index color);
263
264 // Draw an outline circle
265 int gr_ucircle(grs_canvas &, fix x,fix y,fix r, color_palette_index color);
266
267 // Draw an unfilled rectangle into the current canvas
268 #if DXX_USE_EDITOR
269 void gr_box(grs_canvas &, uint_fast32_t left,uint_fast32_t top,uint_fast32_t right,uint_fast32_t bot, color_palette_index color);
270 #endif
271 void gr_ubox(grs_canvas &, int left,int top,int right,int bot, color_palette_index color);
272
273 void gr_scanline(grs_canvas &canvas, int x1, int x2, unsigned y, color_palette_index color);
274 #if !DXX_USE_OGL
275 void gr_uscanline(grs_canvas &canvas, unsigned x1, unsigned x2, unsigned y, color_palette_index color);
276 #endif
277
278 using grs_font_ptr = std::unique_ptr<grs_font>;
279
280 // Reads in a font file... current font set to this one.
281 grs_font_ptr gr_init_font(grs_canvas &canvas, std::span<const char> fontfile);
282
283 }
284
285 #ifdef DXX_BUILD_DESCENT
286 namespace dsx {
287
288 #if DXX_BUILD_DESCENT == 1
289 #define DXX_SDL_WINDOW_CAPTION "Descent"
290 #define DXX_SDL_WINDOW_ICON_BITMAP "d1x-rebirth.bmp"
291 #elif DXX_BUILD_DESCENT == 2
292 #define DXX_SDL_WINDOW_CAPTION "Descent II"
293 #define DXX_SDL_WINDOW_ICON_BITMAP "d2x-rebirth.bmp"
294 void gr_copy_palette(palette_array_t &gr_palette, const palette_array_t &pal);
295 #endif
296
297 }
298 #endif
299
300 // Writes a string using current font. Returns the next column after last char.
301 namespace dcx {
302
303 //remap (by re-reading) all the color fonts
304 void gr_remap_color_fonts();
305 void gr_set_curfont(grs_canvas &, const grs_font &);
306 void gr_string(grs_canvas &, const grs_font &, int x, int y, const char *s, int w, int h);
307 void gr_string(grs_canvas &, const grs_font &, int x, int y, const char *s);
308 void gr_ustring(grs_canvas &, const grs_font &, int x, int y, const char *s);
309 template <void (&)(grs_canvas &, const grs_font &, int, int, const char *)>
310 void gr_printt(grs_canvas &, const grs_font &, int x, int y, const char *format, ...) dxx_compiler_attribute_format_printf(5, 6);
311 template <auto>
312 void gr_printt(grs_canvas &, const grs_font &, int x, int y, const char *) = delete;
313 #define gr_printf gr_printt<gr_string>
314 #define gr_uprintf gr_printt<gr_ustring>
315 std::pair<const char *, unsigned> gr_get_string_wrap(const grs_font &, const char *s, unsigned limit);
316 unsigned gr_get_string_height(const grs_font &cv_font, unsigned lines);
317
318 struct gr_string_size
319 {
320 unsigned long width;
321 unsigned long height;
322 };
323 gr_string_size gr_get_string_size(const grs_font &, const char *s);
324 gr_string_size gr_get_string_size(const grs_font &, const char *s, unsigned max_chars_per_line);
325
326 // From scale.c
327 void scale_bitmap(const grs_bitmap &bp, const std::array<grs_point, 3> &vertbuf, int orientation, grs_bitmap &);
328
329 //===========================================================================
330 // Global variables
331 extern grs_canvas *grd_curcanv; //active canvas
332 extern std::unique_ptr<grs_screen> grd_curscreen; //active screen
333
334 /* Define `DXX_DEBUG_CURRENT_CANVAS_ORIGIN` to a positive integer N to
335 * save __FILE__+__LINE__ of the N most recent callers. If
336 * `DXX_DEBUG_CURRENT_CANVAS_ORIGIN` is not a positive integer, the
337 * file+line is not passed to the canvas update functions.
338 *
339 * Use this to trace which functions update the global canvas pointer
340 * without requiring a debugger trap on every update.
341 */
342 #ifndef DXX_DEBUG_CURRENT_CANVAS_ORIGIN
343 #define DXX_DEBUG_CURRENT_CANVAS_ORIGIN 0
344 #endif
345
346 #if DXX_DEBUG_CURRENT_CANVAS_ORIGIN > 0
347 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_DECL_VARS const char *file = __builtin_FILE(), unsigned line = __builtin_LINE()
348 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_DEFN_VARS const char *const file, const unsigned line
349 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS , DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_DECL_VARS
350 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DEFN_VARS , DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_DEFN_VARS
351 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_PASS_VARS file, line
352 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_PASS_VARS , DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_PASS_VARS
353 #else
354 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_DECL_VARS
355 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_DEFN_VARS
356 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS
357 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DEFN_VARS
358 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_PASS_VARS
359 #define DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_PASS_VARS
360 #endif
361 void (gr_set_default_canvas)(DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_DECL_VARS);
362 void (gr_set_current_canvas)(grs_canvas & DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS);
363 void (gr_set_current_canvas)(std::nullptr_t DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS) = delete;
364 void gr_set_current_canvas2(grs_canvas * DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS);
365
366 static inline void gr_set_current_canvas_inline(grs_canvas *const canv DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS)
367 {
368 if (canv)
369 (gr_set_current_canvas)(*canv DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_PASS_VARS);
370 else
371 (gr_set_default_canvas)(DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_N_PASS_VARS);
372 }
373
374 static inline void (gr_set_current_canvas)(grs_canvas *const canv DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS)
375 {
376 #ifdef DXX_HAVE_BUILTIN_CONSTANT_P
377 if (dxx_builtin_constant_p(!canv))
378 gr_set_current_canvas_inline(canv DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_PASS_VARS);
379 else
380 #endif
381 gr_set_current_canvas2(canv DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_PASS_VARS);
382 }
383
384 //flags for fonts
385 #define FT_COLOR 1
386 #define FT_PROPORTIONAL 2
387 #define FT_KERNED 4
388
389 extern palette_array_t gr_palette;
390 using gft_array1 = enumerated_array<std::array<color_t, 256>, GR_FADE_LEVELS, gr_fade_level>;
391 extern gft_array1 gr_fade_table;
392 }
393
394 extern uint16_t gr_palette_selector;
395 extern uint16_t gr_inverse_table_selector;
396 extern uint16_t gr_fade_table_selector;
397
398 // Remaps a bitmap into the current palette. If transparent_color is
399 // between 0 and 255 then all occurances of that color are mapped to
400 // whatever color the 2d uses for transparency. This is normally used
401 // right after a call to iff_read_bitmap like this:
402 // iff_error = iff_read_bitmap(filename,new,bm_mode::linear,newpal);
403 // if (iff_error != iff_status_code::no_error) Error("Can't load IFF file <%s>, error=%d",filename,iff_error);
404 // if (iff_has_transparency)
405 // gr_remap_bitmap(new, newpal, iff_transparent_color);
406 // else
407 // gr_remap_bitmap(new, newpal, -1);
408
409 // Same as above, but searches using gr_find_closest_color which uses
410 // 18-bit accurracy instead of 15bit when translating colors.
411 namespace dcx {
412 void gr_remap_bitmap_good(grs_bitmap &bmp, const palette_array_t &palette, uint_fast32_t transparent_color, uint_fast32_t super_transparent_color);
413
414 void gr_palette_step_up(int r, int g, int b);
415
416 #define BM_XRGB(r,g,b) gr_find_closest_color((r)*2,(g)*2,(b)*2)
417
418 // Given: r,g,b, each in range of 0-63, return the color index that
419 // best matches the input.
420 color_palette_index gr_find_closest_color(int r, int g, int b);
421 color_palette_index gr_find_closest_color_15bpp(packed_color_r5g5b5 rgb);
422
423 void gr_flip();
424
425 /*
426 * must return 0 if windowed, 1 if fullscreen
427 */
428 int gr_check_fullscreen();
429
430 /*
431 * returns state after toggling (ie, same as if you had called
432 * check_fullscreen immediatly after)
433 */
434 void gr_toggle_fullscreen();
435
436 #if DXX_USE_OGL
437 void ogl_do_palfx();
438 void ogl_init_pixel_buffers(unsigned w, unsigned h);
439 void ogl_close_pixel_buffers();
440 #endif
441 }