-
Notifications
You must be signed in to change notification settings - Fork 0
/
textures.c
76 lines (56 loc) · 2.02 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "raylib.h"
#include "structs.h"
#include <string.h>
#include <malloc.h>
#include "textures.h"
BlockTexture* dirtTexture;
BlockTexture* grassTexture;
BlockTexture* grassTopTexture;
Texture textureAtlas;
int textureWidth = 0;
int textureHeight = 0;
void InitializeTextureDefinitions();
void LoadTextureAtlas() {
InitializeTextureDefinitions();
const int textureCount = 3;
BlockTexture* blockTexture[textureCount];
blockTexture[0] = dirtTexture;
blockTexture[1] = grassTexture;
blockTexture[2] = grassTopTexture;
Texture2D textures[textureCount];
for (int i = 0; i < textureCount; ++i) {
char result[100];
strcpy(result, "textures/");
strcat(result, blockTexture[i]->name);
strcat(result, ".png");
Texture2D texture = LoadTexture(result);
if (texture.height > textureHeight) {
textureHeight = texture.height;
}
textureWidth += texture.width;
textures[i] = texture;
}
RenderTexture2D target = LoadRenderTexture(textureWidth, textureHeight);
BeginTextureMode(target);
int currentWidth = 0;
for (int i = 0; i < textureCount; ++i) {
Texture2D texture2D = textures[i];
DrawTexture(texture2D, currentWidth, textureHeight - texture2D.height, WHITE);
blockTexture[i]->topLeft = (Vector2) {(float) currentWidth, 0};
blockTexture[i]->bottomRight = (Vector2) {(float) currentWidth + (float) texture2D.width,
(float) texture2D.height};
currentWidth += texture2D.width;
}
EndTextureMode();
Image image = LoadImageFromTexture(target.texture);
ExportImage(image, "textureAtlas.png");
textureAtlas = target.texture;
}
void InitializeTextureDefinitions(){
dirtTexture = malloc(sizeof(BlockTexture));
dirtTexture->name = "dirt";
grassTexture = malloc(sizeof(BlockTexture));
grassTexture->name = "grass";
grassTopTexture = malloc(sizeof(BlockTexture));
grassTopTexture->name = "grasstop";
}