为什么我创建的曲面无法加载?
Why won't the surface i created load?
我在尝试使用以下代码加载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系统上。
相关文章:
- std::原子加载和存储都需要吗
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 为什么加载SDF会导致Mobilizer创建闭环错误
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- 如何使用tinyxml2从XML加载父实体和子实体
- 如何在C++中使用pybind11加载一个pickle python列表
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 当我尝试加载内核模块时,如何修复C++中的这个 malloc() 错误?
- 树莓上的 Libtorch 无法加载 pt 文件,但在 ubuntu 上工作
- Python ctypes:不会按预期加载 dll
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 保存/加载大量短数组到二进制文件
- 使用 SDL2 和SDL_image C++加载图像
- 无法使用加载库卸载注入的 dll (C++)
- 是否有原子加载非原子值的函数?
- 将 bmp 文件加载到 TSpeedButton 中
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 为什么我创建的曲面无法加载?