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