4a8486f582ca52f0fb089b6f3b4a39c1fc1ff0c6
3 #include <stdint.h> //uint64_t
4 #include <string.h> //memmove
5 #include <stdlib.h> //malloc
8 #define CURR_CDAT (*cdat_stackp)
9 #define CURR_SET set_list[CURR_CDAT->num_sets]
10 #define CURR_ELE ele_list[CURR_CDAT->CURR_SET.num_ele]
11 #define PREV_REF (ref_buf[num_refs-1])
12 #define CURR_REF (ref_buf[num_refs])
13 #define PREV_ODAT (odat_buf[num_odats-1])
14 #define CURR_ODAT (odat_buf[num_odats])
15 #define CURR_VDAT (vdat_buf[num_vdats])
16 #define PREV_VDAT (vdat_buf[num_vdats-1])
17 #define CURR_MODEL model_list[CURR_VDAT->num_models]
18 #define CURR_LINK (link_buf[num_links])
24 /* Init root cdat and stack */
25 char root
[4] = "root";
27 cdat_buf
[num_cdats
] = (struct cdat
*) malloc(sizeof(struct cdat
) );
28 cdat_buf
[num_cdats
]->idx
= 0;
29 memmove(cdat_buf
[num_cdats
]->name
, root
, 4);
31 cdat_stackp
= cdat_stack
;
32 *cdat_stackp
= cdat_buf
[num_cdats
++];
35 if( (CURR_ODAT
= (struct odat
*) malloc(sizeof(struct odat
))) == NULL
)
36 perror("malloc first odat failed");
39 if( (CURR_VDAT
= (struct vdat
*) malloc(sizeof(struct vdat
))) == NULL
)
40 perror("malloc first vdat failed");
43 if( (CURR_REF
= (struct ref
*) malloc(sizeof(struct ref
))) == NULL
)
44 perror("malloc first ref failed");
47 if( (CURR_LINK
= (struct link
*) malloc(sizeof(struct link
))) == NULL
)
48 perror("malloc first link failed");
56 if(curr_max_cdats
<= num_cdats
)
58 if( (realloc((void*) cdat_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
59 perror("realloc cdat_buf failed");
60 curr_max_cdats
+= PTRS_IN_PAGE
;
61 if( (realloc( (void*) cdat_stack
, PTRS_IN_PAGE
* 4)) == NULL
) //increase cdat_stack also
62 perror("realloc cdat_stack failed");
64 if( (cdat_buf
[num_cdats
] = (struct cdat
*) malloc(sizeof (struct cdat
)) ) == NULL
)
65 perror("malloc cdat failed");
70 /* Dynamically allocate memory for a class data structure,
71 or cdat, after a class has been identified in a grammar.
72 We also create a stack of class pointers so that
73 we can access the cdat during processing of that
74 cdats sets and elements, a requirement because the
75 nature of recursive classes prevents us from accessing
76 the cdat based on the previous index into cdat_buf,
77 which is a list of all allocated cdats*/
84 memmove(cdat_buf
[num_cdats
]->name
, name
, 32);
85 cdat_buf
[num_cdats
]->idx
= num_cdats
;
87 /* Set the cdat as a class of the previous cdat */
88 (*cdat_stackp
)->class_list
[(*cdat_stackp
)->num_classes
++] = cdat_buf
[num_cdats
];
90 /* Push the cdat onto the cdat_stack */
91 *++cdat_stackp
= cdat_buf
[num_cdats
++];
106 if(num_odats
>= curr_max_odats
)
108 if( (realloc((void*) odat_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
109 perror("realloc odat_buf failed");
110 curr_max_odats
+= PTRS_IN_PAGE
;
112 if( (CURR_ODAT
= (struct odat
*) malloc(sizeof (struct odat
))) == NULL
)
114 perror("malloc odat failed");
123 if(num_vdats
>= curr_max_vdats
)
125 if( (realloc((void*) vdat_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
126 perror("realloc vdat_buf failed");
127 curr_max_vdats
+= PTRS_IN_PAGE
;
129 if((CURR_VDAT
= (struct vdat
*) malloc(sizeof (struct vdat
))) == NULL
)
131 perror("malloc vdat failed");
140 if(num_links
>= curr_max_links
)
142 if( (realloc((void*) link_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
143 perror("realloc vdat_buf failed");
144 curr_max_links
+= PTRS_IN_PAGE
;
146 if((CURR_LINK
= (struct link
*) malloc(sizeof (struct link
))) == NULL
)
148 perror("malloc link failed");
156 if(num_refs
% 16 == 0)
158 posts
[num_posts
++] = *CURR_REF
;
162 if(num_refs
>= curr_max_refs
)
164 if( (realloc((void*) ref_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
165 perror("realloc ref_buf failed");
166 curr_max_refs
+= PTRS_IN_PAGE
;
168 if((CURR_REF
= (struct ref
*) malloc(sizeof (struct ref
))) == NULL
)
169 perror("malloc ref failed");
173 insert_set_label(char* name
, uint64_t ref_id
)
177 memmove(CURR_CDAT
->CURR_SET
.name
,name
,32);
178 memmove(&CURR_CDAT
->CURR_SET
.ref_id
,&ref_id
,64);
182 insert_set_olink(uint64_t ref_id
)
184 CURR_CDAT
->CURR_SET
.cdat_idx
= CURR_CDAT
->idx
;
185 CURR_CDAT
->CURR_SET
.ref_id
= ref_id
; /* Will be resolved to offset
186 when link is processed */
188 CURR_LINK
->link_t
.olink
.ref_id
= ref_id
;
189 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
190 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
++;
191 CURR_LINK
->ele_idx
= -1;
197 insert_set_vlink(uint64_t ref_id
, char* anim_name
)
199 /* Insert vlink into link_stack so that it gets processed at
201 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
202 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
204 CURR_LINK
->link_t
.vlink
.ref_id
= ref_id
;
205 memmove(CURR_LINK
->link_t
.vlink
.anim_name
, anim_name
, 32);
211 insert_set_svlink(uint64_t ref_id
)
214 /* Insert vlink into link_stack so that it gets processed at
216 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
217 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
219 CURR_LINK
->link_t
.svlink
.ref_id
= ref_id
;
223 /* At the point of reducing to a set, most of the
224 sets odat information has already been populated
225 during the reduction of its right hand side
226 non terminals (hitbox, root, quad_list). */
232 ref_id
= CURR_CDAT
->CURR_SET
.ref_id
;
234 CURR_CDAT
->CURR_SET
.cdat_idx
= CURR_CDAT
->idx
;
235 memmove(CURR_ODAT
->name
, CURR_CDAT
->CURR_SET
.name
, 32);
236 CURR_CDAT
->num_sets
++;
238 CURR_ODAT
->cdat_idx
= CURR_CDAT
->idx
;
239 CURR_ODAT
->refp
= CURR_REF
;
242 CURR_REF
->lastref
= PREV_REF
;
243 PREV_REF
->nextref
= CURR_REF
;
244 CURR_REF
->odatp
= CURR_ODAT
;
247 if(ref_id
== -1) /* user did not define a ref_id so */
248 { /* use a ref_id from system_space */
253 CURR_REF
->ref_id
= ref_id
;
264 PREV_ODAT
->vdat_id
= num_vdats
; //NULL for vlink, svlink
268 /* Populates both the odat name and ref_id
271 insert_ele_label(char* name
, uint64_t ref_id
)
273 memmove(CURR_CDAT
->CURR_SET
.CURR_ELE
.name
, name
, 32);
274 memmove(&CURR_CDAT
->CURR_SET
.ele_list
[CURR_CDAT
->CURR_SET
.ref_id
].ref_id
, &ref_id
, 64);
278 insert_ele_olink(uint64_t ref_id
)
280 CURR_CDAT
->CURR_SET
.CURR_ELE
.cdat_idx
= CURR_CDAT
->idx
;
281 CURR_CDAT
->CURR_SET
.CURR_ELE
.ref_id
= ref_id
; /* Will be resolved to offset
282 when link is processed */
284 CURR_LINK
->link_t
.olink
.ref_id
= ref_id
;
285 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
286 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
++;
287 CURR_LINK
->ele_idx
= CURR_CDAT
->CURR_SET
.num_ele
++;
291 insert_ele_vlink(uint64_t ref_id
, char* anim_name
)
294 /* Insert vlink into link_stack so that it gets processed at
296 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
298 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
299 CURR_LINK
->ele_idx
= CURR_CDAT
->CURR_SET
.num_ele
;
300 CURR_LINK
->link_t
.vlink
.ref_id
= ref_id
;
301 memmove(CURR_LINK
->link_t
.vlink
.anim_name
, anim_name
, 32);
306 insert_ele_svlink(uint64_t ref_id
)
309 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
311 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
312 CURR_LINK
->ele_idx
= CURR_CDAT
->CURR_SET
.num_ele
;
313 CURR_LINK
->link_t
.svlink
.ref_id
= ref_id
;
317 //Insert element into odat_buf and cdatpages
324 ref_id
= CURR_CDAT
->CURR_SET
.CURR_ELE
.ref_id
;
326 CURR_CDAT
->CURR_SET
.CURR_ELE
.cdat_idx
= CURR_CDAT
->idx
;
327 memmove(CURR_ODAT
->name
,CURR_CDAT
->CURR_SET
.CURR_ELE
.name
, 32);
328 CURR_CDAT
->CURR_SET
.num_ele
++;
330 CURR_ODAT
->cdat_idx
= CURR_CDAT
->idx
;
331 CURR_ODAT
->refp
= CURR_REF
;
333 if(ref_id
== -1) /* user did not define a ref_id so */
334 { /* use a ref_id from system_space */
339 CURR_REF
->ref_id
= ref_id
;
347 insert_framesheets(char direction
, char* name
, uint64_t ref_id
, int height
, int width
, int num_frames
)
349 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].height
= height
;
350 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].width
= width
;
351 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].num_frames
= num_frames
;
352 CURR_VDAT
->num_models
++;
355 #define CURR_QUAD (CURR_ODAT->quad_list[CURR_ODAT->num_quads])
357 insert_quad(int x
, int y
, int z
, uint64_t ref_id
)
362 CURR_QUAD
.ref_id
= ref_id
;
363 CURR_ODAT
->num_quads
++;
366 /* Inserting the hitbox into the set
367 odat. Elements that don't have
368 a hitbox will use the sets root. */
370 insert_hitbox(int hitbox
)
372 CURR_ODAT
->hitbox
= hitbox
;
375 /* Inserting the root into the set
376 odat. Elements that don't have
377 a root will use the sets root. */
379 insert_root(int x
, int y
, int z
)
382 CURR_ODAT
->root
.x
= x
;
383 CURR_ODAT
->root
.y
= y
;
384 CURR_ODAT
->root
.z
= z
;
388 insert_frame_pointer(char direction
, void* frame
)
390 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].frames
[CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].num_frames
++] = frame
;