Zaslal: Weny Sky
Jazyk: C++
Vloženo: 2.2.2008, 11:07
Stáhnout jako soubor
#include "BAMWE.h" #include "BAMWE_log.h" //----------------------------------------------------------------------------- // Name: BAMWE_sprite::BAMWE_sprite() // Desc: implicitni konstruktor //----------------------------------------------------------------------------- BAMWE_sprite::BAMWE_sprite() { texture = 0; size_x = 0; size_y = 0; tile_x = 0; tile_y = 0; is_color_scheme = false; num_of_color_scheme = 0; color_scheme = NULL; } //----------------------------------------------------------------------------- // Name: BAMWE_sprite::~BAMWE_sprite() // Desc: implicitni destruktor //----------------------------------------------------------------------------- BAMWE_sprite::~BAMWE_sprite() { if(color_scheme != NULL) delete [] color_scheme; } //----------------------------------------------------------------------------- // Name: BAMWE_sprite::create(unsigned int tex, unsigned int x, unsigned short y, unsigned short tx, unsigned short ty, unsigned short colors, const BAMWE* engine) // Desc: vytvori a nastavi zakladni parametry spritu //----------------------------------------------------------------------------- bool BAMWE_sprite::create(unsigned int tex, unsigned int x, unsigned short y, unsigned short tx, unsigned short ty, unsigned short colors, const BAMWE* engine) { texture = tex; size_x = x; size_y = y; tile_x = tx == 0 ? 1 : tx; tile_y = ty == 0 ? 1 : ty; num_of_color_scheme = colors; is_color_scheme = colors > 0; try { if(colors > 0) color_scheme = new unsigned int[colors]; else color_scheme = NULL; } catch(...) { return false; } if(!vertices.create(tile_x*tile_y*4, colors == 0)) return false; for(int u = 0; u < tile_x; u++) { for(int v = 0; v < tile_y; v++) { vertices.getVertices()[(v*tile_x + u)*4] = BAMWE_vertex(.0f, .0f, 0.0f); vertices.getVertices()[(v*tile_x + u)*4+1] = BAMWE_vertex((float)x, .0f, 0.0f); vertices.getVertices()[(v*tile_x + u)*4+2] = BAMWE_vertex((float)x, (float)y, 0.0f); vertices.getVertices()[(v*tile_x + u)*4+3] = BAMWE_vertex(.0f, (float)y, 0.0f); vertices.getTextureCords()[(v*tile_x + u)*4] = BAMWE_texture_cord((u*x) / (float)engine->getTextureWidth(texture), (v*y) / (float)engine->getTextureHeight(texture)); vertices.getTextureCords()[(v*tile_x + u)*4+1] = BAMWE_texture_cord(((u+1)*x) / (float)engine->getTextureWidth(texture), (v*y) / (float)engine->getTextureHeight(texture)); vertices.getTextureCords()[(v*tile_x + u)*4+2] = BAMWE_texture_cord(((u+1)*x) / (float)engine->getTextureWidth(texture), ((v+1)*y) / (float)engine->getTextureHeight(texture)); vertices.getTextureCords()[(v*tile_x + u)*4+3] = BAMWE_texture_cord((u*x) / (float)engine->getTextureWidth(texture), ((v+1)*y) / (float)engine->getTextureHeight(texture)); } } return true; } //----------------------------------------------------------------------------- // Name: BAMWE_sprite::setColorScheme(unsigned int scheme, unsigned int color) // Desc: nastavi barvu urcite skupine vertexu //----------------------------------------------------------------------------- void BAMWE_sprite::setColorScheme(unsigned int scheme, unsigned int color) { for(int u = 0; u < tile_x; u++) { for(int v = 0; v < tile_y; v++) { // prekopirovani vrcholou vertices.getVertices()[(tile_x*tile_y*scheme + v*tile_x + u)*4] = vertices.getVertices()[(v*tile_x + u)*4]; vertices.getVertices()[(tile_x*tile_y*scheme + v*tile_x + u)*4+1] = vertices.getVertices()[(v*tile_x + u)*4+1]; vertices.getVertices()[(tile_x*tile_y*scheme + v*tile_x + u)*4+2] = vertices.getVertices()[(v*tile_x + u)*4+2]; vertices.getVertices()[(tile_x*tile_y*scheme + v*tile_x + u)*4+3] = vertices.getVertices()[(v*tile_x + u)*4+3]; // prekopirovani mapovacich souradnic vertices.getTextureCords()[(tile_x*tile_y*scheme + v*tile_x + u)*4] = vertices.getTextureCords()[(v*tile_x + u)*4]; vertices.getTextureCords()[(tile_x*tile_y*scheme + v*tile_x + u)*4+1] = vertices.getTextureCords()[(v*tile_x + u)*4+1]; vertices.getTextureCords()[(tile_x*tile_y*scheme + v*tile_x + u)*4+2] = vertices.getTextureCords()[(v*tile_x + u)*4+2]; vertices.getTextureCords()[(tile_x*tile_y*scheme + v*tile_x + u)*4+3] = vertices.getTextureCords()[(v*tile_x + u)*4+3]; // namapovani barev na vrcholy vertices.getColors()[(tile_x*tile_y*scheme + v*tile_x + u)*4] = vertices.getColors()[(tile_x*tile_y*scheme + v*tile_x + u)*4+1] = vertices.getColors()[(tile_x*tile_y*scheme + v*tile_x + u)*4+2] = vertices.getColors()[(tile_x*tile_y*scheme + v*tile_x + u)*4+3] = BAMWE_texture_color(((color >> 8) & 0xFF) / 256.0f , ((color >> 16) & 0xFF) / 256.0f, (color & 0xFF) / 256.0f, ((color >> 24) & 0xFF) / 256.0f); } } } //----------------------------------------------------------------------------- // Name: BAMWE_sprite::createVBO() // Desc: prekopiruje vertexy primo do pameti graficke karty //----------------------------------------------------------------------------- void BAMWE_sprite::createVBO() { vertices.createVBO(); } //----------------------------------------------------------------------------- // Name: BAMWE_sprite::render(float x, float y, float z, unsigned short tx, unsigned short ty, unsigned short scheme, const BAMWE* engine) // Desc: vykresli sprite //----------------------------------------------------------------------------- void BAMWE_sprite::render(float x, float y, float z, unsigned short tx, unsigned short ty, unsigned short scheme, const BAMWE* engine) { if(scheme > num_of_color_scheme || tx > tile_x || ty > tile_y) { #ifdef _DEBUG log(__FILE__, __LINE__, "Bad render parameters: color_scheme(%d)[max:%d] tile_x(%d)[max:%d] tile_y(%d)[max:%d]", scheme, num_of_color_scheme, tx, tile_x, ty, tile_y); #endif return; } glPushMatrix(); log_error(); glLoadIdentity(); log_error(); glTranslatef((float)x, (float)y, (float)z); log_error(); glEnable(GL_TEXTURE_2D); log_error(); glBindTexture( GL_TEXTURE_2D, engine->getTexture(texture) ); log_error(); vertices.bindBuffer(); log_error(); if(num_of_color_scheme > 0) glEnableClientState(GL_COLOR_ARRAY); log_error(); glDrawArrays(GL_QUADS, (tile_x*tile_y*scheme + tile_x*ty + tx)*4, 4); log_error(); if(num_of_color_scheme > 0) glDisableClientState(GL_COLOR_ARRAY); log_error(); glPopMatrix(); log_error(); }
© 2006 Michal Tuláček, Syntax Highlight - GeSHi (thx bref)
