Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
133 lines (109 loc) · 4.24 KB
/
Copy pathmain.cpp
File metadata and controls
133 lines (109 loc) · 4.24 KB
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
#include "Data.h"
#include <conio.h>
#include <thread>
#include <chrono>
#define VERIFY(x) if (!Verify(x, #x, __FILE__, __LINE__)) return 1;
bool Verify(bool value, const char* cond, const char* fileName, int line)
{
if (value)
return true;
printf("Condition failed (%s %i): %s\n", fileName, line, cond);
return false;
}
int main(int argc, char** argv)
{
Data data;
if (!data.LoadFromFile("packed/Data.bin"))
{
printf("Could not load packed/Data.bin\n");
return 1;
}
// Characters
VERIFY(data.GetCharacterCount() == 3);
Data::CharacterRecord char0 = data.GetCharacter(0);
VERIFY(!strcmp(char0.Get().name.ptr, "Count Tyrone Rugen"));
VERIFY(char0.Get().inventory_count == 1);
VERIFY(char0.Get().inventory.ptr[0].ptr != nullptr);
VERIFY(!strcmp(char0.Get().inventory.ptr[0].ptr->name.ptr, "6 Fingers"));
Data::CharacterRecord char1 = data.GetCharacter(1);
VERIFY(!strcmp(char1.Get().name.ptr, "Dread Pirate Roberts"));
VERIFY(char1.Get().inventory_count == 2);
VERIFY(char1.Get().inventory.ptr[0].ptr != nullptr);
VERIFY(char1.Get().inventory.ptr[1].ptr != nullptr);
VERIFY(!strcmp(char1.Get().inventory.ptr[0].ptr->name.ptr, "Black Mask"));
VERIFY(!strcmp(char1.Get().inventory.ptr[1].ptr->name.ptr, "True Love"));
Data::CharacterRecord char2 = data.GetCharacter(2);
VERIFY(!strcmp(char2.Get().name.ptr, "Inigo Montoya"));
VERIFY(char2.Get().inventory_count == 1);
VERIFY(char2.Get().inventory.ptr[0].ptr != nullptr);
VERIFY(!strcmp(char2.Get().inventory.ptr[0].ptr->name.ptr, "Father's Sword"));
// Items
VERIFY(data.GetItemCount() == 4);
Data::ItemRecord item0 = data.GetItem(0);
VERIFY(!strcmp(item0.Get().name.ptr, "6 Fingers"));
VERIFY(item0.Get().value == 0.0f);
Data::ItemRecord item1 = data.GetItem(1);
VERIFY(!strcmp(item1.Get().name.ptr, "Black Mask"));
VERIFY(item1.Get().value == 10.0f);
Data::ItemRecord item2 = data.GetItem(2);
VERIFY(!strcmp(item2.Get().name.ptr, "Father's Sword"));
VERIFY(item2.Get().value == 1000.0f);
Data::ItemRecord item3 = data.GetItem(3);
VERIFY(!strcmp(item3.Get().name.ptr, "True Love"));
VERIFY(item3.Get().value == 10.0f);
if (argc >= 2 && !strcmp(argv[1], "-test"))
{
printf("All checks passed!\n");
return 0;
}
printf("Hot reloading is watching for updates. Edit and recompile source data. Press Q to exit.\n\n");
bool showData = true;
while (true)
{
if (_kbhit())
{
int ch = _getch();
if (ch == 'q')
break;
}
if (data.Tick())
{
showData = true;
printf("\nData Changed! Press Q to exit.\n\n");
}
if (showData)
{
showData = false;
// Show character data
uint32_t characterCount = data.GetCharacterCount();
printf("%u Characters:\n", characterCount);
for (uint32_t i = 0; i < characterCount; ++i)
{
Data::CharacterRecord record = data.GetCharacter(i);
uint32_t inventoryCount = record.Get().inventory_count;
printf(" [%u] %s has %u items\n", i, record.Get().name.ptr, inventoryCount);
for (uint32_t j = 0; j < inventoryCount; ++j)
{
const char* itemName = nullptr;
if (record.Get().inventory.ptr[j].ptr)
itemName = record.Get().inventory.ptr[j].ptr->name.ptr;
printf(" [%u] %s\n", j, itemName);
}
}
// Show item data
uint32_t itemCount = data.GetItemCount();
printf("%u Items:\n", itemCount);
for (uint32_t i = 0; i < itemCount; ++i)
{
Data::ItemRecord record = data.GetItem(i);
const Data::Item& item = record.Get();
printf(" [%u] %s %f\n", i, item.name.ptr, item.value);
}
}
// Make this thread sleep for 250 milliseconds.
// Hot reloading checks for changes 4 times a second.
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
printf("\nAs you wish!\n");
return 0;
}