forked from memononen/nanovg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_sokol.c
128 lines (114 loc) · 3.46 KB
/
example_sokol.c
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
#include <sokol_app.h>
#include <sokol_gfx.h>
#include <sokol_log.h>
#include <sokol_glue.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#define NANOVG_SOKOL_IMPLEMENTATION 1
#include <nanovg_sokol.h>
#include "frequency.h"
#include "demo.h"
typedef struct State {
sg_pass_action pass_action;
NVGcontext *vg;
DemoData data;
float scaleFactor;
float mouse[2];
} State;
static void init(void* ptr) {
State* state = (State*)ptr;
state->scaleFactor = sapp_dpi_scale();
nvgFrequencyInitTimer();
sg_setup(&(sg_desc){
// .context = sapp_sgcontext(),
.environment = sglue_environment(),
.logger.func = slog_func,
.pipeline_pool_size = 1024,
});
state->vg = nvgCreateSokol(NVG_ANTIALIAS|NVG_STENCIL_STROKES);
assert(state->vg != NULL);
// NVGcolor bgColor = nvgRGBA(0xef, 0xe6, 0xc7, 255);
NVGcolor bgColor = nvgRGBAf(0.3f, 0.3f, 0.32f, 1.0f);
state->pass_action = (sg_pass_action) {
.colors[0] = {
.load_action=SG_LOADACTION_CLEAR,
.clear_value={bgColor.r, bgColor.g, bgColor.b, bgColor.a},
}
};
loadDemoData(state->vg, &state->data);
}
static void demo1(State* state) {
NVGcontext *vg = state->vg;
nvgBeginPath(vg);
nvgRect(vg, 100, 100, 300, 150);
nvgFillColor(vg, nvgRGBAf(1, 0, 0, 0.5));
nvgFill(vg);
nvgClosePath(vg);
}
static void demo3(State* state) {
NVGcontext *vg = state->vg;
nvgScale(state->vg, state->scaleFactor, state->scaleFactor);
nvgTranslate(vg, 50, 50);
nvgRotate(vg, nvgDegToRad(5));
nvgBeginPath(vg);
nvgRect(vg, -10,-10,120,80);
nvgFillColor(vg, nvgRGBA(255,0,0,255));
nvgFill(vg);
}
static void demo2(State* state) {
double t = nvgFrequencyGetTime();
nvgScale(state->vg, state->scaleFactor, state->scaleFactor);
renderDemo(state->vg, state->mouse[0], state->mouse[1], sapp_width()/2, sapp_height()/2, t, 0, &state->data);
}
static void frame(void* ptr) {
State* state = (State*)ptr;
// sg_begin_default_pass(&state->pass_action, sapp_width(), sapp_height());
sg_begin_pass(&(sg_pass){ .action = state->pass_action, .swapchain = sglue_swapchain() });
nvgBeginFrame(state->vg, sapp_width(), sapp_height(), state->scaleFactor);
demo2(state);
nvgEndFrame(state->vg);
sg_end_pass();
sg_commit();
}
static void cleanup(void* ptr) {
State* state = (State*)ptr;
nvgDeleteSokol(state->vg);
state->vg = NULL;
sg_shutdown();
free(state);
}
static void event(const sapp_event* e, void* ptr) {
State* state = (State*)ptr;
int x = 0;
int y = 0;
switch (e->type) {
case SAPP_EVENTTYPE_KEY_DOWN:
if (e->key_code == SAPP_KEYCODE_Q) {
sapp_request_quit();
}
break;
case SAPP_EVENTTYPE_MOUSE_MOVE:
state->mouse[0] = e->mouse_x / state->scaleFactor;
state->mouse[1] = e->mouse_y / state->scaleFactor;
break;
}
}
sapp_desc sokol_main(int argc, char* argv[]) {
State* app = (State*)malloc(sizeof(State));
memset(app, 0, sizeof(State));
return (sapp_desc){
.init_userdata_cb = init,
.frame_userdata_cb = frame,
.cleanup_userdata_cb = cleanup,
.event_userdata_cb = event,
.user_data = app,
.width = 800,
.height = 600,
.window_title = "NanoVG sokol",
.logger.func = slog_func,
.high_dpi = true,
.swap_interval = 1,
.win32_console_utf8 = true,
};
}