要在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應用程序。