[总结]SDL知识介绍

目录

0 参考

1 SDL简介

1.1 特点

1.2 架构

1.3 组成

1.3.1 子系统 SubSystem

1.3.2 扩充库

1.4 API分类以及查询

2 SDL移植到VS中使用

3 简单几个SDL示例VS工程

扫描二维码关注公众号,回复: 5517138 查看本文章

3.1 使用SDL显示本地的BMP图片

3.2 使用SDL渲染视频

3.3 使用SDL渲染音频

4 SDL源码解析


0 参考


Wikipedia: https://en.wikipedia.org/wiki/Simple_DirectMedia_Layer
SDL官网:http://www.libsdl.org/
SDL官网wiki: http://wiki.libsdl.org/FrontPage
雷神Blog:   SDL介绍  SDL源代码分析系列
SDL1.2中文教程: http://kelvmiao.info/sdl-tutorial-cn/

1 SDL简介


1.1 特点


SDL(Simple DirectMedia Layer)是一个跨平台的多媒体库,它通过OpenGL和2D视频帧缓冲,提供了针对音频、视频、键盘、鼠标、控制杆及3D硬件的低级别的访问接口。它在MPEG播放软件、模拟器以及许多游戏中得到广泛的应用,其中包含了获得大奖的“文明:权力的呼唤”的Linux 版本。

SDL 正式支持Windows, Mac OS X, Linux, iOS, and Android. 同时代码中包含了针对AmigaOS, Dreamcast, Atari, AIX, OSF/Tru64, RISC OS, SymbianOS, 和OS/2的支持,但这些并不是正式的支持。

SDL 是用C编写的,但可以原生地配合C++使用,并且它拥有一些其他程序语言的绑定,这包括:Ada, C#, D, Eiffel, Erlang, Euphoria, Go, Guile, Haskell, Java, Lisp, Lua, ML, Objective C, Pascal, Perl, PHP, Pike, Pliant, Python, Ruby, Smalltalk, 以及 Tcl。

SDL 采用GNU的LGPL第二版许可发行。该许可允许你自由地链接其动态链接库,甚至可以在商业软件中自由地使用。意味着动态链接(dynamic link)其库并不需要开放本身的源代码。

1.2 架构


SDL在结构上是将不同操作系统的库再包装成相同的函数,例如SDL在Windows平台上其实是DirectX的再包装。而在使用X11的平台上(包括Linux),SDL则是与Xlib库沟通来输出图像。虽然SDL本身是使用C语言写成,但是它几乎可以被所有的编程语言所使用,例如:C++、Perl、Python(借由pygame库)、Pascal等等,甚至是Euphoria、Pliant这类较不流行的编程语言也都可行。

1.3 组成


1.3.1 子系统 SubSystem

SDL官方编译好的库分为 Video、Audio、Input Event、Joystick 和 Timer 等若干子系统,各子系统功能介绍如下

Video

  • 3D graphics:
    • SDL can be used in combination with the OpenGL API or Direct3D API for 3D graphics
  • Accelerated 2D render API:
    • Supports easy rotation, scaling and alpha blending, all accelerated using modern 3D APIs
    • Acceleration is supported using OpenGL and Direct3D, and there is a software fallback
  • Create and manage multiple windows

Input Events

  • Events and API functions provided for:
    • Application and window state changes
    • Mouse input
    • Keyboard input
    • Joystick and game controller input
    • Multitouch gestures
  • Each event can be enabled or disabled with SDL_EventState()

  • Events are passed through a user-specified filter function before being posted to the internal event queue
  • Thread-safe event queue

Force Feedback

  • Force feedback is supported under Windows, Mac OS X and Linux

Audio

  • Set audio playback of 8-bit and 16-bit audio, mono stereo or 5.1 surround sound, with optional conversion if the format is not supported by the hardware
  • Audio runs independently in a separate thread, filled via a user callback mechanism
  • Designed for custom software audio mixers, but SDL_mixer provides a complete audio/music output library

File I/O Abstraction

  • General purpose abstraction for opening, reading and writing data
  • Built-in support for files and memory

Shared Object Support

  • Load shared objects (DLL on Windows, .dylib on Mac OS X, .so on Linux)
  • Lookup functions in shared objects

Threads

  • Simple thread creation API
  • Simple thread local storage API
  • Mutexes, semaphores and condition variables
  • Atomic operations for lockless programming

Timers

  • Get the number of milliseconds elapsed
  • Wait a specified number of milliseconds
  • Create timers that run alongside your code in a separate thread
  • Use high resolution counter for profiling

CPU Feature Detection

  • Query the number of CPUs
  • Detect CPU features and supported instruction sets

Endian Independence

  • Detect the endianness of the current system
  • Routines for fast swapping of data values
  • Read and write data of a specified endianness

Power Management

  • Querying power management status

1.3.2 扩充库

还有一些单独的官方扩充函数库。这些库由官方网站提供,并包含在官方文档中,共同组成了SDL的“标准库”:

  • SDL_image—支持时下流行的图像格式:BMP、PPM、XPM、 PCX、GIF、JPEG、PNG、TGA。
  • SDL_mixer—更多的声音输出函数以及更多的声音格式支持。
  • SDL_net—网络支持。
  • SDL_ttf—TrueType字体渲染支持。
  • SDL_rtf—简单的RTF渲染支持。

1.4 API分类以及查询


分类查询地址:http://wiki.libsdl.org/APIByCategory

按名称查询地址:http://wiki.libsdl.org/CategoryAPI

2 SDL移植到VS中使用


1. SDL官网下载SDL的开发包,当前为 SDL2-devel-2.0.9-VC.zip,解压后得到头文件include目录,以及lib目录x86以及x64的库;

2. 创建vs工程,工程目录(.vcxproj文件所在目录)创建lib目录,include以及include下的子目录SDL2;

3. 将SDL头文件目录下所有头文件拷贝到步骤2中的SDL2目录下, SDL2.lib以及SDL2main.lib拷贝到lib目录下,SDL2.dll放在工程目录,即xxx.vcxproj同级目录下;

4. 配置vs工程
    配置头文件查询路径:工程->右键属性->配置属性->c/c++->常规->附加包含目录->添加“include\SDL2”;
    配置.lib文件:工程->右键属性->配置属性->链接器->常规->附加库目录->添加“lib”;
                           工程->右键属性->配置属性->链接器->输入->附加依赖项->添加“SDL2.lib SDL2main.lib”

5. 以上配置后,在工程中可以正常使用SDL中的功能
注意:虽然SDL是c语言开发的,但在c++中使用,引用sdl的头文件不必加extern "C",因为SDL库的头文件内部就已经包含了
#ifdef __cplusplus
extern "C" {
#endif
.........

#ifdef __cplusplus
}
#endif

3 简单几个SDL示例VS工程


3.1 使用SDL渲染本地图片


#include "SDL.h"
#include "stdio.h"
#include "SDL_image.h"

int main(int argc, char* argv[]) {

	// 初始化SDL,由于只是渲染一张图片,因此只需要初始化Video即可
	if (SDL_Init(SDL_INIT_VIDEO)) {
		printf("Init SDL error: %s", SDL_GetError());
		return -1;
	}

	//为了显示JPG图片,额外使用了图片库,所以要单独初始化
	IMG_Init(IMG_INIT_JPG);

	// 创建SDL_Window窗体,即作画的画布
	SDL_Window* window = SDL_CreateWindow("This is a SDL window to reder", 
		SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
	if (!window) {
		printf("Create SDL window error: %s", SDL_GetError());
		return -2;
	}

	// 创建SDL_Renderer渲染器,即作画的画笔
	SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
	if (!renderer) {
		printf("Create SDL renderer error: %s", SDL_GetError());
		return -3;
	}

	// 加载需要显示的图片,存储到surface
	SDL_Surface* surface1 = SDL_LoadBMP("hello.bmp");
	if (!surface1) {
		printf("Load hello.bmp error: %s", SDL_GetError());
		return -4;
	}

	SDL_Surface* surface2 = IMG_Load("sdl_imag.jpg");
	if (!surface2) {
		printf("SDL_Image sdl_imag.jpg error: %s", IMG_GetError());
		return -5;
	}

	// 从surface中拷贝图片形成纹理, 即要显示的内容
	SDL_Texture* texture1 = SDL_CreateTextureFromSurface(renderer, surface1);
	if (!texture1) {
		printf("Create texture from surface1 error: %s", SDL_GetError());
		return -6;
	}

	SDL_Texture* texture2 = SDL_CreateTextureFromSurface(renderer, surface2);
	if (!texture2) {
		printf("Create texture from surface2 error: %s", SDL_GetError());
		return -7;
	}

	// 设置渲染区域
	SDL_Rect rect1{ 0, 0, 320, 240 };
	SDL_Rect rect2{ 320, 240, 320, 240 };
	
	// 告知渲染器如何在窗体的哪个位置渲染文理
	SDL_RenderCopy(renderer, texture1, NULL, &rect1);
	SDL_RenderCopy(renderer, texture2, NULL, &rect2);

	// 渲染
	SDL_RenderPresent(renderer);

	// 延迟10s后进入清理程序
	SDL_Delay(10000);
	
	// 销毁创建的纹理,表层,渲染器,窗体,注意销毁顺序与创建顺序相反。
	SDL_DestroyTexture(texture1);
	SDL_DestroyTexture(texture2);
	SDL_FreeSurface(surface1);
	SDL_FreeSurface(surface2);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);

	IMG_Quit();
	SDL_Quit();
	
	return 0;
}

3.2 使用SDL渲染视频


3.3 使用SDL渲染音频


3.4 使用总结


4 SDL源码解析


源码解析这块,雷神已经写了很好的分析文章,在此转发,向前行者致敬:

SDL2源代码分析1:初始化(SDL_Init())

SDL2源代码分析2:窗口(SDL_Window)

SDL2源代码分析3:渲染器(SDL_Renderer)

SDL2源代码分析4:纹理(SDL_Texture)

SDL2源代码分析5:更新纹理(SDL_UpdateTexture())

SDL2源代码分析6:复制到渲染器(SDL_RenderCopy())

SDL2源代码分析7:显示(SDL_RenderPresent())

SDL2源代码分析8:视频显示总结

猜你喜欢

转载自blog.csdn.net/ice_ly000/article/details/88430208
SDL