rendered paste body#include "AlmTiler.h"#include <sstream>#include <iostream>#include <iomanip>//----------------------------------------------------------------------// singleton me//----------------------------------------------------------------------AlmTiler* AlmTiler::instance = 0;AlmTiler::AlmTiler ( const char *filename = "an_road.alm" ){ alm_file.open(filename, std::ios::binary); char buffer; while( !alm_file.eof() ) { alm_file.read(&buffer, 1); Alm.push_back(buffer); } alm_file.close(); tiles.resize(this->getWidth() * (this->getHeight())); fillTiles(); loadSurfaces();}AlmTiler::~AlmTiler ( ){}AlmTiler* AlmTiler::getInstance(){ // TODO: FUCK! SetMap or something, otherwise it looks too hardcoded if (instance == 0) { instance = new AlmTiler(); } return instance;}char AlmTiler::readCharFromFile(){ char buffer; alm_file.read(&buffer, 1); return buffer;}unsigned short AlmTiler::getWidth(){ return getParam(0x28);}unsigned short AlmTiler::getHeight(){ return getParam(0x2C);}unsigned short AlmTiler::getParam(unsigned int address){ return Alm.at(address);}void AlmTiler::fillTiles(){ unsigned short lo = 0; unsigned short hi = 0; for( int y = 0; y < this->getHeight(); y++ ) { for( int x = 0; x < this->getWidth(); x++ ) { lo = getParam( 0x02D0 + (y * (this->getWidth()) + x) * 2); hi = getParam( 0x02D0 + (y * (this->getWidth()) + x) * 2 + 1); tiles[y*(this->getWidth())+ x] = lo + (hi << 8); } }}//----------------------------------------------------------------------// helper function for testing purposes only//----------------------------------------------------------------------GLuint AlmTiler::getTexture(unsigned short a, unsigned short b, unsigned short c){ // TODO: remove this finction when done with BUBEN and BALALAYKA GLuint _ret = textures[a][b][c]; return _ret;}//----------------------------------------------------------------------// helper function to get gltextures//----------------------------------------------------------------------void AlmTiler::setTextureFromFile(GLuint* texture, std::stringstream &filename){ SDL_Surface *surface; GLenum textureFormat = GL_BGRA; GLint colors; if ( ( surface = IMG_Load(filename.str().c_str()) ) ) { // TODO: add check for sizes of image to be a power of 2 colors = surface->format->BytesPerPixel; if (colors == 4) { if (surface->format->Rmask == 0x000000ff) textureFormat = GL_RGBA; else textureFormat = GL_BGRA; } else if (colors == 3) { if (surface->format->Rmask == 0x000000ff) textureFormat = GL_RGB; else textureFormat = GL_BGR; } else { // TODO: add complaining subsystem (logger) std::cout << "Dude, errors! Image " << filename.str() << " is not in trueColor. Colors = " << colors << std::endl; } glGenTextures(1, texture); glBindTexture(GL_TEXTURE_2D, *texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, colors, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels); } else { std::cout << "Dude! That bitch SDL failed to load that fucking " << filename.str() << ", it complains: " << SDL_GetError() << std::endl; SDL_Quit(); } if (surface) SDL_FreeSurface(surface);}void AlmTiler::loadSurfaces(){ for (int i = 0; i < 5; i++) for (int j = 0; j < 14; j++) for (int k = 0; k < 14; k++) textures[i][j][k] = NULL; GLuint tmp_texture = NULL; // , , , for(int i = 1; i < 3; i++) for(int j = 0; j < 14; j++) for( int k = 0; k < 14; k++) { std::stringstream stringo; stringo << "tiles/tile" << i << "-" << std::setw(2) << std::setfill('0') << j << "-" << std::setw(2) << std::setfill('0') << k << ".png"; setTextureFromFile(&tmp_texture, stringo); textures[i][j][k] = tmp_texture; tmp_texture = NULL; } // for(int j = 0; j < 13; j++) for( int k = 0; k < 8; k++) { std::stringstream stringo; stringo << "tiles/tile" << 3 << "-" << std::setw(2) << std::setfill('0') << j << "-" << std::setw(2) << std::setfill('0') << k << ".png"; setTextureFromFile(&tmp_texture, stringo); textures[3][j][k] = tmp_texture; tmp_texture = NULL; } // -. ? %) for(int j = 0; j < 4; j++) for( int k = 0; k < 14; k++) { std::stringstream stringo; stringo << "tiles/tile" << 4 << "-" << std::setw(2) << std::setfill('0') << j << "-" << std::setw(2) << std::setfill('0') << k << ".png"; setTextureFromFile(&tmp_texture, stringo); textures[4][j][k] = tmp_texture; tmp_texture = NULL; }}GLuint AlmTiler::getTileSurface(unsigned short x, unsigned short y){ // TODO: add idiot-check unsigned short a, b, c, xx; xx = tiles[y*(this->getWidth())+ x]; xx &= 0x3FF; a = ((xx & 0x300) >> 8) + 1; b = (xx & 0x0F0) >> 4; c = xx & 0x00F; return textures[a][b][c];}