Fri, 12 Apr 2024 22:03:15 +0200
improve ui/text.h interface a lot
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * Copyright 2024 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "ascension/texture.h" #include "ascension/error.h" #include <assert.h> #include <GL/glew.h> void asc_texture_bind(AscTexture const *tex, int uniform_location, int unit) { glActiveTexture(GL_TEXTURE0 + unit); GLenum error = glGetError(); if (error == GL_INVALID_ENUM) { asc_error("Tried to use more texture units than available."); } glBindTexture(tex->target, tex->tex_id); glUniform1i(uniform_location, unit); } void asc_texture_from_surface(AscTexture *tex, SDL_Surface const *surface) { glBindTexture(tex->target,tex->tex_id); glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel); glTexImage2D(tex->target, 0, GL_RGBA, surface->w, surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels); } void asc_texture_init( AscTexture *tex, enum asc_texture_target target, enum asc_texture_min_filter min_filter, enum asc_texture_mag_filter mag_filter ) { static const GLenum texture_targets[] = { GL_TEXTURE_RECTANGLE }; static const GLint texture_filters[] = { GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR }; assert(target < sizeof(texture_targets) / sizeof(GLenum)); assert(min_filter < sizeof(texture_filters) / sizeof(GLint)); assert(mag_filter < 2); // mag filter only supports nearest/linear tex->target = texture_targets[target]; glGenTextures(1, &tex->tex_id); glBindTexture(tex->target, tex->tex_id); glTexParameteri(tex->target, GL_TEXTURE_MIN_FILTER, texture_filters[min_filter]); glTexParameteri(tex->target, GL_TEXTURE_MAG_FILTER, texture_filters[mag_filter]); asc_dprintf("Generated new texture for text node: %u", tex->tex_id); } void asc_texture_destroy(AscTexture *tex) { asc_dprintf("Release text node texture: %u", tex->tex_id); glDeleteTextures(1, &tex->tex_id); }