From fcf5112b113a94f52b90323d6842a8fbc9f7f3a3 Mon Sep 17 00:00:00 2001 From: tomdhy5133k <118716579+Swarnarajd494@users.noreply.github.com> Date: Sun, 4 Jun 2023 22:24:31 +0530 Subject: [PATCH] Add files via upload --- task2.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++ task3.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 task2.cpp create mode 100644 task3.cpp diff --git a/task2.cpp b/task2.cpp new file mode 100644 index 0000000..482a333 --- /dev/null +++ b/task2.cpp @@ -0,0 +1,58 @@ + #include + +// Function prototype for the window size callback +void framebuffer_size_callback(GLFWwindow* window, int width, int height); + +int main(void) +{ + GLFWwindow* window; + + /* Initialize the library */ + if (!glfwInit()) + return -1; + + /* Create a windowed mode window and its OpenGL context */ + window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); + if (!window) + { + glfwTerminate(); + return -1; + } + + /* Set the window size callback */ + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + + /* Make the window's context current */ + glfwMakeContextCurrent(window); + + /* Loop until the user closes the window */ + while (!glfwWindowShouldClose(window)) + { + /* Render here */ + glClear(GL_COLOR_BUFFER_BIT); + + /* Swap front and back buffers */ + glfwSwapBuffers(window); + + /* Poll for and process events */ + glfwPollEvents(); + } + + glfwTerminate(); + return 0; +} + +void framebuffer_size_callback(GLFWwindow* window, int width, int height) +{ + // Calculate the desired aspect ratio + float aspectRatio = 4.0f / 3.0f; + + // Calculate the new width and height based on the aspect ratio + int newWidth = width; + int newHeight = static_cast(width / aspectRatio); + + // Adjust the viewport to maintain the aspect ratio + int xOffset = 0; + int yOffset = (height - newHeight) / 2; + glViewport(xOffset, yOffset, newWidth, newHeight); +} diff --git a/task3.cpp b/task3.cpp new file mode 100644 index 0000000..4c8485e --- /dev/null +++ b/task3.cpp @@ -0,0 +1,67 @@ + #include + +const int MIN_WIDTH = 200; +const int MIN_HEIGHT = 150; +const int MAX_WIDTH = 1920; +const int MAX_HEIGHT = 1080; + +void framebuffer_size_callback(GLFWwindow* window, int width, int height) +{ + // Enforce minimum and maximum sizes + int newWidth = width; + int newHeight = height; + + if (newWidth < MIN_WIDTH) + newWidth = MIN_WIDTH; + if (newHeight < MIN_HEIGHT) + newHeight = MIN_HEIGHT; + if (newWidth > MAX_WIDTH) + newWidth = MAX_WIDTH; + if (newHeight > MAX_HEIGHT) + newHeight = MAX_HEIGHT; + + // Set the new window size + glfwSetWindowSize(window, newWidth, newHeight); + + // Adjust the viewport to match the new size + glViewport(0, 0, newWidth, newHeight); +} + +int main(void) +{ + GLFWwindow* window; + + /* Initialize the library */ + if (!glfwInit()) + return -1; + + /* Create a windowed mode window and its OpenGL context */ + window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); + if (!window) + { + glfwTerminate(); + return -1; + } + + /* Make the window's context current */ + glfwMakeContextCurrent(window); + + /* Set the framebuffer size callback */ + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + + /* Loop until the user closes the window */ + while (!glfwWindowShouldClose(window)) + { + /* Render here */ + glClear(GL_COLOR_BUFFER_BIT); + + /* Swap front and back buffers */ + glfwSwapBuffers(window); + + /* Poll for and process events */ + glfwPollEvents(); + } + + glfwTerminate(); + return 0; +}