Esempio n. 1
0
void graphics_t::load_texture_2D(GLuint texture,SDL_Surface* image) {
	if(!texture)
		graphics_error("texture handle not set");
	if(image->w&(image->w-1))
		std::cerr << "WARNING! image width "<<image->w<<" is not a power of 2" << std::endl;
	if(image->h&(image->h-1))
		std::cerr << "WARNING! image height "<<image->h<<" is not a power of 2" << std::endl;
        GLenum texture_format;
        switch(image->format->BytesPerPixel) {
        case 4:
        		texture_format = (image->format->Rmask==0xff)? GL_RGBA: GL_BGRA;
        		break;
        	case 3:
        		texture_format = (image->format->Rmask==0xff)? GL_RGB: GL_BGR;
        		break;
        	default:
        		graphics_error("the image is not truecolor ("<<image->format->BytesPerPixel<<" Bpp)");
        }
	glBindTexture(GL_TEXTURE_2D,texture);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D,0,
		image->format->BytesPerPixel,
		image->w,image->h,0,
		texture_format,
		GL_UNSIGNED_BYTE,image->pixels);
	glBindTexture(GL_TEXTURE_2D,0);
}
Esempio n. 2
0
SDL_Surface* graphics_t::load_surface(fs_file_t& file) {
	std::cout << "(loading texture "<<file<<")"<<std::endl;
	if(SDL_Surface* bmp = SDL_LoadBMP(file.path()))
		return bmp;
	if(!strcmp(file.ext(),"tga")) {
		istream_t::ptr_t f(file.reader());
		if(f->byte()) graphics_error("TGA contains an ID: "<<file);
		const int8_t colourMapType __attribute__((unused)) = f->byte();
		const int8_t dataTypeCode = f->byte();
		const int16_t colourMapOrigin __attribute__((unused))= f->uint16();
		const int16_t colourMapLength __attribute__((unused))= f->uint16();
		const int8_t colourMapDepth __attribute__((unused))= f->byte();
		const int16_t xOrigin __attribute__((unused)) = f->uint16();
		const int16_t yOrigin __attribute__((unused)) = f->uint16();
		const int16_t width = f->uint16();
		const int16_t height = f->uint16();
		const int8_t bitsPerPixel = f->byte();
		if((bitsPerPixel!=8)&&(bitsPerPixel!=24)&&(bitsPerPixel!=32))
			graphics_error("unsupported TGA depth: "<<bitsPerPixel<<", "<<file);
		const int components = bitsPerPixel/8;
		const int8_t imageDescriptor __attribute__((unused)) = f->byte();
		const size_t bytes = width*height*components;
		enum type_t { UN_RGB=2, UN_BW=3 };
		if(UN_RGB == dataTypeCode) {
			SDL_Surface* tga = SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,bitsPerPixel,0,0,0,0);
			if(!tga) graphics_error("could not create surface for "<<file<<", "<<SDL_GetError());
			f->read(tga->pixels,bytes);
			return tga;
		} else
			graphics_error("TGA type "<<dataTypeCode<<" not yet supported: "<<file);
	}
	graphics_error("Unsupported type: "<<file << " ("<<file.ext()<<')');
}
Esempio n. 3
0
GLuint graphics_t::alloc_texture() {
	GLuint texture;
	glGenTextures(1,&texture);
	if(!texture)
		graphics_error("could not allocate a texture handle "<<glGetError());
	return texture;
}
Esempio n. 4
0
void graphics_t::load_vbo(GLuint buffer,GLenum target,GLsizeiptr size,const GLvoid* data,GLenum usage) {
	if(!buffer) graphics_error("VBO handle not set");
	glBindBuffer(target,buffer);
	glBufferData(target,size,data,usage);
	glBindBuffer(target,0);
	//#### glCheckErrors
}
Esempio n. 5
0
GLuint graphics_t::alloc_vbo() {
	GLuint buffer;
	glGenBuffers(1,&buffer);
	if(!buffer) graphics_error("could not get GL to allocate a VBO ID");
	return buffer;
}
Esempio n. 6
0
// API ------------------------------------------------------------------------
// ----------------------------------------------------------------------------
static void internal_error_callback(int error, const char *description) {
    graphics_error("%s", description);
}