-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
133 lines (101 loc) · 3.05 KB
/
main.cc
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// src/main.cc
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <gtk-4.0/gtk/gtk.h>
#include <spdlog/spdlog.h>
#include <stdlib.h>
#include <exception>
#include "drawing_manager.hh"
#include "gui_manager.hh"
#include "shader_manager.hh"
#include "utils.hh"
#include "window_manager.hh"
/* Global Variables */
extern bool dark_mode;
extern ImVec4 clear_color;
extern ImVec4 selected_color;
extern float brush_size;
extern int brush_size_display;
int main()
{
/* Set up logging */
spdlog::set_level(spdlog::level::info);
spdlog::set_pattern("[%H:%M:%S %z] [%^%l%$] [thread %t] %v");
spdlog::info("Starting Artistry...");
/* Initialize GLFW */
if (glfwInit() != GLFW_TRUE)
{
spdlog::critical("Failed to initialize GLFW.");
return EXIT_FAILURE;
}
spdlog::info("Successfully initialized GLFW.");
/* Set up the primary window */
GLFWwindow *window = WindowManager::create_window(720, 720, "Artistry");
if (!window)
{
glfwTerminate();
return EXIT_FAILURE;
}
// Set an icon for the window.
WindowManager::set_window_icon(window, "assets/icon.png");
/* Set up window callbacks */
try
{
WindowManager::setup_callbacks(window);
}
catch (const std::exception &e)
{
spdlog::error("Error setting up callbacks: {}", e.what());
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_FAILURE;
}
// Enable V-Sync.
glfwSwapInterval(1);
/* Compile and link the shader programs. */
GLuint program = link_program("src/shaders/shader.vert", "src/shaders/shader.frag");
glUseProgram(program);
GLuint vao = create_vertex_array_object();
GLuint vbo = 0;
glGenBuffers(1, &vbo);
spdlog::debug("Generated vertex buffer object: {}.", vbo);
GUIManager::init(window);
std::vector<std::vector<float>> circles;
while (!glfwWindowShouldClose(window))
{
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
GUIManager::new_frame();
GUIManager::create_ui(circles);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
float x = (2 * xpos - width) / width;
float y = (height - 2 * ypos) / height;
const float radius = brush_size;
std::vector<float> current_circle = DrawingManager::draw_circle(x, y, radius);
vbo = create_vertex_buffer_object(current_circle);
draw_circle(vao, vbo, current_circle, GL_TRIANGLE_FAN);
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
circles.push_back(current_circle);
}
int brush_color_location = glGetUniformLocation(program, "u_BrushColor");
glUniform4f(brush_color_location, selected_color.x, selected_color.y, selected_color.z, selected_color.w);
for (const auto &circle : circles)
{
draw_circle(vao, vbo, circle, GL_TRIANGLE_FAN);
}
GUIManager::render();
glfwPollEvents();
glfwSwapBuffers(window);
}
GUIManager::shutdown();
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
glfwDestroyWindow(window);
glfwTerminate();
spdlog::info("GLFW window destroyed and terminated successfully.");
return EXIT_SUCCESS;
}