要在Ubuntu上搭建SDL2的开发环境,可以按照以下步骤进行:
sudo apt-get update
sudo apt-get install libsdl2-2.0-0 libsdl2-dev
sudo apt-get install build-essential
#include <SDL2/SDL.h>
int main() {
SDL_Init(SDL_INIT_VIDEO);
// 创建一个窗口
SDL_Window* window = SDL_CreateWindow("SDL2 Window",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN);
// 创建一个渲染器
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED);
// 渲染一个绿色的矩形
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderClear(renderer);
SDL_Rect rect = { 200, 200, 400, 200 };
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
// 等待用户退出
SDL_Delay(3000);
// 释放资源
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
gcc -o sdl2_example sdl2_example.c `sdl2-config --cflags --libs`
./sdl2_example
通过上述步骤,您可以在Ubuntu上搭建SDL2的开发环境,并开始编写和运行SDL2应用程序。