some code cleanup, mostly Qt map

The Qt routine NetHackQtMapViewport::Clear() was broken, but
fixing it hasn't changed the glyph display issue.  None of the
other changes here would be expected to affect that but they
are in/among the sections of code under investigation.
This commit is contained in:
PatR
2020-12-27 12:33:03 -08:00
parent ea1ffe5112
commit 6499fc4dd9
3 changed files with 107 additions and 115 deletions
+10 -8
View File
@@ -1,4 +1,4 @@
/* NetHack 3.7 display.c $NHDT-Date: 1606919261 2020/12/02 14:27:41 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.139 $ */ /* NetHack 3.7 display.c $NHDT-Date: 1609101156 2020/12/27 20:32:36 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.141 $ */
/* Copyright (c) Dean Luick, with acknowledgements to Kevin Darcy */ /* Copyright (c) Dean Luick, with acknowledgements to Kevin Darcy */
/* and Dave Cohrs, 1990. */ /* and Dave Cohrs, 1990. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -1370,15 +1370,16 @@ see_traps()
} }
} }
static unsigned no_gm[NUM_GLYPHMOD] = static unsigned no_gm[NUM_GLYPHMOD] = {
{MG_BADXY, (unsigned) ' ', (unsigned) NO_COLOR}; MG_BADXY, (unsigned) ' ', (unsigned) NO_COLOR
};
#ifndef UNBUFFERED_GLYPHMOD #ifndef UNBUFFERED_GLYPHMOD
#define Glyphmod_at(x,y,glyph) ( \ #define Glyphmod_at(x, y, glyph) \
((x) < 0 || (y) < 0 || (x) >= COLNO || (y) >= ROWNO) \ (((x) < 0 || (y) < 0 || (x) >= COLNO || (y) >= ROWNO) ? &no_gm[0] \
? &no_gm[0] : &g.gbuf[(y)][(x)].glyphmod[0]) : &g.gbuf[(y)][(x)].glyphmod[0])
#else #else
static unsigned gm[NUM_GLYPHMOD]; static unsigned gm[NUM_GLYPHMOD];
#define Glyphmod_at(x,y,glyph) glyphmod_at(x, y, glyph) #define Glyphmod_at(x, y, glyph) glyphmod_at(x, y, glyph)
#endif #endif
/* /*
@@ -2425,7 +2426,8 @@ unsigned mgflags, *glyphmod;
} }
} }
glyphmod[GM_TTYCHAR] = ((mgflags & MG_FLAG_RETURNIDX) != 0) ? idx : g.showsyms[idx]; glyphmod[GM_TTYCHAR] = ((mgflags & MG_FLAG_RETURNIDX) != 0) ? idx
: g.showsyms[idx];
#ifdef TEXTCOLOR #ifdef TEXTCOLOR
/* Turn off color if no color defined, or rogue level w/o PC graphics. */ /* Turn off color if no color defined, or rogue level w/o PC graphics. */
+92 -103
View File
@@ -110,16 +110,16 @@ static const QPen& nhcolor_to_pen(int c)
#endif #endif
NetHackQtMapViewport::NetHackQtMapViewport(NetHackQtClickBuffer& click_sink) : NetHackQtMapViewport::NetHackQtMapViewport(NetHackQtClickBuffer& click_sink) :
QWidget(NULL), QWidget(NULL),
rogue_font(NULL), rogue_font(NULL),
clicksink(click_sink), clicksink(click_sink),
change(10) change(10)
{ {
pet_annotation = QPixmap(qt_compact_mode ? pet_mark_small_xpm pet_annotation = QPixmap(qt_compact_mode ? pet_mark_small_xpm
: pet_mark_xpm); : pet_mark_xpm);
pile_annotation = QPixmap(pile_mark_xpm); pile_annotation = QPixmap(pile_mark_xpm);
Clear(); Clear(); // initializes glyph[][], glyphttychar, glyphcolor, glyphflags
cursor.setX(0); cursor.setX(0);
cursor.setY(0); cursor.setY(0);
} }
@@ -129,16 +129,42 @@ NetHackQtMapViewport::~NetHackQtMapViewport(void)
delete rogue_font; delete rogue_font;
} }
// pick a font to use for text map; rogue level is always displayed as text
void NetHackQtMapViewport::SetRogueFont(QPainter &painter)
{
QString fontfamily = iflags.wc_font_map ? iflags.wc_font_map
: "Monospace";
int maybebold = QFont::Normal;
if (fontfamily.right(5).toLower() == "-bold") {
fontfamily.truncate(fontfamily.length() - 5);
maybebold = QFont::Bold;
}
// Find font...
int pts = 5;
while (pts < 32) {
QFont f(fontfamily, pts, maybebold);
painter.setFont(QFont(fontfamily, pts));
QFontMetrics fm = painter.fontMetrics();
if (fm.width("M") > qt_settings->glyphs().width())
break;
if (fm.height() > qt_settings->glyphs().height())
break;
pts++;
}
rogue_font = new QFont(fontfamily, pts - 1);
}
void NetHackQtMapViewport::paintEvent(QPaintEvent* event) void NetHackQtMapViewport::paintEvent(QPaintEvent* event)
{ {
QRect area=event->rect(); NetHackQtGlyphs &glyphs = qt_settings->glyphs();
int gW = glyphs.width(),
gH = glyphs.height();
QRect area = event->rect();
QRect garea; QRect garea;
garea.setCoords( garea.setCoords(std::max(0, area.left() / gW),
std::max(0,area.left()/qt_settings->glyphs().width()), std::max(0, area.top() / gH),
std::max(0,area.top()/qt_settings->glyphs().height()), std::min(COLNO - 1, area.right() / gW),
std::min(COLNO-1,area.right()/qt_settings->glyphs().width()), std::min(ROWNO - 1, area.bottom() / gH));
std::min(ROWNO-1,area.bottom()/qt_settings->glyphs().height())
);
QPainter painter; QPainter painter;
@@ -150,74 +176,42 @@ void NetHackQtMapViewport::paintEvent(QPaintEvent* event)
painter.setClipRect( event->rect() ); // (normally we don't clip) painter.setClipRect( event->rect() ); // (normally we don't clip)
painter.fillRect( event->rect(), Qt::black ); painter.fillRect( event->rect(), Qt::black );
if ( !rogue_font ) { if (!rogue_font)
// Find font... SetRogueFont(painter);
int pts = 5;
QString fontfamily = iflags.wc_font_map
? iflags.wc_font_map : "Monospace";
bool bold = false;
if ( fontfamily.right(5).toLower() == "-bold" ) {
fontfamily.truncate(fontfamily.length()-5);
bold = true;
}
while ( pts < 32 ) {
QFont f(fontfamily, pts, bold ? QFont::Bold : QFont::Normal);
painter.setFont(QFont(fontfamily, pts));
QFontMetrics fm = painter.fontMetrics();
if ( fm.width("M") > qt_settings->glyphs().width() )
break;
if ( fm.height() > qt_settings->glyphs().height() )
break;
pts++;
}
rogue_font = new QFont(fontfamily,pts-1);
}
painter.setFont(*rogue_font); painter.setFont(*rogue_font);
for (int j=garea.top(); j<=garea.bottom(); j++) { for (int j=garea.top(); j<=garea.bottom(); j++) {
for (int i=garea.left(); i<=garea.right(); i++) { for (int i=garea.left(); i<=garea.right(); i++) {
#if 0 #if 0
unsigned short g=Glyph(i,j); unsigned short g = Glyph(i, j);
int color; int colortmp;
int ch; int chtmp;
unsigned special; unsigned special;
#else
int color = Glyphcolor(i,j);
int ch = Glyphttychar(i,j);
unsigned special = Glyphflags(i,j);
#endif
painter.setPen( Qt::green );
/* map glyph to character and color */ /* map glyph to character and color */
// mapglyph(g, &ch, &color, &special, i, j, 0); mapglyph(g, &chtmp, &colortmp, &special, i, j, 0);
uchar ch = (uchar) chtmp, color = (uchar) colortmp;
#else
uchar color = Glyphcolor(i, j);
uchar ch = Glyphttychar(i, j);
unsigned special = Glyphflags(i, j);
#endif
ch = cp437(ch); ch = cp437(ch);
#ifdef TEXTCOLOR #ifdef TEXTCOLOR
painter.setPen( nhcolor_to_pen(color) ); painter.setPen( nhcolor_to_pen(color) );
#else
painter.setPen( Qt::green );
#endif #endif
if (!DrawWalls( if (!DrawWalls(painter, i * gW, j * gH, gW, gH, ch)) {
painter, painter.drawText(i * gW, j * gH, gW, gH, Qt::AlignCenter,
i*qt_settings->glyphs().width(), QString(QChar(ch)).left(1));
j*qt_settings->glyphs().height(),
qt_settings->glyphs().width(),
qt_settings->glyphs().height(),
ch)) {
painter.drawText(
i*qt_settings->glyphs().width(),
j*qt_settings->glyphs().height(),
qt_settings->glyphs().width(),
qt_settings->glyphs().height(),
Qt::AlignCenter,
QString(QChar(ch)).left(1)
);
} }
#ifdef TEXTCOLOR #ifdef TEXTCOLOR
if ((special & MG_PET) != 0 && ::iflags.hilite_pet) { if ((special & MG_PET) != 0 && ::iflags.hilite_pet) {
painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), painter.drawPixmap(QPoint(i * gW, j * gH),
j*qt_settings->glyphs().height()),
pet_annotation); pet_annotation);
} else if ((special & MG_OBJPILE) != 0 } else if ((special & MG_OBJPILE) != 0
&& ::iflags.hilite_pile) { && ::iflags.hilite_pile) {
painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), painter.drawPixmap(QPoint(i * gW, j * gH),
j*qt_settings->glyphs().height()),
pile_annotation); pile_annotation);
} }
#endif #endif
@@ -238,19 +232,16 @@ void NetHackQtMapViewport::paintEvent(QPaintEvent* event)
unsigned special = Glyphflags(i, j); unsigned special = Glyphflags(i, j);
#endif #endif
bool femflag = (special & MG_FEMALE) ? true : false; bool femflag = (special & MG_FEMALE) ? true : false;
qt_settings->glyphs().drawCell(painter, g, i, j, femflag); glyphs.drawCell(painter, g, i, j, femflag);
#ifdef TEXTCOLOR
if ((special & MG_PET) != 0 && ::iflags.hilite_pet) { if ((special & MG_PET) != 0 && ::iflags.hilite_pet) {
painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), painter.drawPixmap(QPoint(i * gW, j * gH),
j*qt_settings->glyphs().height()),
pet_annotation); pet_annotation);
} else if ((special & MG_OBJPILE) != 0 } else if ((special & MG_OBJPILE) != 0
&& ::iflags.hilite_pile) { && ::iflags.hilite_pile) {
painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), painter.drawPixmap(QPoint(i * gW, j * gH),
j*qt_settings->glyphs().height()),
pile_annotation); pile_annotation);
} }
#endif
} }
} }
} }
@@ -262,31 +253,26 @@ void NetHackQtMapViewport::paintEvent(QPaintEvent* event)
#else #else
painter.setPen( Qt::green ); // REALLY primitive painter.setPen( Qt::green ); // REALLY primitive
#endif #endif
} else } else {
{ int hp = !Upolyd ? u.uhp : u.mh,
int hp100; hpmax = !Upolyd ? u.uhpmax : u.mhmax,
if (Upolyd) { hp100 = hpmax ? (hp * 100 / hpmax) : 100;
hp100=u.mhmax ? u.mh*100/u.mhmax : 100;
} else {
hp100=u.uhpmax ? u.uhp*100/u.uhpmax : 100;
}
if (hp100 > 75) // this uses a different color scheme from hitpoint bar but has
painter.setPen(Qt::white); // the same cutoff thresholds (except for lack of separate 100%)
else if (hp100 > 50) if (hp100 < 10 || hp < 5)
painter.setPen(Qt::yellow);
else if (hp100 > 25)
painter.setPen(QColor(0xff, 0xbf, 0x00)); // orange
else if (hp100 > 10)
painter.setPen(Qt::red);
else
painter.setPen(Qt::magenta); painter.setPen(Qt::magenta);
else if (hp100 < 25 || hp < 10 )
painter.setPen(Qt::red);
else if (hp100 < 50)
painter.setPen(QColor(0xff, 0xbf, 0x00)); // orange
else if (hp100 < 75)
painter.setPen(Qt::yellow);
else
painter.setPen(Qt::white);
} }
painter.drawRect(cursor.x() * qt_settings->glyphs().width(), painter.drawRect(cursor.x() * gW, cursor.y() * gH, gW - 1, gH - 1);
cursor.y() * qt_settings->glyphs().height(),
qt_settings->glyphs().width() - 1,
qt_settings->glyphs().height() - 1);
} }
#if 0 #if 0
@@ -312,8 +298,7 @@ bool NetHackQtMapViewport::DrawWalls(
int x, int y, int w, int h, int x, int y, int w, int h,
unsigned ch) unsigned ch)
{ {
enum enum wallbits {
{
w_left = 0x01, w_left = 0x01,
w_right = 0x02, w_right = 0x02,
w_up = 0x04, w_up = 0x04,
@@ -562,6 +547,7 @@ void NetHackQtMapViewport::clickCursor()
qApp->exit(); qApp->exit();
} }
// [re-]init map display to unexplored with no changed cells
void NetHackQtMapViewport::Clear() void NetHackQtMapViewport::Clear()
{ {
for (int j = 0; j < ROWNO; ++j) { for (int j = 0; j < ROWNO; ++j) {
@@ -572,11 +558,13 @@ void NetHackQtMapViewport::Clear()
Glyphttychar(0, j) = ' '; Glyphttychar(0, j) = ' ';
Glyphcolor(0, j) = NO_COLOR; Glyphcolor(0, j) = NO_COLOR;
Glyphflags(0, j) = 0; Glyphflags(0, j) = 0;
for (int i = 1; i < COLNO; ++i)
for (int i = 1; i < COLNO; ++i) {
Glyph(i, j) = GLYPH_UNEXPLORED; Glyph(i, j) = GLYPH_UNEXPLORED;
Glyphttychar(0, j) = ' '; Glyphttychar(i, j) = ' ';
Glyphcolor(0, j) = NO_COLOR; Glyphcolor(i, j) = NO_COLOR;
Glyphflags(0, j) = 0; Glyphflags(i, j) = 0;
}
} }
change.clear(); change.clear();
@@ -610,13 +598,14 @@ void NetHackQtMapViewport::CursorTo(int x,int y)
Changed(cursor.x(),cursor.y()); Changed(cursor.x(),cursor.y());
} }
void NetHackQtMapViewport::PrintGlyph(int x,int y,int theglyph,unsigned *glyphmod) void NetHackQtMapViewport::PrintGlyph(int x, int y, int theglyph,
unsigned *glyphmod)
{ {
Glyph(x,y)=theglyph; Glyph(x, y) = theglyph;
Glyphttychar(x,y)=glyphmod[GM_TTYCHAR]; Glyphttychar(x, y) = (uchar) glyphmod[GM_TTYCHAR];
Glyphcolor(x,y)=glyphmod[GM_COLOR]; Glyphcolor(x, y) = (uchar) glyphmod[GM_COLOR];
Glyphflags(x,y)=glyphmod[GM_FLAGS]; Glyphflags(x, y) = glyphmod[GM_FLAGS];
Changed(x,y); Changed(x, y);
} }
void NetHackQtMapViewport::Changed(int x, int y) void NetHackQtMapViewport::Changed(int x, int y)
+5 -4
View File
@@ -32,10 +32,10 @@ private:
QFont *rogue_font; QFont *rogue_font;
unsigned short glyph[ROWNO][COLNO]; unsigned short glyph[ROWNO][COLNO];
unsigned short& Glyph(int x, int y) { return glyph[y][x]; } unsigned short& Glyph(int x, int y) { return glyph[y][x]; }
unsigned int glyphttychar[ROWNO][COLNO]; uchar glyphttychar[ROWNO][COLNO];
unsigned int& Glyphttychar(int x, int y) { return glyphttychar[y][x]; } uchar& Glyphttychar(int x, int y) { return glyphttychar[y][x]; }
unsigned int glyphcolor[ROWNO][COLNO]; uchar glyphcolor[ROWNO][COLNO];
unsigned int& Glyphcolor(int x, int y) { return glyphcolor[y][x]; } uchar& Glyphcolor(int x, int y) { return glyphcolor[y][x]; }
unsigned int glyphflags[ROWNO][COLNO]; unsigned int glyphflags[ROWNO][COLNO];
unsigned int& Glyphflags(int x, int y) { return glyphflags[y][x]; } unsigned int& Glyphflags(int x, int y) { return glyphflags[y][x]; }
QPoint cursor; QPoint cursor;
@@ -51,6 +51,7 @@ private:
void PrintGlyph(int x,int y,int glyph,unsigned *glyphmod); void PrintGlyph(int x,int y,int glyph,unsigned *glyphmod);
void Changed(int x, int y); void Changed(int x, int y);
void updateTiles(); void updateTiles();
void SetRogueFont(QPainter &painter);
// NetHackQtMapWindow2 passes through many calls to the viewport // NetHackQtMapWindow2 passes through many calls to the viewport
friend class NetHackQtMapWindow2; friend class NetHackQtMapWindow2;