SDL绘制倾斜水印实例代码
时间: 2023-09-14 14:12:26 浏览: 175
用SDL2.0显示图片实例代码
下面是一个简单的SDL绘制倾斜水印的示例代码:
```c
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_gfxPrimitives.h>
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("SDL Watermark Example",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// 加载水印图像
SDL_Surface* watermark = IMG_Load("watermark.png");
if (!watermark) {
SDL_Log("Failed to load watermark image: %s", IMG_GetError());
return -1;
}
// 创建目标SDL_Surface对象
SDL_Surface* target = SDL_CreateRGBSurfaceWithFormat(0, 640, 480, 32, SDL_PIXELFORMAT_RGBA8888);
if (!target) {
SDL_Log("Failed to create target surface: %s", SDL_GetError());
SDL_FreeSurface(watermark);
return -1;
}
// 填充目标SDL_Surface对象为白色背景
SDL_FillRect(target, NULL, SDL_MapRGB(target->format, 255, 255, 255));
// 进行旋转、缩放等变换操作
SDL_Surface* transformed = rotozoomSurface(watermark, 30.0, 1.0, SMOOTHING_ON);
SDL_Rect dstrect = { 100, 100, transformed->w, transformed->h };
// 在目标SDL_Surface对象上绘制变换后的水印
SDL_BlitSurface(transformed, NULL, target, &dstrect);
// 将目标SDL_Surface对象绘制到屏幕上
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, target);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
// 清理资源
SDL_FreeSurface(watermark);
SDL_FreeSurface(transformed);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
```
在这个示例代码中,我们使用了SDL_image库来加载水印图像,并使用了SDL_gfx库中的rotozoomSurface函数来进行旋转、缩放等变换操作。最后,我们将变换后的水印绘制到了目标SDL_Surface对象上,并将目标SDL_Surface对象绘制到了屏幕上。
阅读全文