-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtexture.c
52 lines (48 loc) · 1.74 KB
/
texture.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
#include "texture.h"
#include "utilities.h"
#include <SDL2/SDL_image.h>
bool load_texture_img(Texture * texture, const char *path)
{
SDL_Surface *surface = IMG_Load(path);
if (surface == NULL) {
INFO("Could not load texture from path '%s'\n", path);
return false;
}
if (surface->w != surface->h) {
WARN("Texture '%s' is not a square\n", path);
}
return load_texture_fromSurface(texture, surface);
}
bool load_texture_txt(Texture * texture, TTF_Font * font, const char * txt)
{
SDL_Color fg = {255, 255, 255, 255};
SDL_Surface *surface = TTF_RenderUTF8_Solid(font, txt, fg);
return load_texture_fromSurface(texture, surface);
}
bool load_texture_fromSurface(Texture * texture, SDL_Surface * surface)
{
texture->width_px = surface->w;
texture->height_px = surface->h;
glGenTextures(1, &texture->index);
glBindTexture(GL_TEXTURE_2D, texture->index);
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
SDL_FreeSurface(surface);
return true;
}
void get_tex_quad(Texture * texture, int index, int quad_size,
vec2_t corners[4])
{
int tex_grid_size = texture->width_px / quad_size;
float cell_size = 1 / (float) tex_grid_size;
int quad_y = index / tex_grid_size, quad_x = index % tex_grid_size;
corners[UV_TOP_LEFT] = vec2(quad_x * cell_size, quad_y * cell_size);
corners[UV_TOP_RIGHT] = vec2((quad_x + 1) * cell_size, quad_y * cell_size);
corners[UV_BOTTOM_LEFT] =
vec2(quad_x * cell_size, (quad_y + 1) * cell_size);
corners[UV_BOTTOM_RIGHT] =
vec2((quad_x + 1) * cell_size, (quad_y + 1) * cell_size);
}