summaryrefslogtreecommitdiff
path: root/libs/pwyazh/example.c
blob: c8939c3a960766a4cd51addeec11d53601b8c5d6 (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
#include <stdio.h>

#include "pwyazh.h"

typedef struct Node {
    struct Node *next;
    struct Node *prev;
    F32 value;
} Node;

void
print_dll(Node *f)
{
    for (Node *c = f;
         c;
         c = c->next)
    {
        printf("%f%c", c->value, (c->next) ? ' ' : '\n');
    }
}

void
clear_dll(Node **f, Node **l)
{
    for (Node *c = (*f)->next;
         c;
         c = c->next)
    {
        MemoryZeroStruct(c->prev);
    }
    MemoryZeroStruct(*f);
    MemoryZeroStruct(*l);
    *f = *l = 0;
}

int
main(void)
{
    V3F pos = v3f(1.0f, 2.0f, 1.0f);
    pos = v3f_add(pos, v3f(-1.0f, 2.0f, 0.23f));
    pos = v3f_scalef(pos, 10.0f);
    printf("scaled: %f %f %f\n", pos.x, pos.y, pos.z);

    F32 length = v3f_length(pos);
    Assert(length > 0.0f);
    printf("length: %f\n", length);

    V3F positions[] = {
        { -1.0f, 0.0f, 0.0f },
        { 1.0f, 0.0f, 0.0f },
        { 0.0f, 1.0f, 0.0f },
        { 0.0f, 0.0f, -1.0f },
    };
    for (U32 pos_index = 0;
         pos_index < ArrayCount(positions);
         ++pos_index)
    {
        V3F *pos = positions+pos_index;
        printf("%d: %f %f %f\n", pos_index,
               pos->x, pos->y, pos->z);
        U64 ptr_as_int = IntFromPtr(pos);
        printf("ptr: %lx\n", ptr_as_int);
        void *ptr = PtrFromInt(ptr_as_int);
        V3F *as_pos = (V3F *)ptr;
        printf("decode: %f %f %f\n",
               as_pos->x, as_pos->y, as_pos->z);
    }

    printf("Offset of y: %lld\n", OffsetOfMember(V3F, y));

    S32 min = Min(10, 2);
    S32 max = Max(10, 2);
    printf("min: %d\n", min);
    printf("max: %d\n", max);

    S32 source[100];
    for (S32 index = 0;
         index < 100;
         ++index)
    {
        source[index] = index;
        printf("%d%c", source[index], (index < 99) ? ' ' : '\n');
    }

    S32 dest[100];
    MemoryCopyArray(dest, source);
    printf("dest: ");
    for (S32 index = 0;
         index < 100;
         ++index)
    {
        printf("%d%c", dest[index], (index < 99) ? ' ' : '\n');
    }
    printf("compare: %d\n", MemoryMatch(source, dest, sizeof(source)));

    MemoryZeroArray(dest);
    printf("dest: ");
    for (S32 index = 0;
         index < 100;
         ++index)
    {
        printf("%d%c", dest[index], (index < 99) ? ' ' : '\n');
    }
    printf("compare: %d\n", MemoryMatch(source, dest, sizeof(source)));

    V3F first = v3f_zero();
    V3F second;
    printf("struct compare: %d\n", MemoryMatch(&first, &second, sizeof(V3F)));
    MemoryCopyStruct(&second, &first);
    printf("struct compare: %d\n", MemoryMatch(&first, &second, sizeof(V3F)));

    MAT4 mat = mat4_identity();
    printf("det: %f\n", mat4_det(mat));

    {
        U32 node_count = 10;
        Arena *arena = arena_alloc(Megabytes(64));
        Node *nodes = arena_push_size(arena, node_count*sizeof(Node));
        MemoryZero(nodes, node_count*sizeof(Node));
        Node *f = 0;
        Node *l = 0;
        for (U32 node_index = 0;
             node_index < node_count;
             ++node_index)
        {
            nodes[node_index].value = node_index;
            DLLPushBack(f, l, &nodes[node_index]);
        }
        print_dll(f);

        for (Node *c = f;
             c;
             c = c->next)
        {
            if (c->value == 5.0f) {
                DLLRemove(f, l, c);
            }
        }

        DLLPushBack(f, l, &nodes[5]);

        for (U32 node_index = 0;
             node_index < node_count;
             ++node_index)
        {
            Node *node = nodes+node_index;
            printf("%u: prev: %llx ptr: %llx next: %llx %f\n",
                   node_index, IntFromPtr(node->prev),
                   IntFromPtr(node), IntFromPtr(node->next), node->value);
        }

        clear_dll(&f, &l);
        for (U32 node_index = 0;
             node_index < node_count;
             ++node_index)
        {
            Node *node = nodes+node_index;
            printf("%u: prev: %llx ptr: %llx next: %llx %f\n",
                   node_index, IntFromPtr(node->prev),
                   IntFromPtr(node), IntFromPtr(node->next), node->value);
        }
        print_dll(f);
        arena_release(arena);
    }

    U64 temp_size = Kilobytes(1);
    Arena *temp = arena_alloc(temp_size);
    S32 test_count = temp_size/sizeof(S32);
    S32 *test = arena_push_size(temp, test_count*sizeof(S32));
    MemoryZero(test, test_count*sizeof(S32));
    S32 i;
    for (i = 0;
         i < test_count;
         ++i)
    {
        S32 *item = test+i;
        *item = i;
        printf("%d%c", *item, (i == test_count-1) ? '\n' : ' ');
    }
    arena_release(temp);

    temp = arena_alloc(temp_size);
    String8 str = str8_cstring((U8 *)"This is a c string");
    printf("%s", str.str);
    arena_release(temp);
    return(0);
}