diff options
Diffstat (limited to 'src/render')
| -rw-r--r-- | src/render/textures.cc | 136 | ||||
| -rw-r--r-- | src/render/textures.h | 54 | 
2 files changed, 190 insertions, 0 deletions
diff --git a/src/render/textures.cc b/src/render/textures.cc new file mode 100644 index 0000000..6880d87 --- /dev/null +++ b/src/render/textures.cc @@ -0,0 +1,136 @@ +/* +   render/textures.cc +   This file is part of the Osirion project and is distributed under  +   the terms of the GNU General Public License version 2  +*/ + + + +#include "render/gl.h" +#include "render/textures.h" +#include "render/tga.h" + +#include "sys/sys.h" +#include "core/application.h" + +namespace render { + +std::map<std::string, size_t> Textures::registry; +size_t Textures::index = 0; +GLuint Textures::textures[MAXTEXTURES]; + +void Textures::init() +{ +	clear(); +	con_print << "Loading textures..." << std::endl; + +	// "no texture" bitmap +	load("textures/common/notex"); + +	// console characters +	if (load("bitmaps/conchars") == 0) { +		con_error << "Essential file bitmaps/conchars missing" << std::endl; +		core::application()->shutdown(); +	} + +	// loading screen background +	load("bitmaps/loader"); + +	// crosshairs +	load("bitmaps/crosshair"); +	 +	// light flares +	load("bitmaps/fx/flare00"); +	load("bitmaps/fx/flare01"); +} + +void Textures::shutdown() +{ +	clear(); +} + +void Textures::clear() +{ +	if (index) +		glDeleteTextures(index, textures); + +	registry.clear(); +	memset(textures,0, sizeof(textures)); +	index = 0; +} + +size_t Textures::load(std::string name) +{ +	// check if it is already loaded +	iterator it = registry.find(name); +	if (it != registry.end()) +		return (*it).second; + +	// try the tga version  +	std::string filename(name); +	filename.append(".tga"); +	Image *image = TGA::load(filename.c_str()); +	if (!image) { +		// add to the registry with id 0 (texture not found) +		registry[name] = 0; +		return 0; +	} + +	if (index == MAXTEXTURES) { +		con_error << "Texture limit " << MAXTEXTURES << " exceeded!" << std::endl; +		delete image; +		registry[name] = 0; +		return 0; +	} + +	size_t id = index; + +	glGenTextures(1, &textures[id]); +	glBindTexture(GL_TEXTURE_2D, textures[id]); + +	int texture_type; +	if (image->channels() == 4) +		texture_type = GL_RGBA; +	else +		texture_type = GL_RGB; + +	gluBuild2DMipmaps(GL_TEXTURE_2D, image->channels(), +		image->width(), image->height(), texture_type, GL_UNSIGNED_BYTE, image->data()); + +	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); +	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);	 + +	// add to the registry +	registry[name] = id; +	index++; + +	// delete image data +	delete image; + +	return id; +} + +size_t Textures::bind(std::string name) +{ +	size_t id = 0; +	iterator it = registry.find(name); +	if (it != registry.end()) +		id = (*it).second; +	else +		id = load(name); + +	glBindTexture(GL_TEXTURE_2D, textures[id]); +	return id; +} + +size_t Textures::bind(size_t texture) +{ +	size_t id = texture; +	if (texture >= index) +		id = 0; + +	glBindTexture(GL_TEXTURE_2D, textures[id]); +	return id; +} + +} diff --git a/src/render/textures.h b/src/render/textures.h new file mode 100644 index 0000000..ad38451 --- /dev/null +++ b/src/render/textures.h @@ -0,0 +1,54 @@ +/* +   render/textures.h +   This file is part of the Osirion project and is distributed under  +   the terms of the GNU General Public License version 2  +*/ + +#ifndef __INCLUDED_RENDER_TEXTURES_H__ +#define __INCLUDED_RENDER_TEXTURES_H__ + +#include <string> +#include <map> + +#include "render/gl.h" + +namespace render { + +const size_t MAXTEXTURES = 256; + +/// Texture managment  +class Textures +{ +public: +	/// Initialize the textures registry +	static void init(); + +	/// Shutdown the textures registry +	static void shutdown(); + +	/// Load a texture +	/** Returns 0 on failure, and the texture index on success  +	 */ +	static size_t load(std::string name); + +	/// bind a texture for OpenGL usage +	/** Returns 0 on failure, and the texture index on success  +	 */ +	static size_t bind(std::string name); + +	/// bind a texture for OpenGL usage +	static size_t bind(size_t texture); + +private: +	static void clear(); + +	typedef std::map<std::string, size_t>::iterator iterator; + +	static std::map<std::string, size_t> registry; +	static size_t index; +	static GLuint textures[MAXTEXTURES]; +}; + +} + +#endif // __INCLUDED_RENDER_TEXTURES_H__  | 
