IMG_Load:无法打开xxx.png

时间:2018-02-19 01:13:20

标签: c sdl-2 sdl-image

上下文:我目前正在尝试使用SDL 2.0.7和SDL2_image-2.0.2来练习我的C技能。

问题:我在程序的执行期间收到错误消息" IMG_Load:无法打开xxx.png"。错误似乎很愚蠢,因为它非常明确:"我无法找到图像",但由于图像位于相应的文件夹中...我想我需要一个新的眼睛来发现愚蠢的错误。

平台: Windows 10

IDE: Visual Studio 2017

解决问题的步骤:

1)试图将代码长度/功能降低到最小。 结果:错误仍然存​​在。

2)我创建了一个新项目并复制/粘贴了简化代码。 结果:在新项目中,没有错误,一切正常。

3)我比较了项目的选项和文件夹。对我来说他们是一样的:

Options in both projects

Debug folder in both projects

它不应该有用,但为了以防万一,这是我的:

代码示例:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_render.h>
#include "SDL_timer.h"

int main(int argc, char *argv[])
{
    printf("argc = %d\n", argc);
    for (int i = 0; i < argc; ++i) 
    {
        printf("argv[ %d ] = %s\n", i, argv[i]);
    }

    SDL_Window* pWindow = NULL;
    SDL_Renderer* pRenderer = NULL;
    SDL_Texture* pTexture = NULL;
    SDL_Surface* pLoadedSurface = NULL;
    SDL_Rect* tileClipsArray = NULL;

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
    {
        fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
    }

    //Initialize PNG loading
    int imgFlags = IMG_INIT_PNG;
    if (!(IMG_Init(imgFlags) & imgFlags))
    {
        printf("IMG_Load: %s\n", IMG_GetError());
    }

    pWindow = SDL_CreateWindow("TestLoadingImage",
        SDL_WINDOWPOS_CENTERED, // initial X position.
        SDL_WINDOWPOS_CENTERED, // Initial Y position.
        640, // Width, in pixels.
        480, // Height, in pixels.
        SDL_WINDOW_OPENGL); // Window flags

    assert(NULL != pWindow);

    //Create renderer for the window
    pRenderer = SDL_CreateRenderer(pWindow,
        -1, // Index of the rendering driver to initialize, -1 to initialize the first one supporting the requested flags.
        SDL_RENDERER_ACCELERATED
        | SDL_RENDERER_PRESENTVSYNC); // RendererFlags

    assert(NULL != pRenderer);

    //Initialize renderer color
    SDL_SetRenderDrawColor(pRenderer, 0xFF, 0xFF, 0xFF, 0xFF);

    pLoadedSurface = IMG_Load("GroundTiles.png");
    if (NULL == pLoadedSurface)
    {
        printf("IMG_Load: %s\n", IMG_GetError());
        assert(NULL != pLoadedSurface);
    }

    //Create texture from surface pixels
    pTexture = SDL_CreateTextureFromSurface(pRenderer, pLoadedSurface);
    assert(NULL != pTexture);

    //Get image dimensions
    const int textureWidth = pLoadedSurface->w;
    const int textureHeight = pLoadedSurface->h;
    const int tileClipWidth = 128;
    const int tileClipHeight = 128;
    const int nbLines = textureHeight / tileClipHeight;
    const int nbColumns = textureWidth / tileClipWidth;
    const int nbTileClips = nbLines + nbColumns;

    tileClipsArray = malloc(nbTileClips * sizeof(SDL_Rect));

    int tileClipIndex = 0;
    for (int tileClipLineIndex = 0; tileClipLineIndex < nbLines; ++tileClipLineIndex)
    {
        for (int tileClipColumnIndex = 0; tileClipColumnIndex < nbColumns; ++tileClipColumnIndex)
        {
            tileClipsArray[tileClipIndex].x = tileClipColumnIndex * tileClipWidth;
            tileClipsArray[tileClipIndex].y = tileClipLineIndex * tileClipHeight;
            tileClipsArray[tileClipIndex].w = tileClipWidth;
            tileClipsArray[tileClipIndex].h = tileClipHeight;
            ++tileClipIndex;
        }
    }

    //Get rid of old loaded surface
    SDL_FreeSurface(pLoadedSurface);
    pLoadedSurface = NULL;

    int canLoop = 1;
    SDL_Event event;
    int lastUpdate = SDL_GetTicks();
    int now = 0;
    int timeToSpendPerClip = 5000;
    int timeSpentwithThisClip = 0;
    int clipToUse = 0;

    while (canLoop)
    {
        now = SDL_GetTicks();

        if (now - lastUpdate > 16)
        {
            timeSpentwithThisClip += now - lastUpdate;
            lastUpdate = now;

            // We are processing all the events received this frame.
            while (SDL_PollEvent(&event))
            {
                // We need to know what kind of event we are dealing with.
                switch (event.type)
                {
                case SDL_QUIT:
                    canLoop = 0;
                    break;
                }
            }

            SDL_RenderClear(pRenderer);

            if (timeSpentwithThisClip > timeToSpendPerClip)
            {
                clipToUse = rand() % 4;
                timeSpentwithThisClip = 0;
            }
            // Set rendering space and render to screen.
            SDL_Rect renderQuad;
            renderQuad.x = 50;
            renderQuad.y = 50;
            renderQuad.w = tileClipsArray[clipToUse].w;
            renderQuad.h = tileClipsArray[clipToUse].h;

            SDL_RenderCopyEx(pRenderer, pTexture, &tileClipsArray[clipToUse], &renderQuad, 0.0, NULL, SDL_FLIP_NONE);

            SDL_RenderPresent(pRenderer);
        }
    }

    SDL_DestroyTexture(pTexture);
    free(tileClipsArray);

    SDL_DestroyRenderer(pRenderer);
    pRenderer = NULL;

    SDL_DestroyWindow(pWindow);
    pWindow = NULL;

    IMG_Quit();
    SDL_Quit();

    return EXIT_SUCCESS;
}

我可能会将项目1中的所有文件复制/粘贴到项目2中,但我想了解我的错误!

0 个答案:

没有答案