📄 shapepalette.cpp
字号:
shapeCount = (int)shapeValueVector.size() + 1; // clean up temp. shape objects // FIXME: do we need to free the vector's elements? shapeValueVector.clear(); // add some special, "internal" shapes shapes[shapeCount] = new GLTorch(textureGroup[14], textures[9].id, 1, 1, 2, strdup("SPELL_FIREBALL"), 0, strtoul("6070ffff", NULL, 16), dl, shapeCount, torchback, Constants::SOUTH); // Hack: use SOUTH for a spell shapes[shapeCount]->setSkipSide(false); shapes[shapeCount]->setStencil(false); shapes[shapeCount]->setLightBlocking(false); string nameStr = shapes[shapeCount]->getName(); shapeMap[nameStr] = shapes[shapeCount]; shapeCount++; // FIXME: do something with these... formationTexIndex = texture_count; strcpy(textures[texture_count++].filename, "formation1.bmp"); strcpy(textures[texture_count++].filename, "formation2.bmp"); strcpy(textures[texture_count++].filename, "formation3.bmp"); strcpy(textures[texture_count++].filename, "formation4.bmp"); strcpy(textures[texture_count++].filename, "formation5.bmp"); strcpy(textures[texture_count++].filename, "formation6.bmp"); // load the status modifier icons for(int i = 0; i < Constants::STATE_MOD_COUNT; i++) { sprintf(path, "/icons/i%d.bmp", i); GLuint icon = loadGLTextures(path); cerr << "Loading stat mod icon: " << path << " found it? " << (icon ? "yes" : "no") << endl; if(icon) statModIcons[i] = icon; } // set up the cursor setupAlphaBlendedBMP("/cursor.bmp", &cursor, &cursorImage); setupAlphaBlendedBMP("/crosshair.bmp", &crosshair, &crosshairImage); setupAlphaBlendedBMP("/paperdoll.bmp", &paperDoll, &paperDollImage); // set up the logo setupAlphaBlendedBMP("/logo.bmp", &logo, &logoImage); logo_texture = loadGLTextures("/logo.bmp"); // set up the scourge setupAlphaBlendedBMP("/scourge.bmp", &scourge, &scourgeImage); if(!instance) instance = this;}ShapePalette::~ShapePalette(){ // for(int i =0; i < (int)creature_models.size(); i++){ // delete creature_models[i]; // }}char *ShapePalette::getRandomDescription(int descriptionGroup) { if(descriptionGroup >= 0 && descriptionGroup < (int)descriptions.size()) { vector<string> *list = descriptions[descriptionGroup]; int n = (int)((float)list->size() * rand()/RAND_MAX); return(char*)((*list)[n].c_str()); } return NULL;}t3DModel * ShapePalette::LoadMd2Model(char *file_name){ t3DModel *t3d = new t3DModel; g_LoadMd2.ImportMD2(t3d, file_name); return t3d; } GLShape *ShapePalette::getCreatureShape(char *model_name, char *skin_name, float scale, int w, int d, int h) { // find the model string model = model_name; if(creature_models.find(model) == creature_models.end()){ cerr << "Can't find md2 model named " << model << endl; exit(1); } Md2ModelInfo *model_info = creature_models[model]; // find or load the skin string skin = skin_name; GLuint skin_texture; char path[300]; if(creature_skins.find(skin) == creature_skins.end()){ sprintf(path, "%s%s", rootDir, skin_name); cerr << "&&&&&&&&&& Loading texture: " << path << endl; CreateTexture(&skin_texture, path, 0); cerr << "&&&&&&&&&& Loaded texture: " << skin_texture << endl; creature_skins[skin] = skin_texture; } else { skin_texture = creature_skins[skin]; } // increment its ref. count if(loaded_skins.find(skin_texture) == loaded_skins.end()) { loaded_skins[skin_texture] = 1; } else { loaded_skins[skin_texture] = loaded_skins[skin_texture] + 1; } // cerr << "&&&&&&&&&& Texture ref count at load for id: " << skin_texture << // " count: " << loaded_skins[skin_texture] << endl; // cerr << "Creating creature shape with model: " << model << " and skin: " << skin << endl; // create the shape. // FIXME: shapeindex is always FIGHTER. Does it matter? MD2Shape *shape; if(scale == 0.0f) { shape = new MD2Shape(model_info->model, skin_texture, model_info->scale, textureGroup[14], model_info->width, model_info->depth, model_info->height, model_info->name, -1, 0xf0f0ffff, 0); //Constants::FIGHTER); } else { shape = new MD2Shape(model_info->model, skin_texture, scale, textureGroup[14], w, d, h, model_info->name, -1, 0xf0f0ffff, 0); //Constants::FIGHTER); } shape->setSkinName(skin_name); return shape;}void ShapePalette::decrementSkinRefCount(char *skin_name) { string skin = skin_name; GLuint skin_texture; if(creature_skins.find(skin) == creature_skins.end()){ cerr << "&&&&&&&&&& WARNING: could not find skin: " << skin_name << endl; return; } else { skin_texture = creature_skins[skin]; } if(loaded_skins.find(skin_texture) == loaded_skins.end()) { cerr << "&&&&&&&&&& WARNING: could not find skin id=" << skin_texture << endl; return; } loaded_skins[skin_texture] = loaded_skins[skin_texture] - 1; // cerr << "&&&&&&&&&& Texture ref count at load for id: " << skin_texture << // " count: " << loaded_skins[skin_texture] << endl; // unload texture if no more references if(loaded_skins[skin_texture] == 0) { cerr << "&&&&&&&&&& Deleting texture: " << skin_texture << endl; loaded_skins.erase(skin_texture); creature_skins.erase(skin); glDeleteTextures(1, &skin_texture); }}// the next two methods are slow, only use during initializationGLuint ShapePalette::findTextureByName(const char *filename) { for(int i = 0; i < texture_count; i++) { if(!strcmp(textures[i].filename, filename)) return textures[i].id; } return 0;}GLShape *ShapePalette::findShapeByName(const char *name) { if(!name || !strlen(name)) return NULL; string s = name; if(shapeMap.find(s) == shapeMap.end()) { cerr << "&&& warning: could not find shape by name " << s << endl; return NULL; } return shapeMap[s];}// defaults to SWORD for unknown shapesint ShapePalette::findShapeIndexByName(const char *name) { string s; if(!name || !strlen(name)) s = "SWORD"; else s = name; if(shapeMap.find(s) == shapeMap.end()) { cerr << "&&& warning: could not find shape INDEX by name " << s << endl; return 0; } return shapeMap[s]->getShapePalIndex();}/* function to load in bitmap as a GL texture */GLuint ShapePalette::loadGLTextures(char *filename) { char fn[300]; strcpy(fn, rootDir); strcat(fn, filename); GLuint texture[1]; /* Create storage space for the texture */ SDL_Surface *TextureImage[1]; /* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */ fprintf(stderr, "Loading texture: %s\n", fn); if( ( TextureImage[0] = SDL_LoadBMP( fn ) ) ) { fprintf(stderr, "\tFound it. width=%d height=%d\n", TextureImage[0]->w, TextureImage[0]->h); /* Create The Texture */ glGenTextures( 1, &texture[0] ); /* Typical Texture Generation Using Data From The Bitmap */ glBindTexture( GL_TEXTURE_2D, texture[0] ); /* Generate The Texture */ // glTexImage2D( GL_TEXTURE_2D, 0, 3, // TextureImage[0]->w, TextureImage[0]->h, 0, GL_BGR, // GL_UNSIGNED_BYTE, TextureImage[0]->pixels ); /* Linear Filtering */ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->w, TextureImage[0]->h, GL_BGR, GL_UNSIGNED_BYTE, TextureImage[0]->pixels); } else { texture[0] = 0; fprintf(stderr, "\tNot found.\n"); } fprintf(stderr, "\tStored texture at: %u\n", texture[0]); /* Free up any memory we may have used */ if( TextureImage[0] ) SDL_FreeSurface( TextureImage[0] ); return texture[0];}void ShapePalette::swap(unsigned char & a, unsigned char & b) { unsigned char temp; temp = a; a = b; b = temp; return;}void ShapePalette::setupAlphaBlendedBMP(char *filename, SDL_Surface **surface, GLubyte **image, int red, int green, int blue) { cerr << "file: " << filename << " red=" << red << " green=" << green << " blue=" << blue << endl; *image = NULL; char fn[300]; fprintf(stderr, "setupAlphaBlendedBMP, rootDir=%s\n", rootDir); strcpy(fn, rootDir); strcat(fn, filename); if(((*surface) = SDL_LoadBMP( fn ))) { // Rearrange the pixelData int width = (*surface) -> w; int height = (*surface) -> h; fprintf(stderr, "*** file=%s w=%d h=%d bpp=%d byte/pix=%d scanline=%d\n", fn, width, height, (*surface)->format->BitsPerPixel, (*surface)->format->BytesPerPixel, (*surface)->pitch); unsigned char * data = (unsigned char *) ((*surface) -> pixels); // the pixel data (*image) = (unsigned char*)malloc(width * height * 4); int count = 0; int c = 0; unsigned char r,g,b; // the following lines extract R,G and B values from any bitmap for(int i = 0; i < width * height; ++i) { if(i > 0 && i % width == 0) c += ( (*surface)->pitch - (width * (*surface)->format->BytesPerPixel) ); r = data[c++]; g = data[c++]; b = data[c++]; (*image)[count++] = r; (*image)[count++] = g; (*image)[count++] = b; //(*image)[count++] = (GLubyte)( (float)(b + g + r) / 3.0f ); //(*image)[count++] = (GLubyte)( (b + g + r == 0 ? 0x00 : 0xff) ); (*image)[count++] = (GLubyte)( ((int)r == blue && (int)g == green && (int)b == red ? 0x00 : 0xff) ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -