From ca64248ace9a89a21c823af1c9b85284d97d4da1 Mon Sep 17 00:00:00 2001 From: ShyProton Date: Mon, 21 Apr 2025 23:08:56 -0400 Subject: [PATCH] Adds formatting config and formats project --- .clang-format | 31 ++++++++ src/main.cpp | 198 ++++++++++++++++++++++++-------------------------- 2 files changed, 126 insertions(+), 103 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a90af09 --- /dev/null +++ b/.clang-format @@ -0,0 +1,31 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +ColumnLimit: 120 +AllowShortIfStatementsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Inline +BreakBeforeBraces: Linux +SpaceBeforeParens: ControlStatements +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpacesInAngles: false +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +PointerAlignment: Left +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignTrailingComments: true +AlignOperands: true +AlwaysBreakAfterReturnType: None +AllowShortBlocksOnASingleLine: Empty +Cpp11BracedListStyle: false +DerivePointerAlignment: false +KeepEmptyLinesAtTheStartOfBlocks: false +SortIncludes: false +IncludeBlocks: Preserve +IndentCaseLabels: true +IndentGotoLabels: false +IndentPPDirectives: None +MaxEmptyLinesToKeep: 1 +SpacesBeforeTrailingComments: 1 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 99fda50..b156c98 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,155 +11,147 @@ void processInput(GLFWwindow* window); void setupGLFW(GLFWwindow** window); typedef struct { - int width, height, nrChannels; - unsigned char* data; + int width, height, nrChannels; + unsigned char* data; } texture_t; float vertices[] = { - // positions colours texture coords - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // bottom left + // positions colours texture coords + 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left + -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // bottom left }; unsigned int indices[] = { - 0, 1, 3, - 1, 2, 3 + 0, 1, 3, 1, 2, 3, }; - + bool wireframe = false; int main() { - GLFWwindow* window; - setupGLFW(&window); + GLFWwindow* window; + setupGLFW(&window); + Shader myShader("shaders/shader.vs", "shaders/shader.fs"); - Shader myShader("shaders/shader.vs", "shaders/shader.fs"); + texture_t tex; - texture_t tex; + stbi_set_flip_vertically_on_load(true); - stbi_set_flip_vertically_on_load(true); + tex.data = stbi_load("textures/wg.jpg", &tex.width, &tex.height, &tex.nrChannels, 0); - tex.data = stbi_load("textures/wg.jpg", &tex.width, &tex.height, &tex.nrChannels, 0); + unsigned int texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); - unsigned int texture; - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - if (tex.data) { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.width, tex.height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data); - glGenerateMipmap(GL_TEXTURE_2D); - } else { - std::cout << "Failed to load texture\n"; - } + if (tex.data) { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.width, tex.height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data); + glGenerateMipmap(GL_TEXTURE_2D); + } else { + std::cout << "Failed to load texture\n"; + } + stbi_image_free(tex.data); - - stbi_image_free(tex.data); - - unsigned int VAO; - glGenVertexArrays(1, &VAO); + unsigned int VAO; + glGenVertexArrays(1, &VAO); - unsigned int VBO; - glGenBuffers(1, &VBO); + unsigned int VBO; + glGenBuffers(1, &VBO); - unsigned int EBO; - glGenBuffers(1, &EBO); + unsigned int EBO; + glGenBuffers(1, &EBO); - // bind Vertex Array Object - glBindVertexArray(VAO); - // copy our vertices into a buffer for opengl to use - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - // copy our indices - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - - // set the vertex attributes pointers - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) 0); - glEnableVertexAttribArray(0); + // bind Vertex Array Object + glBindVertexArray(VAO); + // copy our vertices into a buffer for opengl to use + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + // copy our indices + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) (3 * sizeof(float))); - glEnableVertexAttribArray(1); + // set the vertex attributes pointers + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) (6 * sizeof(float))); - glEnableVertexAttribArray(2); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); - myShader.use(); - myShader.setInt("tex", 0); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); + glEnableVertexAttribArray(2); + myShader.use(); + myShader.setInt("tex", 0); - while(!glfwWindowShouldClose(window)) { - //input - processInput(window); + while (!glfwWindowShouldClose(window)) { + // input + processInput(window); - //rendering - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + // rendering + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); - myShader.use(); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture); + myShader.use(); - glBindVertexArray(VAO); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - glBindVertexArray(0); // unbinds the VAO so its ready to be bound again for the next render + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture); - // check and call events and swap buffers - glfwSwapBuffers(window); - glfwPollEvents(); - } + glBindVertexArray(VAO); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); // unbinds the VAO so its ready to be bound again for the next render - glfwTerminate(); - return 0; + // check and call events and swap buffers + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwTerminate(); + return 0; } void setupGLFW(GLFWwindow** window) { - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - if (window == NULL) { - std::cout << "Failed to create GLFW window\n"; - glfwTerminate(); - exit(-1); - } - glfwMakeContextCurrent(*window); + *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); + if (window == NULL) { + std::cout << "Failed to create GLFW window\n"; + glfwTerminate(); + exit(-1); + } + glfwMakeContextCurrent(*window); - if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { - std::cout << "Failed to init GLAD\n"; - exit(-1); - } + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + std::cout << "Failed to init GLAD\n"; + exit(-1); + } - glViewport(0, 0, 800, 600); - glfwSetFramebufferSizeCallback(*window, framebuffer_size_callback); + glViewport(0, 0, 800, 600); + glfwSetFramebufferSizeCallback(*window, framebuffer_size_callback); } - void framebuffer_size_callback(GLFWwindow* window, int width, int height) { - glViewport(0, 0, width, height); + glViewport(0, 0, width, height); } void processInput(GLFWwindow* window) { - if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) - glfwSetWindowShouldClose(window, true); - if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) // triggers wireframe mode - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) // disables wireframe mode - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); + if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) // triggers wireframe mode + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) // disables wireframe mode + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); }