summaryrefslogtreecommitdiff
path: root/example/example.c
blob: 1fa85b3676482e54a6524c99903814462539bf48 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "prb.h"

typedef struct {
	S32 first;
	S32 second;
	B32 flags;
} SomeStruct;

typedef struct SLLNode {
	S32 val;
	struct SLLNode *next;
} SLLNode;

typedef struct DLLNode {
	S32 val;
	struct DLLNode *next;
	struct DLLNode *prev;
} DLLNode;

int main(void)
{
	F32 verts[] = {
		1.0f, 0.0f, 0.0f,
		0.0f, 1.0f, 0.0f,
		0.0f, 0.0f, 1.0f
	};

	Assert(v3a(1.0f).y);
	U64 memory_size = Kilobytes(4);
	void *some_memory = malloc(memory_size);
	*(S32 *)some_memory = 69;
	U64 ptr_as_int = IntFromPtr(some_memory);
	void *ptr_to_memory = PtrFromInt(ptr_as_int);
	printf("IntFromPtr: %lu\n", ptr_as_int);
	printf("Using PtrFromInt(%lu) to get the first element of some_memory: %d\n\n",
	       ptr_as_int, *(S32 *)ptr_to_memory);
	free(some_memory);

	printf("OffsetOfMember(SomeStruct, flags): %lld\n\n",
	       OffsetOfMember(SomeStruct, flags));

	F32 range_min = 0.0f, range_max = 1.0f;
	F32 clamped_number = 1.5f;
	F32 another_clamped_number = -0.2f;
	printf("ClampBottom(%f, %f): %f\n", range_min,
	       another_clamped_number, ClampBottom(another_clamped_number, range_min));
	printf("ClampTop(%f, %f): %f\n", range_max,
	       clamped_number, ClampTop(clamped_number, range_max));

	printf("Clamp(%f, %f, %f): %f\n", range_min, clamped_number, range_max,
	       Clamp(range_min, clamped_number, range_max));
	printf("Clamp(%f, %f, %f): %f\n\n", range_min, another_clamped_number, range_max,
	       Clamp(range_min, another_clamped_number, range_max));

	F32 swap_test_first = 10.0f, swap_test_second = 20.0f;
	printf("Before Swap: swap_test_first = %f, swap_test_second = %f\n",
	       swap_test_first, swap_test_second);
	Swap(F32, swap_test_first, swap_test_second);
	printf("After Swap: swap_test_first = %f, swap_test_second = %f\n\n",
	       swap_test_first, swap_test_second);

	SomeStruct some_stuct = { 10, 20, 30 };
	printf("SomeStruct: first: %d, second: %d, flags: %d\n",
	       some_stuct.first, some_stuct.second, some_stuct.flags);
	MemoryZero(&some_stuct, sizeof(SomeStruct));
	printf("After MemoryZero(&some_stuct, sizeof(SomeStruct)):\n");
	printf("some_stuct: first: %d, second: %d, flags: %d\n\n",
	       some_stuct.first, some_stuct.second, some_stuct.flags);

	some_stuct = (SomeStruct){ 10, 20, 30 };
	printf("some_stuct: first: %d, second: %d, flags: %d\n",
	       some_stuct.first, some_stuct.second, some_stuct.flags);
	MemoryZeroStruct(&some_stuct);
	printf("After MemoryZeroStruct(&some_stuct):\n");
	printf("some_stuct: first: %d, second: %d, flags: %d\n\n",
	       some_stuct.first, some_stuct.second, some_stuct.flags);

	printf("Before MemoryZeroArray:\n");
	for (S32 i = 0; i < ArrayCount(verts); ++i)
		printf("%f%c", verts[i], (((i+1)%4 == 0) ||
					  (i == ArrayCount(verts)-1)) ? '\n' : ' ');
	MemoryZeroArray(verts);
	printf("After:\n");
	for (S32 i = 0; i < ArrayCount(verts); ++i)
		printf("%f%c", verts[i], (((i+1)%4 == 0) ||
					  (i == ArrayCount(verts)-1)) ? '\n' : ' ');
	printf("\n");

	S32 dynamic_array_length = 10;
	U64 dynamic_array_size = dynamic_array_length*sizeof(S32);
	F32 *dynamic_array_first = malloc(dynamic_array_size);
	F32 *dynamic_array_second = malloc(dynamic_array_size);
	printf("first dynamic array:\n");
	for (S32 i = 0; i < dynamic_array_length; ++i) {
		F32 *element = dynamic_array_first+i;
		*element = i+420;
		printf("%f%c", *element, (i == dynamic_array_length-1) ? '\n' : ' ');
	}
	printf("second dynamic array:\n");
	for (S32 i = 0; i < dynamic_array_length; ++i) {
		F32 *element = dynamic_array_second+i;
		printf("%f%c", *element, (i == dynamic_array_length-1) ? '\n' : ' ');
	}
	MemoryCopy(dynamic_array_second, dynamic_array_first, dynamic_array_size);
	printf("After MemoryCopy(dynamic_array_second, dynamic_array_first, dynamic_array_size):\n");
	printf("second dynamic array:\n");
	for (S32 i = 0; i < dynamic_array_length; ++i) {
		F32 *element = dynamic_array_second+i;
		printf("%f%c", *element, (i == dynamic_array_length-1) ? '\n' : ' ');
	}
	S32 memory_match = MemoryMatch(dynamic_array_first, dynamic_array_second, dynamic_array_size);
	printf("And MemoryMatch(MemoryMatch(dynamic_array_first, dynamic_array_second, dynamic_array_size): %d\n\n", memory_match);
	free(dynamic_array_first);
	free(dynamic_array_second);

	printf("Linked Lists:\n");
	printf("Singly linked list: ");
	S32 list_length = 5;
	U64 list_size = list_length*sizeof(SLLNode);
	SLLNode *sllist = malloc(list_size);
	SLLNode *sllfirst = 0;
	SLLNode *slllast = 0;
	for (S32 i = 0; i < list_length; ++i) {
		SLLNode *new = sllist+i;
		new->val = i;
		SLLPush(sllfirst, slllast, new);
	}
	for (SLLNode *node = sllfirst; node; node = node->next)
		printf("%d%s", node->val, ((node->next) ? " -> " : "\n"));
	SLLPop(sllfirst, slllast);
	printf("After SLLPop(sllfirst, slllast): ");
	for (SLLNode *node = sllfirst; node; node = node->next)
		printf("%d%s", node->val, ((node->next) ? " -> " : "\n"));
	free(sllist);

	printf("Doubly linked list: ");
	list_length = 8;
	list_size = list_length*sizeof(DLLNode);
	DLLNode *dllist = malloc(list_size);
	DLLNode *dllfirst = 0;
	DLLNode *dlllast = 0;
	for (S32 i = 0; i < list_length; ++i) {
		DLLNode *new = dllist+i;
		new->val = i;
		DLLPushBack(dllfirst, dlllast, new);
	}
	for (DLLNode *node = dllfirst; node; node = node->next)
		printf("%d%s", node->val, ((node->next) ? " -> " : "\n"));
	printf("Remove odd numbers: ");
	for (DLLNode *node = dllfirst; node; node = node->next)
		if (node->val % 2 == 1)
			DLLRemove(dllfirst, dlllast, node);
	for (DLLNode *node = dllfirst; node; node = node->next)
		printf("%d%s", node->val, ((node->next) ? " -> " : "\n"));
	free(dllist);
	printf("\n");

	printf("I'm pretty sure the vectors are fine.\nAt least for the last few months :)\n\n");

	printf("Matrices:\n");
	printf("Identity:\n");
	Mat4 m = MAT4_IDENTITY;
	mat4print(m);
	m = mat4scale(m, v3a(10.0f));
	m = mat4translate(m, v3a(1.0f));
	m = mat4translate(m, v3(0.0f, 68.0f, 0.0f));
	mat4print(m);
	printf("Determinant: %f\n", mat4det(m));
	printf("Transpose:\n"); mat4print(mat4transpose(m));
	printf("mat4rotate(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f)):\n");
	mat4print(mat4rotate(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f)));

	printf("Using Arenas:\n");
	Arena *a = arena_alloc(0);
	SLLNode *first = 0;
	SLLNode *last = 0;
	S32 node_count = 10;
	for (S32 i = 0; i < node_count; ++i) {
		SLLNode *new = arena_push(a, sizeof(SLLNode));
		new->val = i;
		SLLPush(first, last, new);
	}
	for (SLLNode *node = first; node; node = node->next)
		printf("%d%s", node->val, ((node->next) ? " -> " : "\n"));
	arena_release(a);

	a = arena_alloc(sizeof(verts));
	S32 verts_count = ArrayCount(verts);
	F32 *dynamic_verts = arena_push(a, sizeof(verts));
	MemoryZero(dynamic_verts, verts_count*sizeof(F32));
	for (S32 i = 0; i < verts_count; ++i) {
		F32 *vert = dynamic_verts+i;
		printf("%f%c", *vert, (i == verts_count-1) ? '\n' : ' ');
	}
	arena_release(a);
	printf("\n");

	Arena *str_arena = arena_alloc(0);
	printf("Strings:\n");
	char *cstr = "This is a C str\n";
	Str8 str = str8_from_cstr(cstr);
	str = str8_chop_start(str, 10);
	cstr = str8_to_cstr(a, str);
	printf("%s", cstr);
	str8print(str);
	Str8 choped_str = str8_chop_end(str, 3);
	str8print(choped_str);
	printf("\n");
	Str8List *list = str8_list(str_arena);
	str8_list_push(str_arena, list, str, 0);
	str8_list_push(str_arena, list, str8_from_cstr("test"), 0);
	str8_list_push(str_arena, list, str8_from_cstr("and this is also a test\n"), 0);
	str8_list_push(str_arena, list, str8_from_cstr("Kinda works!"), 1);
	printf("Str8List: ");
	str8_list_print(list);
	arena_release(str_arena);
	str8_list_print(list);

	str_arena = arena_alloc(Kilobytes(10));
	memory_size = 512;
	U32 count = memory_size/sizeof(S32);
	S32 *some = arena_push(str_arena, memory_size);
	printf("str_arena(size): %ld\nMemory:\n", str_arena->cap);
	for (S32 i = 0; i < count; i++) {
		S32 *e = some+i;
		*e = 69;
		printf("%d: %d%c", i, *e, ((i == count-1) ? '\n' : ' '));
	}
	printf("Memory used: %lu\n", str_arena->used);
	printf("Remaining memory: %lu\n",
	       str_arena->cap-str_arena->used);

	arena_pop(str_arena, memory_size);

	printf("After arena pop\n");
	printf("Memory used: %lu\n", str_arena->used);
	printf("Remaining memory: %lu\n",
	       str_arena->cap-str_arena->used);
	printf("some ptr is %s\n", some ? "not null" : "null");
	printf("str_arena(size): %lu\nMemory:\n", str_arena->cap);

	Str8 new_str = str8_pushf(str_arena, "Test of the formatted string: %d\n", 69);
	str8print(new_str);

	list = str8_list(str_arena);
	str8_list_pushf(str_arena, list, 0, "This is a list %d", 34);
	str8_list_pushf(str_arena, list, 0, " of formatted strings: %d", 35);
	str8_list_pushf(str_arena, list, 1, "And you can push to the start");
	str8_list_print(list);

	arena_release(str_arena);

	a = arena_alloc(Megabytes(1));
	str8print(str8lit("build.sh content:\n"));
	Str8 file_content = str8_read_entire_file(a, str8lit("build.sh"));
	str8print(file_content);
	arena_release(a);

	return(0);
}