-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextures.c
58 lines (47 loc) · 1.13 KB
/
textures.c
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
48
49
50
51
52
53
54
55
56
57
58
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#if defined(WIN32) || defined(_WIN32)
#define PATH_SEPARATOR "\\"
#else
#define PATH_SEPARATOR "/"
#endif
struct Texture
{
GLuint handle;
char name[100];
};
Texture loadTexture(const char *filename, const char *name)
{
Texture tex;
SDL_Surface* surface = IMG_Load(filename);
strcpy(tex.name, name);
tex.handle = 0;
if (!surface)
return tex;
glGenTextures(1, &tex.handle);
glBindTexture(GL_TEXTURE_2D, tex.handle);
// mipmap level border
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
SDL_FreeSurface(surface);
return tex;
}
void unloadTexture(Texture* tex)
{
glDeleteTextures(1, &tex->handle);
}
void bindTexture(Texture* tex)
{
if (!tex)
return;
glBindTexture(GL_TEXTURE_2D, tex->handle);
glEnable(GL_TEXTURE_2D);
}
void unbindTexture(Texture* tex)
{
if (!tex)
return;
glDisable(GL_TEXTURE_2D);
}