aboutsummaryrefslogtreecommitdiff
path: root/07-july/src/texture.c
blob: 26fdddedd50ce1c3c34f22e281720f4696969c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
}