-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaderboy.c
110 lines (94 loc) · 2.59 KB
/
shaderboy.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
#include "shaderboy.h"
#include "game.h"
#include "common.h"
#include "render.h"
#include "tanto/r_geo.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <tanto/v_video.h>
#include <tanto/d_display.h>
#include <tanto/r_render.h>
#include <tanto/r_raytrace.h>
#include <tanto/m_math.h>
#include <tanto/t_utils.h>
#include <tanto/i_input.h>
#define NS_TARGET 16666666 // 1 / 60 seconds
#define NS_PER_S 1000000000
void shaderboy_Init(const char* shaderName)
{
tanto_v_config.rayTraceEnabled = false;
#ifndef NDEBUG
tanto_v_config.validationEnabled = true;
#else
tanto_v_config.validationEnabled = false;
#endif
SHADER_NAME = shaderName;
TANTO_WINDOW_WIDTH = 2000;
TANTO_WINDOW_HEIGHT = 2000;
tanto_d_Init(false);
printf("Display initialized\n");
tanto_v_Init();
printf("Video initialized\n");
tanto_v_InitSurfaceXcb(d_XcbWindow.connection, d_XcbWindow.window);
printf("Swapchain initialized\n");
tanto_r_Init();
printf("Renderer initialized\n");
tanto_i_Init();
printf("Input initialized\n");
tanto_i_Subscribe(g_Responder);
r_InitRenderer();
g_Init();
}
void shaderboy_StartLoop(void)
{
Tanto_Timer timer;
Tanto_LoopStats stats;
tanto_TimerInit(&timer);
tanto_LoopStatsInit(&stats);
parms.shouldRun = true;
parms.renderNeedsUpdate = false;
bool presentationSuccess = true;
for (int i = 0; i < TANTO_FRAME_COUNT; i++)
{
r_UpdateRenderCommands(i);
}
while( parms.shouldRun )
{
tanto_TimerStart(&timer);
tanto_i_GetEvents();
tanto_i_ProcessEvents();
g_Update();
if (parms.renderNeedsUpdate)
{
tanto_r_WaitOnQueueSubmit();
for (int8_t i = 0; i < TANTO_FRAME_COUNT; i++)
{
r_UpdateRenderCommands(i);
}
parms.renderNeedsUpdate = false;
}
else
{
int8_t frameIndex = tanto_r_RequestFrame();
if (frameIndex >= 0) // success
presentationSuccess = tanto_r_PresentFrame();
else
{
presentationSuccess = false;
printf("Failed to retrieve frame. Likely window resized\n");
}
}
if (!presentationSuccess)
r_RecreateSwapchain();
tanto_TimerStop(&timer);
tanto_LoopStatsUpdate(&timer, &stats);
//printf("Delta ns: %ld\n", stats.nsDelta);
tanto_LoopSleep(&stats, NS_TARGET);
}
}