Skip to content

Commit 19f3d7a

Browse files
author
Allen Webster
committed
Finished library loading logic; added api definition system; defined system api (and related)
1 parent 364d9ba commit 19f3d7a

23 files changed

+1218
-89
lines changed

4ed_api_definition.cpp

+276
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
/*
2+
* Mr. 4th Dimention - Allen Webster
3+
*
4+
* 02.10.2019
5+
*
6+
* System API definition program.
7+
*
8+
*/
9+
10+
// TOP
11+
12+
#include "4coder_base_types.h"
13+
14+
#include "4coder_base_types.cpp"
15+
#include "4coder_stringf.cpp"
16+
17+
#include "4coder_malloc_allocator.cpp"
18+
19+
////////////////////////////////
20+
21+
struct API_Param{
22+
API_Param *next;
23+
String_Const_u8 type_name;
24+
String_Const_u8 name;
25+
};
26+
27+
struct API_Param_List{
28+
API_Param *first;
29+
API_Param *last;
30+
i32 count;
31+
};
32+
33+
struct API_Call{
34+
API_Call *next;
35+
String_Const_u8 name;
36+
String_Const_u8 return_type;
37+
API_Param_List params;
38+
};
39+
40+
struct API_Definition{
41+
API_Call *first;
42+
API_Call *last;
43+
i32 count;
44+
45+
String_Const_u8 name;
46+
};
47+
48+
////////////////////////////////
49+
50+
function API_Definition*
51+
begin_api(Arena *arena, char *name){
52+
API_Definition *api = push_array_zero(arena, API_Definition, 1);
53+
api->name = SCu8(name);
54+
return(api);
55+
}
56+
57+
function API_Call*
58+
api_call(Arena *arena, API_Definition *api, char *name, char *return_type){
59+
API_Call *call = push_array_zero(arena, API_Call, 1);
60+
sll_queue_push(api->first, api->last, call);
61+
api->count += 1;
62+
call->name = SCu8(name);
63+
call->return_type = SCu8(return_type);
64+
return(call);
65+
}
66+
67+
function API_Param*
68+
api_param(Arena *arena, API_Call *call, char *type_name, char *name){
69+
API_Param *param = push_array_zero(arena, API_Param, 1);
70+
sll_queue_push(call->params.first, call->params.last, param);
71+
call->params.count += 1;
72+
param->type_name = SCu8(type_name);
73+
param->name = SCu8(name);
74+
return(param);
75+
}
76+
77+
////////////////////////////////
78+
79+
function void
80+
generate_header(Arena *scratch, API_Definition *api, FILE *out){
81+
for (API_Call *call = api->first;
82+
call != 0;
83+
call = call->next){
84+
fprintf(out, "#define %.*s_%.*s_sig() %.*s %.*s_%.*s(",
85+
string_expand(api->name),
86+
string_expand(call->name),
87+
string_expand(call->return_type),
88+
string_expand(api->name),
89+
string_expand(call->name));
90+
if (call->params.count == 0){
91+
fprintf(out, "void");
92+
}
93+
else{
94+
for (API_Param *param = call->params.first;
95+
param != 0;
96+
param = param->next){
97+
fprintf(out, "%.*s %.*s",
98+
string_expand(param->type_name),
99+
string_expand(param->name));
100+
if (param->next != 0){
101+
fprintf(out, ", ");
102+
}
103+
}
104+
}
105+
fprintf(out, ")\n");
106+
}
107+
108+
for (API_Call *call = api->first;
109+
call != 0;
110+
call = call->next){
111+
fprintf(out, "typedef %.*s %.*s_%.*s_type(",
112+
string_expand(call->return_type),
113+
string_expand(api->name),
114+
string_expand(call->name));
115+
if (call->params.count == 0){
116+
fprintf(out, "void");
117+
}
118+
else{
119+
for (API_Param *param = call->params.first;
120+
param != 0;
121+
param = param->next){
122+
fprintf(out, "%.*s %.*s",
123+
string_expand(param->type_name),
124+
string_expand(param->name));
125+
if (param->next != 0){
126+
fprintf(out, ", ");
127+
}
128+
}
129+
}
130+
fprintf(out, ");\n");
131+
}
132+
133+
fprintf(out, "struct API_VTable_%.*s{\n", string_expand(api->name));
134+
for (API_Call *call = api->first;
135+
call != 0;
136+
call = call->next){
137+
fprintf(out, "%.*s_%.*s_type *",
138+
string_expand(api->name),
139+
string_expand(call->name));
140+
fprintf(out, "%.*s",
141+
string_expand(call->name));
142+
fprintf(out, ";\n");
143+
}
144+
fprintf(out, "};\n");
145+
146+
fprintf(out, "#if defined(STATIC_LINK_API)\n");
147+
for (API_Call *call = api->first;
148+
call != 0;
149+
call = call->next){
150+
fprintf(out, "internal %.*s %.*s_%.*s(",
151+
string_expand(call->return_type),
152+
string_expand(api->name),
153+
string_expand(call->name));
154+
if (call->params.count == 0){
155+
fprintf(out, "void");
156+
}
157+
else{
158+
for (API_Param *param = call->params.first;
159+
param != 0;
160+
param = param->next){
161+
fprintf(out, "%.*s %.*s",
162+
string_expand(param->type_name),
163+
string_expand(param->name));
164+
if (param->next != 0){
165+
fprintf(out, ", ");
166+
}
167+
}
168+
}
169+
fprintf(out, ");\n");
170+
}
171+
fprintf(out, "#undef STATIC_LINK_API\n");
172+
fprintf(out, "#elif defined(DYNAMIC_LINK_API)\n");
173+
for (API_Call *call = api->first;
174+
call != 0;
175+
call = call->next){
176+
fprintf(out, "global %.*s_%.*s_type *%.*s_%.*s = 0;\n",
177+
string_expand(api->name),
178+
string_expand(call->name),
179+
string_expand(api->name),
180+
string_expand(call->name));
181+
}
182+
fprintf(out, "#undef DYNAMIC_LINK_API\n");
183+
fprintf(out, "#endif\n");
184+
}
185+
186+
function void
187+
generate_cpp(Arena *scratch, API_Definition *api, FILE *out){
188+
fprintf(out, "function void\n");
189+
fprintf(out, "%.*s_api_fill_vtable(API_VTable_%.*s *vtable){\n",
190+
string_expand(api->name),
191+
string_expand(api->name));
192+
for (API_Call *call = api->first;
193+
call != 0;
194+
call = call->next){
195+
fprintf(out, "vtable->%.*s = %.*s_%.*s;\n",
196+
string_expand(call->name),
197+
string_expand(api->name),
198+
string_expand(call->name));
199+
}
200+
fprintf(out, "}\n");
201+
202+
fprintf(out, "#if defined(DYNAMIC_LINK_API)\n");
203+
fprintf(out, "function void\n");
204+
fprintf(out, "%.*s_api_read_vtable(API_VTable_%.*s *vtable){\n",
205+
string_expand(api->name),
206+
string_expand(api->name));
207+
for (API_Call *call = api->first;
208+
call != 0;
209+
call = call->next){
210+
fprintf(out, "%.*s_%.*s = vtable->%.*s;\n",
211+
string_expand(api->name),
212+
string_expand(call->name),
213+
string_expand(call->name));
214+
}
215+
fprintf(out, "}\n");
216+
fprintf(out, "#undef DYNAMIC_LINK_API\n");
217+
fprintf(out, "#endif\n");
218+
}
219+
220+
////////////////////////////////
221+
222+
function API_Definition*
223+
define_api(Arena *arena);
224+
225+
int
226+
main(void){
227+
Arena arena = make_arena_malloc();
228+
API_Definition *api = define_api(&arena);
229+
230+
////////////////////////////////
231+
232+
// NOTE(allen): Arrange output files
233+
234+
String_Const_u8 path_to_self = string_u8_litexpr(__FILE__);
235+
path_to_self = string_remove_last_folder(path_to_self);
236+
237+
String_Const_u8 fname_h = push_u8_stringf(&arena, "%.*sgenerated/%.*s_api.h",
238+
string_expand(path_to_self),
239+
string_expand(api->name));
240+
241+
String_Const_u8 fname_cpp = push_u8_stringf(&arena, "%.*sgenerated/%.*s_api.cpp",
242+
string_expand(path_to_self),
243+
string_expand(api->name));
244+
245+
FILE *out_file_h = fopen((char*)fname_h.str, "wb");
246+
if (out_file_h == 0){
247+
printf("could not open output file: '%s'\n", fname_h.str);
248+
exit(1);
249+
}
250+
251+
FILE *out_file_cpp = fopen((char*)fname_cpp.str, "wb");
252+
if (out_file_cpp == 0){
253+
printf("could not open output file: '%s'\n", fname_cpp.str);
254+
exit(1);
255+
}
256+
257+
printf("%s:1:\n", fname_h.str);
258+
printf("%s:1:\n", fname_cpp.str);
259+
260+
////////////////////////////////
261+
262+
// NOTE(allen): Generate output
263+
264+
generate_header(&arena, api, out_file_h);
265+
generate_cpp(&arena, api, out_file_cpp);
266+
267+
////////////////////////////////
268+
269+
fclose(out_file_h);
270+
fclose(out_file_cpp);
271+
272+
return(0);
273+
}
274+
275+
// BOTTOM
276+

4ed_api_implementation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2648,7 +2648,7 @@ Push_4ed_Path(Application_Links *app, Arena *arena)
26482648
{
26492649
Models *models = (Models*)app->cmd_context;
26502650
System_Functions *system = models->system;
2651-
return(system->get_4ed_path(arena));
2651+
return(system->get_path(arena, SystemPath_Binary));
26522652
}
26532653

26542654
// TODO(allen): do(add a "shown but auto-hides on timer" setting for cursor show type)

4ed_font_api.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Mr. 4th Dimention - Allen Webster
3+
*
4+
* 02.10.2019
5+
*
6+
* Font API definition program.
7+
*
8+
*/
9+
10+
// TOP
11+
12+
#include "4ed_api_definition.cpp"
13+
14+
function API_Definition*
15+
define_api(Arena *arena){
16+
API_Definition *api = begin_api(arena, "font");
17+
18+
{
19+
API_Call *call = api_call(arena, api, "make_face", "Face*");
20+
api_param(arena, call, "Arena*", "arena");
21+
api_param(arena, call, "Face_Description*", "description");
22+
api_param(arena, call, "f32", "scale_factor");
23+
}
24+
25+
return(api);
26+
}
27+
28+
// BOTTOM
29+

4ed_font_provider_freetype.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ internal Face*
164164
ft__font_make_face(Arena *arena, Face_Description *description, f32 scale_factor){
165165
String_Const_u8 file_name = {};
166166
if (description->font.in_4coder_font_folder){
167-
String_Const_u8 binary_path = sysfunc.get_4ed_path(arena);
167+
String_Const_u8 binary_path = sysfunc.get_path(arena, SystemPath_Binary);
168168
binary_path = string_mod_replace_character(binary_path, '\\', '/');
169169
file_name = push_u8_stringf(arena, "%.*sfonts/%.*s", string_expand(binary_path),
170170
string_expand(description->font.file_name));

4ed_graphics_api.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Mr. 4th Dimention - Allen Webster
3+
*
4+
* 02.10.2019
5+
*
6+
* Graphics API definition program.
7+
*
8+
*/
9+
10+
// TOP
11+
12+
#include "4ed_api_definition.cpp"
13+
14+
function API_Definition*
15+
define_api(Arena *arena){
16+
API_Definition *api = begin_api(arena, "graphics");
17+
18+
{
19+
API_Call *call = api_call(arena, api, "get_texture", "u32");
20+
api_param(arena, call, "Vec3_i32", "dim");
21+
api_param(arena, call, "Texture_Kind", "texture_kind");
22+
}
23+
24+
{
25+
API_Call *call = api_call(arena, api, "fill_texture", "b32");
26+
api_param(arena, call, "Texture_Kind", "texture_kind");
27+
api_param(arena, call, "u32", "texture");
28+
api_param(arena, call, "Vec3_i32", "p");
29+
api_param(arena, call, "Vec3_i32", "dim");
30+
api_param(arena, call, "void*", "data");
31+
}
32+
33+
return(api);
34+
}
35+
36+
// BOTTOM
37+

0 commit comments

Comments
 (0)