R
Ich lerne Spieleprogrammierung über das gleichnamige Buch von Lennart Steinke.
Nun bin ich an dem Punkt, wo ich mit Mappy Karten mit mehreren Ebenene/Layern erstelle und mit dem mitgelieferten Programm von .fmp zu .map konvertiere.
Jedoch habe ich das Problem, dass, sobald ich eine Karte mit mehreren Ebenen konvertiere und dann einlese ins Programm, diese nicht korrekt angezeigt wird sondern zerrissen und eigentlich auch nur ein paar (2 - 5) einzelne Tiles.
Die Grafiken sind .pcx Grafiken.
Nun wollte ich fragen, ob das ein generelles Problem ist, welches bekannt ist, oder was das sein könnte, denn mein code entspricht eigenentlich dem aus dem Buch.
Achja ich benutze Windows Vista welches mir schon das einlesen von .tga Dateien mit mappy vermiest hat, weswegen ich nun pcx Dateien benutze.
Kann Vista der Grund sein, weswegen die mehr-ebenen Karten nicht korrekt gezeichnet werden?
Hier mal der Code meiner Map Klasse, auf Wunsch poste ich auch gerne den Code des Autors:
#include "map.h"
void Map::_drawTile(BITMAP *dest, int x, int y, int tile) {
masked_blit(this->_tiles, dest, tile * this->_tile_width, 0, x, y, this->_tile_width, this->_tile_height);
}
void Map::_create(int w, int h, int d) {
this->_w = w;
this->_h = h;
this->_d = d;
this->_line_size = w * d;
this->_size = w * h * d;
this->_data = new char[this->_size];
this->_dest = NULL;
this->_visible_x = 0;
this->_visible_y = 0;
}
Map::Map(int w, int h, int d) {
this->_create(w, h, d);
}
Map::Map(int w, int h, int d, char *map_str, char *tile_str) {
this->_create(w, h, d);
for (int a = 0; a < this->_size; a++) {
for (int tile = 0; tile_str[tile]; tile++) {
if (map_str[a] == tile_str[tile]) {
this->_data[a] = tile;
break;
}
}
}
}
Map::~Map() {
if (this->_data) {
delete [] this->_data;
}
}
void Map::setTileAt(int tileX, int tileY, int tileZ, int tile) {
int pos = tileX * this->_d + tileY * this->_line_size;
if (pos >= 0 && pos < this->_size) {
this->_data[pos] = (char) (tile & 0xff);
}
}
int Map::getTileAt(int tileX, int tileY, int tileZ) {
int pos = tileZ + tileX * this->_d + tileY * this->_line_size;
if (pos >= 0 && pos < this->_size) {
return this->_data[pos];
}
return 0;
}
bool Map::isWalkable(int tileX, int tileY) {
int tile = this->getTileAt(tileX, tileY);
return tile >= this->_walkable_tile;
}
void Map::setTiles(BITMAP *tiles, int tw, int th, int walkable_tile) {
this->_tiles = tiles;
this->_tile_width = tw;
this->_tile_height = th;
this->_walkable_tile = walkable_tile;
}
void Map::setTarget(BITMAP *dest) {
this->_dest = dest;
this->_visible_x = MIN(this->_w, (dest->w / this->_tile_width) + ((dest->w % this->_tile_width) == 0 ? 1 : 0));
this->_visible_y = MIN(this->_h, (dest->h / this->_tile_height) + ((dest->h % this->_tile_height) == 0 ? 1 : 0));
this->_max_x = (this->_w * this->_tile_width) - dest->w;
this->_max_y = (this->_h * this->_tile_height) - dest->h;
}
BITMAP* Map::getTarget() {
return this->_dest;
}
void Map::draw(int wx, int wy) {
int pos = 0;
wx = MID(0, wx, this->_max_x);
wy = MID(0, wy, this->_max_y);
int ox = wx % this->_tile_width;
int oy = wy % this->_tile_height;
wx /= this->_tile_width;
wy /= this->_tile_height;
for (int y = 0; y < this->_visible_y + 1; ++y) {
pos = wx * this->_d + (y + wy) * this->_line_size;
for (int x = 0; x < this->_visible_x + 1; ++x) {
for (int z = 0; z < this->_d; ++z) {
this->_drawTile(
this->_dest,
(x * this->_tile_width) - ox,
(y * this->_tile_height) - oy,
this->_data[pos]
);
++pos;
}
}
}
}
int Map::getWidth() {
return this->_w;
}
int Map::getHeight() {
return this->_h;
}
int Map::getDepth() {
return this->_d;
}
int Map::getStartX() {
return this->_start_x;
}
int Map::getStartY() {
return this->_start_y;
}
int Map::getTileWidth() {
return this->_tile_width;
}
int Map::getTileHeight() {
return this->_tile_height;
}
bool Map::load(string file) {
PACKFILE *f = pack_fopen(file.c_str(), "rp");
if (!f) {
return false;
}
int width, height, depth;
int tile;
pack_fread(&width, sizeof(int), f);
pack_fread(&height, sizeof(int), f);
pack_fread(&depth, sizeof(int), f);
this->_create(width, height, depth);
int max_tile = -1;
int max_x = 0;
int max_y = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
for (int z = 0; z < depth; z++) {
pack_fread(&tile, sizeof(int), f);
this->setTileAt(x, y, z, tile);
if (z == depth - 1 && tile > max_tile) {
max_tile = tile;
max_x = x;
max_y = y;
}
}
}
}
if (max_tile > 0) {
this->setTileAt(max_x, max_y, depth - 1, 0);
this->_start_x = max_x;
this->_start_y = max_y;
}
pack_fclose(f);
return true;
}