git.haldean.org ana / b1b55c9
everything's changing haldean 8 months ago
6 changed file(s) with 95 addition(s) and 94 deletion(s). Raw diff Collapse all Expand all
1616 static ana::viewport viewport;
1717
1818 void error_callback(int error, const char* description) {
19 std::cerr << "[E] glfw error: " << description << std::endl;
19 std::cerr << "[E] glfw error: " << description << std::endl;
2020 }
2121
2222 void scroll_callback(GLFWwindow *w, double xoff, double yoff) {
23 ImGuiIO &io = ImGui::GetIO();
24 if (!io.WantCaptureMouse && yoff != 0) {
25 viewport.view_step(yoff);
26 }
23 ImGuiIO &io = ImGui::GetIO();
24 if (!io.WantCaptureMouse && yoff != 0) {
25 viewport.view_step(yoff);
26 }
2727 }
2828
2929 void mouse_button_callback(GLFWwindow *w, int button, int action, int mods) {
30 ImGuiIO &io = ImGui::GetIO();
31 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS && !io.WantCaptureMouse) {
32 std::cout << "start" << std::endl;
33 }
30 ImGuiIO &io = ImGui::GetIO();
31 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS && !io.WantCaptureMouse) {
32 std::cout << "start" << std::endl;
33 }
3434 }
3535
3636 void framebuffer_size_callback(GLFWwindow* w, int width, int height) {
37 glViewport(0, 0, width, height);
38 viewport.set_aspect(width, height);
39 glfwSwapBuffers(w);
37 printf("size callback %d %d\n", width, height);
38 glViewport(0, 0, width, height);
39 viewport.set_aspect(width, height);
40 glfwSwapBuffers(w);
4041 }
4142
4243 int main(void) {
43 if (!glfwInit()) {
44 std::cerr << "[E] init glfw" << std::endl;
45 return EXIT_FAILURE;
46 }
47 glfwSetErrorCallback(error_callback);
44 if (!glfwInit()) {
45 std::cerr << "[E] init glfw" << std::endl;
46 return EXIT_FAILURE;
47 }
48 glfwSetErrorCallback(error_callback);
4849
49 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
50 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
51 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
52 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
50 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
51 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
52 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
53 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
5354
54 GLFWwindow *window = glfwCreateWindow(800, 800, "ana", NULL, NULL);
55 if (!window) {
56 std::cerr << "[E] failed to create window" << std::endl;
57 return EXIT_FAILURE;
58 }
55 GLFWwindow *window = glfwCreateWindow(
56 viewport.width, viewport.height, "ana", NULL, NULL);
57 if (!window) {
58 std::cerr << "[E] failed to create window" << std::endl;
59 return EXIT_FAILURE;
60 }
5961
60 glfwMakeContextCurrent(window);
61 GLenum glew_status = glewInit();
62 if (glew_status != GLEW_OK) {
63 std::cerr << "[E] glewInit: " << glewGetErrorString(glew_status) << std::endl;
64 return EXIT_FAILURE;
65 }
62 glfwMakeContextCurrent(window);
63 GLenum glew_status = glewInit();
64 if (glew_status != GLEW_OK) {
65 std::cerr << "[E] glewInit: " << glewGetErrorString(glew_status) << std::endl;
66 return EXIT_FAILURE;
67 }
6668
67 glfwSetMouseButtonCallback(window, mouse_button_callback);
68 glfwSetScrollCallback(window, scroll_callback);
69 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
69 glfwSetMouseButtonCallback(window, mouse_button_callback);
70 glfwSetScrollCallback(window, scroll_callback);
71 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
7072
71 if (!ana::shaders::init())
72 return EXIT_FAILURE;
73 if (!ana::shaders::init())
74 return EXIT_FAILURE;
7375
74 glEnable(GL_BLEND);
75 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
76 glEnable(GL_BLEND);
77 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
7678
77 ana::nodeset nodes;
78 nodes.nodes.push_back(ana::node{glm::vec2(0, 0), glm::vec2(0.1, 0.2), "hello"});
79 nodes.nodes.push_back(ana::node{glm::vec2(-0.5, -0.5), glm::vec2(0.2, 0.2), "hello"});
79 ana::nodeset nodes;
80 nodes.nodes.push_back(ana::node("track one"));
81 nodes.nodes.push_back(ana::node("track two"));
82 nodes.nodes.push_back(ana::node("track three"));
83 nodes.nodes.push_back(ana::node("track four"));
84 nodes.nodes.push_back(ana::node("track five"));
85 nodes.nodes.push_back(ana::node("track six"));
86 nodes.nodes.push_back(ana::node("track seven"));
87 nodes.nodes.push_back(ana::node("track eight"));
88 nodes.nodes.push_back(ana::node("track nine"));
89 nodes.nodes.push_back(ana::node("track ten"));
8090
81 ImGui::CreateContext();
82 ana::ui::init_imgui();
91 ImGui::CreateContext();
92 ana::ui::init_imgui();
8393
84 ImGui_ImplGlfw_InitForOpenGL(window, true);
85 ImGui_ImplOpenGL3_Init("#version 410");
94 ImGui_ImplGlfw_InitForOpenGL(window, true);
95 ImGui_ImplOpenGL3_Init("#version 410");
8696
87 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
97 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
8898
89 while (!glfwWindowShouldClose(window)) {
90 glfwPollEvents();
99 while (!glfwWindowShouldClose(window)) {
100 glfwPollEvents();
91101
92 ImGui_ImplOpenGL3_NewFrame();
93 ImGui_ImplGlfw_NewFrame();
94 ImGui::NewFrame();
102 ImGui_ImplOpenGL3_NewFrame();
103 ImGui_ImplGlfw_NewFrame();
104 ImGui::NewFrame();
95105
96 ImGui::ShowDemoWindow();
106 ImGui::SetNextWindowPos(ImVec2(0, 0));
107 ImGui::SetNextWindowSize(ImVec2(viewport.width, viewport.height));
108 ImGui::Begin(
109 "main",
110 nullptr,
111 ImGuiWindowFlags_NoMove |
112 ImGuiWindowFlags_NoResize |
113 ImGuiWindowFlags_NoDecoration);
97114
98 /*
99 SDL_Event ev;
100 while (SDL_PollEvent(&ev)) {
101 ImGui_ImplSDL2_ProcessEvent(&ev);
102 switch (ev.type) {
103 case SDL_WINDOWEVENT:
104 if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
105 glViewport(0, 0, ev.window.data1, ev.window.data2);
106 viewport.set_aspect(ev.window.data1, ev.window.data2);
115 for (const ana::node &n : nodes.nodes) {
116 ImGui::Text(n.name.c_str());
107117 }
108 break;
109118
110 case SDL_MOUSEMOTION:
111 if (!io.WantCaptureMouse && ev.motion.state & SDL_BUTTON_MMASK) {
112 viewport.translate(-ev.motion.xrel, ev.motion.yrel);
113 }
114 break;
115 }
119 ImGui::End();
120
121 ImGui::Render();
122
123 glClear(GL_COLOR_BUFFER_BIT);
124 ana::shaders::draw(viewport, nodes);
125 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
126 glfwMakeContextCurrent(window);
127 glfwSwapBuffers(window);
116128 }
117 */
118 ImGui::Render();
119129
120 glClear(GL_COLOR_BUFFER_BIT);
121 ana::shaders::draw(viewport, nodes);
122 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
123 glfwMakeContextCurrent(window);
124 glfwSwapBuffers(window);
125 }
126
127 ana::shaders::destroy();
128 ImGui_ImplOpenGL3_Shutdown();
129 ImGui_ImplGlfw_Shutdown();
130 glfwTerminate();
131 return EXIT_SUCCESS;
130 ana::shaders::destroy();
131 ImGui_ImplOpenGL3_Shutdown();
132 ImGui_ImplGlfw_Shutdown();
133 glfwTerminate();
134 return EXIT_SUCCESS;
132135 }
11
22 namespace ana {
33
4 node::node(glm::vec2 loc, glm::vec2 size, std::string n)
5 : loc(loc), size(size), name(n) {}
4 node::node(std::string n) : name(n) {}
65
76 size_t nodeset::size() const {
87 return nodes.size();
55 namespace ana {
66
77 struct node {
8 glm::vec2 loc;
9 glm::vec2 size;
108 std::string name;
119
12 node() = default;
10 explicit node(std::string n);
1311 node(const node&) = default;
14 node(glm::vec2 loc, glm::vec2 size, std::string n);
1512 };
1613
1714 struct nodeset {
15 size_t size() const;
1816 std::vector<node> nodes;
19 size_t size() const;
2017 };
2118
2219 }
165165 static GLfloat *node_data = nullptr;
166166 static size_t node_data_size = 0;
167167
168 /*
168169 size_t elems = nodes.size() * 4;
169170 if (elems != node_data_size) {
170171 node_data = (GLfloat*) reallocarray(node_data, elems, sizeof(GLfloat));
171172 }
172173 int i = 0;
173174 for (const ana::node &n : nodes.nodes) {
174 node_data[4 * i + 0] = n.loc.x;
175 node_data[4 * i + 0] = 0;
175176 node_data[4 * i + 1] = n.loc.y;
176177 node_data[4 * i + 2] = n.size.x;
177178 node_data[4 * i + 3] = n.size.y;
197198 glDrawArrays(GL_POINTS, 0, (GLint) nodes.size()); INSERT_GL_ERROR_CHECK;
198199 glDisableVertexAttribArray(node_data_attr); INSERT_GL_ERROR_CHECK;
199200 glBindVertexArray(0);
201 */
200202 }
201203
202204 void destroy(void) {
5353 ImGuiIO &io = ImGui::GetIO();
5454 void *data = malloc(font_data_len);
5555 memcpy(data, &font_data, font_data_len);
56 io.Fonts->AddFontFromMemoryTTF(data, font_data_len, 24);
56 io.Fonts->AddFontFromMemoryTTF(data, font_data_len, 12);
5757
5858 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
5959
55 struct viewport {
66 float aspect = 1.f;
77 float view = 1.f;
8 int width = 1;
9 int height = 1;
8 int width = 1280;
9 int height = 800;
1010 glm::vec2 center{0.f, 0.f};
1111
1212 void set_aspect(int w, int h);