http://wenku.baidu.com/view/c953c0878762caaedd33d4d8.html

一、安装:

sudo apt-get install libsdl1.2-dev libsdl-image1.2-dev libsdl-ttf2.0-dev libsdl-mixer1.2-dev libsdl-net1.2-dev libsdl-sound1.2-dev

二、编译测试用例:

sdl: 添加`sdl-config --cflags --libs` // 或-lSDL

opengl : -lGL -LGLU

三、函数详解:

1.初始化,反初始化

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)            //

SDL_INIT_EVERYTHING

初始化音视频以及timer库

SDL_Quit();

结束

2.初始化display

SDL_Surface* screen = NULL;

//Initialize the display in a 640x480 8-bit palettized mode,requesting a software surface

screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);

3.timer&callback

SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void* param);

typedef Uint32 (*SDL_NewTimerCallback)(Uint32 interval, void* param);

SDL_WaitEvent(&event);

SDL_PollEvent()

例子:

  {
... delay = ;
my_timer_id = SDL_AddTimer(delay, my_callbackfunc, my_callback_param); ...
} Uint32 my_callbackfunc(Uint32 interval, void *param)
{
SDL_Event event;
SDL_UserEvent userevent; userevent.type = SDL_USEREVENT;
userevent.code = ;
userevent.data1 = &my_func;
userevent.data2 = NULL; event.type = SDL_USEREVENT;
event.user = userevent; SDL_PushEvent(&event);
return(interval);
} {
...
SDL_Event event;
while (SDL_PollEvent (&event)) {
switch(event.type) {
case SDL_USEREVENT: {
void (*p) (void*) = event.user.data1;
p(event.user.data2);
break;
}
}
}
...
} void CheckMouseHover(void)
{
int mouse_x, mouse_y; SDL_PumpEvents(); SDL_GetMouseState(&mouse_x, &mouse_y);
if ( (mouse_x < ) && (mouse_y < ) ) {
printf("鼠标在左上角!\n");
}
}

检测鼠标事件例子:

 #include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h> #define bool int
#define false 0
#define true 1 SDL_Surface * screen = NULL;
const int SCREEN_BPP = ; int main( int argc, char * args[] )
{
// Start SDL bool quit = false ;
SDL_Rect rectLeft;
SDL_Rect rectRight;
rectLeft.x = ;
rectLeft.y = ;
rectLeft.w = ;
rectLeft.h = ;
rectRight.x = ;
rectRight.y = ;
rectRight.w = ;
rectRight.h = ; SDL_Init( SDL_INIT_EVERYTHING ); screen = SDL_SetVideoMode( , , SCREEN_BPP, SDL_SWSURFACE );
if (screen == NULL)
return false ; Uint32 colorBlue = SDL_MapRGB(screen -> format, , , );
Uint32 colorGreen = SDL_MapRGB(screen -> format, , , );
Uint32 colorRed = SDL_MapRGB(screen -> format, , , );
Uint32 colorBlack = SDL_MapRGB(screen -> format, , , ); SDL_Event event ;
while ( ! quit) {
if (SDL_PollEvent( & event )) {
switch (event.type) {
case SDL_MOUSEMOTION:
{
Uint16 x = event .motion.x;
Uint16 y = event .motion.y; if (x > && x < && y > && y < ) {
SDL_FillRect(screen, & rectLeft,colorBlue);
SDL_FillRect(screen, & rectRight,colorBlack);
} if (x > && x < && y > && y < ) {
SDL_FillRect(screen, & rectRight,colorGreen);
SDL_FillRect(screen, & rectLeft,colorBlack);
}
break;
} case SDL_MOUSEBUTTONDOWN:
{
Uint16 x = event .motion.x;
Uint16 y = event .motion.y;
if ( event .button.button == SDL_BUTTON_LEFT) {
if (x > && x < && y > && y < )
SDL_FillRect(screen, & rectLeft,colorRed); if (x > && x < && y > && y < )
SDL_FillRect(screen, & rectRight,colorRed);
}
break;
}
case SDL_QUIT:
quit = true ;
break; default :
break;
}
} if (SDL_Flip(screen) == - )
return false ;
} SDL_FreeSurface(screen);
SDL_Quit(); return ;
}

4.SDL_LoadBMP解析图片

例子:

 #include <stdio.h>
#include <SDL/SDL.h> SDL_Surface *screen = NULL;
const int SCREEN_BPP = ; void display_bmp(char* file_name)
{
SDL_Surface* image = NULL; /* Load the BMP file into a surface */
image = SDL_LoadBMP(file_name);
if (image == NULL) {
fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError());
return;
} /*
* Palettized screen modes will have a default palette (a standard
* 8*8*4 colour cube), but if the image is palettized as well we can
* use that palette for a nicer colour matching
*/
if (image->format->palette && screen->format->palette) {
SDL_SetColors(screen, image->format->palette->colors, , image->format->palette->ncolors);
} /* Blit onto the screen surface */
if (SDL_BlitSurface(image, NULL, screen, NULL) < )
fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError()); /* Update the modified region of the screen */
SDL_UpdateRect(screen, , , image->w, image->h); /* Free the allocated BMP surface */
SDL_FreeSurface(image);
} int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_EVERYTHING); screen = SDL_SetVideoMode( , , SCREEN_BPP, SDL_SWSURFACE );
if (screen == NULL)
return -; display_bmp("test.bmp"); getchar();
return ;
}

5.OPENGL&SDL

 /*
* SDL OpenGL Tutorial.
* (c) Michael Vance, 2000
* briareos@lokigames.com
*
* Distributed under terms of the LGPL.
*/ #include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h> #include <stdio.h>
#include <stdlib.h> static GLboolean should_rotate = GL_TRUE; static void quit_tutorial( int code )
{
/*
* Quit SDL so we can release the fullscreen
* mode and restore the previous video settings, etc.
*/
SDL_Quit( ); /* Exit program. */
exit( code );
} static void handle_key_down( SDL_keysym* keysym )
{
/*
* We're only interested if 'Esc' has been presssed.
*
* EXERCISE:
* Handle the arrow keys and have that change the
* viewing position/angle.
*/
switch( keysym->sym ) {
case SDLK_ESCAPE:
quit_tutorial( );
break;
case SDLK_SPACE:
should_rotate = !should_rotate;
break;
default:
break;
}
} static void process_events( void )
{
/* Our SDL event placeholder. */
SDL_Event event; /* Grab all the events off the queue. */
while( SDL_PollEvent( &event ) ) {
switch( event.type ) {
case SDL_KEYDOWN:
/* Handle key presses. */
handle_key_down( &event.key.keysym );
break; case SDL_QUIT:
/* Handle quit requests (like Ctrl-c). */
quit_tutorial( );
break; default:
break;
}
} /* while */
} static void draw_screen( void )
{
/* Our angle of rotation. */
static float angle = 0.0f;
/*
* EXERCISE:
* Replace this awful mess with vertex
* arrays and a call to glDrawElements.
*
* EXERCISE:
* After completing the above, change
* it to use compiled vertex arrays.
*
* EXERCISE:
* Verify my windings are correct here ;).
*/ static GLfloat v0[] = { -1.0f, -1.0f, 1.0f };
static GLfloat v1[] = { 1.0f, -1.0f, 1.0f };
static GLfloat v2[] = { 1.0f, 1.0f, 1.0f };
static GLfloat v3[] = { -1.0f, 1.0f, 1.0f };
static GLfloat v4[] = { -1.0f, -1.0f, -1.0f };
static GLfloat v5[] = { 1.0f, -1.0f, -1.0f };
static GLfloat v6[] = { 1.0f, 1.0f, -1.0f };
static GLfloat v7[] = { -1.0f, 1.0f, -1.0f };
static GLubyte red[] = { , , , };
static GLubyte green[] = { , , , };
static GLubyte blue[] = { , , , };
static GLubyte white[] = { , , , };
static GLubyte yellow[] = { , , , };
static GLubyte black[] = { , , , };
static GLubyte orange[] = { , , , };
static GLubyte purple[] = { , , , }; /* Clear the color and depth buffers. */
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); /* We don't want to modify the projection matrix. */
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( ); /* Move down the z-axis. */
glTranslatef( 0.0, 0.0, -5.0 ); /* Rotate. */
glRotatef( angle, 0.0, 1.0, 0.0 ); if ( should_rotate ) {
if ( ++angle > 360.0f ) {
angle = 0.0f;
}
} /* Send our triangle data to the pipeline. */
glBegin( GL_TRIANGLES ); glColor4ubv( red );
glVertex3fv( v0 );
glColor4ubv( green );
glVertex3fv( v1 );
glColor4ubv( blue );
glVertex3fv( v2 ); glColor4ubv( red );
glVertex3fv( v0 );
glColor4ubv( blue );
glVertex3fv( v2 );
glColor4ubv( white );
glVertex3fv( v3 );
glColor4ubv( green );
glVertex3fv( v1 );
glColor4ubv( black );
glVertex3fv( v5 );
glColor4ubv( orange );
glVertex3fv( v6 ); glColor4ubv( green );
glVertex3fv( v1 );
glColor4ubv( orange );
glVertex3fv( v6 );
glColor4ubv( blue );
glVertex3fv( v2 );
glColor4ubv( black );
glVertex3fv( v5 );
glColor4ubv( yellow );
glVertex3fv( v4 );
glColor4ubv( purple );
glVertex3fv( v7 ); glColor4ubv( black );
glVertex3fv( v5 );
glColor4ubv( purple );
glVertex3fv( v7 );
glColor4ubv( orange );
glVertex3fv( v6 );
glColor4ubv( yellow );
glVertex3fv( v4 );
glColor4ubv( red );
glVertex3fv( v0 );
glColor4ubv( white );
glVertex3fv( v3 ); glColor4ubv( yellow );
glVertex3fv( v4 );
glColor4ubv( white );
glVertex3fv( v3 );
glColor4ubv( purple );
glVertex3fv( v7 );
glColor4ubv( white );
glVertex3fv( v3 );
glColor4ubv( blue );
glVertex3fv( v2 );
glColor4ubv( orange );
glVertex3fv( v6 ); glColor4ubv( white );
glVertex3fv( v3 );
glColor4ubv( orange );
glVertex3fv( v6 );
glColor4ubv( purple );
glVertex3fv( v7 );
glColor4ubv( green );
glVertex3fv( v1 );
glColor4ubv( red );
glVertex3fv( v0 );
glColor4ubv( yellow );
glVertex3fv( v4 );
glColor4ubv( green );
glVertex3fv( v1 );
glColor4ubv( yellow );
glVertex3fv( v4 );
glColor4ubv( black );
glVertex3fv( v5 );
glEnd( ); /*
* EXERCISE:
* Draw text telling the user that 'Spc' pauses the rotation and 'Esc' quits.
* Do it using vetors and textured quads.
*/ /*
* Swap the buffers. This this tells the driver to
* render the next frame from the contents of the
* back-buffer, and to set all rendering operations
* to occur on what was the front-buffer.
*
* Double buffering prevents nasty visual tearing
* from the application drawing on areas of the
* screen that are being updated at the same time.
*/
SDL_GL_SwapBuffers( );
} static void setup_opengl( int width, int height )
{
float ratio = (float) width / (float) height; /* Our shading model--Gouraud (smooth). */
glShadeModel( GL_SMOOTH ); /* Culling. */
glCullFace( GL_BACK );
glFrontFace( GL_CCW );
glEnable( GL_CULL_FACE ); /* Set the clear color. */
glClearColor( , , , ); /* Setup our viewport. */
glViewport( , , width, height ); /*
* Change to the projection matrix and set
* our viewing volume.
*/
glMatrixMode( GL_PROJECTION );
glLoadIdentity( ); /*
* EXERCISE:
* Replace this with a call to glFrustum.
*/
gluPerspective( 60.0, ratio, 1.0, 1024.0 );
} int main( int argc, char* argv[] )
{
/* Information about the current video settings. */
const SDL_VideoInfo* info = NULL; /* Dimensions of our window. */
int width = ;
int height = ; /* Color depth in bits of our window. */
int bpp = ; /* Flags we will pass into SDL_SetVideoMode. */
int flags = ; /* First, initialize SDL's video subsystem. */
if ( SDL_Init( SDL_INIT_VIDEO ) < ) {
/* Failed, exit. */
fprintf( stderr, "Video initialization failed: %s\n", SDL_GetError( ) );
quit_tutorial( );
} /* Let's get some video information. */
info = SDL_GetVideoInfo( ); if ( !info ) {
/* This should probably never happen. */
fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) );
quit_tutorial( );
} /*
* Set our width/height to 640/480 (you would of course let the user
* decide this in a normal app). We get the bpp we will request from
* the display. On X11, VidMode can't change resolution, so this is
* probably being overly safe. Under Win32, ChangeDisplaySettings
* can change the bpp.
*/ width = ;
height = ;
bpp = info->vfmt->BitsPerPixel; /*
* Now, we want to setup our requested
* window attributes for our OpenGL window.
* We want *at least* 5 bits of red, green
* and blue. We also want at least a 16-bit
* depth buffer.
*
* The last thing we do is request a double
* buffered window. '1' turns on double
* buffering, '0' turns it off.
*
* Note that we do not use SDL_DOUBLEBUF in
* the flags to SDL_SetVideoMode. That does
* not affect the GL attribute state, only
* the standard 2D blitting setup.
*/ SDL_GL_SetAttribute( SDL_GL_RED_SIZE, );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, ); /*
* We want to request that SDL provide us
* with an OpenGL window, in a fullscreen
* video mode.
*
* EXERCISE:
* Make starting windowed an option, and
* handle the resize events properly with
* glViewport.
*/
flags = SDL_OPENGL | SDL_FULLSCREEN; /*
* Set the video mode
*/
if ( SDL_SetVideoMode( width, height, bpp, flags ) == ) {
/*
* This could happen for a variety of reasons,
* including DISPLAY not being set, the specified
* resolution not being available, etc.
*/
fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) );
quit_tutorial( );
} /*
* At this point, we should have a properly setup
* double-buffered window for use with OpenGL.
*/
setup_opengl( width, height ); /*
* Now we want to begin our normal app process--
* an event loop with a lot of redrawing.
*/
setup_opengl( width, height ); /*
* Now we want to begin our normal app process--
* an event loop with a lot of redrawing.
*/
while () {
/* Process incoming events. */
process_events( ); /* Draw the screen. */
draw_screen( ); /* Avoid to eat all the CPU power */
SDL_Delay( );
} /*
* EXERCISE:
* Record timings using SDL_GetTicks() and
* and print out frames per second at program
* end.
*/ /* Never reached. */
return ;
}

最新文章

  1. 如何在web中实现类似excel的表格控件
  2. python中的常用方法
  3. javascript设计模式与开发实践
  4. Inside The C++ Object Model - 02
  5. 25个实用的jquery技巧
  6. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
  7. Linux内核文档翻译之Squashfs文件系统
  8. Git for Windows
  9. C#操作Xml:如何定义Xsd文件
  10. navicat 连接远程mysql
  11. 2个byte类型数据相加(转型问题的分析)
  12. Adobe Flash Player 29.0.0.140官方正式版
  13. 版本控制--git+idea
  14. [HDFS Manual] CH8 HDFS Snapshots
  15. 普通PC安装ESXi5.5以及以上的方法
  16. Ubuntu 安装nginx
  17. Devexpress RichEditControl 导入word文件后字体变为方正姚体的解决方案
  18. Python 入门(二)Unicode字符串
  19. Eclipse 使用mybatis generator插件自动生成代码
  20. Python 开发者的 6 个必备库,你都了解吗?

热门文章

  1. struts拦截器实现原理
  2. android 入门-android Studio 解决方案
  3. [Linux] yum和apt-get用法及区别
  4. 限定符【const】用法大全
  5. 如何更快的删除String中的空格[未完]
  6. 为GDI函数增加透明度处理
  7. C# jsonhelper
  8. ubuntu初始化root帐号密码
  9. Vue#Class 与 Style 绑定
  10. PC端重置