-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathDrawAtlas.c
138 lines (109 loc) · 3.47 KB
/
DrawAtlas.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2017-1-2017
* Update : 2019-2-1
* Author : scott.cgi
*/
#include "Engine/Graphics/OpenGL/SubMesh.h"
#include "Engine/Extension/DrawAtlas.h"
#include "Engine/Toolkit/HeaderUtils/Struct.h"
#include "Engine/Toolkit/Platform/Log.h"
static ArrayList(DrawAtlas*) drawAtlasCacheList[1] = AArrayList_Init(DrawAtlas*, 10);
static DrawAtlas* Get(const char* filePath)
{
TextureAtlas* textureAtlas = ATextureAtlas->Get(filePath);
ALog_A
(
textureAtlas->textureList->size == 1,
"DrawAtlas not support TextureAtlas has multiple textures"
);
DrawAtlas* drawAtlas = AArrayList_Pop(drawAtlasCacheList, DrawAtlas*);
if (drawAtlas == NULL)
{
drawAtlas = malloc(sizeof(DrawAtlas));
AMesh->InitWithCapacity
(
AArrayList_Get(textureAtlas->textureList, 0, Texture*),
20,
drawAtlas->mesh
);
AArrayList->InitWithCapacity(sizeof(SubMesh*), 20, drawAtlas->quadList);
}
else
{
AMesh ->Clear(drawAtlas->mesh);
AArrayList->Clear(drawAtlas->quadList);
}
drawAtlas->textureAtlas = textureAtlas;
return drawAtlas;
}
static SubMesh* GetQuad(DrawAtlas* drawAtlas, const char* quadName)
{
TextureAtlasQuad* atlasQuad = ATextureAtlas_GetQuad(drawAtlas->textureAtlas, quadName);
ALog_A(atlasQuad != NULL, "ADrawAtlas GetQuad not found quadName = %s", quadName);
SubMesh* subMesh = AArrayList_Pop(drawAtlas->quadList, SubMesh*);
if (subMesh == NULL)
{
// cache more quads into quadList when cache empty
for (int i = 0; i < 5; ++i)
{
subMesh = AMesh->AddChildWithQuad(drawAtlas->mesh, atlasQuad->quad);
ADrawable_SetInvisible(subMesh->drawable);
AArrayList_Add(drawAtlas->quadList, subMesh);
}
subMesh = AMesh->AddChildWithQuad(drawAtlas->mesh, atlasQuad->quad);
AMesh->GenerateBuffer(drawAtlas->mesh);
}
else
{
ADrawable->Init(subMesh->drawable);
ASubMesh->SetUVWithQuad
(
subMesh,
atlasQuad->quad
);
}
return subMesh;
}
static void Release(DrawAtlas* drawAtlas)
{
ALog_A
(
drawAtlas->textureAtlas != NULL,
"ADrawAtlas Release drawAtlas %p already released",
drawAtlas
);
// quadList clear by Get function
for (int i = 0; i < drawAtlas->quadList->size; ++i)
{
ADrawable_SetInvisible(AArrayList_Get(drawAtlas->quadList, i, SubMesh*)->drawable);
}
drawAtlas->textureAtlas = NULL;
AArrayList_Add(drawAtlasCacheList, drawAtlas);
}
static void ReleaseQuad(DrawAtlas* drawAtlas, SubMesh* subMesh)
{
ALog_A
(
drawAtlas->mesh == subMesh->parent,
"ADrawAtlas %s ReleaseQuad drawable not in this drawAtlas",
drawAtlas->textureAtlas->filePath
);
ADrawable_SetInvisible(subMesh->drawable);
AArrayList_Add(drawAtlas->quadList, subMesh);
}
struct ADrawAtlas ADrawAtlas[1] =
{{
Get,
GetQuad,
Release,
ReleaseQuad,
}};