为什么我创建的曲面无法加载?

Why won't the surface i created load?

本文关键字:加载 曲面 创建 为什么      更新时间:2023-10-16

我在尝试使用以下代码加载this.png图像时遇到问题:

#include <iostream>
#include <SDL.h>
#include<SDL_image.h>
#include<string>

SDL_Texture *LoadTexture(std::string filePath,SDL_Renderer*renderTarget)
{
SDL_Texture *texture=NULL;
SDL_Surface *surface= IMG_Load(filePath.c_str());
if(surface==NULL)
std::cout<<"Error1"<<std::endl;
else
{
texture=SDL_CreateTextureFromSurface(renderTarget,surface);
if(texture==NULL)
std::cout<<"Error2"<<std::endl;
}
SDL_FreeSurface(surface);
return texture;
}
int main(int argc, char*argv[]) {
//Initializing and loading variables
SDL_Window *window=NULL;
SDL_Renderer *renderTarget=NULL;
int currentTime=0;
int prevTime=0;
float delta=0.0f;
const Uint8 *keystate;
SDL_Rect camerRect={0,0,222,290};
SDL_Init(SDL_INIT_VIDEO);
int data=10;
window=SDL_CreateWindow("SDL CodingMadeEeasY Series", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,222,290, SDL_WINDOW_SHOWN);
renderTarget= SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED );
SDL_Texture *texture=LoadTexture("spaceship_stock2.png",renderTarget);

bool isRunning=true;
SDL_Event ev;
while(isRunning)
{
prevTime=currentTime;
currentTime=SDL_GetTicks();
delta=(currentTime-prevTime)/1000.0f;
while(SDL_PollEvent(&ev) != 0)
{
//Getting the events
if(ev.type==SDL_QUIT)
isRunning=false;
}
keystate=SDL_GetKeyboardState(NULL);
SDL_RenderClear(renderTarget);
SDL_RenderCopy(renderTarget,texture,&camerRect,NULL);
SDL_RenderPresent(renderTarget);
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderTarget);
SDL_DestroyTexture(texture);
texture=nullptr;
window=nullptr;
renderTarget=nullptr;
IMG_Quit();
SDL_Quit();

return 0;
}

窗口输出:大小正确的黑色窗口。

控制台输出:错误1

错误:此项目中没有其他相关错误,只有未使用的变量引起的警告。

编译器找不到我的png文件,即使我在调试文件夹中保存了spaceship_stock2.png的副本,在发布文件夹中也保存了一份副本。调试当前处于活动状态,我正在使用Eclipse Helios。

我已经包含了库标志,并为库和包含设置了正确的路径。

这是对滚动背景教程中CODING MADE EASY代码的一个轻微修改。

我能够获得这些代码来编译和显示图像。我只改变了<SDL.h>"SDL.h"<SDL_image.h>"SDL_image.h"

然后为了编译,我运行了

c++ code_file_name.cpp `sdl2-config --cflags --libs` -lSDL2_image

在我的Linux系统上。