Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to fixed width integer types: proven to be free of errors #268

Merged
merged 16 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
56 changes: 28 additions & 28 deletions 2dlib/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@
typedef CFILE *FONTFILE;

static inline int READ_FONT_INT(FONTFILE ffile);
static inline short READ_FONT_SHORT(FONTFILE ffile);
static inline ubyte READ_FONT_BYTE(FONTFILE ffile);
static inline int16_t READ_FONT_SHORT(FONTFILE ffile);
static inline uint8_t READ_FONT_BYTE(FONTFILE ffile);
static inline int READ_FONT_DATA(FONTFILE ffile, void *buf, int size, int nelem);
static inline FONTFILE OPEN_FONT(char *filename, bool &success);
static inline void CLOSE_FONT(FONTFILE ffile);
Expand All @@ -153,14 +153,14 @@ static inline void CLOSE_FONT(FONTFILE ffile);

inline int READ_FONT_INT(FONTFILE ffile) { return cf_ReadInt(ffile); }

inline short READ_FONT_SHORT(FONTFILE ffile) { return cf_ReadShort(ffile); }
inline int16_t READ_FONT_SHORT(FONTFILE ffile) { return cf_ReadShort(ffile); }

inline ubyte READ_FONT_BYTE(FONTFILE ffile) { return (ubyte)cf_ReadByte(ffile); }
inline uint8_t READ_FONT_BYTE(FONTFILE ffile) { return (uint8_t)cf_ReadByte(ffile); }

inline int READ_FONT_DATA(FONTFILE ffile, void *buf, int size, int nelem) {
int i;

i = cf_ReadBytes((ubyte *)buf, size * nelem, ffile);
i = cf_ReadBytes((uint8_t *)buf, size * nelem, ffile);

ASSERT(i == (size * nelem));

Expand Down Expand Up @@ -405,7 +405,7 @@ void grFont::load(char *filename, int slot) {

// Read in all widths
if (ft->flags & FT_PROPORTIONAL) {
ft->char_widths = new short[num_char];
ft->char_widths = new int16_t[num_char];
for (i = 0; i < num_char; i++)
ft->char_widths[i] = READ_FONT_SHORT(ff);
mprintf((0, "::proportional"));
Expand All @@ -420,8 +420,8 @@ void grFont::load(char *filename, int slot) {
// generate character data pointer table
int bytesize = READ_FONT_INT(ff);

ft->raw_data = (ubyte *)mem_malloc(bytesize);
ft->char_data = (ubyte **)mem_malloc(num_char * sizeof(ubyte *));
ft->raw_data = (uint8_t *)mem_malloc(bytesize);
ft->char_data = (uint8_t **)mem_malloc(num_char * sizeof(uint8_t *));

READ_FONT_DATA(ff, ft->raw_data, bytesize, 1);

Expand All @@ -436,7 +436,7 @@ void grFont::load(char *filename, int slot) {
off += (ft->width * ft->height * BITS_TO_BYTES(BPP_16));
}
} else { // Monochrome
ubyte *ptr = ft->raw_data;
uint8_t *ptr = ft->raw_data;
mprintf((0, "::mono"));
for (i = 0; i < num_char; i++) {
ft->char_data[i] = ptr;
Expand Down Expand Up @@ -474,18 +474,18 @@ void grFont::translate_to_surfaces(int slot) {
// create a 128x128 surface first.
// draw each character into surface until we need to create another
// surface.
ubyte u = 0, v = 0, w;
uint8_t u = 0, v = 0, w;
int ch, num_ch;
ubyte surf_index = 0;
uint8_t surf_index = 0;

num_ch = fntfile->max_ascii - fntfile->min_ascii + 1;

// initialize memory
fnt->ch_w = new ubyte[num_ch];
fnt->ch_h = new ubyte[num_ch];
fnt->ch_u = new ubyte[num_ch];
fnt->ch_v = new ubyte[num_ch];
fnt->ch_surf = new ubyte[num_ch];
fnt->ch_w = new uint8_t[num_ch];
fnt->ch_h = new uint8_t[num_ch];
fnt->ch_u = new uint8_t[num_ch];
fnt->ch_v = new uint8_t[num_ch];
fnt->ch_surf = new uint8_t[num_ch];
fnt->ch_uf = new float[num_ch];
fnt->ch_vf = new float[num_ch];
fnt->ch_wf = new float[num_ch];
Expand Down Expand Up @@ -554,19 +554,19 @@ void grFont::translate_to_surfaces(int slot) {
void grFont::translate_mono_char(grSurface *sf, int x, int y, int index, gr_font_file_record *ft, int width) {
int row, col; // byte width of char
int rowsize;
ubyte bit_mask = 0, byte;
ubyte *fp;
uint8_t bit_mask = 0, byte;
uint8_t *fp;

fp = ft->char_data[index];

switch (sf->bpp()) {
case BPP_16: {
/* draw one-bit one color. */
ushort *dest_ptr;
ushort col_w = GR_COLOR_TO_16(GR_RGB(255, 255, 255));
uint16_t *dest_ptr;
uint16_t col_w = GR_COLOR_TO_16(GR_RGB(255, 255, 255));
int rowsize_w;

dest_ptr = (ushort *)sf->lock(&rowsize);
dest_ptr = (uint16_t *)sf->lock(&rowsize);
rowsize_w = sf->rowsize() / 2;
dest_ptr += (y * rowsize_w) + x;

Expand Down Expand Up @@ -704,9 +704,9 @@ int grFont::get_char_info(int ch, int *width) {
return 0;
}

ubyte *grFont::get_kern_info(ubyte c1, ubyte c2) {
uint8_t *grFont::get_kern_info(uint8_t c1, uint8_t c2) {
// gr_font_file_record *ft;
// ubyte *p;
// uint8_t *p;

// p = ft->kern_data;
// ft = &grFont::m_FontList[m_FontHandle].font;
Expand All @@ -723,14 +723,14 @@ ubyte *grFont::get_kern_info(ubyte c1, ubyte c2) {
}

void grFont::charblt16(grSurface *dsf, ddgr_color col, int dx, int dy, grSurface *ssf, int sx, int sy, int sw, int sh) {
ushort *dbits;
ushort *sbits;
uint16_t *dbits;
uint16_t *sbits;
int srowsize_w, drowsize_w, row, coln;
ushort scol = GR_COLOR_TO_16(col);
uint16_t scol = GR_COLOR_TO_16(col);

dbits = (ushort *)dsf->lock(&drowsize_w);
dbits = (uint16_t *)dsf->lock(&drowsize_w);
if (dbits) {
sbits = (ushort *)ssf->lock(&srowsize_w);
sbits = (uint16_t *)ssf->lock(&srowsize_w);
if (sbits) {
srowsize_w >>= 1; // rowsize in shorts
drowsize_w >>= 1;
Expand Down
6 changes: 3 additions & 3 deletions 2dlib/lib2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@

typedef struct mem_bitmap {
char *data;
short bpp;
int16_t bpp;
int rowsize;
ushort alloced : 2;
ushort flag : 14;
uint16_t alloced : 2;
uint16_t flag : 14;
} mem_bitmap;

#define MEMFLAG_TRANSBLT 1
Expand Down
28 changes: 14 additions & 14 deletions 2dlib/memsurf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bool grMemorySurface::init(int w, int h, int bpp, char *data, int rowsize, unsig
ddsfObj.h = h;
ddsfObj.bpp = bpp;
ddsfObj.type = SURFTYPE_MEMORY;
ddsfObj.flags = (ushort)flags;
ddsfObj.flags = (uint16_t)flags;
if (name)
strncpy(ddsfObj.name, name, 15);

Expand Down Expand Up @@ -224,7 +224,7 @@ bool gr_mem_surf_Init(ddgr_surface *sf, char *data, int rowsize) {

mbm->data = data;
mbm->rowsize = rowsize;
mbm->bpp = (short)sf->bpp;
mbm->bpp = (int16_t)sf->bpp;
mbm->flag = 0;

sf->obj = (void *)mbm;
Expand All @@ -240,11 +240,11 @@ void gr_mem_surf_Clear(ddgr_surface *dsf, ddgr_color col, int l, int t, int w, i

switch (dbm->bpp) {
case BPP_16: {
ushort *sptr;
ushort scol;
uint16_t *sptr;
uint16_t scol;

scol = (ushort)GR_COLOR_TO_16(col);
sptr = (ushort *)dbm->data + (t * ((dbm->rowsize) >> 1));
scol = (uint16_t)GR_COLOR_TO_16(col);
sptr = (uint16_t *)dbm->data + (t * ((dbm->rowsize) >> 1));

for (int y = t; y < (t + h); y++) {
for (int x = l; x < (l + w); x++)
Expand Down Expand Up @@ -314,15 +314,15 @@ bool gr_mem_surf_AttachHandle(ddgr_surface *sf, unsigned handle) { return true;

bool gr_bm_mem_Blt16(ddgr_surface *dsf, int dx, int dy, ddgr_surface *ssf, int sx, int sy, int sw, int sh) {
mem_bitmap *dbm = (mem_bitmap *)dsf->obj, *sbm = (mem_bitmap *)ssf->obj;
ushort *dbits;
ushort *sbits;
uint16_t *dbits;
uint16_t *sbits;
int srowsize_w, drowsize_w, row, col;

// set up blt.
srowsize_w = sbm->rowsize >> 1; // rowsize in shorts
drowsize_w = dbm->rowsize >> 1;
dbits = (ushort *)dbm->data + (dy * drowsize_w) + dx;
sbits = (ushort *)sbm->data + (sy * srowsize_w) + sx;
dbits = (uint16_t *)dbm->data + (dy * drowsize_w) + dx;
sbits = (uint16_t *)sbm->data + (sy * srowsize_w) + sx;

for (row = 0; row < sh; row++) {
for (col = 0; col < sw; col++)
Expand All @@ -336,15 +336,15 @@ bool gr_bm_mem_Blt16(ddgr_surface *dsf, int dx, int dy, ddgr_surface *ssf, int s

bool gr_bm_mem_Blt16ck(ddgr_surface *dsf, int dx, int dy, ddgr_surface *ssf, int sx, int sy, int sw, int sh) {
mem_bitmap *dbm = (mem_bitmap *)dsf->obj, *sbm = (mem_bitmap *)ssf->obj;
ushort *dbits;
ushort *sbits;
uint16_t *dbits;
uint16_t *sbits;
int srowsize_w, drowsize_w, row, col;

// set up blt.
srowsize_w = sbm->rowsize >> 1; // rowsize in shorts
drowsize_w = dbm->rowsize >> 1;
dbits = (ushort *)dbm->data + (dy * drowsize_w) + dx;
sbits = (ushort *)sbm->data + (sy * srowsize_w) + sx;
dbits = (uint16_t *)dbm->data + (dy * drowsize_w) + dx;
sbits = (uint16_t *)sbm->data + (sy * srowsize_w) + sx;

for (row = 0; row < sh; row++) {
for (col = 0; col < sw; col++) {
Expand Down
12 changes: 6 additions & 6 deletions 2dlib/pentext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ void grViewport::draw_text_line_clip(int x, int y, char *str) {

cur_x = x;
for (i = 0; i < (int)strlen(str); i++) {
ubyte ch;
uint8_t ch;
int w;
ch = (ubyte)str[i];
ch = (uint8_t)str[i];
text_Font.get_char_info((int)ch, &w);

if (ch == GR_COLOR_CHAR) {
Expand All @@ -300,7 +300,7 @@ void grViewport::draw_text_line_clip(int x, int y, char *str) {
i += 4;
if (i >= (int)strlen(str))
Int3(); // This shouldn't happen too.
ch = (ubyte)str[i];
ch = (uint8_t)str[i];
} else if (ch == '\t') { // tab char
int space_width;
text_Font.get_char_info(' ', &space_width);
Expand Down Expand Up @@ -342,9 +342,9 @@ void grViewport::draw_text_line(int x, int y, char *str) {
*/
cur_x = x;
for (i = 0; i < (int)strlen(str); i++) {
ubyte ch;
uint8_t ch;

ch = (ubyte)str[i];
ch = (uint8_t)str[i];

if (ch == GR_COLOR_CHAR) {
if ((i + 3) >= (int)strlen(str))
Expand All @@ -353,7 +353,7 @@ void grViewport::draw_text_line(int x, int y, char *str) {
i += 4;
if (i >= (int)strlen(str))
Int3(); // This shouldn't happen too.
ch = (ubyte)str[i];
ch = (uint8_t)str[i];
} else if (ch == '\t') { // tab char
int space_width;
text_Font.get_char_info(' ', &space_width);
Expand Down