void TextureResource::load(bool reload) {

    if(textureid != 0) {
        if(!reload) return;
        debugLog("texture %d is being reloaded", textureid);
    }

    SDL_Surface *surface = 0;

    if(!filename.empty()) {
        debugLog("creating texture from %s", filename.c_str());

        surface = IMG_Load(filename.c_str());

        if(surface==0) throw TextureException(filename);

        w = surface->w;
        h = surface->h;

        //figure out image colour order
        format = colourFormat(surface);

        data = (GLubyte*) surface->pixels;

        if(format==0) throw TextureException(filename);
    }

    createTexture();

    if(surface != 0) {
        SDL_FreeSurface(surface);
        data = 0;
    }
}
Example #2
0
Texture::Texture(const std::string& filename)
{
	std::string path = "res\\textures\\" + filename;

	int w;
	int h;
	int comp;

	unsigned char* image = stbi_load(path.c_str(), &w, &h, &comp, STBI_rgb_alpha);

	if(!image)
	{
		throw TextureException("Failed to load texture: " + path);
	}

	glGenTextures(1, &m_texture);

	glBindTexture(GL_TEXTURE_2D, m_texture);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

	glBindTexture(GL_TEXTURE_2D, 0);

	stbi_image_free(image);
}