Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit c59b4b0

Browse files
committed
work
1 parent 27eccf2 commit c59b4b0

File tree

3 files changed

+125
-25
lines changed

3 files changed

+125
-25
lines changed

header/actrprng.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef ACTRPRNG_H
2+
#define ACTRPRNG_H
3+
#include "actrtime.h"
4+
5+
// this is probably a "bad" idea, some guy on SO said making my own prng was bad so I did it
6+
//
7+
extern void prngd(double d);
8+
9+
float actr_prng_max = 4294967295;
10+
//unsigned int actr_prng_prime = 2147483647;
11+
unsigned int actr_prng_prime = 2147483629;
12+
13+
//unsigned int actr_prng_prime = 479001599;
14+
15+
//unsigned int actr_prng_prime = 13;
16+
unsigned int actr_prng_seed = 419420;
17+
18+
void actr_sprng(unsigned int seed) {
19+
actr_prng_seed = seed;
20+
}
21+
unsigned int actr_prng() {
22+
actr_prng_seed *= actr_prng_prime;
23+
return actr_prng_seed;
24+
}
25+
26+
float actr_prngd() {
27+
actr_prng_seed *= actr_prng_prime;
28+
float result = actr_prng_seed / actr_prng_max;
29+
prngd(result);
30+
return result;
31+
}
32+
33+
#endif

header/actrquadtree.h

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
#define ACTRQUADTREE_H
33
#include "actralloc.h"
44
#include "actrvector.h"
5+
#include "actrcanvas.h"
6+
7+
extern void _actr_debug_length(char *ptr, int len, int val);
8+
void actr_debug(char *ptr, int val)
9+
{
10+
_actr_debug_length(ptr, strlen(ptr), val);
11+
}
512
struct ActrQuadTreeBounds
613
{
714
long top;
@@ -23,7 +30,6 @@ struct ActrQuadTree
2330
struct ActrQuadTree *three;
2431
struct ActrQuadTree *four;
2532
struct ActrVector *items;
26-
int itemCount;
2733
};
2834

2935
struct ActrQuadTreeBounds *_actr_quad_tree_bounds(long top, long right, long bottom, long left)
@@ -49,19 +55,37 @@ struct ActrQuadTree *actr_quad_tree_init()
4955
{
5056
struct ActrQuadTree *result = actr_malloc(sizeof(struct ActrQuadTree));
5157
result->root = 1;
52-
result->bounds = _actr_quad_tree_bounds(64, 64, 64, 64);
58+
result->bounds = _actr_quad_tree_bounds(0, 64, 64, 0);
5359
return result;
5460
}
5561
int _actr_quad_tree_bounds_contains(struct ActrQuadTreeBounds *bounds, struct ActrQuadTreeBounds *other)
5662
{
63+
5764
if (other->top < bounds->top)
65+
{
66+
actr_debug("1contains one", other->top);
67+
actr_debug("2contains one", bounds->top);
5868
return 0;
69+
}
70+
// 566 > 0
5971
if (other->right > bounds->right)
72+
{
73+
actr_debug("1contains two", other->right);
74+
actr_debug("2contains two", bounds->right);
6075
return 0;
76+
}
6177
if (other->bottom > bounds->bottom)
78+
{
79+
actr_debug("1contains three", other->bottom);
80+
actr_debug("2contains three", bounds->bottom);
6281
return 0;
82+
}
6383
if (other->left < bounds->left)
84+
{
85+
actr_debug("1contains four", other->left);
86+
actr_debug("2contains four", bounds->left);
6487
return 0;
88+
}
6589
return 1;
6690
}
6791
// 1 2
@@ -70,7 +94,9 @@ int _actr_quad_tree_bounds_contains(struct ActrQuadTreeBounds *bounds, struct Ac
7094
void _actr_quad_tree_grow(struct ActrQuadTree *tree)
7195
{
7296
long size = tree->bounds->right - tree->bounds->left;
97+
actr_debug("grow size", size);
7398
long grow = (size) / 2;
99+
actr_debug("grow grow", grow);
74100
struct ActrQuadTree *new;
75101
if (tree->one)
76102
{
@@ -154,13 +180,50 @@ int _actr_quad_tree_index(struct ActrQuadTree *tree, struct ActrQuadTreeBounds *
154180
}
155181
return 3;
156182
}
183+
void actr_quad_tree_draw(struct ActrQuadTree *tree)
184+
{
185+
struct ActrQuadTreeLeaf *leaf;
186+
if (tree->items)
187+
{
188+
for (int i = 0; i < tree->items->pointer; i++)
189+
{
190+
leaf = *(struct ActrQuadTreeLeaf **)(tree->items->head + i * sizeof(void *));
191+
actr_canvas2d_stroke_rect(leaf->bounds->left - 0.5, leaf->bounds->top - 0.5, leaf->bounds->right - leaf->bounds->left, leaf->bounds->bottom - leaf->bounds->top);
192+
}
193+
}
194+
}
157195
void actr_quad_tree_insert(struct ActrQuadTree *tree, long top, long right, long bottom, long left, void *item)
158196
{
159197
struct ActrQuadTreeLeaf *leaf = _actr_quad_tree_leaf(top, right, bottom, left, item);
160-
while (!_actr_quad_tree_bounds_contains(tree->bounds, leaf->bounds))
198+
if (tree->root)
199+
{
200+
while (!_actr_quad_tree_bounds_contains(tree->bounds, leaf->bounds))
201+
{
202+
_actr_quad_tree_grow(tree);
203+
}
204+
}
205+
if (!tree->items)
161206
{
162-
_actr_quad_tree_grow(tree);
207+
tree->items = actr_malloc(sizeof(struct ActrVector));
208+
actr_vector_add(tree->items, leaf);
209+
return;
210+
} else if (tree->items) {
211+
actr_vector_add(tree->items, leaf);
163212
}
164213
int index = _actr_quad_tree_index(tree, leaf->bounds);
214+
215+
if (index == 1)
216+
{
217+
}
218+
else if (index == 2)
219+
{
220+
}
221+
else if (index == 3)
222+
{
223+
}
224+
else if (index == 4)
225+
{
226+
}
227+
actr_debug("qt insert index", index);
165228
}
166229
#endif

ui-example.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
#include "actrwasm.h"
22
#include "actrquadtree.h"
33
#include "actrcanvas.h"
4+
#include "actrprng.h"
45

56
struct ActrQuadTree *tree;
6-
struct ActrVector *list;
77
char *name;
8+
int tapCount = 0;
89
[[clang::export_name("actr_init")]]
9-
void actr_init() {
10-
list = actr_malloc(sizeof(struct ActrVector));
11-
list->length = 0;
12-
list->pointer = 0;
13-
// tree = actr_quad_tree_init();
14-
for (int i = 0; i < 10; i++) {
15-
name = actr_memory_report();
16-
actr_vector_add(list, name);
10+
void actr_init()
11+
{
12+
actr_sprng(actr_time());
13+
tree = actr_quad_tree_init();
14+
for (int i = 0; i < 77; i++)
15+
{
16+
actr_prngd();
1717
}
1818
}
19+
[[clang::export_name("actr_tap")]]
20+
void actr_tap(long x, long y)
21+
{
22+
int *tap = actr_malloc(sizeof(int));
23+
*tap = tapCount++;
24+
actr_quad_tree_insert(tree, y - 5, x + 5, y + 5, x - 5, tap);
25+
}
1926

2027
int top = 0;
21-
void printLine(char * line) {
28+
void printLine(char *line)
29+
{
2230
top += 15;
23-
actr_canvas2d_fill_text(10, top, (char*)line);
31+
actr_canvas2d_fill_text(10, top, (char *)line);
2432
}
2533
[[clang::export_name("actr_step")]]
26-
void actr_step() {
34+
void actr_step()
35+
{
2736
top = 0;
28-
actr_canvas2d_fill_style(0,0,0,100);
29-
actr_canvas2d_fill_rect(-10,-10,9999,9999);
30-
actr_canvas2d_fill_style(255,255,255,100);
31-
for (int i = 0; i < list->pointer; i++) {
32-
printLine(*(char**)(list->head + i * sizeof(void*)));
33-
}
37+
actr_canvas2d_fill_style(0, 0, 0, 100);
38+
actr_canvas2d_fill_rect(-10, -10, 9999, 9999);
39+
actr_canvas2d_stroke_style(255, 255, 255, 100);
40+
actr_quad_tree_draw(tree);
3441
}
35-
36-
37-

0 commit comments

Comments
 (0)