在SDL中显示PNG
Displaying a PNG in SDL?
我正在试着让一个骑马的骑士(名字很不吉利的"家伙")跑过屏幕。骑士目前以2 .png文件的形式存在于我的目录中,以模拟一些动画效果不佳的驰骋。当他是。bmp的时候,我可以让他出现,但我想利用png文件的透明度——我也试过打开tif文件,但失败了。我怎样才能改变我的代码,以让他出现从一个。png加载到SDL正确?
这是我的。cpp文件:
#include "SDL/SDL.h"
#include <iostream>
#include "source_sdl.h"
# include <string>
using namespace std;
int source_sdl( int argc, char* args[] ) {
int switch = 1;
int running = 1;
// surface & rect declarations
SDL_Surface* ground = NULL;
SDL_Surface* guy = NULL;
SDL_Surface* guy2 = NULL;
SDL_Rect guylocationRect;
SDL_Rect groundlocationRect;
// initialize SDL & the screen
SDL_Init( SDL_INIT_EVERYTHING );
screen = SDL_SetVideoMode( 875, 625, 32, SDL_SWSURFACE );
// open .png files
SDL_RWops* guy_rwop;
SDL_RWops* guy2_rwop;
guy_rwop = SDL_RWFromFile("tiffKnight.png", "rb");
guy2_rwop = SDL_RWFromFile("tiffKnight2.png", "rb");
guy = IMG_LoadPNG_RW(guy_rwop);
guy2 = IMG_LoadPNG_RW(guy2_rwop);
guylocationRect.x = 300;
guylocationRect.y = 300;
groundlocationRect.x = 300;
groundlocationRect.y = 300;
SDL_Event occur;
// animation loop (currently endless)
while (running == 1){
SDL_Flip( screen );
SDL_Delay( 300 );
if (gallop > 89) gallop=0;
// draw the ground
for( int yu = 0; yu<35; yu++){
groundlocationRect.x=25*yu;
groundlocationRect.y=5*25;
SDL_BlitSurface( ground, NULL, screen, &groundlocationRect );
}
// draw the gallopping
guylocationRect.x=10*gallop;
guylocationRect.y=5*25;
if( switch ){
SDL_BlitSurface( guy, NULL, screen, &guylocationRect );
}else{
SDL_BlitSurface( guy2, NULL, screen, &guylocationRect );
}
gallop++;
switch = (switch+1)%2;
for(int u = 6; u < 25; u++){
for(int yu = 0; yu < 35; yu++){
groundlocationRect.x = 25*yu;
groundlocationRect.y = 25*u;
SDL_BlitSurface( ground, NULL, screen, &groundlocationRect );
}
}
}
SDL_FreeSurface( guy );
SDL_FreeSurface( guy2 );
SDL_FreeSurface( ground );
}
就像我目前拥有的那样,骑士没有出现,我也没有收到任何错误(SDL屏幕打开的结果?)失败检查,如
if(!guy) {
cout << "IMG_LoadPNG_RW: %sn" << IMG_GetError();
}
也没有结果。我的。h文件很简单:
#include "SDL/SDL.h"
#include <iostream>
using namespace std;
int source_sdl( int argc, char* args[] );
And my main.cpp:
#include "SDL/SDL.h"
#include <iostream>
#include "source_sdl.h"
using namespace std;
int main( int argc, char* args[] ){
source_sdl( argc, args );
}
如果您真的想坚持使用c++中的SDL,我建议您使用SDL_image库并使用如下函数:
SDL_Surface * loadImage(std::string const & filename)
{
SDL_Surface * img = nullptr;
SDL_Surface * tmp = IMG_Load(filename.c_str());
if (tmp)
{
img = SDL_DisplayFormatAlpha(tmp);
SDL_FreeSurface(tmp);
}
return img;
}
一个更现代和安全的方法的例子是这样的:
using SurfacePtr = std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)>;
SurfacePtr loadImage(std::string const & filename)
{
SurfacePtr img { nullptr, &SDL_FreeSurface };
SurfacePtr tmp { IMG_Load(filename.c_str()), &SDL_FreeSurface };
if (tmp)
img.reset(SDL_DisplayFormatAlpha(tmp.get()));
return img;
}
或者更好,使用现有的c++绑定,如libSDL2pp(我与它们无关)。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- Qt-png图像未显示在部署应用程序中
- 尝试使用 CPP 在一个 Qt 窗口中显示多个 png 文件
- 如何使用GDI 显示具有25%不透明度的PNG图像?(MFC)
- 从libpng加载的png纹理在OpenGL中显示为黑色
- 如何加载和显示动画png
- 无法使用 Glut 或 OpenGL 显示 PNG
- 使用资源中的PNG图标显示Win32弹出菜单
- MFC显示具有透明背景的png
- QT .png文件没有显示在屏幕上
- 如何在程序的内存中搜索图像/png/jpeg/任何其他类型并显示它
- 在SDL中显示PNG
- 图像 (PNG) 未显示在 c++ Win32 项目的最终执行中