0

So with created a window in C ++ using the GLFW library which also creates an OpenGL context. And after that I decided to add an icon to this window. There were 2 options, use either STB or SOIL to load the icon. I chose STB Image. I downloaded it and added it to my project. Everything works no errors, but for some reason my icon does not appear. It seems like the path I indicated is correct and included the path in the project in VS 2019.

Since I use premake5 to set the paths and to simplify the assembly, my path is specified how "%{prj.name}/libs/stb/include" and in VS 2019 there is just a path based on the name of the application libs/stb/include .

Here the image

And it's my code:

#define STB_IMAGE_IMPLEMENTATION
#include "CTXEngine/utils/stb_image.h"

void WinWindow::setIcon()
{
    //stb load 

    GLFWimage images[2];
    images[0].pixels = stbi_load("assets/textures/logo_16x16.png", &images[0].width, &images[0].height, 0, 4);
    images[1].pixels = stbi_load("assets/textures/logo_32x32.png", &images[1].width, &images[1].height, 0, 4);
    glfwSetWindowIcon(this->window, 1, images);

    stbi_image_free(images[0].pixels);
    stbi_image_free(images[1].pixels);
}

If you need see my window creating code, here it its:

#include "GLFW/glfw3.h"

/*
    Intialize the WINDOWS GLFW library for window. If library is not 
    initialize, then application will be closed.
*/
void WinWindow::initLibrary()
{
    if (!glfwInit())
    {
        this->glfwInitialized = false;
    }
    else
    {
        this->glfwInitialized = true;
    }

    if (!this->glfwInitialized)
    {
        CTX_ENGINE_ERROR("Glfw window is not initialized.")
        CTX_ENGINE_INFO("Shuttdown internal servers...")
        exit(-1);
    }
}

/*
    Create new WINDOWS GLFW window and create the context form graphical API
    OpenGl.
*/
void WinWindow::createWindow()
{
    CTX_ENGINE_INFO("Creating GLFW window...")
    this->initLibrary();

    /* Create a windowed mode window and its OpenGL context */
    this->window = glfwCreateWindow(this->width, this->height, this->title, this->fullscreen ? glfwGetPrimaryMonitor() : NULL, NULL);

    if (!window)
    {
        CTX_ENGINE_ERROR("Glfw window is not initialized.")
        CTX_ENGINE_INFO("Shuttdown internal servers...")
        this->terminateAPI();
        exit(-1);
    }

    this->videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());

    /* Make the window's context current */
    glfwMakeContextCurrent(this->window);
    glfwSetWindowTitle(this->window, this->title);
    glfwSetWindowSize(this->window, this->width, this->height);
    glfwSetWindowAspectRatio(this->window, 16, 9);
    glfwSetWindowSizeLimits(this->window, 854, 480, 1600, 900);
    glfwSetWindowPos(this->window, ((this->videoMode->width - this->width) / 2),
        ((this->videoMode->height - this->height) / 2));
}

And finally code in the init function:

#include "CTXImportHeaders.h"
#include "CTXEngine/core/Core.h"              //its just my class
#include "CTXEngine/core/CoreBehaviour.h"     //its just my class
#include "CTXEngine/settings/GameSettings.h"  //its just my class

using namespace std;

/*
    This method be inititalize all core engine classes, structs, namespaces,
    and other parameters, and configurations.
*/
void Core::init()
{
    GameSettings settings;

    this->window.setTitle(settings.gameTitle); // move params to game configuration
    this->window.setWindowResolution(settings.gameWidth, settings.gameHeight); // move params to game configuration
    this->window.setFullscreen(settings.gameFullscreen);
    this->window.setIcon();
    this->window.createWindow();

}

Its screenshot of my window without icon

  • Quick test is to use an absolute path. If this works then the problem is the current working directory is not where you think it is when the application is running (so amend the paths). If the absolute path does not work you either have an API (bad parameters) or data content (icon not in the right format) issue – Richard Critten Oct 30 '19 at 18:23
  • Thanks, i'll check now – Kenny Tutorials Oct 30 '19 at 18:43

1 Answers1

0

I think you are confused in path. The path of the image should be relative to your .vcxproj files

├───assets
│   ├───Logos
|   |   |_ Icon.png
|   |   
│   ├───Shaders
│   └───textures
└───src
|
|___.vcxproj 

I used this code:

int width, height, chennal;
stbi_uc* img = stbi_load("assets/Logos/Icon.png", &width, &height, &chennal, 0); //rgba channels 
if (img == NULL) std::cout << "Icon Can Not Be Loaded\n";
m_Images->height = height;
m_Images->width = width;
m_Images[0].pixels = img;
glfwSetWindowIcon(m_Window, 1, m_Images);
Kshitij azad
  • 179
  • 2
  • 6