0

因此,使用 GLFW 库在 C++ 中创建了一个窗口,该库还创建了一个 OpenGL 上下文。之后我决定在这个窗口中添加一个图标。有 2 个选项,使用 STB 或 SOIL 加载图标。我选择了机顶盒图像。我下载了它并将其添加到我的项目中。一切正常,没有错误,但由于某种原因我的图标没有出现。看来我指出的路径是正确的,并且在 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();

}

它没有图标的我的窗口的屏幕截图

4

1 回答 1

0

我认为您对路径感到困惑。图像的路径应该相对于您的 .vcxproj 文件

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

我使用了这段代码:

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);
于 2020-11-12T09:46:30.927 回答