Windows下使用VS2013编译使用SDL库

时间:2022-07-22
本文章向大家介绍Windows下使用VS2013编译使用SDL库,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

关于SDL

Simple DirectMedia Layer是一个跨平台开发库,旨在通过OpenGL和Direct3D提供对音频,键盘,鼠标,操纵杆和图形硬件的低级访问。 视频播放软件,仿真器和流行游戏(包括Valve屡获殊荣的目录和许多Humble Bundle游戏)都使用它。

SDL正式支持Windows,Mac OS X,Linux,iOS和Android。 在源代码中可以找到对其他平台的支持。

SDL用C编写,可与C ++一起使用,并且有绑定可用于其他几种语言,包括C#和Python。

SDL 2.0在zlib许可下分发。 该许可证允许您在任何软件中自由使用SDL。 SDL官网地址为:SDL

在VS2013中编译SDL源代码相关的静态库和动态库

参考官方文档Using SDL with Microsoft Visual C++ 本博客编写时,SDL的最新稳定版本是SDL version 2.0.12,先下载SDL 2.0.12的源代码,下载地址为:

关于在Visual Studio下编译SDL源代码,官方已经说得很清楚了,如下图所示:

进入源代码所在根目录(例如:我的SDL解压目录为:D:env_buildSDL2-2.0.12)下的 VisualC目录,VisualC目录结构如下图所示:

使用VS2013、或者VS2015、VS2017、VS2019等开发工具打开SDL.sln文件,例如我使用VS2013打开,

然后选择解决方案配置(Debug或Release)以及解决方案平台(Win32或X64)组合(一共4种),这里以Win32下的Debug为例,使用VS2103生成SDL2-2.0.12对应的x86的Debug静态库和动态库, 选择Debug模式,Win32平台后,然后选择菜单【生成】-》【生成解决方案】,

可以看到所有工程和测试都成功了,一共是25个,我们使用SDL开发时主要用到的是SDL2以及SDL2main的静态库以及相应的头文件,生成的Win32的Debug库如下图所示:

其他三种Win32和Release、Win64和Debug、Win64和Release的生成方法相同。 将编译好的SDL的头文件include以及lib库文件放在自己指定的目录下,方便后续开发使用,比如我存放的目录为:D:env_buildSDL2_2.0.12_VS2013,其目录树结构如下所示:

卷 Data 的文件夹 PATH 列表
卷序列号为 3EE8-BA3A
D:.
│  BUGS.txt
│  COPYING.txt
│  demo_tree.txt
│  README-SDL.txt
│  README.txt
│  WhatsNew.txt
│  
├─docs
│      doxyfile
│      README-android.md
│      README-cmake.md
│      README-directfb.md
│      README-dynapi.md
│      README-emscripten.md
│      README-gesture.md
│      README-hg.md
│      README-ios.md
│      README-linux.md
│      README-macosx.md
│      README-nacl.md
│      README-pandora.md
│      README-platforms.md
│      README-porting.md
│      README-psp.md
│      README-raspberrypi.md
│      README-touch.md
│      README-wince.md
│      README-windows.md
│      README-winrt.md
│      README.md
│      
├─include
│      begin_code.h
│      close_code.h
│      SDL.h
│      SDL_assert.h
│      SDL_atomic.h
│      SDL_audio.h
│      SDL_bits.h
│      SDL_blendmode.h
│      SDL_clipboard.h
│      SDL_config.h
│      SDL_config.h.cmake
│      SDL_config.h.in
│      SDL_config_android.h
│      SDL_config_iphoneos.h
│      SDL_config_macosx.h
│      SDL_config_macosx.h.orig
│      SDL_config_minimal.h
│      SDL_config_pandora.h
│      SDL_config_psp.h
│      SDL_config_windows.h
│      SDL_config_winrt.h
│      SDL_config_wiz.h
│      SDL_copying.h
│      SDL_cpuinfo.h
│      SDL_egl.h
│      SDL_endian.h
│      SDL_error.h
│      SDL_events.h
│      SDL_filesystem.h
│      SDL_gamecontroller.h
│      SDL_gesture.h
│      SDL_haptic.h
│      SDL_hints.h
│      SDL_joystick.h
│      SDL_keyboard.h
│      SDL_keycode.h
│      SDL_loadso.h
│      SDL_log.h
│      SDL_main.h
│      SDL_messagebox.h
│      SDL_metal.h
│      SDL_mouse.h
│      SDL_mutex.h
│      SDL_name.h
│      SDL_opengl.h
│      SDL_opengles.h
│      SDL_opengles2.h
│      SDL_opengles2_gl2.h
│      SDL_opengles2_gl2ext.h
│      SDL_opengles2_gl2platform.h
│      SDL_opengles2_khrplatform.h
│      SDL_opengl_glext.h
│      SDL_pixels.h
│      SDL_platform.h
│      SDL_power.h
│      SDL_quit.h
│      SDL_rect.h
│      SDL_render.h
│      SDL_revision.h
│      SDL_rwops.h
│      SDL_scancode.h
│      SDL_sensor.h
│      SDL_shape.h
│      SDL_stdinc.h
│      SDL_surface.h
│      SDL_system.h
│      SDL_syswm.h
│      SDL_test.h
│      SDL_test_assert.h
│      SDL_test_common.h
│      SDL_test_compare.h
│      SDL_test_crc32.h
│      SDL_test_font.h
│      SDL_test_fuzzer.h
│      SDL_test_harness.h
│      SDL_test_images.h
│      SDL_test_log.h
│      SDL_test_md5.h
│      SDL_test_memory.h
│      SDL_test_random.h
│      SDL_thread.h
│      SDL_timer.h
│      SDL_touch.h
│      SDL_types.h
│      SDL_version.h
│      SDL_video.h
│      SDL_vulkan.h
│      
└─lib
    ├─win32
    │  ├─Debug
    │  │      SDL2.dll
    │  │      SDL2.exp
    │  │      SDL2.lib
    │  │      SDL2main.lib
    │  │      SDL2test.lib
    │  │      
    │  └─Release
    │          SDL2.dll
    │          SDL2.lib
    │          SDL2main.lib
    │          SDL2test.lib
    │          
    └─x64
        ├─Debug
        │      SDL2.dll
        │      SDL2.lib
        │      SDL2main.lib
        │      SDL2test.lib
        │      
        └─Release
                SDL2.dll
                SDL2.lib
                SDL2main.lib
                SDL2test.lib

其中include就是头文件,直接从源代码中拷贝过来的,lib下是四种平台和配置对应的lib和dll库。 下面是我在Windows10系统下使用VS2013编译好的SDL2_2.0.12的库,可用于在Windows平台下使用VS2013开发使用。 百度网盘链接:https://pan.baidu.com/s/15BD7C2z7VICpVoIYcejcXg 提取码:rfai

使用SDL库编写SDL程序

参考Using SDL with Microsoft Visual C++中的Creating a Project with SDL小节

使用VS2013创建一个基于Win32的控制台应用程序,命令为SDLDemo1,为工程创建一个main.cpp文件,如下图所示:

可以看到没有导入SDL库的头文件之前,引入SDL.h头文件会报错,变成红色。

导入头文件

我的SDL库的路径为:D:env_buildSDL2_2.0.12_VS2013,导入子目录include,其全路径为:D:env_buildSDL2_2.0.12_VS2013include

导入库文件

我的SDL库的路径为:D:env_buildSDL2_2.0.12_VS2013,导入子目录lib下对应平台(win32或x64)的配置(Debug或Release),比如我选择的是其全路径为:D:env_buildSDL2_2.0.12_VS2013lib(Platform)(Configuration) 注意:此处为了通用我使用了(Platform)和(Platform)和(Platform)和(Configuration)这两个宏,分别用于表示对应的平台和配置, 具体为:D:env_buildSDL2_2.0.12_VS2013libwin32Debug 然后再添加附加依赖项及lib库:

  • SDL2.lib
  • SDL2main.lib 如下图所示:

运行测试程序

在main.cpp中输入如下测试代码:

#include "SDL.h"

int main(int argc, char* argv[])
{
	// Body of the program goes here.
	return 0;
}

按Ctrl+F5或者菜单【调试】-》【开始执行】,报错,说是找不到SDL.dll 如下图所示:

由于我生成的可执行程序SDLDemo1.exe所在目录E:SoftDevelopCPlusSDLProjectsSDLDemo1Debug下没有SDL.dll,所以报错,将之前编译生成的SDL.dll拷贝到可执行程序SDLDemo1.exe同级目录,再重新运行:

属性文件

当然为了简单,也可以自己写一个SDL库的属性文件sdl2_vs2013.props,假如我的SDL2.0.12所在的库目录为:D:env_buildSDL2_2.0.12_VS2013,其目录结构为:

其内容如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup />
  <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>D:env_buildSDL2_2.0.12_VS2013include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <AdditionalLibraryDirectories>D:env_buildSDL2_2.0.12_VS2013lib$(Platform)$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
      <AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2test.lib;%(AdditionalDependencies)</AdditionalDependencies>
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup />
</Project>

这里分享下我从官网下载的SDL的源代码以及使用VS2013编译好的库下载地址: 百度网盘下载链接:https://pan.baidu.com/s/1OkVWYy1XVBYlTkIwd_3F2Q 提取码:9b6a

参考资料