1. 加载bmp图片到SDL窗口
步骤 |
SDL 函数 |
1.初始化SDL |
SDL_Init |
2.创建窗口 |
SDL_CreateWindow |
3.获取窗口表面 |
SDL_GetWindowSurface |
4.加载bmp图片到surface |
SDL_LoadBMP |
5.将图片surface设置到窗口surface上 |
SDL_BlitSurface |
6.更新窗口surface显示 |
SDL_UpdateWindowSurface |
7.释放资源,退出SDL |
SDL_FreeSurface, SDL_DestroyWindow, SDL_Quit |
#include <iostream>
/* 加载bmp图片
* 1. SDL默认只能加载bmp图片
*/
extern "C"
{
#include <SDL.h>
}
#pragma comment(lib, "SDL2.lib")
#undef main
int main03()
{
// 1. 初始化SDL
int nRet = SDL_Init(SDL_INIT_EVERYTHING);
if (nRet < 0)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 2. 创建窗口
SDL_Window* pWnd = SDL_CreateWindow("BMP SHOW",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_SHOWN);
if (NULL == pWnd)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 3. 获取窗口表面
SDL_Surface* pWndSurface = SDL_GetWindowSurface(pWnd);
if (NULL == pWndSurface)
{
SDL_DestroyWindow(pWnd);
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 4. 加载bmp图片
SDL_Surface* pBmpSurface = SDL_LoadBMP("./Panda.bmp");
// 5. 将图片的surface设置到窗口的surface上
SDL_BlitSurface(pBmpSurface, NULL, pWndSurface, NULL);
// 6. 更新窗口surface显示
SDL_UpdateWindowSurface(pWnd);
SDL_Delay(3000);
// 7. 释放资源
if (pBmpSurface)
{
SDL_FreeSurface(pBmpSurface);
}
if (pWnd)
{
SDL_DestroyWindow(pWnd);
}
// 8. 退出SDL
SDL_Quit();
return 0;
}
2. 纹理和渲染的简单使用
1. 初始化SDL SDL\_Init
2. 创建窗口 SDL\_CreateWindow
3. 基于窗口创建渲染器 SDL\_CreateRenderer
4. 基于渲染器创建纹理 SDL\_CreateTexture
5. 绘制三部曲
5.1 清空渲染器上一次的显示 SDL\_RenderClear
/// 一些绘制操作 SDL\_SetRenderDrawColor SDL\_RenderFillRect SDL\_RenderDrawRect
5.2 拷贝纹理数据到渲染器 SDL\_RenderCopy
5.3 更新渲染器显示 SDL\_RenderPresent
6. 释放资源,退出 SDL\_DestroyTexture SDL\_DestroyRenderer SDL\_DestroyWindow SDL\_Quit
#include <iostream>
/* 纹理和渲染(方块跳动) */
extern "C"
{
#include <SDL.h>
}
#pragma comment(lib, "SDL2.lib")
#undef main
int main()
{
// 1. 初始化
int nRet = SDL_Init(SDL_INIT_EVERYTHING);
if (nRet < 0)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 2. 创建窗口
SDL_Window* pWnd = SDL_CreateWindow("Title",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_SHOWN);
if (NULL == pWnd)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 3. 创建渲染器
SDL_Renderer* pRenderer = SDL_CreateRenderer(pWnd, -1, 0);
if (NULL == pRenderer)
{
SDL_DestroyWindow(pWnd);
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 4. 创建纹理
SDL_Texture* pTexture = SDL_CreateTexture(pRenderer, // 渲染器
SDL_PIXELFORMAT_RGBA8888, // 渲染数据格式(YUV RGB)
SDL_TEXTUREACCESS_TARGET, // 纹理类型 static、stream、target
640, 480); // 纹理宽高
if (NULL == pTexture)
{
SDL_DestroyRenderer(pRenderer);
SDL_DestroyWindow(pWnd);
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
SDL_Event event;
SDL_Rect rect;
rect.w = 50, rect.h = 50;
while (true)
{
SDL_PollEvent(&event); // 获取事件
if (event.type == SDL_QUIT)
break;
rect.x = rand() % 640; // 随机方块的位置
rect.y = rand() % 480;
SDL_SetRenderTarget(pRenderer, pTexture); // 设置渲染器目标(此时绘制目标为整个纹理)
SDL_SetRenderDrawColor(pRenderer, 0xFF, 0x00, 0x00, 0x00); // 设置渲染器绘制颜色(背景颜色)(不设置,下一次循环渲染器绘制颜色为下面设置的颜色)
SDL_RenderClear(pRenderer); // 刷新渲染器(将渲染器上次渲染清空)
SDL_SetRenderDrawColor(pRenderer, 0x00, 0xFF, 0x00, 0x00); // 设置渲染器绘制颜色
SDL_RenderDrawRect(pRenderer, &rect); // 绘制矩形
SDL_RenderFillRect(pRenderer, &rect); // 绘制填充矩形
// 渲染三部曲:1. 清空上一次的显示 2. 拷贝纹理数据到渲染器 3. 更新渲染器显示
SDL_SetRenderTarget(pRenderer, NULL); // 清除一下渲染器的目标
SDL_RenderCopy(pRenderer, pTexture, NULL, NULL); // 拷贝纹理绘制信息到渲染器
SDL_RenderPresent(pRenderer); // 更新渲染器
SDL_Delay(500);
}
// 释放纹理
if (pTexture)
{
SDL_DestroyTexture(pTexture);
}
// 释放渲染
if (pRenderer)
{
SDL_DestroyRenderer(pRenderer);
}
// 释放窗口
if (pWnd)
{
SDL_DestroyWindow(pWnd);
}
SDL_Quit();
return 0;
}