尝试包含纹理 SDL2 c++ 时出现断言错误

Assertion error when trying to include textures SDL2 c++

我用SDL2做了一个图形引擎,添加贴图的时候报错:


但是,如果我不包含纹理,一切正常。

我也有理由相信,错误与 IDE 有关,因为我阅读了其他问题。

我的IDE:VS 2017 专业版

代码

运行()

void maingame::run()
{
    initSystems();
    _sprite.init(-1.0f, -1.0f, 2.0f, 2.0f);
    _enemyTexture = ImageLoader::loadPNG("textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
    gameLoop();
}

运行() 方法被 main() func

调用

精灵的初始化:

void targetSprite::init(float x, float y, float width, float height)
{
    _x = x;
    _y = y;
    _width = width;
    _height = height;

    if (_vboID == 0) {
        glGenBuffers(1, &_vboID);
    }
    Vertex vertexData[6];
    //1st
    vertexData[0].setPos(_x + _width, _y + _height);
    vertexData[0].setUV(1.0f, 1.0f);

    vertexData[1].setPos(_x, _y + _height);
    vertexData[1].setUV(0.0f, 1.0f);

    vertexData[2].setPos(_x, _y);
    vertexData[2].setUV(0.0f, 0.0f);

    //2nd

    vertexData[3].setPos(_x, _y);
    vertexData[3].setUV(0.0f, 0.0f);

    vertexData[4].setPos(_x + _width, _y);
    vertexData[4].setUV(1.0f, 0.0f);

    vertexData[5].setPos(_x + _width, _y + _height);
    vertexData[5].setUV(1.0f, 1.0f);

    for (int i = 0; i < 6; i++) {
        vertexData[i].setColor(127, 127, 255, 255);
    }

    vertexData[1].setColor(255, 0, 255, 255);
    vertexData[4].setColor(0, 255, 255, 255);

    glBindBuffer(GL_ARRAY_BUFFER, _vboID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, _vboID);
}

我发布这个是因为我相信错误就在这里。

但这不是必需的,因为同样的代码 运行 没有纹理。

第 10 行 "Vertex" 是一个结构。

我尝试将构建从调试更改为发布,但后来我遇到了 .dll 文件的问题。

编辑:

这调用向量中的错误:

GLTexture ImageLoader::loadPNG(std::string filePath)
{
    GLTexture texture = {};
    std::vector<unsigned char> out;
    unsigned long width;
    unsigned long height;
    std::vector<unsigned char> in;
    if (IOManager::readFileToBuffer(filePath, in) == false) { //read the texture to buffer
        fatalError2("FTB 1");
    }
    int errorCode = decodePNG(out, width, height, &(in[0]), in.size()); //error line
    if (errorCode != 0) {
        fatalError2("DPNG 1: " + std::to_string(errorCode));
    }

    glGenTextures(1, &(texture.id));
    glBindTexture(GL_TEXTURE_2D, texture.id);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &(out[0]));
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glGenerateMipmap(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);

    texture.width = width;
    texture.height = height;

    return texture;
}

第25行,然后回到运行(),main(),然后打开一个"sdl_windows_main.c not found"window.

所以我想问题是该文件位于何处?

编辑 2

readFileToBuffer() 方法,我相信负责填充"in"向量

bool IOManager::readFileToBuffer(std::string filePath, std::vector<unsigned char> buffer)
{
    std::ifstream file(filePath, std::ios::binary);
    if (file.fail()) {
        perror(filePath.c_str());
        return false;
    }
    file.seekg(0, std::ios::end);
    int fileSize = file.tellg();
    file.seekg(0, std::ios::beg);
    fileSize = fileSize - file.tellg();
    buffer.resize(fileSize);
    file.read((char *)&(buffer[0]), fileSize);
    file.close();
    return true;
}

解决方案是通过引用"buffer"

bool IOManager::readFileToBuffer(std::string filePath, std::vector<unsigned char>&buffer)
{
    std::ifstream file(filePath, std::ios::binary);
    if (file.fail()) {
        perror(filePath.c_str());
        return false;
    }
    file.seekg(0, std::ios::end);
    int fileSize = file.tellg();
    file.seekg(0, std::ios::beg);
    fileSize = fileSize - file.tellg();
    buffer.resize(fileSize);
    file.read((char *)&(buffer[0]), fileSize);
    file.close();
    return true;
}
´´´