-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
169 lines (142 loc) · 4.55 KB
/
main.cpp
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
#include "pixie.h"
#include "font.h"
#include "imgui.h"
#include <string.h>
#include <stdio.h>
#include <algorithm>
static const TCHAR* WindowTitle = TEXT("Hello, World!");
static const int WindowWidth = 640;
static const int WindowHeight = 400;
static void draw(int x, int y, uint32_t* pixels)
{
for (int i = x; i < x+4; i++)
{
for (int j = y; j < y+4; j++)
{
if (i < WindowWidth && j < WindowHeight)
{
int index = (i + (j * WindowWidth));
pixels[index] = MAKE_RGB(0, 0, 255);
}
}
}
}
int main(int argc, char** argv)
{
Pixie::Font font;
if (!font.Load("font.bmp", 9, 16))
{
#if PIXIE_PLATFORM_WIN
MessageBox(NULL, TEXT("Failed to load font.bmp"), TEXT("Pixie Error"), MB_ICONERROR);
#else
printf("pixie: failed to load font.bmp\n");
#endif
return 0;
}
Pixie::Window window;
if (!window.Open(WindowTitle, WindowWidth, WindowHeight, true))
return 0;
uint32_t* pixels = window.GetPixels();
const float SPEED = 100.0f;
float x = 0, y = 0;
float xadd = SPEED, yadd = SPEED;
char buf[16] = { 0 };
strcat_s(buf, sizeof(buf), "Hello, World!");
while (!window.HasKeyGoneUp(Pixie::Key_Escape))
{
Pixie::ImGui::Begin(&window, &font);
float delta = window.GetDelta();
x += xadd*delta;
y += yadd*delta;
if (x >= WindowWidth - 1)
{
x = WindowWidth - 1;
xadd = -SPEED;
}
else if (x < 0)
{
x = 0;
xadd = SPEED;
}
if (y >= WindowHeight - 1)
{
y = WindowHeight - 1;
yadd = -SPEED;
}
else if (y < 0)
{
y = 0;
yadd = SPEED;
}
memset(pixels, 0, WindowWidth * WindowHeight * sizeof(uint32_t));
int cx = 0, cy = 0;
for (int i = 0; i < 256; i++)
{
char buf[128];
sprintf_s(buf, sizeof(buf), "%c", i);
if (cx >= WindowWidth-9)
{
cx = 0;
cy += 16;
}
font.Draw(buf, cx, cy, &window);
cx += 9;
}
{
char buf[128];
sprintf_s(buf, sizeof(buf), "%.4f", window.GetTime());
font.Draw(buf, 10, 90, &window);
}
draw((int)x, (int)y, pixels);
Pixie::ImGui::FilledRect(10, 240, 100, 100, MAKE_RGB(255, 0, 0), MAKE_RGB(128, 0, 0));
if (Pixie::ImGui::Button("Hello", 100, 100, 100, 30))
strcpy_s(buf, sizeof(buf), "Hello, World!");
if (Pixie::ImGui::Button("Goodbye", 100, 140, 100, 30))
strcpy_s(buf, sizeof(buf), "Goodbye, World!");
Pixie::ImGui::Input(buf, sizeof(buf), 100, 180, 400, 20);
static bool checked = false;
checked = Pixie::ImGui::Checkbox("Do the thing", checked, 100, 210);
static int selection = 0;
if (Pixie::ImGui::RadioButton("Banana", selection == 0, 300, 210))
selection = 0;
if (Pixie::ImGui::RadioButton("Apple", selection == 1, 300, 230))
selection = 1;
if (Pixie::ImGui::RadioButton("Pear", selection == 3, 300, 250))
selection = 3;
for (int i = 0; i < Pixie::MouseButton_Num; i++)
{
if (window.IsMouseDown((Pixie::MouseButton)i))
{
Pixie::ImGui::FilledRect((i*33) + 240, 280, 32, 32, MAKE_RGB(255, 0, 0), MAKE_RGB(255, 0, 0));
}
else
{
Pixie::ImGui::Rect((i*33) + 240, 280, 32, 32, MAKE_RGB(255, 0, 0));
}
}
static float accumTime = 0.0f;
static int numFrames = 0;
static float avgFrameTime = 0.0f;
numFrames++;
accumTime += delta;
if (numFrames == 16)
{
avgFrameTime = accumTime / (float)numFrames;
numFrames = 0;
accumTime = 0.0f;
}
int fpsWidth = std::min(WindowWidth, (int)((avgFrameTime*20.0f)*WindowWidth));
Pixie::ImGui::FilledRect(0, 0, fpsWidth, 10, MAKE_RGB(255, 0, 0), MAKE_RGB(255, 0, 0));
Pixie::ImGui::FilledRect((int)((1.0f/60.0f)*20.0f*WindowWidth), 0, 2, 10, MAKE_RGB(0, 255, 0), MAKE_RGB(0, 255, 0));
{
char buf[128];
sprintf_s(buf, sizeof(buf), "%.2f fps", 1.0f / avgFrameTime);
font.Draw(buf, 10, 106, &window);
}
Pixie::ImGui::End();
if (!window.Update())
break;
}
window.Close();
printf("done\n");
}