www.libsdl.orgSDL(Simple DirectMedia Layer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成。
SDL提供了数种控制图像、声音、输出入的函数,让开发者只要用相同或是相似的代码就可以开发出跨多个平台(Linux、Windows、Mac OS X等)的应用软件。
目前SDL多用于开发游戏、模拟器、媒体播放器等多媒体应用领域。
#include <SDL.h>
int main(int argc, char* argv[])
{
SDL_Window* pWindow = 0;
SDL_Renderer* pRenderer = 0;
// initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// if succeeded create our window
pWindow = SDL_CreateWindow("testSDL",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN);
// if the window creation succeeded create our renderer
if (g_pWindow != 0)
{
pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
}
}
else
return 1; // sdl could not initialize
// everything succeeded lets draw the window
bool bQuit = false;
while (true)
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
bQuit = true;
break;
}
}
if (bQuit)
break;
// set to black
// This function expects Red, Green, Blue and Alpha as color values
SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 255);
// clear the window to black
SDL_RenderClear(pRenderer);
// ... draw something ...
// show the window
SDL_RenderPresent(pRenderer);
}
SDL_DestroyRenderer(pRenderer);
SDL_DestroyWindow(pWindow);
// clean up SDL
SDL_Quit();
return 0;
}