如何使用SDL_surface在用户定义的矩形上显示图像?
How to display a image on a user defined rectangle with SDL_surface?
我和我的朋友一起做一个项目,我们在 SDL 中遇到了表面和窗口的问题。
目前,我们能够创建一个窗口并在该窗口上显示一个矩形并移动它。我们要做的下一件事是拍摄图像并将其显示在矩形上,然后在屏幕上移动它。
我们从获取SDL_window*并将其转换为SDL_surface*开始,尽管这会获取图像并将其显示在窗口的背景上。
有没有办法将我们创建的矩形变成表面并在该矩形上显示图像?
我也尝试过使用纹理,当我尝试移动它时它会扭曲图像,并且整个图像不会随矩形一起移动。
这发生在构造函数中
temp_image_sur = IMG_Load( image_location.c_str() );
if( temp_image_sur == NULL )
{
std::cout << "Image could not be loaded" <<std::endl;
exit(1);
}
这是在实际的绘制函数中。
display_surface = SDL_GetWindowSurface( display_window );
if(display_surface == NULL )
{
printf(" null im exiting here %sn", SDL_GetError());
exit(1);
}
image_surface = SDL_ConvertSurface( temp_image_sur, display_surface->format, 0 );
image_size = { this->location.x, this->location.y, this->size.width, this->size.height };
SDL_BlitSurface( image_surface, &image_size, display_surface, &image_size );
这是我们第一次尝试时所做的,图像显示在基本窗口中。我相信我理解为什么它显示在基本窗口上,这是因为我们使用该窗口作为表面,尽管我很困惑如何使用户定义的矩形成为表面?
我们确实尝试使用SDL_CreateRGBSurface,尽管当我们这样做时,屏幕上也没有显示任何内容。
display_surface = SDL_CreateRGBSurface(0, this->size.width, this->size.height, 1, this->color.red, this->color.green, this->color.blue, this->color.alpha);
谢谢大家! 如果您需要更多信息,请告诉我,这是我第一次发布,我试图把我能想到的所有信息都放进去。
使用SDL_CreateTextureFromSurface
从图像表面创建纹理:
SDL_Texture* image_surface = SDL_CreateTextureFromSurface(renderer, temp_image_sur);
(记得用SDL_DestroyTexture
释放它)
然后使用SDL_RenderCopy
绘制它:
SDL_RenderCopy(renderer, image_texture, nullptr, &image_rect);
其中image_rect
是SDL_Rect
和要将图像绘制到的目标矩形,例如:
SDL_rect image_rect = {10, 10, 200, 200};
要移动图像,只需更改image_rect.x
和/或image_rect.y
相关文章:
- 比较并显示使用最小值(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 c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果
- std::strftime 在 Windows 中使用 GMTIME 将时区 UTC 显示为 UTC+1