-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprite.c
More file actions
46 lines (40 loc) · 853 Bytes
/
sprite.c
File metadata and controls
46 lines (40 loc) · 853 Bytes
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
#include "error.h"
#include "memory.h"
#include "sprite.h"
#include "texture.h"
#include <assert.h>
struct Sprite*
sprite_from_file(const char *filename)
{
assert(filename != NULL);
// create an empty sprite struct
struct Sprite *spr = make(struct Sprite);
if (!spr) {
return NULL;
}
// load the texture from image file
spr->texture = texture_from_file(filename);
if (!spr->texture) {
sprite_destroy(spr);
return NULL;
}
spr->width = spr->texture->width;
spr->height = spr->texture->height;
// generate a VAO for the sprite
glGenVertexArrays(1, &spr->vao);
if (glGetError() != GL_NO_ERROR || !spr->vao) {
error(ERR_OPENGL);
sprite_destroy(spr);
return NULL;
}
return spr;
}
void
sprite_destroy(struct Sprite *spr)
{
if (spr) {
glDeleteVertexArrays(1, &spr->vao);
texture_destroy(spr->texture);
destroy(spr);
}
}