Esempio n. 1
0
Font::Font(Subsystem& subsystem, const std::string& filename, ZipReader *zip)
    throw (KeyValueException, FontException)
    : Properties(filename + ".font", zip), subsystem(subsystem)
{
    try {
        PNG png(filename + ".png", zip);
        /* setup font operations */
        FontOperations fo;
        if (zip) {
            fo.handle = zip;
            fo.open = &fo_zip_open;
            fo.read = &fo_zip_read;
            fo.close = &fo_zip_close;
        } else {
            fo.handle = 0;
            fo.open = &fo_file_open;
            fo.read = &fo_file_read;
            fo.close = &fo_file_close;
        }

        std::string font_description = filename + ".fds";
        fo.open(fo, font_description);

        char header[4];
        fo.read(fo, header, sizeof(header));
        if (memcmp(header, "FNT1", 4)) {
            throw FontException("wrong font file " + font_description);
        }
        fo.read(fo, &font_height, sizeof font_height);
        font_height = ntohl(font_height) * 2;
        spacing = atoi(get_value("spacing").c_str());

        font_char_t font;
        for (int i = 0; i < NumOfChars; i++) {
            fo.read(fo, &font, sizeof font);
            font.origin_x = ntohl(font.origin_x);
            font.origin_y = ntohl(font.origin_y);
            font.width = ntohl(font.width);
            font.height = ntohl(font.height);

            TileGraphic *tg = subsystem.create_tilegraphic(font.width, font.height);
            tg->punch_out_tile(png, font.origin_x, font.origin_y, false);
            tiles[i] = new Tile(tg, false, Tile::TileTypeNonblocking, 0, false, 0.0f);
            fw[i] = font.width;
            fh[i] = font.height;
        }
    } catch (const Exception& e) {
        throw FontException(e.what());
    }

}
Esempio n. 2
0
void Animation::create_tile(const std::string& filename, ZipReader *zip) throw (Exception) {
    try {
        int tile_type_val = atoi(get_value("tile_type").c_str());
        Tile::TileType tile_type = static_cast<Tile::TileType>(tile_type_val);

        bool background = (atoi(get_value("background").c_str()) == 0 ? false : true);
        bool one_shot = (atoi(get_value("one_shot").c_str()) != 0 ? true : false);

        PNG png(filename, zip);

        const int& png_width = png.get_width();
        const int& png_height = png.get_height();

        int tiley = 0;
        int tilex = 0;

        /* create all pictures consecutively for this tile */
        TileGraphic *tg = subsystem.create_tilegraphic(tile_width, tile_height);
        while (tiley < png_height) {
            /* store current picture in the graphics layer */
            tg->punch_out_tile(png, tilex, tiley, false);

            /* advance to next tile */
            tilex += tile_width;
            if (tilex >= png_width) {
                tilex = 0;
                tiley += tile_height;
            }
        }

        /* create tile with its pictures */
        tile = new Tile(tg, background, tile_type, animation_speed, one_shot, 0.0f);
    } catch (const Exception&) {
        cleanup();
        throw;
    }
}
Esempio n. 3
0
void Tileset::create_tile(const std::string& filename, ZipReader *zip) throw (Exception) {
    try {
        PNG png(filename, zip);

        const int& png_width = png.get_width();
        const int& png_height = png.get_height();

        int tileno = 0;
        int tiley = 0;
        int tilex = 0;
        char kvb[128];
        while (true) {
            /* get tile properties */
            sprintf(kvb, "frames%d", tileno);
            int frames = atoi(get_value(kvb).c_str());
            if (!frames) frames = 1;

            sprintf(kvb, "tiletype%d", tileno);
            int tile_type_val = atoi(get_value(kvb).c_str());
            Tile::TileType tile_type = static_cast<Tile::TileType>(tile_type_val);

            sprintf(kvb, "background%d", tileno);
            bool background = true;
            if (get_value(kvb).length()) {
                background = (atoi(get_value(kvb).c_str()) == 0 ? false : true);
            }

            sprintf(kvb, "animation_speed%d", tileno);
            int animation_speed = atoi(get_value(kvb).c_str());
            if (!animation_speed) {
                animation_speed = 45;
            }

            sprintf(kvb, "friction%d", tileno);
            double friction = atof(get_value(kvb).c_str());
            if (friction > -Epsilon && friction < Epsilon) {
                friction = 0.05f;
            }

            sprintf(kvb, "light_blocking%d", tileno);
            bool light_blocking = false;
            if (get_value(kvb).length()) {
                light_blocking = (atoi(get_value(kvb).c_str()) != 0 ? true : false);
            }

            /* create all pictures consecutively for this tile */
            TileGraphic *tg = subsystem.create_tilegraphic(tile_width, tile_height);
            while (frames && tiley < png_height) {
                /* store current picture in the graphics layer */
                tg->punch_out_tile(png, tilex, tiley, false);

                /* advance to next tile */
                frames--;
                tilex += tile_width;
                if (tilex >= png_width) {
                    tilex = 0;
                    tiley += tile_height;
                }
            }

            /* create tile with its pictures */
            Tile *tile = new Tile(tg, background, tile_type, animation_speed, false, friction);
            tile->set_light_blocking(light_blocking);
            tiles.push_back(tile);
            sz = static_cast<int>(tiles.size());

            /* test if all pics in tileset are grabbed */
            if (tiley >= png_height) {
                break;
            }

            /* increment tileno */
            tileno++;
        }
    } catch (const Exception&) {
        cleanup();
        throw;
    }
}