SDL创建窗口有相应的步骤,如下
(1)调用SDL_CreateWindow,
(2)创建渲染器SDL_CreateRenderer,
(3)显示,调用SDL_RenderPresent,在显示前最好清空之前的显示,调用SDL_RenderClear,
根据上述步骤,编写SDL窗口代码,如下:
#include <iostream>
#include "SDL.h"
using namespace std;
int main(int argc, char *argv[]) //main函数必须要有参数
{
SDL_Window *pWindow = NULL;
SDL_Renderer *pRender = NULL;
//1 SDL初始化
SDL_Init(SDL_INIT_VIDEO);
// 2 创建窗口
pWindow = SDL_CreateWindow("SDL2 Window", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS);
if (!pWindow)
{
cout << "window create failed" << endl;
SDL_Quit();
return -1;
}
cout << "Window Create Success" << endl;
//3 创建Render
pRender = SDL_CreateRenderer(pWindow, -1, 0);
if (!pRender)
{
SDL_Log("Create Render Failed");
SDL_DestroyWindow(pWindow);
return -1;
}
SDL_SetRenderDrawColor(pRender, 255, 0, 0, 255); //设置窗口颜色,默认是黑色
SDL_RenderClear(pRender); //清空之前的数
//4 显示render
SDL_RenderPresent(pRender);
SDL_Delay(30000); //延时显示30秒,不然窗口一闪而过
// 销毁窗口
SDL_DestroyWindow(pWindow);
// SDL退出
SDL_Quit();
return 0;
}
如果不用延时,窗口在显示后将一闪而过,也可以用SDL事件来处理。
来源:oschina
链接:https://my.oschina.net/u/4381686/blog/4668927