aboutsummaryrefslogtreecommitdiff
path: root/07-july/src/texture.c
diff options
context:
space:
mode:
Diffstat (limited to '07-july/src/texture.c')
-rw-r--r--07-july/src/texture.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/07-july/src/texture.c b/07-july/src/texture.c
new file mode 100644
index 0000000..26fddde
--- /dev/null
+++ b/07-july/src/texture.c
@@ -0,0 +1,47 @@
+#include "texture.h"
+#include "util/util.h"
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_image.h>
+
+texture_t* Texture_Create( const char* path )
+{
+ texture_t* texture = (texture_t*) malloc( sizeof(texture_t) );
+ SDL_Surface* data = IMG_Load(path);
+ if(data == NULL)
+ Util_FatalError("Texture could not be found!\n");
+
+ glGenTextures(1, &texture->ID);
+ glBindTexture(GL_TEXTURE_2D, texture->ID);
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->w, data->h, GL_FALSE, GL_RGBA, GL_UNSIGNED_BYTE, data->pixels);
+ glGenerateMipmap(GL_TEXTURE_2D);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+
+ SDL_FreeSurface(data);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ texture->width = data->w;
+ texture->height = data->h;
+
+ return texture;
+}
+
+void Texture_Bind(texture_t* texture)
+{
+ glBindTexture(GL_TEXTURE_2D, texture->ID);
+}
+
+void Texture_Unbind()
+{
+ glBindTexture(GL_TEXTURE_2D, 0);
+}
+
+void Texture_Destroy(texture_t* texture)
+{
+ glDeleteTextures(1, &texture->ID);
+ free(texture);
+}