SDL2入门demo
Simple DirectMedia Layer is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. It is used by video playback software, emulators, and popular games including Valve’s award winning catalog and many Humble Bundle games.
安装
下载网址:https://www.libsdl.org/download-2.0.php
wget https://www.libsdl.org/release/SDL2-2.0.14.tar.gz;
tar zxvf SDL2-2.0.14.tar.gz
cd SDL2-2.0.14/
/configure --prefix=/usr/local/sdl2
make -j4
sudo make install
测试
#include <iostream>
extern "C" {
#include <SDL2/SDL.h>
}
using namespace std;
const int WIDTH = 400, HEIGHT = 400; // SDL窗口的宽和高
int main() {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { // 初始化SDL
cout << "SDL could not initialized with error: " << SDL_GetError() << endl;
}
SDL_Window *window = SDL_CreateWindow("Hello SDL world!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI); // 创建SDL窗口
if (NULL == window) {
cout << "SDL could not create window with error: " << SDL_GetError() << endl;
}
SDL_Event windowEvent; // SDL窗口事件
while(true) {
if (SDL_PollEvent(&windowEvent)) { // 对当前待处理事件进行轮询。
if (SDL_QUIT == windowEvent.type) { // 如果事件为推出SDL,结束循环。
cout << "SDL quit!!" << endl;
break;
}
}
}
SDL_DestroyWindow(window); // 推出SDL窗体
SDL_Quit(); // SDL推出
return 0;
}