139 lines
3.4 KiB
C++
139 lines
3.4 KiB
C++
// Copyright (c) Warwick Allison, 1999.
|
|
// Qt4 conversion copyright (c) Ray Chason, 2012-2014.
|
|
// NetHack may be freely redistributed. See license for details.
|
|
|
|
// qt_glyph.cpp -- class to manage the glyphs in a tile set
|
|
|
|
extern "C" {
|
|
#include "hack.h"
|
|
#include "tile2x11.h" /* x11tiles is potential fallback for nhtiles.bmp */
|
|
}
|
|
|
|
#include "qt_pre.h"
|
|
#include <QtGui/QtGui>
|
|
#if QT_VERSION >= 0x050000
|
|
#include <QtWidgets/QtWidgets>
|
|
#endif
|
|
#include "qt_post.h"
|
|
#include "qt_glyph.h"
|
|
#include "qt_bind.h"
|
|
#include "qt_set.h"
|
|
#include "qt_str.h"
|
|
|
|
extern short glyph2tile[]; // from tile.c
|
|
|
|
namespace nethack_qt_ {
|
|
|
|
static int tilefile_tile_W=16;
|
|
static int tilefile_tile_H=16;
|
|
|
|
// Debian uses a separate PIXMAPDIR
|
|
#ifndef PIXMAPDIR
|
|
# ifdef HACKDIR
|
|
# define PIXMAPDIR HACKDIR
|
|
# else
|
|
# define PIXMAPDIR "."
|
|
# endif
|
|
#endif
|
|
|
|
NetHackQtGlyphs::NetHackQtGlyphs()
|
|
{
|
|
const char* tile_file = PIXMAPDIR "/nhtiles.bmp";
|
|
|
|
if (iflags.wc_tile_file)
|
|
tile_file = iflags.wc_tile_file;
|
|
|
|
if (!img.load(tile_file)) {
|
|
tile_file = PIXMAPDIR "/x11tiles";
|
|
if (!img.load(tile_file)) {
|
|
QString msg;
|
|
|
|
msg.sprintf("Cannot load 'nhtiles.bmp' or 'x11tiles'.");
|
|
QMessageBox::warning(0, "IO Error", msg);
|
|
NetHackQtBind::qt_exit_nhwindows((const char *) 0);
|
|
nh_terminate(EXIT_FAILURE);
|
|
} else {
|
|
tiles_per_row = TILES_PER_ROW;
|
|
if (img.width() % tiles_per_row) {
|
|
impossible(
|
|
"Tile file \"%s\" has %d columns, not multiple of row count (%d)",
|
|
tile_file, img.width(), tiles_per_row);
|
|
}
|
|
}
|
|
} else {
|
|
tiles_per_row = 40;
|
|
}
|
|
|
|
if (iflags.wc_tile_width)
|
|
tilefile_tile_W = iflags.wc_tile_width;
|
|
else
|
|
tilefile_tile_W = img.width() / tiles_per_row;
|
|
if (iflags.wc_tile_height)
|
|
tilefile_tile_H = iflags.wc_tile_height;
|
|
else
|
|
tilefile_tile_H = tilefile_tile_W;
|
|
|
|
setSize(tilefile_tile_W, tilefile_tile_H);
|
|
}
|
|
|
|
void NetHackQtGlyphs::drawGlyph(QPainter& painter, int glyph, int x, int y)
|
|
{
|
|
int tile = glyph2tile[glyph];
|
|
int px = (tile % tiles_per_row) * width();
|
|
int py = tile / tiles_per_row * height();
|
|
|
|
painter.drawPixmap(x, y, pm, px, py, width(), height());
|
|
}
|
|
|
|
void NetHackQtGlyphs::drawCell(QPainter& painter, int glyph,
|
|
int cellx, int celly)
|
|
{
|
|
drawGlyph(painter, glyph, cellx * width(), celly * height());
|
|
}
|
|
|
|
QPixmap NetHackQtGlyphs::glyph(int glyph)
|
|
{
|
|
int tile = glyph2tile[glyph];
|
|
int px = (tile % tiles_per_row) * tilefile_tile_W;
|
|
int py = tile / tiles_per_row * tilefile_tile_H;
|
|
|
|
return QPixmap::fromImage(img.copy(px, py,
|
|
tilefile_tile_W, tilefile_tile_H));
|
|
}
|
|
|
|
void NetHackQtGlyphs::setSize(int w, int h)
|
|
{
|
|
if (size == QSize(w, h))
|
|
return;
|
|
size = QSize(w, h);
|
|
if (!w || !h)
|
|
return; // Still not decided
|
|
|
|
if (size == pm1.size()) { // not zoomed
|
|
pm = pm1;
|
|
return;
|
|
}
|
|
if (size == pm2.size()) { // zoomed
|
|
pm = pm2;
|
|
return;
|
|
}
|
|
|
|
bool was1 = (size == pm1.size());
|
|
if (w == tilefile_tile_W && h == tilefile_tile_H) {
|
|
pm.convertFromImage(img);
|
|
} else {
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
QImage scaled = img.scaled(
|
|
w * img.width() / tilefile_tile_W,
|
|
h * img.height() / tilefile_tile_H,
|
|
Qt::IgnoreAspectRatio,
|
|
Qt::FastTransformation
|
|
);
|
|
pm.convertFromImage(scaled, Qt::ThresholdDither | Qt::PreferDither);
|
|
QApplication::restoreOverrideCursor();
|
|
}
|
|
(was1 ? pm2 : pm1) = pm;
|
|
}
|
|
|
|
} // namespace nethack_qt_
|