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.
5 // qt4glyph.cpp -- class to manage the glyphs in a tile set
21 #include <QtGui/QtGui>
22 #if QT_VERSION >= 0x050000
23 #include <QtWidgets/QtWidgets>
29 extern short glyph2tile[]; // from tile.c
31 namespace nethack_qt4 {
33 static int tilefile_tile_W=16;
34 static int tilefile_tile_H=16;
36 // Debian uses a separate PIXMAPDIR
39 # define PIXMAPDIR HACKDIR
41 # define PIXMAPDIR "."
45 NetHackQtGlyphs::NetHackQtGlyphs()
47 const char* tile_file = PIXMAPDIR "/nhtiles.bmp";
48 if ( iflags.wc_tile_file )
49 tile_file = iflags.wc_tile_file;
51 if (!img.load(tile_file)) {
52 tile_file = PIXMAPDIR "/x11tiles";
53 if (!img.load(tile_file)) {
55 msg.sprintf("Cannot load x11tiles or nhtiles.bmp");
56 QMessageBox::warning(0, "IO Error", msg);
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);
68 if ( iflags.wc_tile_width )
69 tilefile_tile_W = iflags.wc_tile_width;
71 tilefile_tile_W = img.width() / tiles_per_row;
72 if ( iflags.wc_tile_height )
73 tilefile_tile_H = iflags.wc_tile_height;
75 tilefile_tile_H = tilefile_tile_W;
77 setSize(tilefile_tile_W, tilefile_tile_H);
80 void NetHackQtGlyphs::drawGlyph(QPainter& painter, int glyph, int x, int y)
82 int tile = glyph2tile[glyph];
83 int px = (tile%tiles_per_row)*width();
84 int py = tile/tiles_per_row*height();
94 void NetHackQtGlyphs::drawCell(QPainter& painter, int glyph, int cellx, int celly)
96 drawGlyph(painter,glyph,cellx*width(),celly*height());
98 QPixmap NetHackQtGlyphs::glyph(int glyph)
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;
104 return QPixmap::fromImage(img.copy(px, py, tilefile_tile_W, tilefile_tile_H));
106 void NetHackQtGlyphs::setSize(int w, int h)
108 if ( size == QSize(w,h) )
111 bool was1 = size == pm1.size();
114 return; // Still not decided
116 if ( size == pm1.size() ) {
120 if ( size == pm2.size() ) {
125 if (w==tilefile_tile_W && h==tilefile_tile_H) {
126 pm.convertFromImage(img);
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
135 pm.convertFromImage(scaled,Qt::ThresholdDither|Qt::PreferDither);
136 QApplication::restoreOverrideCursor();
138 (was1 ? pm2 : pm1) = pm;
141 } // namespace nethack_qt4