-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootapp.cpp
More file actions
127 lines (123 loc) · 5.42 KB
/
bootapp.cpp
File metadata and controls
127 lines (123 loc) · 5.42 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
#include "bootapp.h"
using namespace app;
using namespace cat;
// ----------------------------------------------------------------------------
BootApp::BootApp() {
m_counter = 0;
}
// ----------------------------------------------------------------------------
BootApp::~BootApp() {
}
// ----------------------------------------------------------------------------
// App Lifecycle: cb_startup -> cb_resume <-> cb_pause -> cb_shutdown
// ----------------------------------------------------------------------------
// cb_startup is called upon kernel->run(app)
// ----------------------------------------------------------------------------
bool BootApp::cb_startup(Timestamp now) {
Logger::d("App", "cb_startup");
return true;
}
// cb_resume is called when the program has resumed
// ----------------------------------------------------------------------------
void BootApp::cb_resume() {
Logger::d("App", "cb_resume");
// Http Test: simple request
{
HttpRequest req("https://httpbin.org/post");
req.add_header("foo", "bar");
req.add_header("foo2", "dumb");
req.post("Post Data", "text/plain; charset=utf-8");
auto http_id = kernel()->net()->http_fetch(std::move(req), [](HttpResponse&& res) -> void {
for (auto it = res.headers.begin(); it != res.headers.end(); ++it) {
Logger::d("App", "http -> header = %s:%s", it->first.c_str(), it->second.c_str());
}
Logger::d("App", "http -> %d - %s", res.code, res.body.ptr());
});
// kernel()->net()->http_cancel(http_id);
}
// Http Test: json request
{
HttpRequest req("https://httpbin.org/post");
req.add_header("foo", "bar");
req.add_header("foo2", "dumb");
req.post("{ \"foo\": \"bar\" }", "application/json");
auto http2_id = kernel()->net()->http_fetch(std::move(req), [this](HttpResponse&& res) -> void {
for (auto it = res.headers.begin(); it != res.headers.end(); ++it) {
Logger::d("App", "http -> header = %s:%s", it->first.c_str(), it->second.c_str());
}
kernel()->vfs()->write("/doc/http.txt", res.body);
Logger::d("App", "http -> %d - %s", res.code, res.body.ptr());
});
}
}
// cb_pause is called when the program is going background
// ----------------------------------------------------------------------------
void BootApp::cb_pause() {
Logger::d("App", "cb_pause");
}
// cb_shutdown is called after app->exit()
// ----------------------------------------------------------------------------
void BootApp::cb_shutdown(Timestamp now) {
Logger::d("App", "cb_shutdown");
}
// ----------------------------------------------------------------------------
// OpenGL Context. Everything retained from ResourceManager is managed and
// automatically restored upon cb_context_restored()
// You only need to handle your own resources here.
// ----------------------------------------------------------------------------
// cb_context_lost is called when the GL context is lost
// you should release any manual created gfx resources here.
// resources retained by resource manager will be auto reloaded by the kernel.
// ----------------------------------------------------------------------------
void BootApp::cb_context_lost() {
Logger::d("App", "cb_context_lost");
}
// cb_context_restored is called when the GL context is restored
// you should reload any manual created gfx resources here.
// resources retained by resource manager will be auto reloaded by the kernel.
// ----------------------------------------------------------------------------
bool BootApp::cb_context_restored() {
Logger::d("App", "cb_context_restored");
return true;
}
// cb_resize is called when the screen is resized, you may adjust ui scale here
// ----------------------------------------------------------------------------
void BootApp::cb_resize(int width, int height) {
int preferredW, preferredH;
switch (kernel()->platform()) {
case Platform::Windows: preferredW = 1280; preferredH = 720; break;
case Platform::Mac: preferredW = 1280; preferredH = 720; break;
case Platform::IOS: preferredW = 512; preferredH = 960; break;
case Platform::Android: preferredW = 512; preferredH = 960; break;
default: preferredW = 512; preferredH = 960;
}
float scaleX = (float)width / preferredW;
float scaleY = (float)height / preferredH;
kernel()->ui()->scale(scaleX<scaleY ? scaleX : scaleY);
}
// cb_render is called in the render pipeline
// ----------------------------------------------------------------------------
void BootApp::cb_render(Renderer* r, Timestamp now) {
Rect2i rect;
TextStyle style;
rect.set(10, 10, 100, 40);
style.color = 0xffff00ff;
r->draw2d.drawtext(rect, "Hello", style);
}
// ----------------------------------------------------------------------------
bool BootApp::cb_timer(Timestamp now, int msg) {
Logger::d("App", "cb_timer: %d - time: %llu", msg, now);
switch (msg) {
case 1:
kernel()->time()->post_timer(this, 1, 1000);
break;
case 2:
m_counter ++;
if (m_counter < 5) {
kernel()->time()->post_timer(this, 2, 2000);
} else {
kernel()->time()->remove_timer(this, 1);
} break;
} return true;
}
// ----------------------------------------------------------------------------