So, I am trying to set the window icon for my glfw window (obviously) but I am having trouble doing so because I don't know how to set an icon using GLFWimage. The code in the docs is as shown:
GLFWimage images[2];
images[0] = load_icon("my_icon.png");
images[1] = load_icon("my_icon_small.png");
glfwSetWindowIcon(window, 2, images);
But there, it doesn't show how to use or create a "load_icon" function. So, can anyone help cause I have been searching for months.
The code in the docs is as shown: GLFWimage images[2]; images[0] = load_icon("my_icon. png"); images[1] = load_icon("my_icon_small. png"); glfwSetWindowIcon(window, 2, images);
This window object holds all the windowing data and is required by most of GLFW's other functions. GLFWwindow* window = glfwCreateWindow (800, 600, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate (); return -1; } glfwMakeContextCurrent (window);
glfwWindowHint (int hint, int value) Sets the specified window hint to the desired value.
glfwSetWindowIcon(GLFWwindow * window, int count, const GLFWimage * images)
I think the first two parameters are clear and there is the third, GLFWimage
which is a structure with fields int width, int height, unsigned char * pixels
but its your task to provide data for it, GLFW does not give you functions to load your images but there are several libraries which does or you can write your own function to get image data.
I'm not really c++ guy so I'm not aware of the newest/best libraries currently but there is soil for example which would work just fine. Something like this:
GLFWimage icons[1];
icons[0].pixels = SOIL_load_image("icon.png", &icons[0].width, &icons[0].height, 0, SOIL_LOAD_RGBA);
glfwSetWindowIcon(window.window, 1, icons);
SOIL_free_image_data(icons[0].pixels);
If you are using "stb_image.h", this is the code you should be using:
GLFWimage images[1];
images[0].pixels = stbi_load("PATH", &images[0].width, &images[0].height, 0, 4); //rgba channels
glfwSetWindowIcon(window, 1, images);
stbi_image_free(images[0].pixels);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With