1 // Copyright (c) Warwick Allison, 1999.
2 // Qt4 conversion copyright (c) Ray Chason, 2012-2014.
3 // NetHack may be freely redistributed.  See license for details.
4 
5 // qt4glyph.cpp -- class to manage the glyphs in a tile set
6 
7 extern "C" {
8 #include "hack.h"
9 }
10 #include "tile2x11.h"
11 #undef Invisible
12 #undef Warning
13 #undef index
14 #undef msleep
15 #undef rindex
16 #undef wizard
17 #undef yn
18 #undef min
19 #undef max
20 
21 #include <QtGui/QtGui>
22 #if QT_VERSION >= 0x050000
23 #include <QtWidgets/QtWidgets>
24 #endif
25 #include "qt4glyph.h"
26 #include "qt4set.h"
27 #include "qt4str.h"
28 
29 extern short glyph2tile[]; // from tile.c
30 
31 namespace nethack_qt4 {
32 
33 static int tilefile_tile_W=16;
34 static int tilefile_tile_H=16;
35 
36 // Debian uses a separate PIXMAPDIR
37 #ifndef PIXMAPDIR
38 # ifdef HACKDIR
39 #  define PIXMAPDIR HACKDIR
40 # else
41 #  define PIXMAPDIR "."
42 # endif
43 #endif
44 
NetHackQtGlyphs()45 NetHackQtGlyphs::NetHackQtGlyphs()
46 {
47     const char* tile_file = PIXMAPDIR "/nhtiles.bmp";
48     if ( iflags.wc_tile_file )
49 	tile_file = iflags.wc_tile_file;
50 
51     if (!img.load(tile_file)) {
52 	tile_file = PIXMAPDIR "/x11tiles";
53 	if (!img.load(tile_file)) {
54 	    QString msg;
55 	    msg.sprintf("Cannot load x11tiles or nhtiles.bmp");
56 	    QMessageBox::warning(0, "IO Error", msg);
57 	} else {
58 	    tiles_per_row = TILES_PER_ROW;
59 	    if (img.width()%tiles_per_row) {
60 		impossible("Tile file \"%s\" has %d columns, not multiple of row count (%d)",
61 			tile_file, img.width(), tiles_per_row);
62 	    }
63 	}
64     } else {
65 	tiles_per_row = 40;
66     }
67 
68     if ( iflags.wc_tile_width )
69 	tilefile_tile_W = iflags.wc_tile_width;
70     else
71 	tilefile_tile_W = img.width() / tiles_per_row;
72     if ( iflags.wc_tile_height )
73 	tilefile_tile_H = iflags.wc_tile_height;
74     else
75 	tilefile_tile_H = tilefile_tile_W;
76 
77     setSize(tilefile_tile_W, tilefile_tile_H);
78 }
79 
drawGlyph(QPainter & painter,int glyph,int x,int y)80 void NetHackQtGlyphs::drawGlyph(QPainter& painter, int glyph, int x, int y)
81 {
82     int tile = glyph2tile[glyph];
83     int px = (tile%tiles_per_row)*width();
84     int py = tile/tiles_per_row*height();
85 
86     painter.drawPixmap(
87 	x,
88 	y,
89 	pm,
90 	px,py,
91 	width(),height()
92     );
93 }
drawCell(QPainter & painter,int glyph,int cellx,int celly)94 void NetHackQtGlyphs::drawCell(QPainter& painter, int glyph, int cellx, int celly)
95 {
96     drawGlyph(painter,glyph,cellx*width(),celly*height());
97 }
glyph(int glyph)98 QPixmap NetHackQtGlyphs::glyph(int glyph)
99 {
100     int tile = glyph2tile[glyph];
101     int px = (tile%tiles_per_row)*tilefile_tile_W;
102     int py = tile/tiles_per_row*tilefile_tile_H;
103 
104     return QPixmap::fromImage(img.copy(px, py, tilefile_tile_W, tilefile_tile_H));
105 }
setSize(int w,int h)106 void NetHackQtGlyphs::setSize(int w, int h)
107 {
108     if ( size == QSize(w,h) )
109 	return;
110 
111     bool was1 = size == pm1.size();
112     size = QSize(w,h);
113     if (!w || !h)
114 	return; // Still not decided
115 
116     if ( size == pm1.size() ) {
117 	pm = pm1;
118 	return;
119     }
120     if ( size == pm2.size() ) {
121 	pm = pm2;
122 	return;
123     }
124 
125     if (w==tilefile_tile_W && h==tilefile_tile_H) {
126 	pm.convertFromImage(img);
127     } else {
128 	QApplication::setOverrideCursor( Qt::WaitCursor );
129 	QImage scaled = img.scaled(
130 	    w*img.width()/tilefile_tile_W,
131 	    h*img.height()/tilefile_tile_H,
132 	    Qt::IgnoreAspectRatio,
133 	    Qt::FastTransformation
134 	);
135 	pm.convertFromImage(scaled,Qt::ThresholdDither|Qt::PreferDither);
136 	QApplication::restoreOverrideCursor();
137     }
138     (was1 ? pm2 : pm1) = pm;
139 }
140 
141 } // namespace nethack_qt4
142