From 64b59757322e29c331c0a75262baec4382673e6f Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 28 Jan 2011 01:38:16 +0200 Subject: Now texture handling is fast. Also now players are saved on disk. --- src/irrlichtwrapper.cpp | 471 ++++++++++++++++++------------------------------ 1 file changed, 179 insertions(+), 292 deletions(-) (limited to 'src/irrlichtwrapper.cpp') diff --git a/src/irrlichtwrapper.cpp b/src/irrlichtwrapper.cpp index 4e1ebdd74..e5cab98c6 100644 --- a/src/irrlichtwrapper.cpp +++ b/src/irrlichtwrapper.cpp @@ -17,13 +17,15 @@ void IrrlichtWrapper::Run() */ if(m_get_texture_queue.size() > 0) { - GetRequest + GetRequest request = m_get_texture_queue.pop(); - dstream<<"got texture request with key=" - < dim(16, 16); - // Position to copy the blitted to in the base image - core::position2d pos_base(0, 0); - // Position to copy the blitted from in the blitted image - core::position2d pos_other(0, 0); - - video::IImage *baseimage = driver->createImage(original, pos_base, dim); - assert(baseimage); - - video::IImage *blittedimage = driver->createImageFromFile(porting::getDataPath(blitted_name.c_str()).c_str()); - assert(blittedimage); - - // Then copy the right part of blittedimage to baseimage - - blittedimage->copyToWithAlpha(baseimage, v2s32(0,0), - core::rect(pos_other, dim), - video::SColor(255,255,255,255), - NULL); + // Don't generate existing stuff + video::ITexture *t = m_texturecache.get(spec); + if(t != NULL) + { + dstream<<"WARNING: Existing stuff requested from " + "getTextureDirect()"<drop(); - - // Create texture from resulting image - - video::ITexture *newtexture = driver->addTexture(newname, baseimage); + video::IVideoDriver* driver = m_device->getVideoDriver(); - baseimage->drop(); + /* + An image will be built from files and then converted into a texture. + */ + video::IImage *baseimg = NULL; - return newtexture; -} + /* + Irrlicht requires a name for every texture, with which it + will be stored internally in irrlicht. + */ + std::string texture_name; -video::ITexture * make_crack(u16 progression, video::ITexture *original, - const char *newname, video::IVideoDriver* driver) -{ - if(original == NULL) - return NULL; - - // Size of the base image - core::dimension2d dim(16, 16); - // Size of the crack image - //core::dimension2d dim_crack(16, 16 * CRACK_ANIMATION_LENGTH); - // Position to copy the crack to in the base image - core::position2d pos_base(0, 0); - // Position to copy the crack from in the crack image - core::position2d pos_other(0, 16 * progression); - - video::IImage *baseimage = driver->createImage(original, pos_base, dim); - assert(baseimage); - - video::IImage *crackimage = driver->createImageFromFile(porting::getDataPath("crack.png").c_str()); - assert(crackimage); - - // Then copy the right part of crackimage to baseimage - - crackimage->copyToWithAlpha(baseimage, v2s32(0,0), - core::rect(pos_other, dim), - video::SColor(255,255,255,255), - NULL); - - crackimage->drop(); + for(u32 i=0; iaddTexture(newname, baseimage); + if(name[0] != '[') + { + // A normal texture; load it from a file + std::string path = porting::getDataPath(name.c_str()); + dstream<<"getTextureDirect(): Loading path \""<createImageFromFile(path.c_str()); - baseimage->drop(); + if(image == NULL) + { + dstream<<"WARNING: Could not load image \""<createImage(video::ECF_A8R8G8B8, image); + core::dimension2d dim = image->getDimension(); + baseimg = driver->createImage(video::ECF_A8R8G8B8, dim); + image->copyTo(baseimg); + image->drop(); + //baseimg = image; + } + // Else blit on base. + else + { + dstream<<"Blitting "< dim = image->getDimension(); + //core::dimension2d dim(16,16); + // Position to copy the blitted to in the base image + core::position2d pos_to(0,0); + // Position to copy the blitted from in the blitted image + core::position2d pos_from(0,0); + // Blit + image->copyToWithAlpha(baseimg, pos_to, + core::rect(pos_from, dim), + video::SColor(255,255,255,255), + NULL); + // Drop image + image->drop(); + } + } + else + { + // A special texture modification + dstream<<"getTextureDirect(): generating \""< dim(16, 16); + // Size of the crack image + //core::dimension2d dim_crack(16, 16 * CRACK_ANIMATION_LENGTH); + // Position to copy the crack to in the base image + core::position2d pos_base(0, 0); + // Position to copy the crack from in the crack image + core::position2d pos_other(0, 16 * progression); + + video::IImage *crackimage = driver->createImageFromFile( + porting::getDataPath("crack.png").c_str()); + crackimage->copyToWithAlpha(baseimg, v2s32(0,0), + core::rect(pos_other, dim), + video::SColor(255,255,255,255), + NULL); + crackimage->drop(); + } + else if(name.substr(0,12) == "[progressbar") + { + float value = stof(name.substr(12)); + make_progressbar(value, baseimg); + } + else + { + dstream<<"WARNING: getTextureDirect(): Invalid " + " texture: \""< dim(16, 16); - // Position to copy the grass to in the base image - core::position2d pos_base(0, 0); - // Position to copy the grass from in the grass image - core::position2d pos_other(0, 0); - - video::IImage *baseimage = driver->createImage(original, pos_base, dim); - assert(baseimage); - - video::IImage *grassimage = driver->createImageFromFile(porting::getDataPath("grass_side.png").c_str()); - assert(grassimage); - - // Then copy the right part of grassimage to baseimage - - grassimage->copyToWithAlpha(baseimage, v2s32(0,0), - core::rect(pos_other, dim), - video::SColor(255,255,255,255), - NULL); - - grassimage->drop(); + /*// DEBUG: Paint some pixels + video::SColor c(255,255,0,0); + baseimg->setPixel(1,1, c); + baseimg->setPixel(1,14, c); + baseimg->setPixel(14,1, c); + baseimg->setPixel(14,14, c);*/ // Create texture from resulting image + t = driver->addTexture(texture_name.c_str(), baseimg); + baseimg->drop(); - video::ITexture *newtexture = driver->addTexture(newname, baseimage); + dstream<<"getTextureDirect(): created texture \""<drop(); + return t; - return newtexture; } -#endif -video::ITexture * make_progressbar(float value, video::ITexture *original, - const char *newname, video::IVideoDriver* driver) +void make_progressbar(float value, video::IImage *image) { - if(original == NULL) - return NULL; - - core::position2d pos_base(0, 0); - core::dimension2d dim = original->getOriginalSize(); - - video::IImage *baseimage = driver->createImage(original, pos_base, dim); - assert(baseimage); + if(image == NULL) + return; - core::dimension2d size = baseimage->getDimension(); + core::dimension2d size = image->getDimension(); u32 barheight = 1; u32 barpad_x = 1; @@ -242,177 +297,9 @@ video::ITexture * make_progressbar(float value, video::ITexture *original, u32 x = x0 + barpos.X; for(u32 y=barpos.Y; ysetPixel(x,y, *c); - } - } - - video::ITexture *newtexture = driver->addTexture(newname, baseimage); - - baseimage->drop(); - - return newtexture; -} - -/* - Texture fetcher/maker function, called always from the main thread -*/ - -video::ITexture* IrrlichtWrapper::getTextureDirect(const std::string &spec) -{ - if(spec == "") - return NULL; - - video::IVideoDriver* driver = m_device->getVideoDriver(); - - /* - Input (spec) is something like this: - "/usr/share/minetest/stone.png[[mod:mineral0[[mod:crack3" - */ - - video::ITexture* t = NULL; - std::string modmagic = "[[mod:"; - Strfnd f(spec); - std::string path = f.next(modmagic); - t = driver->getTexture(path.c_str()); - std::string texture_name = path; - while(f.atend() == false) - { - std::string mod = f.next(modmagic); - texture_name += modmagic + mod; - dstream<<"Making texture \""<= spec.size()) - { - strcache[length] = '\0'; - // Now our string is in strcache, ending in \0 - - if(readmode == READMODE_PATH) - { - // Get initial texture (strcache is path) - assert(t == NULL); - t = driver->getTexture(strcache); - readmode = READMODE_MOD; - path = strcache; - strcache = (char*)malloc(specsize); - assert(strcache); - } - else - { - dstream<<"Parsing mod \""<setPixel(x,y, *c); } - - if(i >= spec.size()) - break; - } - - /*if(spec.mod == NULL) - { - dstream<<"IrrlichtWrapper::getTextureDirect: Loading texture " - <getTexture(spec.path.c_str()); } - - dstream<<"IrrlichtWrapper::getTextureDirect: Loading and modifying " - "texture "<getTexture(spec.path.c_str()); - video::ITexture *result = spec.mod->make(base, spec.name.c_str(), driver); - - delete spec.mod;*/ - - if(strcache) - free(strcache); - if(path) - free(path); - - return t; -#endif } -- cgit v1.2.3