Something went wrong. Try again.
This is my attempt at a basic rendering engine in C
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181#include "dom.h"#include "hash.h"
union { char buf[8]; usize value;} static const attr_map = { .buf = "attrmap_"};
pdom_attribute_map_t pdom_attribute_map(void) { return (pdom_attribute_map_t) {};}void pd_attr_put(pdom_attribute_map_t map[static const 1], pcow_t key, pcow_t value) { usize hash = phash(key.c_str, key.length, attr_map.value);
if (map->buckets[hash % 32]) { struct pdom_attribute_bucket_t *stretchy bucket = map->buckets[hash % 32]; psb_foreach(bucket, key_value) { if (key_value->hash == hash && pcow_equal(key_value->key, key)) { key_value->value = value; return; } }
psb_pushback(bucket, ((struct pdom_attribute_bucket_t){ .hash = hash, .key = key, .value = value }));
map->buckets[hash % 32] = bucket; } else { psb_pushback(map->buckets[hash % 32], ((struct pdom_attribute_bucket_t){ .hash = hash, .key = key, .value = value })); }}
pcow_t pd_attr_get(const pdom_attribute_map_t map[static const 1], pcow_t key) { usize hash = phash(key.c_str, key.length, attr_map.value);
if (map->buckets[hash % 32]) { struct pdom_attribute_bucket_t *stretchy bucket = map->buckets[hash % 32]; psb_foreach(bucket, key_value) { if (key_value->hash == hash && pcow_equal(key_value->key, key)) { return key_value->value; } } }
return pcow("", 0);}
pdom_attribute_iter_t pd_attr_iter(const pdom_attribute_map_t map[static const 1]) { if (!map->num_items) return (pdom_attribute_iter_t){}; for (usize bucket_index = 0; bucket_index < 32; bucket_index++) { struct pdom_attribute_bucket_t *current = map->buckets[bucket_index]; if (!current) continue;
return (pdom_attribute_iter_t) { PDOM_ATTR_ITER_SOME, bucket_index, 0, current->key, current->value }; }
// shouldn't be possible, if we have 0 items we early exit return (pdom_attribute_iter_t){};}
bool pd_attr_iter_next(const pdom_attribute_map_t map[static const 1], pdom_attribute_iter_t it[static const 1]) { if (!map->num_items) return false; if (it->kind == PDOM_ATTR_ITER_NONE) return false;
bool using_next_bucket = false; for (usize bucket_index = it->bucket_index; bucket_index < 32; bucket_index++) { struct pdom_attribute_bucket_t *current = map->buckets[bucket_index]; if (!current) { using_next_bucket = true; continue; }
if (using_next_bucket) { *it = (pdom_attribute_iter_t) { PDOM_ATTR_ITER_SOME, bucket_index, 0, current->key, current->value }; return true; } else if (it->index + 1 < psb_size(current)) { usize new_index = it->index + 1; *it = (pdom_attribute_iter_t) { PDOM_ATTR_ITER_SOME, bucket_index, new_index, current[new_index].key, current[new_index].value }; return true; } else { using_next_bucket = true; } }
return false;}
pdom_node_t pdom_text_node(pcow_t text) { return (pdom_node_t) { .kind = PDOM_NODE_TEXT, .text = text };}
pdom_node_t pdom_element_node(pcow_t tag_name, pdom_attribute_map_t attr, pdom_node_t *stretchy children) { return (pdom_node_t) { .kind = PDOM_NODE_ELEMENT, .children = children, .element = { tag_name, attr } };}
void pdom_debug_print_node_tabulated(pdom_node_t *node, usize num_tabs) { for (usize i = 0; i < num_tabs; i++) { printf(" "); }
switch (node->kind) { case PDOM_NODE_TEXT: printf("TEXT_NODE: \"%*.*s\"\n", (int)node->text.length, (int)node->text.length, node->text.c_str ); break; case PDOM_NODE_ELEMENT: printf("ELEMENT_NODE: %*.*s\n", (int)node->element.tag_name.length, (int)node->element.tag_name.length, node->element.tag_name.c_str ); for (usize i = 0; i < num_tabs; i++) { printf(" "); } printf("attributes:\n"); for (pdom_attribute_iter_t it = pd_attr_iter(&node->element.attr); it.kind == PDOM_ATTR_ITER_SOME; pd_attr_iter_next(&node->element.attr, &it)) { for (usize i = 0; i < num_tabs; i++) { printf(" "); }
printf("%*.*s: %*.*s\n", (int)it.key.length, (int)it.key.length, it.key.c_str, (int)it.value.length, (int)it.value.length, it.value.c_str ); }
for (usize i = 0; i < num_tabs; i++) { printf(" "); } printf("children:\n");
break; }
psb_foreach(node->children, child) { pdom_debug_print_node_tabulated(child, num_tabs); }
}
void pdom_debug_print_node(pdom_node_t *node) { pdom_debug_print_node_tabulated(node, 0);}