summaryrefslogtreecommitdiff
path: root/src/texture.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/texture.c')
-rw-r--r--src/texture.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/texture.c b/src/texture.c
new file mode 100644
index 0000000..d365e3b
--- /dev/null
+++ b/src/texture.c
@@ -0,0 +1,42 @@
+#include "stb_image.h"
+#include "texture.h"
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+int
+texture_init(Texture *this, const char *img_path)
+{
+ glGenTextures(1, &this->id);
+ if (this->id == 0) {
+ fprintf(stderr, "glGenTextures failed\n");
+ return -1;
+ }
+
+ stbi_set_flip_vertically_on_load(1);
+ int width, height, nb_chans;
+ uint8_t *const data = stbi_load(img_path, &width, &height, &nb_chans, 3);
+ if (data == NULL) {
+ fprintf(stderr, "failed to load image '%s'\n", img_path);
+ texture_deinit(this);
+ return -1;
+ }
+
+ glBindTexture(GL_TEXTURE_2D, this->id);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
+ 0, GL_RGB, GL_UNSIGNED_BYTE, data);
+ stbi_image_free(data);
+
+ glGenerateMipmap(GL_TEXTURE_2D);
+
+ return 0;
+}
+
+void
+texture_deinit(Texture *this)
+{
+ if (this->id) {
+ glDeleteTextures(1, &this->id);
+ this->id = 0;
+ }
+}