C ++ GLFW setWindowIcon - PullRequest
       26

C ++ GLFW setWindowIcon

0 голосов
/ 30 октября 2019

Таким образом, с созданием окна в C ++ с использованием библиотеки GLFW, которая также создает контекст OpenGL. И после этого я решил добавить иконку в это окно. Было 2 варианта, используйте либо STB, либо SOIL, чтобы загрузить значок. Я выбрал STB Image. Я скачал его и добавил в свой проект. Все работает без ошибок, но почему-то моя иконка не появляется. Кажется, что путь, который я указал, является правильным и включил путь в проекте в VS 2019.

Так как я использую premake5, чтобы установить пути и упростить сборку, мой путь указан как "%{prj.name}/libs/stb/include" и вVS 2019 есть просто путь, основанный на названии приложения libs/stb/include.

Здесь изображение

И это мой код:

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

Если вам нужно увидеть, как мое окно создает код, вот оно:

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

И, наконец, код в функции init:

#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();

}

Его скриншот моего окнабез значка

...