2006-03-20 17:12:09 +00:00
|
|
|
/*
|
2014-06-01 17:55:23 +00:00
|
|
|
* Portions of this file are copyright Rebirth contributors and licensed as
|
|
|
|
* described in COPYING.txt.
|
|
|
|
* Portions of this file are copyright Parallax Software and licensed
|
|
|
|
* according to the Parallax license below.
|
|
|
|
* See COPYING.txt for license details.
|
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
|
|
|
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Definitions for graphics lib.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-01-12 00:26:03 +00:00
|
|
|
#pragma once
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2017-11-05 20:49:08 +00:00
|
|
|
#include <cassert>
|
2015-07-25 23:10:45 +00:00
|
|
|
#include "fwd-gr.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "pstypes.h"
|
2012-07-01 02:54:33 +00:00
|
|
|
#include "maths.h"
|
2013-01-06 21:03:57 +00:00
|
|
|
#include "palette.h"
|
2013-06-27 02:35:22 +00:00
|
|
|
#include "dxxsconf.h"
|
2016-03-19 19:08:10 +00:00
|
|
|
#include "dsx-ns.h"
|
2013-12-08 23:37:40 +00:00
|
|
|
#include "fmtcheck.h"
|
2014-06-21 23:55:24 +00:00
|
|
|
#include "pack.h"
|
2014-07-17 03:05:21 +00:00
|
|
|
#include "compiler-array.h"
|
2016-10-29 23:16:15 +00:00
|
|
|
#include "compiler-exchange.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2013-12-22 22:03:07 +00:00
|
|
|
struct grs_point
|
|
|
|
{
|
2006-03-20 17:12:09 +00:00
|
|
|
fix x,y;
|
2013-12-22 22:03:07 +00:00
|
|
|
};
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-07-14 02:40:42 +00:00
|
|
|
struct grs_bitmap : prohibit_void_ptr<grs_bitmap>
|
2013-12-22 22:03:07 +00:00
|
|
|
{
|
2014-12-05 03:31:07 +00:00
|
|
|
uint16_t bm_x,bm_y; // Offset from parent's origin
|
2014-09-20 23:47:27 +00:00
|
|
|
uint16_t bm_w,bm_h; // width,height
|
2015-07-25 23:10:47 +00:00
|
|
|
private:
|
|
|
|
uint8_t bm_type;
|
2017-01-15 00:03:13 +00:00
|
|
|
uint8_t bm_flags;
|
2015-07-25 23:10:47 +00:00
|
|
|
public:
|
|
|
|
uint8_t get_type() const
|
|
|
|
{
|
|
|
|
return bm_type;
|
|
|
|
}
|
|
|
|
void set_type(uint8_t t)
|
|
|
|
{
|
|
|
|
bm_type = t;
|
|
|
|
}
|
2017-01-15 00:03:13 +00:00
|
|
|
uint8_t get_flags() const
|
|
|
|
{
|
|
|
|
return bm_flags;
|
|
|
|
}
|
|
|
|
uint8_t get_flag_mask(const uint8_t mask) const
|
|
|
|
{
|
|
|
|
return get_flags() & mask;
|
|
|
|
}
|
|
|
|
void set_flags(const uint8_t f)
|
|
|
|
{
|
|
|
|
bm_flags = f;
|
|
|
|
}
|
|
|
|
void clear_flags()
|
|
|
|
{
|
|
|
|
set_flags(0);
|
|
|
|
}
|
|
|
|
void set_flag_mask(const bool set, const uint8_t mask)
|
|
|
|
{
|
|
|
|
const auto f = get_flags();
|
|
|
|
set_flags(set ? f | mask : f & ~mask);
|
|
|
|
}
|
|
|
|
void add_flags(const uint8_t f)
|
|
|
|
{
|
|
|
|
bm_flags |= f;
|
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
// bit 1 on means it has supertransparency
|
|
|
|
// bit 2 on means it doesn't get passed through lighting.
|
|
|
|
short bm_rowsize; // unsigned char offset to next row
|
2015-05-09 17:38:59 +00:00
|
|
|
array<fix, 3> avg_color_rgb; // same as above but real rgb value to be used to textured objects that should emit light
|
2014-12-02 03:24:38 +00:00
|
|
|
union {
|
|
|
|
const uint8_t *bm_data; // ptr to pixel data...
|
2006-03-20 17:12:09 +00:00
|
|
|
// Linear = *parent+(rowsize*y+x)
|
|
|
|
// ModeX = *parent+(rowsize*y+x/4)
|
|
|
|
// SVGA = *parent+(rowsize*y+x)
|
2014-12-02 03:24:38 +00:00
|
|
|
uint8_t *bm_mdata;
|
|
|
|
};
|
|
|
|
const uint8_t *get_bitmap_data() const { return bm_data; }
|
|
|
|
uint8_t *get_bitmap_data() { return bm_mdata; }
|
2013-10-26 03:40:42 +00:00
|
|
|
struct grs_bitmap *bm_parent;
|
2016-09-24 18:06:11 +00:00
|
|
|
#if DXX_USE_OGL
|
2013-12-22 22:03:07 +00:00
|
|
|
struct ogl_texture *gltexture;
|
2006-03-20 17:12:09 +00:00
|
|
|
#endif /* def OGL */
|
2015-05-09 17:38:59 +00:00
|
|
|
ubyte avg_color; // Average color of all pixels in texture map.
|
2013-12-22 22:03:07 +00:00
|
|
|
};
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
//font structure
|
2014-06-21 23:55:24 +00:00
|
|
|
struct grs_font : public prohibit_void_ptr<grs_font>
|
2013-12-22 22:03:07 +00:00
|
|
|
{
|
2006-03-20 17:12:09 +00:00
|
|
|
short ft_w; // Width in pixels
|
|
|
|
short ft_h; // Height in pixels
|
|
|
|
short ft_flags; // Proportional?
|
|
|
|
short ft_baseline; //
|
|
|
|
ubyte ft_minchar; // First char defined by this font
|
|
|
|
ubyte ft_maxchar; // Last char defined by this font
|
2016-01-10 21:18:09 +00:00
|
|
|
array<char, 13> ft_filename;
|
2016-02-12 04:02:28 +00:00
|
|
|
const uint8_t *ft_data; // Ptr to raw data.
|
|
|
|
const uint8_t *const *ft_chars; // Ptrs to data for each char (required for prop font)
|
|
|
|
const int16_t *ft_widths; // Array of widths (required for prop font)
|
2015-11-14 18:17:21 +00:00
|
|
|
const uint8_t *ft_kerndata; // Array of kerning triplet data
|
2015-11-14 18:17:21 +00:00
|
|
|
std::unique_ptr<uint8_t[]> ft_allocdata;
|
2016-09-24 18:06:11 +00:00
|
|
|
#if DXX_USE_OGL
|
2006-03-20 17:12:09 +00:00
|
|
|
// These fields do not participate in disk i/o!
|
2014-08-07 03:09:28 +00:00
|
|
|
std::unique_ptr<grs_bitmap[]> ft_bitmaps;
|
2006-03-20 17:12:09 +00:00
|
|
|
grs_bitmap ft_parent_bitmap;
|
|
|
|
#endif /* def OGL */
|
2013-12-22 22:03:07 +00:00
|
|
|
};
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-11-30 22:09:19 +00:00
|
|
|
struct grs_canvas : prohibit_void_ptr<grs_canvas>
|
2013-12-22 22:03:07 +00:00
|
|
|
{
|
2017-11-05 20:49:08 +00:00
|
|
|
~grs_canvas()
|
|
|
|
{
|
|
|
|
/* `grd_curcanv` points to the currently active canvas. If it
|
|
|
|
* points to `this` when the destructor runs, then any further
|
|
|
|
* attempts to use the referenced canvas will access an
|
|
|
|
* out-of-scope variable, leading to undefined behavior. Assert
|
|
|
|
* that `grd_curcanv` does not point to the expiring variable so
|
|
|
|
* that potential misuses are detected before they manifest as
|
|
|
|
* undefined behavior.
|
|
|
|
*
|
2017-11-05 20:49:09 +00:00
|
|
|
* If you get an assertion failure here, enable
|
|
|
|
* `DXX_DEBUG_CURRENT_CANVAS_ORIGIN` to trace where the canvas
|
|
|
|
* was most recently set.
|
|
|
|
*
|
2017-11-05 20:49:08 +00:00
|
|
|
* Eventually, `grd_curcanv` will be removed and this test will
|
|
|
|
* become obsolete.
|
|
|
|
*/
|
|
|
|
assert(this != grd_curcanv);
|
2018-06-14 02:39:15 +00:00
|
|
|
/* If the canvas is reset before next use, then no crash.
|
|
|
|
* If the canvas is not reset, then crash instead of accessing
|
|
|
|
* freed memory.
|
|
|
|
*/
|
|
|
|
if (grd_curcanv == this)
|
|
|
|
grd_curcanv = nullptr;
|
2017-11-05 20:49:08 +00:00
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
grs_bitmap cv_bitmap; // the bitmap for this canvas
|
2014-07-20 03:48:27 +00:00
|
|
|
const grs_font * cv_font; // the currently selected font
|
2006-03-20 17:12:09 +00:00
|
|
|
short cv_font_fg_color; // current font foreground color (-1==Invisible)
|
|
|
|
short cv_font_bg_color; // current font background color (-1==Invisible)
|
2016-05-28 17:31:26 +00:00
|
|
|
unsigned cv_fade_level; // transparency level
|
2013-12-22 22:03:07 +00:00
|
|
|
};
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
//=========================================================================
|
|
|
|
// System functions:
|
|
|
|
// setup and set mode. this creates a grs_screen structure and sets
|
|
|
|
// grd_curscreen to point to it. grs_curcanv points to this screen's
|
|
|
|
// canvas. Saves the current VGA state and screen mode.
|
|
|
|
|
2012-11-11 22:12:51 +00:00
|
|
|
#ifdef __cplusplus
|
2015-05-14 02:23:13 +00:00
|
|
|
union screen_mode
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
uint32_t wh;
|
|
|
|
public:
|
|
|
|
struct {
|
|
|
|
uint16_t width, height;
|
|
|
|
};
|
|
|
|
bool operator==(const screen_mode &rhs)
|
|
|
|
{
|
|
|
|
return wh == rhs.wh;
|
|
|
|
}
|
|
|
|
bool operator!=(const screen_mode &rhs)
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
screen_mode() = default;
|
2016-09-25 04:52:49 +00:00
|
|
|
constexpr screen_mode(uint16_t &&w, uint16_t &&h) :
|
2015-05-14 02:23:13 +00:00
|
|
|
width(w), height(h)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline uint16_t SM_W(const screen_mode &s)
|
|
|
|
{
|
|
|
|
return s.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint16_t SM_H(const screen_mode &s)
|
|
|
|
{
|
|
|
|
return s.height;
|
|
|
|
}
|
|
|
|
|
2015-03-22 18:49:21 +00:00
|
|
|
class grs_screen : prohibit_void_ptr<grs_screen>
|
|
|
|
{ // This is a video screen
|
2015-05-14 02:23:13 +00:00
|
|
|
screen_mode sc_mode;
|
2015-03-22 18:49:21 +00:00
|
|
|
public:
|
|
|
|
grs_canvas sc_canvas; // Represents the entire screen
|
|
|
|
fix sc_aspect; //aspect ratio (w/h) for this screen
|
2015-05-14 02:23:13 +00:00
|
|
|
uint16_t get_screen_width() const
|
2015-03-22 18:49:21 +00:00
|
|
|
{
|
2015-05-14 02:23:13 +00:00
|
|
|
return SM_W(sc_mode);
|
2015-03-22 18:49:21 +00:00
|
|
|
}
|
2015-05-14 02:23:13 +00:00
|
|
|
uint16_t get_screen_height() const
|
2015-03-22 18:49:21 +00:00
|
|
|
{
|
2015-05-14 02:23:13 +00:00
|
|
|
return SM_H(sc_mode);
|
2015-03-22 18:49:21 +00:00
|
|
|
}
|
2015-05-14 02:23:13 +00:00
|
|
|
screen_mode get_screen_mode() const
|
2015-03-22 18:49:21 +00:00
|
|
|
{
|
2015-05-14 02:23:13 +00:00
|
|
|
return sc_mode;
|
2015-03-22 18:49:21 +00:00
|
|
|
}
|
2015-03-22 18:49:21 +00:00
|
|
|
void set_screen_width_height(uint16_t w, uint16_t h)
|
|
|
|
{
|
2015-05-14 02:23:13 +00:00
|
|
|
sc_mode.width = w;
|
|
|
|
sc_mode.height = h;
|
2015-03-22 18:49:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
//=========================================================================
|
|
|
|
// Canvas functions:
|
|
|
|
|
|
|
|
// Makes a new canvas. allocates memory for the canvas and its bitmap,
|
|
|
|
// including the raw pixel buffer.
|
|
|
|
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dcx {
|
2015-12-05 22:57:23 +00:00
|
|
|
|
2014-09-17 02:45:12 +00:00
|
|
|
struct grs_main_canvas : grs_canvas
|
|
|
|
{
|
|
|
|
~grs_main_canvas();
|
|
|
|
};
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
// Creates a canvas that is part of another canvas. this can be used to make
|
|
|
|
// a window on the screen. the canvas structure is malloc'd; the address of
|
|
|
|
// the raw pixel data is inherited from the parent canvas.
|
|
|
|
|
2014-09-06 23:55:43 +00:00
|
|
|
struct grs_subcanvas : grs_canvas {};
|
2008-11-09 14:31:30 +00:00
|
|
|
|
2014-07-22 02:14:56 +00:00
|
|
|
// Free the bitmap and its pixel data
|
2015-01-17 18:31:40 +00:00
|
|
|
class grs_main_bitmap : public grs_bitmap
|
2014-07-22 02:14:56 +00:00
|
|
|
{
|
2015-01-17 18:31:40 +00:00
|
|
|
public:
|
2017-02-11 21:42:31 +00:00
|
|
|
grs_main_bitmap();
|
2015-01-17 18:31:40 +00:00
|
|
|
grs_main_bitmap(const grs_main_bitmap &) = delete;
|
|
|
|
grs_main_bitmap &operator=(const grs_main_bitmap &) = delete;
|
2016-10-29 23:16:15 +00:00
|
|
|
grs_main_bitmap(grs_main_bitmap &&r) :
|
|
|
|
grs_bitmap(std::move(static_cast<grs_bitmap &>(r)))
|
|
|
|
{
|
|
|
|
r.bm_data = nullptr;
|
2018-07-14 17:23:15 +00:00
|
|
|
#if DXX_USE_OGL
|
|
|
|
r.gltexture = nullptr;
|
|
|
|
#endif
|
2016-10-29 23:16:15 +00:00
|
|
|
}
|
|
|
|
grs_main_bitmap &operator=(grs_main_bitmap &&r)
|
|
|
|
{
|
2019-02-02 18:36:39 +00:00
|
|
|
if (this == &r)
|
|
|
|
return *this;
|
|
|
|
reset();
|
2016-10-29 23:16:15 +00:00
|
|
|
grs_bitmap::operator=(std::move(static_cast<grs_bitmap &>(r)));
|
2019-02-02 18:36:39 +00:00
|
|
|
r.bm_data = nullptr;
|
2018-07-14 17:23:15 +00:00
|
|
|
#if DXX_USE_OGL
|
2019-02-02 18:36:39 +00:00
|
|
|
r.gltexture = nullptr;
|
2018-07-14 17:23:15 +00:00
|
|
|
#endif
|
2016-10-29 23:16:15 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2015-01-17 18:31:40 +00:00
|
|
|
~grs_main_bitmap()
|
2015-05-22 03:33:19 +00:00
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
void reset()
|
2014-07-22 02:14:56 +00:00
|
|
|
{
|
2015-01-17 18:31:40 +00:00
|
|
|
gr_free_bitmap_data(*this);
|
2014-07-22 02:14:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-22 02:50:01 +00:00
|
|
|
// Free the bitmap, but not the pixel data buffer
|
2015-01-17 18:31:40 +00:00
|
|
|
class grs_subbitmap : public grs_bitmap
|
2014-07-22 02:50:01 +00:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2015-12-05 22:57:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 22:09:21 +00:00
|
|
|
static inline void gr_set_bitmap_flags(grs_bitmap &bm, uint8_t flags)
|
|
|
|
{
|
2017-01-15 00:03:13 +00:00
|
|
|
bm.set_flags(flags);
|
2014-11-30 22:09:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 22:09:21 +00:00
|
|
|
static inline void gr_set_transparent(grs_bitmap &bm, bool bTransparent)
|
|
|
|
{
|
2017-01-15 00:03:13 +00:00
|
|
|
bm.set_flag_mask(bTransparent, BM_FLAG_TRANSPARENT);
|
2014-11-30 22:09:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dcx {
|
2014-07-20 03:48:27 +00:00
|
|
|
struct font_delete
|
|
|
|
{
|
|
|
|
void operator()(grs_font *p) const
|
|
|
|
{
|
|
|
|
gr_close_font(std::unique_ptr<grs_font>(p));
|
|
|
|
}
|
|
|
|
};
|
2015-12-13 18:00:48 +00:00
|
|
|
}
|
2014-07-20 03:48:27 +00:00
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
// Writes a string using current font. Returns the next column after last char.
|
2017-02-11 21:42:38 +00:00
|
|
|
static inline void gr_set_curfont(grs_canvas &canvas, const grs_font_ptr &p)
|
2014-07-20 03:48:27 +00:00
|
|
|
{
|
2017-02-11 21:42:38 +00:00
|
|
|
gr_set_curfont(canvas, p.get());
|
2014-07-20 03:48:27 +00:00
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2017-11-05 20:49:09 +00:00
|
|
|
static inline void (gr_set_current_canvas)(grs_canvas_ptr &canv DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS)
|
2014-09-17 02:45:12 +00:00
|
|
|
{
|
2017-11-05 20:49:09 +00:00
|
|
|
(gr_set_current_canvas)(canv.get() DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_PASS_VARS);
|
2014-09-17 02:45:12 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 20:49:09 +00:00
|
|
|
static inline void (gr_set_current_canvas)(grs_subcanvas_ptr &canv DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_DECL_VARS)
|
2014-09-06 23:55:43 +00:00
|
|
|
{
|
2017-11-05 20:49:09 +00:00
|
|
|
(gr_set_current_canvas)(canv.get() DXX_DEBUG_CURRENT_CANVAS_FILE_LINE_COMMA_L_PASS_VARS);
|
2014-09-06 23:55:43 +00:00
|
|
|
}
|
|
|
|
|
2012-11-11 22:12:51 +00:00
|
|
|
#endif
|