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 ;
}

SDL1.2学习的更多相关文章

  1. (一) ffmpeg filter学习-使用流程

    FFMPEG中有一个类库:libavfilter.该类库提供了各种视音频过滤器.之前一直没有怎么使用过这个类库,最近看了一下它的使用说明,发现还是很强大的,有很多现成的filter供使用,完成视频的处 ...

  2. FFmpeg再学习 -- SDL 环境搭建和视频显示

    继续看雷霄骅的 课程资料 - 基于FFmpeg+SDL的视频播放器的制作 一.SDL 简介 参看:WIKI -- Simple DirectMedia Layer 参看:最简单的视音频播放示例9:SD ...

  3. 100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x)【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/8652605 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] ...

  4. 从直播编程到直播教育:LiveEdu.tv开启多元化的在线学习直播时代

    2015年9月,一个叫Livecoding.tv的网站在互联网上引起了编程界的注意.缘于Pingwest品玩的一位编辑在上网时无意中发现了这个网站,并写了一篇文章<一个比直播睡觉更奇怪的网站:直 ...

  5. Angular2学习笔记(1)

    Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...

  6. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

  7. 消息队列——RabbitMQ学习笔记

    消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...

  8. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  9. Unity3d学习 制作地形

    这周学习了如何在unity中制作地形,就是在一个Terrain的对象上盖几座小山,在山底种几棵树,那就讲一下如何完成上述内容. 1.在新键得项目的游戏的Hierarchy目录中新键一个Terrain对 ...

随机推荐

  1. [LeetCode] Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  2. float 的有效数字为七位是怎么得出来的

    以下内容来自CSDN网友xian_wwq的回答(http://bbs.csdn.net/topics/390874239): float:   1bit(符号位) 8bits(指数位) 23bits( ...

  3. <转> jsp:include 乱码问题解决

    jsp include页面出现乱码问题的几种通用解决方法: 1.当jsp include动态文件时(jsp文件)可以在被include的jsp文件头部加上代码: <%@ page languag ...

  4. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 333人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  5. golang 索引

    入门的基础路线 a Tour of GoEffective GoGo By Example 以上的三部分通读算是入门. 4个重要的组成部分 1. 基础知识2. 并发特性3. 异常处理4. 常用开源项目 ...

  6. RMAN恢复目录

    是否使用RMAN恢复目录(Recovery Catalog 你可能从其他人或书上听过RMAN恢复目录(也有可能是其他名字,RMAN Recovery Catalog的翻译较多较杂,以下简称恢复目录), ...

  7. 第四篇:SOUI资源文件组织

    什么是资源? 现代的软件只要有UI,基本上少不了资源. 资源是什么?资源就是在程序运行时提供固定的数据源的文件. 在MFC当道的时代,资源一般就是位图(Bitmap),图标(Icon),光标(Curs ...

  8. Android SDK Manager 中如果没有相应的镜像ARM XX Image

    Android SDK Manager 中如果没有相应的镜像ARM XX Image 处理做法是:先更新 相应版本Android SDK Tools 然后出现 ARM XX Image

  9. web2py学习之getting start环境搭建

    一般如果做一个工程,可能需要ide,需要好的工具,web2py自包含了一个基于web的开发工具,但是并不算很好的编辑器 第二个可以使用的是pycharm,利用pycharm可以创建web2py的web ...

  10. json方式的面向对象

    json方式只适合于一个对象. var p1 = { name:"香菇", sex :"女", dreamdu: { URL: "www.dreamd ...