src/texture.c

Thu, 18 Apr 2024 22:53:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Apr 2024 22:53:55 +0200
changeset 65
9c44c55d327a
parent 50
d8d2e4817db1
permissions
-rw-r--r--

consistently refer to windows by ID - fixes #381

This change discovered that the font cache is completely broken. We created issue #387 for this.

universe@50 1 /*
universe@50 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@50 3 * Copyright 2024 Mike Becker. All rights reserved.
universe@50 4 *
universe@50 5 * Redistribution and use in source and binary forms, with or without
universe@50 6 * modification, are permitted provided that the following conditions are met:
universe@50 7 *
universe@50 8 * 1. Redistributions of source code must retain the above copyright
universe@50 9 * notice, this list of conditions and the following disclaimer.
universe@50 10 *
universe@50 11 * 2. Redistributions in binary form must reproduce the above copyright
universe@50 12 * notice, this list of conditions and the following disclaimer in the
universe@50 13 * documentation and/or other materials provided with the distribution.
universe@50 14 *
universe@50 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@50 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@50 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@50 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@50 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@50 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@50 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@50 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@50 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@50 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@50 25 * POSSIBILITY OF SUCH DAMAGE.
universe@50 26 */
universe@50 27
universe@50 28 #include "ascension/texture.h"
universe@50 29 #include "ascension/error.h"
universe@50 30
universe@50 31 #include <assert.h>
universe@50 32 #include <GL/glew.h>
universe@50 33
universe@50 34 void asc_texture_bind(AscTexture const *tex, int uniform_location, int unit) {
universe@50 35 glActiveTexture(GL_TEXTURE0 + unit);
universe@50 36 GLenum error = glGetError();
universe@50 37 if (error == GL_INVALID_ENUM) {
universe@50 38 asc_error("Tried to use more texture units than available.");
universe@50 39 }
universe@50 40 glBindTexture(tex->target, tex->tex_id);
universe@50 41 glUniform1i(uniform_location, unit);
universe@50 42 }
universe@50 43
universe@50 44 void asc_texture_from_surface(AscTexture *tex, SDL_Surface const *surface) {
universe@50 45 glBindTexture(tex->target,tex->tex_id);
universe@50 46 glPixelStorei(GL_UNPACK_ROW_LENGTH,
universe@50 47 surface->pitch / surface->format->BytesPerPixel);
universe@50 48 glTexImage2D(tex->target, 0, GL_RGBA,
universe@50 49 surface->w, surface->h,
universe@50 50 0, GL_BGRA,
universe@50 51 GL_UNSIGNED_BYTE, surface->pixels);
universe@50 52 }
universe@50 53
universe@50 54 void asc_texture_init(
universe@50 55 AscTexture *tex,
universe@50 56 enum asc_texture_target target,
universe@50 57 enum asc_texture_min_filter min_filter,
universe@50 58 enum asc_texture_mag_filter mag_filter
universe@50 59 ) {
universe@50 60 static const GLenum texture_targets[] = {
universe@50 61 GL_TEXTURE_RECTANGLE
universe@50 62 };
universe@50 63 static const GLint texture_filters[] = {
universe@50 64 GL_NEAREST,
universe@50 65 GL_LINEAR,
universe@50 66 GL_NEAREST_MIPMAP_NEAREST,
universe@50 67 GL_LINEAR_MIPMAP_NEAREST,
universe@50 68 GL_NEAREST_MIPMAP_LINEAR,
universe@50 69 GL_LINEAR_MIPMAP_LINEAR
universe@50 70 };
universe@50 71 assert(target < sizeof(texture_targets) / sizeof(GLenum));
universe@50 72 assert(min_filter < sizeof(texture_filters) / sizeof(GLint));
universe@50 73 assert(mag_filter < 2); // mag filter only supports nearest/linear
universe@50 74 tex->target = texture_targets[target];
universe@50 75 glGenTextures(1, &tex->tex_id);
universe@50 76 glBindTexture(tex->target, tex->tex_id);
universe@50 77 glTexParameteri(tex->target, GL_TEXTURE_MIN_FILTER,
universe@50 78 texture_filters[min_filter]);
universe@50 79 glTexParameteri(tex->target, GL_TEXTURE_MAG_FILTER,
universe@50 80 texture_filters[mag_filter]);
universe@50 81 asc_dprintf("Generated new texture for text node: %u", tex->tex_id);
universe@50 82 }
universe@50 83
universe@50 84 void asc_texture_destroy(AscTexture *tex) {
universe@50 85 asc_dprintf("Release text node texture: %u", tex->tex_id);
universe@50 86 glDeleteTextures(1, &tex->tex_id);
universe@50 87 }

mercurial