summaryrefslogtreecommitdiff
path: root/src/state.c
blob: d9b2ad4965e34f9c5aec35b6638fb4bc60bf7e62 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "main.h"
#include "state.h"
#include "lua_core.h"
#include "textures.h"

State *state;

bool stateInit( const char *exePath ) {
	state = malloc( sizeof( State ) );

	state->exePath = malloc( STRING_LEN * sizeof( char ) );
	strncpy( state->exePath, exePath, STRING_LEN - 1 );

	state->hasWindow = true;
	state->run = true;
	state->resolution = (Vector2){ 1024, 720 };
	state->luaState = NULL;
	state->targetFPS = 60;
	state->textureSource = TEXTURE_SOURCE_TEXTURE;
	/* Images. */
	state->imageAlloc = ALLOC_PAGE_SIZE;
	state->imageCount = 0;
	state->images = malloc( state->imageAlloc * sizeof( Image* ) );
	/* Textures. */
	state->textureAlloc = ALLOC_PAGE_SIZE;
	state->textureCount = 0;
	state->textures = malloc( state->textureAlloc * sizeof( Texture2D* ) );
	/* RenderTextures. */
	state->renderTextureAlloc = ALLOC_PAGE_SIZE;
	state->renderTextureCount = 0;
	state->renderTextures = malloc( state->renderTextureAlloc * sizeof( RenderTexture2D* ) );
	/* Fonts. */
	state->fontAlloc = ALLOC_PAGE_SIZE;
	state->fontCount = 1;
	state->fonts = malloc( state->fontAlloc * sizeof( Font* ) );
	/* Sounds. */
	state->soundAlloc = ALLOC_PAGE_SIZE;
	state->soundCount = 0;
	state->sounds = malloc( state->soundAlloc * sizeof( Sound* ) );
	/* Camera3D's. */
	state->camera3DAlloc = ALLOC_PAGE_SIZE;
	state->camera3DCount = 0;
	state->camera3Ds = malloc( state->camera3DAlloc * sizeof( Camera3D* ) );
	/* Meshes. */
	state->meshAlloc = ALLOC_PAGE_SIZE;
	state->meshCount = 0;
	state->meshes = malloc( state->meshAlloc * sizeof( Mesh* ) );
	/* Materials. */
	state->materialAlloc = ALLOC_PAGE_SIZE;
	state->materialCount = 1;
	state->materials = malloc( state->materialAlloc * sizeof( Material* ) );
	/* Models. */
	state->modelAlloc = ALLOC_PAGE_SIZE;
	state->modelCount = 0;
	state->models = malloc( state->modelAlloc * sizeof( Model* ) );
	/* ModelsAnimations. */
	state->animationAlloc = ALLOC_PAGE_SIZE;
	state->animationCount = 0;
	state->animations = malloc( state->animationAlloc * sizeof( ModelAnimations* ) );
	/* Shaders. */
	state->shaderAlloc = ALLOC_PAGE_SIZE;
	state->shaderCount = 0;
	state->shaders = malloc( state->shaderAlloc * sizeof( Shader* ) );

	for ( int i = 0; i < ALLOC_PAGE_SIZE; i++ ) {
		state->images[i] = NULL;
		state->textures[i] = NULL;
		state->renderTextures[i] = NULL;
		state->sounds[i] = NULL;
		state->camera3Ds[i] = NULL;
		state->meshes[i] = NULL;
		state->models[i] = NULL;
		state->animations[i] = NULL;
		state->shaders[i] = NULL;

		/* The ones we want to save the first. */
		if ( 0 < i ) {
			state->fonts[i] = NULL;
			state->materials[i] = NULL;
		}
	}
	
    InitWindow( state->resolution.x, state->resolution.y, "ReiLua" );
	/* Has to be after InitWindod where opengl context is created. */
	state->materials[0] = malloc( sizeof( Material ) );
	*state->materials[0] = LoadMaterialDefault();
	state->fonts[0] = malloc( sizeof( Font ) );
	*state->fonts[0] = GetFontDefault();

	if ( !IsWindowReady() ) {
		state->hasWindow = false;
		state->run = false;
	}
	else {
		SetTargetFPS( state->targetFPS );
	}

	InitAudioDevice();
	state->run = luaInit();

	return state->run;
}

void stateFree() {
	for ( int i = 0; i < state->imageCount; ++i ) {
		if ( state->images[i] != NULL ) {
			UnloadImage( *state->images[i] );
			free( state->images[i] );
		}
	}	
	for ( int i = 0; i < state->textureCount; ++i ) {
		if ( state->textures[i] != NULL ) {
			UnloadTexture( *state->textures[i] );
			free( state->textures[i] );
		}
	}
	for ( int i = 0; i < state->renderTextureCount; ++i ) {
		if ( state->renderTextures[i] != NULL ) {
			UnloadRenderTexture( *state->renderTextures[i] );
			free( state->renderTextures[i] );
		}
	}
	for ( int i = 0; i < state->fontCount; ++i ) {
		if ( state->fonts[i] != NULL ) {
			UnloadFont( *state->fonts[i] );
			free( state->fonts[i] );
		}
	}
	for ( int i = 0; i < state->soundCount; ++i ) {
		if ( state->sounds[i] != NULL ) {
			UnloadSound( *state->sounds[i] );
			free( state->sounds[i] );
		}
	}
	for ( int i = 0; i < state->camera3DCount; ++i ) {
		if ( state->camera3Ds[i] != NULL ) {
			free( state->camera3Ds[i] );
		}
	}
	for ( int i = 0; i < state->modelCount; ++i ) {
		if ( state->models[i] != NULL ) {
			// UnloadModel( *state->models[i] );
			UnloadModelKeepMeshes( *state->models[i] );
			free( state->models[i] );
		}
	}
	for ( int i = 0; i < state->meshCount; ++i ) {
		if ( state->meshes[i] != NULL ) {
			UnloadMesh( *state->meshes[i] );
			free( state->meshes[i] );
		}
	}
	for ( int i = 0; i < state->materialCount; ++i ) {
		if ( state->materials[i] != NULL ) {
			UnloadMaterial( *state->materials[i] );
			free( state->materials[i] );
		}
	}
	for ( int i = 0; i < state->animationCount; ++i ) {
		if ( state->animations[i] != NULL ) {
			UnloadModelAnimations( state->animations[i]->animations, state->animations[i]->animCount );
			free( state->animations[i] );
		}
	}
	for ( int i = 0; i < state->shaderCount; ++i ) {
		if ( state->shaders[i] != NULL ) {
			UnloadShader( *state->shaders[i] );
			free( state->shaders[i] );
		}
	}

	if ( IsAudioDeviceReady() ) {
		CloseAudioDevice();
		UnloadMusicStream( state->music );
	}
	if ( state->hasWindow ) {
		CloseWindow();
	}
	if ( state->luaState != NULL ) {
		lua_close( state->luaState );
	}
	free( state->images );
	free( state->textures );
	free( state->renderTextures );
	free( state->fonts );
	free( state->sounds );
	free( state->camera3Ds );
	free( state->meshes );
	free( state->materials );
	free( state->models );
	free( state->animations );
	free( state->shaders );
	free( state );
}