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
|
#include "konwinlib.h"
#include "konsofren.h"
#include "kontime.h"
#ifdef KON_MODULE_DEBUG
#include "konassets.h"
#include "konfont.h"
#include "kondebug.h"
#endif
#define GAME_WIDTH 320
#define GAME_HEIGHT 180
typedef struct app {
kon_window_t *window;
kon_display_t *display;
kon_camera_t camera;
kon_material_t material;
kon_light_t light;
kon_time last;
float angle;
#ifdef KON_MODULE_DEBUG
kon_assets_t *assets;
kon_font_t *font;
kon_debug_t *debug;
#endif
} app_t;
static void frame(void *data)
{
app_t *app = (app_t *)data;
kon_display_t *display = app->display;
kon_event_t ev;
kon_time now;
kon_mat4_t model, viewProjection;
while (kon_pollEvent(app->window, &ev)) {
#ifdef KON_MODULE_DEBUG
if (kon_debugFeedEvent(app->debug, &ev)) continue;
#endif
if (ev.type == KON_EVENT_RESIZE) {
kon_resizeDisplay(display, ev.width, ev.height);
}
if (ev.type == KON_EVENT_KEY_DOWN && ev.key == KON_KEY_O) {
app->camera.projection = (app->camera.projection == KON_PROJECTION_PERSPECTIVE)
? KON_PROJECTION_ORTHOGRAPHIC : KON_PROJECTION_PERSPECTIVE;
}
}
now = kon_getTime();
app->angle += (float)(now - app->last);
app->last = now;
model = kon_mat4Identity();
model = kon_mat4Rotate(model, KON_VEC3(app->angle * 0.5f, app->angle, 0));
viewProjection = kon_cameraViewProjection(&app->camera, display->game->width, display->game->height);
kon_clearFramebuffer(display->game, KON_BACKGROUND_COLOR);
kon_drawMesh(display->game, viewProjection, model, &kon_cubeMesh, &app->material, &app->light, KON_RENDER_DEFAULT);
kon_presentDisplay(display);
#ifdef KON_MODULE_DEBUG
kon_debugFrame(app->debug);
if (kon_debugQuitRequested(app->debug)) kon_setShouldClose(app->window, true);
kon_debugDraw(app->debug, display->screen);
#endif
kon_blitPixels(app->window, display->screen->data, display->screen->width, display->screen->height);
kon_sleep(0.008);
}
int main(void)
{
app_t app = {0};
if (!kon_init()) return 1;
app.window = kon_createWindow("engine", 0, 0, 800, 600, KON_WINDOW_CENTER | KON_WINDOW_RESIZABLE);
if (!app.window) return 1;
#ifdef KON_DEVELOPER
kon_setExitKey(app.window, KON_KEY_F10);
#else
kon_setExitKey(app.window, KON_KEY_ESCAPE);
#endif
app.display = kon_createDisplay(GAME_WIDTH, GAME_HEIGHT, 800, 600);
if (!app.display) return 1;
app.camera = kon_cameraDefault();
app.camera.position = KON_VEC3(3, 3, 6);
app.material = kon_materialColor(0xFFC8C8FF);
app.light = kon_lightDefault();
app.last = kon_getTime();
#ifdef KON_MODULE_DEBUG
app.assets = kon_assetsCreate(NULL, "assets");
{
kon_blob_t *ttf = kon_assetBlob(app.assets, "font.ttf");
if (ttf) app.font = kon_loadFont(ttf->data, ttf->size, 14.0f);
app.debug = kon_debugCreate(app.font);
kon_debugSetAssets(app.debug, app.assets);
kon_debugLog(app.debug, ttf ? "loaded font.ttf" : "put a font.ttf in assets/ for the console");
}
#endif
kon_runMainLoop(app.window, frame, &app);
#ifdef KON_MODULE_DEBUG
kon_debugDestroy(app.debug);
kon_freeFont(app.font);
kon_assetsDestroy(app.assets);
#endif
kon_freeDisplay(app.display);
kon_destroyWindow(app.window);
kon_deinit();
return 0;
}
|