Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tasks #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions task2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <GLFW/glfw3.h>

// 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<int>(width / aspectRatio);

// Adjust the viewport to maintain the aspect ratio
int xOffset = 0;
int yOffset = (height - newHeight) / 2;
glViewport(xOffset, yOffset, newWidth, newHeight);
}
67 changes: 67 additions & 0 deletions task3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <GLFW/glfw3.h>

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;
}