Exemple #1
0
void Sprite::create(uint16 width, uint16 height, const Graphics::PixelFormat &f) {
	free();

	actualWidth = width;
	actualHeight = height;
	format = f;
	w = MAX(nextHigher2(width), 64u);
	h = MAX(nextHigher2(height), 64u);
	pitch = w * format.bytesPerPixel;
	dirtyPixels = true;

	if (width && height) {
		pixels = linearAlloc(h * pitch);
		C3D_TexInit(&texture, w, h, GPU_RGBA8);
		C3D_TexSetFilter(&texture, GPU_LINEAR, GPU_LINEAR);
		assert(pixels && texture.data);
		clear();
	}

	float x = 0.f, y = 0.f;
	float u = (float)width/w;
	float v = (float)height/h;
	vertex tmp[4] = {
		{{x,       y,        0.5f}, {0, 0}},
		{{x+width, y,        0.5f}, {u, 0}},
		{{x,       y+height, 0.5f}, {0, v}},
		{{x+width, y+height, 0.5f}, {u, v}},
	};
	memcpy(vertices, tmp, sizeof(vertex) * 4);
}
Exemple #2
0
void GLTexture::allocBuffer(GLuint w, GLuint h) {
	_realWidth = w;
	_realHeight = h;

	if (!_refresh) {
		if (npot_supported && _filter == GL_LINEAR) {
			// Check if we already allocated a correctly-sized buffer
			// This is so we don't need to duplicate the last row/column
			if (w == _textureWidth && h == _textureHeight)
				return;
		} else {
			// Check if we already have a large enough buffer
			if (w <= _textureWidth && h <= _textureHeight)
				return;
		}
	}

	if (npot_supported) {
		_textureWidth = w;
		_textureHeight = h;
	} else {
		_textureWidth = nextHigher2(w);
		_textureHeight = nextHigher2(h);
	}

	// Select this OpenGL texture
	glBindTexture(GL_TEXTURE_2D, _textureName); CHECK_GL_ERROR();

	// Set the texture parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _filter); CHECK_GL_ERROR();
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _filter); CHECK_GL_ERROR();
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); CHECK_GL_ERROR();
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); CHECK_GL_ERROR();

	// Allocate room for the texture
	glTexImage2D(GL_TEXTURE_2D, 0, _internalFormat,
	             _textureWidth, _textureHeight, 0, _glFormat, _glType, NULL); CHECK_GL_ERROR();

	_refresh = false;
}