SDL2 例子代码
SDL经常用到的版本是1.2,很多网络上的代码都是基于这个版本。后来出了2.0,叫之前的代码较大的改变,下面列出变化:
- Full 3D hardware acceleration
- Support for OpenGL 3.0+ in various profiles (core, compatibility, debug, robust, etc)
- Support for OpenGL ES
- Support for multiple windows
- Support for multiple displays
- Support for multiple audio devices
- Android and iOS support
- Emscripten and Native Client support
- Simple 2D rendering API that can use Direct3D, OpenGL, OpenGL ES, or software rendering behind the scenes
- Force Feedback available on Windows, Mac OS X and Linux
- XInput and XAudio2 support for Windows
- Atomic operations
- Power management (exposes battery life remaining, etc)
- Shaped windows
- 32-bit audio (int and float)
- Simplified Game Controller API (the Joystick API is still here, too!)
- Touch support (multitouch, gestures, etc)
- Better fullscreen support
- Better keyboard support (scancodes vs keycodes, etc).
- Message boxes
- Clipboard support
- Basic Drag'n'Drop support
- Proper unicode input and IME support
- zlib license instead of LGPL.
- Lots of old annoyances from 1.2 are gone
- Many other things!
这不是本文的重点,重点是函数和使用方法也改变了,因此本文就新版本的sdl,给出实例代码:
-
#include <iostream>
#include <errno.h>
#include "SDL.h"
using namespace std;int sdlBasic( void )
{
// init SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
{
cout<<"failed to init SDL"<<endl;
return -1;
}
// create SDL window and render
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_SHOWN, &sdlWindow, &sdlRenderer);// main loop
while( 1 )
{
// clear background
SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
SDL_RenderClear(sdlRenderer);// render
SDL_RenderPresent(sdlRenderer);// sleep
SDL_Delay( 1 );
}
// quit SDL
SDL_Quit();
}int sdlRect( void )
{
// init SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
{
cout<<"failed to init SDL"<<endl;
return -1;
}
// create SDL window and render
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_SHOWN, &sdlWindow, &sdlRenderer);// main loop
SDL_Rect rc;
rc.x = 100;
rc.y = 100;
rc.w = 200;
rc.h = 200;
while( 1 )
{
// clear background
SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
SDL_RenderClear(sdlRenderer);// draw a rectangle
SDL_SetRenderDrawColor( sdlRenderer, 0, 0, 255, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect( sdlRenderer, &rc);// render
SDL_RenderPresent(sdlRenderer);// sleep
SDL_Delay( 1 );
}
// quit SDL
SDL_Quit();
}int sdlAnimatedRect( void )
{
// init SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
{
cout<<"failed to init SDL"<<endl;
return -1;
}
// create SDL window and render
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_SHOWN, &sdlWindow, &sdlRenderer);// main loop
int r = 0;
int g = 0;
int b = 255;
SDL_Rect rc;
rc.x = 100;
rc.y = 100;
rc.w = 200;
rc.h = 200;
while( 1 )
{
// clear background
SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
SDL_RenderClear(sdlRenderer);// draw a rectangle
//r = r > 255 ? 0 : (++r);
//b = b > 255 ? 0 : (++b);
g = g > 255 ? 0 : (++g);
SDL_SetRenderDrawColor( sdlRenderer, r, g, b, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect( sdlRenderer, &rc);// render
SDL_RenderPresent(sdlRenderer);// sleep
SDL_Delay( 1 );
}
// quit SDL
SDL_Quit();
}int sdlRenderImage( void )
{
// init SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
{
cout<<"failed to init SDL"<<endl;
return -1;
}
// create SDL window and render
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_SHOWN, &sdlWindow, &sdlRenderer);
SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer,
SDL_PIXELFORMAT_RGB24,
SDL_TEXTUREACCESS_STREAMING,
274, 251);// main loop
FILE *pf = fopen( "test.ppm", "rb" );
if( pf == NULL )
{
cout<<"failed to open image file:"<<errno<<endl;
return -2;
}
//fseek( pf, 15, SEEK_SET );
char szHeader[16] = { 0 };
fread( szHeader, 1, 15, pf );
//cout<<hex<<szHeader<<endl;
unsigned int size = 274*251;
unsigned char* pData = new unsigned char[size*3];
fread( pData, 1, size*3, pf );
fclose( pf );
//uint32_t* pData32 = new uint32_t[size*sizeof(uint32_t)];
//for( int i = 0; i < size * 3; i+=3 )
//{
// pData32[i] = pData[i];
// pData32[i+1] = pData[i+1];
// pData32[i+2] = pData[i+2];
// pData32[i+3] = 255;
//}
while( 1 )
{
// clear background
SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
SDL_RenderClear(sdlRenderer);// draw image
SDL_UpdateTexture(sdlTexture, NULL, pData, 274 * 3 );
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);// render
SDL_RenderPresent(sdlRenderer);// sleep
SDL_Delay( 1 );
}
// quit SDL
SDL_Quit();
}int sdlEvent( void )
{
// init SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
{
cout<<"failed to init SDL"<<endl;
return -1;
}
// create SDL window and render
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_SHOWN, &sdlWindow, &sdlRenderer);
SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer,
SDL_PIXELFORMAT_RGB24,
SDL_TEXTUREACCESS_STREAMING,
800, 600);// load image
FILE *pf = fopen( "test.ppm", "rb" );
if( pf == NULL )
{
cout<<"failed to open image file:"<<errno<<endl;
return -2;
}
//fseek( pf, 15, SEEK_SET );
char szHeader[16] = { 0 };
fread( szHeader, 1, 15, pf );
unsigned int size = 274*251;
unsigned char* pData = new unsigned char[size*3];
fread( pData, 1, size*3, pf );
fclose( pf );
SDL_Event event ;
// main loop
bool bExit = false;
while( !bExit )
{
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT: bExit = true; break;
case SDL_KEYDOWN:
cout<<event.key.keysym.sym<<endl;
if( event.key.keysym.sym == 113 )
bExit = true;
break ;
case SDL_KEYUP:
break ;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION:
break ;
}
}// clear background
SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
SDL_RenderClear(sdlRenderer);// draw image
SDL_Rect rc = { 100, 100, 274, 251 };
SDL_UpdateTexture(sdlTexture, &rc, pData, 274 * 3 );
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);// render
SDL_RenderPresent(sdlRenderer);// sleep
SDL_Delay( 1 );
}
// quit SDL
SDL_Quit();return 0;
}int main( int argc, char** argv )
{
if( argc < 2 )
return -1;
if( 0 == strcmp( argv[1], "1" ))
{
sdlBasic();
}
else if( 0 == strcmp( argv[1], "2" ))
{
sdlRect();
}
else if( 0 == strcmp( argv[1], "3" ))
{
sdlAnimatedRect();
}
else if( 0 == strcmp( argv[1], "4" ))
{
sdlRenderImage();
}
else if( 0 == strcmp( argv[1], "5" ))
{
sdlEvent();
}
else
{
}
return 0;
}
将上面的文件保存位main.cpp
然后再用下面的命令编译
g++ main.cpp -Wall -g -I/xxx/SDL2-2.0.1/include /xxx/SDL2-2.0.1/build/.libs/libSDL2.a -lpthread -lrt -ldl -o sdltest
将xxx换成你自己的sdl的源码路径
libSDL2.a这个文件需要是编译sdl之后才有的
上面的命令会编译出sdltest,然后可以运行它
$ ./sdltest [1 2, 3, 4, 5]
参数是1,2,3,4,5的其中一个。
参考:
https://wiki.libsdl.org/MigrationGuide
版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章转载自:IT夜班车,否则按侵权处理.