1+ import std;
2+
3+ #include < SDL3/SDL.h>
4+ #include < bgfx/bgfx.h>
5+ #include < bx/math.h>
6+
7+ import core.filesystem;
8+
9+ import rendering.rhi;
10+ import rendering.rhi.vertex;
11+
12+ int main (int argc, char * argv[])
13+ {
14+ if (!SDL_Init (SDL_INIT_VIDEO)) {
15+ std::println (" SDL init failed: {}" , SDL_GetError ());
16+ return -1 ;
17+ }
18+
19+ SDL_Window* window = SDL_CreateWindow (
20+ " Draconic Engine" ,
21+ 1280 , 720 ,
22+ SDL_WINDOW_RESIZABLE
23+ );
24+
25+ if (!window) {
26+ std::println (" Failed to create window: {}" , SDL_GetError ());
27+ return -1 ;
28+ }
29+
30+ const char * driver = SDL_GetCurrentVideoDriver ();
31+ std::println (" Driver: {}" , driver ? driver : " Unknown" );
32+
33+ void * nwh = nullptr ; // Native window handle
34+ void * ndt = nullptr ; // Native display type
35+
36+ #if defined(__linux__)
37+
38+ SDL_PropertiesID props = SDL_GetWindowProperties (window);
39+
40+ if (driver && std::string_view (driver) == " x11" )
41+ {
42+ ndt = SDL_GetPointerProperty (props, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, nullptr ); // Get the X11 display pointer
43+ nwh = (void *)(uintptr_t )SDL_GetNumberProperty (props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0 ); // Get the X11 window number and cast it to a pointer
44+ }
45+
46+ #endif
47+
48+ if (!nwh) {
49+ std::println (" Failed to get native window handle" );
50+ return -1 ;
51+ }
52+
53+ if (!ndt) {
54+ std::println (" Failed to get native display type" );
55+ return -1 ;
56+ }
57+
58+ // Init the RHI with the native window handle and initial size
59+ if (!draco::rhi::init (ndt, nwh, 1280 , 720 )) {
60+ std::println (" Failed to initialize RHI" );
61+ SDL_DestroyWindow (window);
62+ SDL_Quit ();
63+ return -1 ;
64+ }
65+
66+ // Geometry data for a triangle to test rendering
67+ // It includes both positions & colors
68+ draco::rhi::PosColorVertex triangle[] = {
69+ { 0 .0f , 0 .5f , 0 .0f , 0xff0000ff },
70+ { 0 .5f , -0 .5f , 0 .0f , 0xff00ff00 },
71+ { -0 .5f , -0 .5f , 0 .0f , 0xffff0000 }
72+ };
73+
74+ auto vbh = draco::rhi::create_vertex_buffer (triangle, sizeof (triangle));
75+
76+ // Load the vertex & fragment shaders
77+ auto vs_data = draco::filesystem::load_binary (" vs_triangle.bin" );
78+ auto fs_data = draco::filesystem::load_binary (" fs_triangle.bin" );
79+
80+ // If the path is empty, return an error
81+ if (vs_data.empty () || fs_data.empty ()) {
82+ std::println (" Failed to load shaders" );
83+ std::println (" Workin' dir: {}" , std::filesystem::current_path ().string ());
84+ return -1 ;
85+ }
86+
87+ auto vsh = draco::rhi::create_shader (vs_data.data (), (uint32_t )vs_data.size ());
88+ auto fsh = draco::rhi::create_shader (fs_data.data (), (uint32_t )fs_data.size ());
89+
90+
91+ // TODO: Expose our own macros for the state flags instead of using bgfx's directly, tis is just for testin'
92+ auto pipeline = draco::rhi::create_pipeline ({vsh, fsh, (0
93+ | BGFX_STATE_WRITE_RGB
94+ | BGFX_STATE_WRITE_A
95+ | BGFX_STATE_MSAA
96+ | BGFX_STATE_PT_TRISTRIP)});
97+
98+ bool running = true ;
99+
100+ while (running)
101+ {
102+ SDL_Event event;
103+ while (SDL_PollEvent (&event))
104+ {
105+ if (event.type == SDL_EVENT_QUIT)
106+ running = false ;
107+ }
108+
109+ int w, h;
110+ SDL_GetWindowSize (window, &w, &h);
111+
112+ draco::rhi::resize (uint16_t (w), uint16_t (h));
113+
114+ draco::rhi::begin_frame ();
115+
116+ draco::rhi::RenderPacket packet{};
117+ packet.vertex_buffer = vbh;
118+ packet.pipeline = pipeline;
119+ bx::mtxIdentity (packet.model );
120+
121+ draco::rhi::submit (packet, 0 );
122+
123+ draco::rhi::end_frame ();
124+ }
125+
126+ draco::rhi::shutdown ();
127+
128+ SDL_DestroyWindow (window);
129+ SDL_Quit ();
130+ return 0 ;
131+ }
132+
133+ // Fun fact: AR literally went mad & tis is the result
0 commit comments