Debian Codegraph @

2dlib/gr.h

raw symbols SCIP
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/Lib/gr.h $
21 * $Revision: 18 $
22 * $Date: 4/17/99 6:15p $
23 * $Author: Samir $
24 *
25 * the 2d lib main header
26 *
27 * $Log: /DescentIII/Main/Lib/gr.h $
28 *
29 * 18 4/17/99 6:15p Samir
30 * replaced gr.h with grdefs.h and fixed resulting compile bugs.
31 *
32 * 17 4/14/99 3:59a Jeff
33 * fixed case mismatches in #includes
34 *
35 * 16 11/30/98 5:50p Jason
36 * added 4444 bitmap support
37 *
38 * 15 12/29/97 5:50p Samir
39 * Moved GR_COLOR_CHAR to grdefs.h from gr.h.
40 *
41 * 14 12/22/97 7:39p Samir
42 * Moved color constants from gr.h to grdefs.h
43 *
44 * 13 12/22/97 7:13p Samir
45 * Moved constants to grdefs.h
46 *
47 * 12 12/19/97 12:33p Samir
48 * Added alpha for text and took out abilitiy to create viewports inside
49 * viewports.
50 *
51 * 11 12/12/97 6:40p Samir
52 * Some viewport font functions.
53 *
54 * 10 11/16/97 6:54p Samir
55 * Added font::get_name function.
56 *
57 * 9 11/16/97 2:20p Samir
58 * Added macro to draw a bitmap to a viewport.
59 *
60 * 8 11/11/97 1:27p Samir
61 * Added functions to set text alpha values.
62 *
63 * 7 11/04/97 4:57p Samir
64 * Added get_text_color.
65 *
66 * 6 10/13/97 3:56p Jason
67 * made a better 3d bitmap system
68 *
69 * 5 10/03/97 11:59a Samir
70 * Added some color constants.
71 *
72 * 4 9/19/97 5:37p Samir
73 * Software font coloring works.
74 *
75 * 1 6/23/97 9:25p Samir
76 * added because source safe sucks
77 *
78 * 39 6/16/97 3:01p Samir
79 * Changed font system to work better with renderer. Fonts are rendered
80 * on bitmaps of 128x128 at load time.
81 *
82 * 38 6/12/97 6:30p Samir
83 * DDGR v2.0 Changes in 2d system implemented.
84 *
85 * 37 6/09/97 3:47p Jason
86 * fixed some color mapping problems
87 *
88 * 36 5/21/97 3:39p Jason
89 * added terrain editing stuff
90 *
91 * 35 5/21/97 1:08p Samir
92 * added conversion from 565 to ddgr_color format.
93 *
94 * 34 5/15/97 2:08p Samir
95 * Added grSurface::get_surface_desc and grViewport::get_parent.
96 *
97 * 33 5/13/97 6:36p Samir
98 * added lock_clear functions.
99 *
100 *
101 * $NoKeywords: $
102 */
103
104 #ifndef _GR_H
105 #define _GR_H
106
107 #include "Ddgr.h"
108 #include "fix.h"
109 #include "pserror.h"
110
111 class grMemorySurface;
112 class grViewport;
113 class grSurface;
114 class grPen;
115
116 // a 'pen' structure used by the 2d system to draw things.
117 struct gr_pen {
118 char *data;
119 int rowsize;
120 ddgr_color rgbcolor;
121 ddgr_color color;
122 ddgr_surface *sf;
123 };
124
125 enum grTextAlign { // used to align text along viewport
126 grTextUnjustified,
127 grTextLeft,
128 grTextCentered,
129 grTextRight,
130 grTextWordWrap
131 };
132
133 struct tCharProperties {
134 uint8_t alpha;
135 ddgr_color col[4];
136 };
137
138 #define GR_VPCP_ALPHA 1 // alpha value is valid in CharProperties
139 #define GR_VPCP_COLOR 2 // color values are valid in CharProperties.
140 #define GR_VPCP_ALL (1 + 2)
141
142 // ----------------------------------------------------------------------------
143 // class grFont
144 // will load and manage a font's capabilities.
145 // all fonts are stored in a 16bit hicolor format, or bitmasked.
146 // ----------------------------------------------------------------------------
147
148 const int MAX_FONTS = 16, MAX_FONT_BITMAPS = 12;
149
150 #define DEFAULT_FONT 0
151
152 struct gr_font_file_record {
153 int16_t width, height; // width of widest character and height of longest char
154 int16_t flags; // flags used by the character renderer
155 int16_t baseline; // pixels given to lowercase below script line start at baseline
156 uint8_t min_ascii; // minimum ascii value used by font
157 uint8_t max_ascii; // max ascii value used by the font
158 int16_t byte_width; // width of a character in the font in bytes
159 uint8_t *raw_data; // pixel, map data.
160 uint8_t **char_data; // pointers to each character
161 int16_t *char_widths; // individual pixel widths of each character
162 uint8_t *kern_data; // kerning information for specific letter combos
163 };
164
165 struct gr_font_record {
166 char name[32];
167 char filename[32]; // filename of font
168 int references; // number of references of that font
169 int bmps[MAX_FONT_BITMAPS]; // font bitmap handles
170 grMemorySurface *surfs[MAX_FONT_BITMAPS];
171 uint8_t *ch_u, *ch_v, *ch_w, *ch_h, *ch_surf;
172 float *ch_uf, *ch_vf, *ch_wf, *ch_hf;
173 gr_font_file_record font;
174
175 };
176
177 class grFont {
178 static gr_font_record m_FontList[MAX_FONTS];
179 static grMemorySurface *m_CharSurf;
180
181 int m_FontHandle; // offset into m_FontList
182
183 public:
184 grFont();
185 ~grFont();
186 grFont(const grFont &font) { m_FontHandle = font.m_FontHandle; };
187
188 const grFont &operator=(const grFont &font) {
189 m_FontHandle = font.m_FontHandle;
190 return *this;
191 };
192
193 public: // Manipulation functions
194 static void init_system();
195 static void close_system();
196 static int register_font(char *fontname, char *filename);
197
198 void init(const char *fontname);
199 void free();
200
201 // draw_char returns the next x value taking spacing and kerning into consideration.
202 int draw_char(grSurface *sf, int x, int y, int ch, tCharProperties *chprop);
203 int draw_char(grSurface *sf, int x, int y, int ch, int sx, int sy, int sw, int sh, tCharProperties *chprop);
204
205 private:
206 static void free_font(gr_font_record *ft);
207
208 static void load(char *filename, int slot);
209 static void translate_to_surfaces(int slot);
210 static void translate_mono_char(grSurface *sf, int x, int y, int index, gr_font_file_record *ft, int width);
211
212 uint8_t *get_kern_info(uint8_t c1, uint8_t c2);
213 void charblt16(grSurface *dsf, ddgr_color col, int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh);
214
215 public: // Accessor functions
216 char *get_name() const {
217 if (m_FontHandle == -1)
218 return NULL;
219 else
220 return m_FontList[m_FontHandle].name;
221 };
222 int min_ascii() const {
223 ASSERT(m_FontHandle > -1);
224 return m_FontList[m_FontHandle].font.min_ascii;
225 };
226 int max_ascii() const {
227 ASSERT(m_FontHandle > -1);
228 return m_FontList[m_FontHandle].font.max_ascii;
229 };
230 int height() const {
231 ASSERT(m_FontHandle > -1);
232 return m_FontList[m_FontHandle].font.height;
233 };
234 int get_char_info(int ch, int *width);
235 };
236
237 // ----------------------------------------------------------------------------
238 // class grSurface
239 // contains a ddgr_bitmap object and allows for blting
240 // ----------------------------------------------------------------------------
241
242 struct ddgr_surface_node {
243 ddgr_surface *sf;
244 ddgr_surface_node *prev;
245 };
246
247 const int SURFTYPE_MEMORY = 256;
248
249 const int SURFFLAG_RENDERER = 256; // SURFACE will use renderer functions.
250
251 class grSurface {
252 public:
253 static void init_system(); // initializes some global stuff for surface system
254 static void close_system();
255
256 private:
257 friend class grViewport;
258 int surf_Locked; // same value for all grViewport objects
259 int m_SurfInit; // surface initialized
260 char *m_OrigDataPtr; // non-adjusted data pointer
261 char *m_DataPtr; // adjusted data pointer
262 int m_DataRowsize; // Data rowsize
263
264 static ddgr_surface_node *surf_list; // the surface list
265 static ddgr_surface_node *surf_list_cur; // current node on list.
266 static ddgr_surface *scratch_mem_surf; // use as temporary memory surface
267
268 private:
269 // coversion blts.
270 void xlat8_16(char *data, int w, int h, char *pal);
271 void xlat16_16(char *data, int w, int h, int format = 0);
272 void xlat16_24(char *data, int w, int h);
273 void xlat24_16(char *data, int w, int h);
274 void xlat32_16(char *data, int w, int h);
275
276 void uniblt(int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh);
277
278 protected:
279 ddgr_surface ddsfObj; // Used to call ddgr_surf_ functions
280
281 protected:
282 void add_to_list(ddgr_surface *sf); // used for surface heap management
283 void remove_from_list(ddgr_surface *sf);
284
285 int surf_init() const { return m_SurfInit; };
286 void surf_init(int init) { m_SurfInit = init; };
287
288 public:
289 grSurface();
290 virtual ~grSurface();
291
292 // creates a surface, allocates memory based off of parameters.
293 grSurface(int w, int h, int bpp, unsigned type, unsigned flags = 0, const char *name = NULL);
294
295 public:
296 unsigned get_flags() const { return ddsfObj.flags; };
297
298 void create(int w, int h, int bpp, unsigned type, unsigned flags = 0, const char *name = NULL);
299
300 void load(char *data, int w, int h, int bpp, int format = 0);
301 void load(int handle);
302 void load(char *data, int w, int h, char *pal);
303 void load(int handle, char *pal);
304
305 void free(); // non-dynamic version
306
307 int width() const {
308 ASSERT(m_SurfInit);
309 return ddsfObj.w;
310 };
311 int height() const {
312 ASSERT(m_SurfInit);
313 return ddsfObj.h;
314 };
315 int bpp() const {
316 ASSERT(m_SurfInit);
317 return ddsfObj.bpp;
318 };
319 unsigned flags() const {
320 ASSERT(m_SurfInit);
321 return ddsfObj.flags;
322 };
323
324 int rowsize() const {
325 ASSERT(m_SurfInit);
326 return m_DataRowsize;
327 };
328 char *data() const {
329 ASSERT(m_SurfInit);
330 return m_DataPtr;
331 };
332
333 void clear(ddgr_color col = 0);
334 void clear(int l, int t, int w, int h, ddgr_color col = 0);
335 void replace_color(ddgr_color sc, ddgr_color dc);
336
337 void blt(int dx, int dy, grSurface *ssf);
338 void blt(int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh);
339
340 char *lock(int *rowsize);
341 char *lock(int x, int y, int *rowsize);
342 void unlock();
343
344 void get_surface_desc(ddgr_surface *surf) { *surf = ddsfObj; };
345
346 // This is VERY system dependent when called, sorry but I think this may be necessary
347 void attach_to_window(unsigned handle);
348 };
349
350 // ----------------------------------------------------------------------------
351 // class grMemorySurface
352 // this is a software bitmap not video driver dependant
353 // ----------------------------------------------------------------------------
354
355 class grMemorySurface : public grSurface {
356 bool m_FirstTimeInit; // do special stuff if we are initializing for first time
357 bool m_AllowInit; // we only allow initialization if we didn't create it.
358
359 public:
360 grMemorySurface();
361 virtual ~grMemorySurface();
362
363 // allocates a surface from system memory (quicker version of grSurface)
364 grMemorySurface(int w, int h, int bpp, unsigned flags = 0, const char *name = NULL);
365
366 // initializes a surface using a bitmap handle (no allocation)
367 grMemorySurface(int bm);
368
369 // inititalizes a surface using raw data,width,rowsize info information.
370 bool init(int w, int h, int bpp, char *data, int rowsize, unsigned flags = 0, const char *name = NULL);
371 };
372
373 // ----------------------------------------------------------------------------
374 // class grHardwareSurface
375 // this is a hardware bitmap allocated from the video driver
376 // ----------------------------------------------------------------------------
377
378 class grHardwareSurface : public grSurface {
379 public:
380 grHardwareSurface();
381 grHardwareSurface(int w, int h, int bpp, unsigned flags = 0, const char *name = NULL);
382 virtual ~grHardwareSurface();
383
384 bool create(int w, int h, int bpp, unsigned flags = 0, const char *name = NULL);
385 };
386
387 // ----------------------------------------------------------------------------
388 // class grScreen
389 // child class of grSurface. Handles initializing the screen, and
390 // is used by the grViewport class to draw onto the screen
391 // ----------------------------------------------------------------------------
392
393 class grScreen : public grSurface {
394 friend class grViewport;
395
396 public:
397 grScreen(int w, int h, int bpp, const char *name = NULL);
398 virtual ~grScreen();
399
400 void flip();
401 };
402
403 // ----------------------------------------------------------------------------
404 // class grViewport
405 // a viewport is a bounded rectangle on either a screen or bitmap.
406 // one should be able to draw any 2d object on a viewport region.
407 // ----------------------------------------------------------------------------
408
409 // contains state information for a viewport.
410 struct tVPState {
411 grFont font;
412 int text_spacing;
413 int text_line_spacing;
414 tCharProperties chprops;
415 };
416
417 class grViewport {
418 static int vp_Locked; // tells us if any viewport is locked
419 static grViewport *vp_Current_VP; // current locked viewport
420
421 grFont text_Font;
422 int text_Spacing;
423 int text_Line_spacing;
424 tCharProperties char_Props;
425
426 private:
427 void draw_text(grTextAlign align, int x, int y, char *str);
428 void draw_text_line(int x, int y, char *str);
429 void draw_text_line_clip(int x, int y, char *str);
430 int clip_line(int &l, int &t, int &r, int &b);
431 int clip_rect(int &x1, int &y1, int &x2, int &y2);
432
433 protected:
434 int vp_Left, vp_Top, vp_Right, // viewport's global screen/bitmap bounds
435 vp_Bottom;
436
437 int vp_InitLeft, vp_InitTop, vp_InitRight, vp_InitBottom; // viewport's initial global screen coords
438
439 grSurface *sf_Parent; // bitmap where viewport points to
440 gr_pen pen_Obj;
441
442 public:
443 grViewport(grScreen *scr_parent);
444 grViewport(grSurface *sf_parent);
445 ~grViewport();
446
447 grSurface *get_parent() const { return sf_Parent; };
448
449 // these are the viewport's initial bounds with respect to parent surface
450 // these can't be changed.
451 int width() const { return (vp_InitRight - vp_InitLeft) + 1; };
452 int height() const { return (vp_InitBottom - vp_InitTop) + 1; };
453 int left() const { return vp_InitLeft; };
454 int top() const { return vp_InitTop; };
455 int right() const { return vp_InitRight; };
456 int bottom() const { return vp_InitBottom; };
457
458 // these clipping coordinates are local to the viewports initial rectangle, based off
459 // of parent surface. All 2d functions use these coords to base their drawing.
460 // If we draw at (0,0) and the clip region is at (80,90,200,160) then we will draw at 80,90.
461 int clip_width() const { return (vp_Right - vp_Left) + 1; };
462 int clip_height() const { return (vp_Bottom - vp_Top) + 1; };
463 int clip_left() const { return vp_Left; };
464 int clip_top() const { return vp_Top; };
465 int clip_right() const { return vp_Right; };
466 int clip_bottom() const { return vp_Bottom; };
467 void set_clipping_rect(int left, int top, int right, int bottom);
468
469 // these convert local viewport coords to global screen/surface coords
470 int global_x(int lx) const { return vp_InitLeft + vp_Left + lx; };
471 int global_y(int ly) const { return vp_InitTop + vp_Top + ly; };
472 int local_x(int gx) const { return gx - vp_Left - vp_InitLeft; };
473 int local_y(int gy) const { return gy - vp_Top - vp_InitTop; };
474
475 // these functions set and restore the state of the viewport (font, colors, etc.)
476 void get_state(tVPState *state);
477 void set_state(const tVPState *state);
478
479 // bltting functions on viewport
480 void clear(ddgr_color col = 0); // clear with background color
481 void lock_clear(ddgr_color col = 0); // software clear. must be used in lock
482 void lock_clear(ddgr_color col, int l, int t, int w, int h);
483 void blt(int dx, int dy, grSurface *ssf);
484 void blt(int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh);
485
486 public:
487 grSurface *lock(); // lock and
488 void unlock(); // unlock (implicitly locks surface)
489
490 friend grSurface *VP_GET_DRAWING_SURFACE();
491 friend void VP_BEGIN_DRAWING(grViewport *vp);
492 friend void VP_END_DRAWING();
493
494 // these functions only work on locked surfaces.
495 void hline(ddgr_color, int x1, int x2, int y1);
496 void line(ddgr_color color, int x1, int y1, int x2, int y2);
497 void rect(ddgr_color color, int l, int t, int r, int b);
498 void fillrect(ddgr_color color, int l, int t, int r, int b);
499 void setpixel(ddgr_color col, int x, int y);
500 void setpixelc(ddgr_color col, int x, int y);
501 ddgr_color getpixel(int x, int y);
502 ddgr_color getpixelc(int x, int y);
503 void circle(ddgr_color col, int xc, int yc, int r);
504 void fillcircle(ddgr_color col, int xc, int yc, int r);
505 // end
506
507 public:
508 // These functions only work on locked surfaces
509 int printf(grTextAlign align, int x, int y, char *fmt, ...);
510 int printf(int x, int y, char *fmt, ...);
511 int puts(grTextAlign align, int x, int y, char *str);
512 int puts(int x, int y, char *str);
513
514 // text formatting functions
515 public:
516 int get_text_line_width(char *text);
517 void set_font(const char *fontname);
518 void get_font(char *fontname, int size);
519 int get_current_font_height() const { return text_Font.height(); };
520 int get_text_spacing() const { return text_Spacing; };
521 int get_text_line_spacing() const { return text_Line_spacing; };
522 void set_text_spacing(int spacing) { text_Spacing = spacing; };
523 void set_text_line_spacing(int spacing) { text_Line_spacing = spacing; };
524
525 // visual properties of text functions
526 public:
527 void set_text_color(ddgr_color color);
528 ddgr_color get_text_color() const;
529 void set_char_properties(unsigned flags, const tCharProperties *props);
530 void get_char_properties(tCharProperties *props) const;
531 // end
532 };
533
534 // inline definitions for grViewport functions
535
536 inline void grViewport::set_text_color(ddgr_color color) {
537 char_Props.col[0] = color;
538 char_Props.col[1] = color;
539 char_Props.col[2] = color;
540 char_Props.col[3] = color;
541 }
542
543 inline ddgr_color grViewport::get_text_color() const { return char_Props.col[0]; }
544
545 inline void grViewport::set_char_properties(unsigned flags, const tCharProperties *props) {
546 if (flags & GR_VPCP_COLOR) {
547 char_Props.col[0] = props->col[0];
548 char_Props.col[1] = props->col[1];
549 char_Props.col[2] = props->col[2];
550 char_Props.col[3] = props->col[3];
551 }
552 if (flags & GR_VPCP_ALPHA) {
553 char_Props.alpha = props->alpha;
554 }
555 }
556
557 inline void grViewport::get_char_properties(tCharProperties *props) const {
558 props->col[0] = char_Props.col[0];
559 props->col[1] = char_Props.col[1];
560 props->col[2] = char_Props.col[2];
561 props->col[3] = char_Props.col[3];
562 props->alpha = char_Props.alpha;
563 }
564
565 // useful macros
566
567 static inline void VP_BITBLT(grViewport *vp, int dx, int dy, int bm) {
568 grMemorySurface surf(bm);
569 vp->blt(dx, dy, &surf);
570 }
571
572 #endif