-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics.c
66 lines (55 loc) · 1.8 KB
/
Graphics.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
59
60
61
62
63
64
65
66
#include <SDL_image.h>
#include "Graphics.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
StoredTexture textureLibrary[MAX_TEXTURE];
int16_t amountOfTextures = 0;
void loadSprite(SDL_Renderer* _renderer, Sprite* _sprite, const char* _name) {
char* buffer = calloc(512, sizeof(char));
snprintf(buffer, 512, "%s%s", TEXTURE_LOCATION, _name);
SDL_Texture* texture = seekExistingSprite(buffer);
bool newTexture = false;
if(texture == NULL){
SDL_Surface* tempSurface = IMG_Load(buffer);
texture = SDL_CreateTextureFromSurface(_renderer, tempSurface);
SDL_FreeSurface(tempSurface);
newTexture = true;
}
if(_sprite == NULL){
_sprite = (Sprite*)malloc(sizeof(Sprite));
}
_sprite->texture = texture;
SDL_QueryTexture(texture, NULL, NULL, &_sprite->width, &_sprite->height);
_sprite->clip_x = 0;
_sprite->clip_y = 0;
_sprite->clip_w = _sprite->width;
_sprite->clip_h = _sprite->height;
StoredTexture newStoredTexture;
newStoredTexture.texture = texture;
newStoredTexture.name = buffer;
if(newTexture){
textureLibrary[amountOfTextures] = newStoredTexture;
++amountOfTextures;
}
}
SDL_Texture* seekExistingSprite(const char *_name) {
for(uint16_t i = 0; i < amountOfTextures; ++i){
if(strcmp(textureLibrary[i].name, _name) == 0){
return textureLibrary[i].texture;
}
}
return NULL;
}
void showSpritesLoaded() {
printf("Amount of Textures loaded: %d\n", amountOfTextures);
for(uint16_t i = 0; i < amountOfTextures; ++i){
printf("[%d]: %s\n", i, textureLibrary[i].name);
}
}
void destroyTextureLibrary() {
for(uint16_t i = 0; i < amountOfTextures; ++i){
SDL_DestroyTexture(textureLibrary[i].texture);
}
}