bool TextureController::_SaveTempTextures() {
	bool success = true;

	for (map<string, ImageTexture*>::iterator i = _images.begin(); i != _images.end(); i++) {
		ImageTexture *image = i->second;

		// Check that this is a temporary texture and if so, save it to disk as a .png file
		if (image->tags.find("<T>") != string::npos) {
			IF_PRINT_DEBUG(VIDEO_DEBUG) << " saving temporary texture " << image->filename << endl;
			ImageMemory buffer;
			buffer.CopyFromImage(image);
			string path = GetUserDataPath(true);
			if (buffer.SaveImage(path + image->filename + ".png", true) == false) {
				success = false;
				IF_PRINT_WARNING(VIDEO_DEBUG) << "call to ImageMemory::SaveImage() failed" << endl;
			}
		}
	}
	return success;
}
Exemple #2
0
bool TexSheet::CopyRect(int32_t x, int32_t y, ImageMemory& data)
{
    TextureManager->_BindTexture(tex_id);

    data.GlTexSubImage(x, y);

    if(VideoManager->CheckGLError() == true) {
        IF_PRINT_WARNING(VIDEO_DEBUG) << "an OpenGL error occured: " << VideoManager->CreateGLErrorString() << std::endl;
        return false;
    }

    return true;
}
bool TextureController::_ReloadImagesToSheet(TexSheet* sheet) {
	// Delete images
	std::map<string, pair<ImageMemory, ImageMemory> > multi_image_info;

	bool success = true;
	for (map<string, ImageTexture*>::iterator i = _images.begin(); i != _images.end(); i++) {
		// Only operate on images which belong to the requested TexSheet
		if (i->second->texture_sheet != sheet) {
			continue;
		}

		ImageTexture* img = i->second;
		ImageMemory load_info;
		bool is_multi_image = (img->tags.find("<X", 0) != img->filename.npos);

		// Multi Images require a different reloading process
		if (is_multi_image) {
			ImageMemory image;

			if (multi_image_info.find(img->filename) == multi_image_info.end()) {
				// Load the image
				if (load_info.LoadImage(img->filename) == false) {
					IF_PRINT_WARNING(VIDEO_DEBUG) << "call to _LoadRawImage() failed" << endl;
					success = false;
					continue;
				}

				// Copy the part of the image in a buffer
				image.height = img->height;
				image.width = img->width;
				image.pixels = malloc(image.height * image.width * 4);

				if (image.pixels == NULL) {
					IF_PRINT_WARNING(VIDEO_DEBUG) << "call to malloc returned NULL" << endl;
					success = false;
					continue;
				}

				multi_image_info[img->filename] = make_pair(load_info, image);
			}
			else {
				load_info = multi_image_info[img->filename].first;
				image = multi_image_info[img->filename].second;
			}

			uint16 pos0, pos1; // Used to find the start and end positions of a sub-string
			uint32 x, y; //
			uint32 rows, cols;

			pos0 = img->tags.find("<X", 0);
			pos1 = img->tags.find('_', pos0);
			x = atoi(img->tags.substr(pos0 + 2, pos1).c_str());

			pos0 = img->tags.find("<Y", 0);
			pos1 = img->tags.find('_', pos0);
			y = atoi(img->tags.substr(pos0 + 2, pos1).c_str());

			rows = load_info.height / image.height;
			cols = load_info.width / image.width;

			for (int32 row = 0; row < image.height; row++) {
				memcpy((uint8*)image.pixels + 4 * image.width * row, (uint8*)load_info.pixels + (((x * load_info.height / rows) + row)
					* load_info.width + y * load_info.width / cols) * 4, 4 * image.width);
			}

			// Convert to grayscale if needed
			if (img->tags.find("<G>", 0) != img->filename.npos)
				image.ConvertToGrayscale();

			// Copy the image into the texture sheet
			if (sheet->CopyRect(img->x, img->y, image) == false) {
				IF_PRINT_WARNING(VIDEO_DEBUG) << "call to TexSheet::CopyRect() failed" << endl;
				success = false;
			}
		} // if (is_multi_image)

		// Reload a normal image file
		else {
			std::string fname = img->filename;
			IF_PRINT_DEBUG(VIDEO_DEBUG) << " Reloading image " << fname << endl;

			// Check if it is a temporary image, and if so retrieve it from the img/temp directory
			if (img->tags.find("<T>", 0) != img->tags.npos) {
				fname = "img/temp/" + fname + ".png";
			}

			if (load_info.LoadImage(fname) == false) {
				IF_PRINT_WARNING(VIDEO_DEBUG) << "call to _LoadRawImage() failed" << endl;
				success = false;
			}

			// Convert to grayscale if needed
			if (img->tags.find("<G>", 0) != img->filename.npos)
				load_info.ConvertToGrayscale();

			if (sheet->CopyRect(img->x, img->y, load_info) == false) {
				IF_PRINT_WARNING(VIDEO_DEBUG) << "call to TexSheet::CopyRect() failed" << endl;
				success = false;
			}

			if (load_info.pixels) {
				free(load_info.pixels);
				load_info.pixels = NULL;
			}
		}
	} // for (map<string, ImageTexture*>::iterator i = _images.begin(); i != _images.end(); i++)

	for (map<string, pair<ImageMemory, ImageMemory> >::iterator i = multi_image_info.begin(); i != multi_image_info.end(); ++i) {
		free(i->second.first.pixels);
		i->second.first.pixels = NULL;
		free(i->second.second.pixels);
		i->second.second.pixels = NULL;
	}

	// Regenerate all font textures
	for (set<TextTexture*>::iterator i = _text_images.begin(); i != _text_images.end(); i++) {
		if ((*i)->texture_sheet == sheet) {
			if ((*i)->Reload() == false) {
				IF_PRINT_WARNING(VIDEO_DEBUG) << "failed to reload a TextTexture" << endl;
				success = false;
			}
		}
	}

	return success;
} // bool TextureController::_ReloadImagesToSheet(TexSheet* sheet)
bool TextureController::_ReloadImagesToSheet(TexSheet *sheet)
{
    // Delete images
    std::map<std::string, std::pair<ImageMemory, ImageMemory> > multi_image_info;

    bool success = true;
    for(std::map<std::string, ImageTexture *>::iterator i = _images.begin(); i != _images.end(); ++i) {
        // Only operate on images which belong to the requested TexSheet
        if(i->second->texture_sheet != sheet) {
            continue;
        }

        ImageTexture *img = i->second;
        ImageMemory load_info;
        bool is_multi_image = (img->tags.find("<X", 0) != img->filename.npos);

        // Multi Images require a different reloading process
        if(is_multi_image) {
            ImageMemory image;

            if(multi_image_info.find(img->filename) == multi_image_info.end()) {
                // Load the image
                if(load_info.LoadImage(img->filename) == false) {
                    IF_PRINT_WARNING(VIDEO_DEBUG) << "call to _LoadRawImage() failed" << std::endl;
                    success = false;
                    continue;
                }

                // Copy the part of the image in a buffer
                try {
                    image.Resize(img->width, img->height, false);
                }
                catch( std::exception &e ) {
                    IF_PRINT_WARNING(VIDEO_DEBUG) << "Resize failed." << std::endl;
                    success = false;
                    continue;
                }

                multi_image_info[img->filename] = std::make_pair(load_info, image);
            } else {
                load_info = multi_image_info[img->filename].first;
                image = multi_image_info[img->filename].second;
            }

            uint16_t pos0, pos1; // Used to find the start and end positions of a sub-string
            uint32_t x, y; //
            uint32_t rows, cols;

            pos0 = img->tags.find("<X", 0);
            pos1 = img->tags.find('_', pos0);
            x = std::stoi(img->tags.substr(pos0 + 2, pos1));

            pos0 = img->tags.find("<Y", 0);
            pos1 = img->tags.find('_', pos0);
            y = std::stoi(img->tags.substr(pos0 + 2, pos1));

            rows = load_info.GetHeight() / image.GetHeight();
            cols = load_info.GetWidth() / image.GetWidth();

            image.CopyFrom(load_info,
                           load_info.GetWidth() * (x * load_info.GetHeight() / rows)
                               + load_info.GetWidth() * y / cols);

            // Convert to grayscale if needed
            if(img->tags.find("<G>", 0) != img->filename.npos)
                image.ConvertToGrayscale();

            // Copy the image into the texture sheet
            if(sheet->CopyRect(img->x, img->y, image) == false) {
                IF_PRINT_WARNING(VIDEO_DEBUG) << "call to TexSheet::CopyRect() failed" << std::endl;
                success = false;
            }
        } // if (is_multi_image)

        // Reload a normal image file
        else {
            std::string fname = img->filename;

            IF_PRINT_DEBUG(VIDEO_DEBUG) << " Reloading image " << fname << std::endl;

            if(load_info.LoadImage(fname) == false) {
                IF_PRINT_WARNING(VIDEO_DEBUG) << "call to _LoadRawImage() failed" << std::endl;
                success = false;
            }

            // Convert to grayscale if needed
            if(img->tags.find("<G>", 0) != img->filename.npos)
                load_info.ConvertToGrayscale();

            if(sheet->CopyRect(img->x, img->y, load_info) == false) {
                IF_PRINT_WARNING(VIDEO_DEBUG) << "call to TexSheet::CopyRect() failed" << std::endl;
                success = false;
            }
        }
    } // for (std::map<string, ImageTexture*>::iterator i = _images.begin(); i != _images.end(); i++)

    // Regenerate all font textures
    for(std::set<TextTexture *>::iterator i = _text_images.begin(); i != _text_images.end(); ++i) {
        if((*i)->texture_sheet == sheet) {
            if((*i)->Reload() == false) {
                IF_PRINT_WARNING(VIDEO_DEBUG) << "failed to reload a TextTexture" << std::endl;
                success = false;
            }
        }
    }

    return success;
} // bool TextureController::_ReloadImagesToSheet(TexSheet* sheet)