first git commit: scene, object, mesh, texture, shader, material etc
[demo] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <vector>
5
6 #include "opengl/opengl.h"
7 #include "vulkan/vk.h"
8
9 /* static functions */
10
11 static bool init();
12 static void cleanup();
13 static void display();
14
15 /* glfw callbacks */
16 static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods);
17
18 /* global variables */
19 bool use_vulkan;
20 GLFWwindow *win;
21
22 /* variables */
23
24 int main(int argc, char **argv)
25 {
26         for(int i=0; i<argc; ++i) {
27                 if(strcmp(argv[i], "-opengl") == 0) {
28                         use_vulkan = false;
29                         printf("Backend: OpenGL.\n");
30                 }
31                 else if(strcmp(argv[i], "-vulkan") == 0) {
32                         use_vulkan = true;
33                         printf("Backend: Vulkan.\n");
34                 }
35                 else {
36                         printf("No backend specified. Using OpenGL.\n");
37                 }
38         }
39
40         if(!init()) {
41                 fprintf(stderr, "Failed to initialize program.\n");
42                 return 1;
43         }
44
45         glfwSetKeyCallback(win, key_clbk);
46
47         while(!glfwWindowShouldClose(win)) {
48                 display();
49
50                 glfwSwapBuffers(win);
51                 glfwPollEvents();
52         }
53
54         atexit(cleanup);
55         return 0;
56 }
57
58 static bool init()
59 {
60         if(use_vulkan) {
61                 if(!init_vulkan())
62                         return false;
63         }
64         else {
65                 if(!init_opengl())
66                         return false;
67         }
68
69         return true;
70 }
71
72 static void cleanup()
73 {
74         if(use_vulkan) {
75                 cleanup_vulkan();
76         }
77         else {
78                 cleanup_opengl();
79         }
80 }
81
82 static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods)
83 {
84         if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
85                 glfwSetWindowShouldClose(win, GLFW_TRUE);
86         }
87 }
88
89 static void display()
90 {
91         if(use_vulkan) {
92                 display_vulkan();
93         }
94         else {
95                 display_opengl();
96         }
97 }