SDL2的使用 | Linux系统下的编译与安装、使用

目录

1.系统环境:ubuntu 16.04 64位

2.SDL2介绍

3.下载

4.编译、安装

5.设置环境变量

6.测试SDL 

7.参考


1.系统环境:ubuntu 16.04 64位

Linux fly-vm 4.15.0-142-generic #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

2.SDL2介绍

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。

3.下载

官网地址:Simple DirectMedia Layer - Homepage

源码下载地址(SDL version 2.0.14):https://www.libsdl.org/release/SDL2-2.0.14.tar.gz

4.编译、安装

fly@fly-vm:~/workSpace/SDL2$tar -zxvf SDL2-2.0.14.tar.gz
fly@fly-vm:~/workSpace/SDL2$cd SDL2-2.0.14/
fly@fly-vm:~/workSpace/SDL2/SDL2-2.0.14$./configure --prefix=$PWD/_instal
fly@fly-vm:~/workSpace/SDL2/SDL2-2.0.14$make && make install
fly@fly-vm:~/workSpace/SDL2/SDL2-2.0.14$cd _install/
fly@fly-vm:~/workSpace/SDL2/SDL2-2.0.14/_install$ ls
bin  include  lib  share
fly@fly-vm:~/workSpace/SDL2/SDL2-2.0.14/_install$ tree
.
├── bin
│   └── sdl2-config
├── include
│   └── SDL2
│       ├── begin_code.h
│       ├── close_code.h
│       ├── SDL_assert.h
│       ├── SDL_atomic.h
│       ├── SDL_audio.h
│       ├── SDL_bits.h
│       ├── SDL_blendmode.h
│       ├── SDL_clipboard.h
│       ├── SDL_config.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.h
│       ├── SDL_haptic.h
│       ├── SDL_hints.h
│       ├── SDL_joystick.h
│       ├── SDL_keyboard.h
│       ├── SDL_keycode.h
│       ├── SDL_loadso.h
│       ├── SDL_locale.h
│       ├── SDL_log.h
│       ├── SDL_main.h
│       ├── SDL_messagebox.h
│       ├── SDL_metal.h
│       ├── SDL_misc.h
│       ├── SDL_mouse.h
│       ├── SDL_mutex.h
│       ├── SDL_name.h
│       ├── SDL_opengles2_gl2ext.h
│       ├── SDL_opengles2_gl2.h
│       ├── SDL_opengles2_gl2platform.h
│       ├── SDL_opengles2.h
│       ├── SDL_opengles2_khrplatform.h
│       ├── SDL_opengles.h
│       ├── SDL_opengl_glext.h
│       ├── SDL_opengl.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_assert.h
│       ├── SDL_test_common.h
│       ├── SDL_test_compare.h
│       ├── SDL_test_crc32.h
│       ├── SDL_test_font.h
│       ├── SDL_test_fuzzer.h
│       ├── SDL_test.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
│   ├── cmake
│   │   └── SDL2
│   │       ├── sdl2-config.cmake
│   │       └── sdl2-config-version.cmake
│   ├── libSDL2-2.0.so.0 -> libSDL2-2.0.so.0.14.0
│   ├── libSDL2-2.0.so.0.14.0
│   ├── libSDL2.a
│   ├── libSDL2.la
│   ├── libSDL2main.a
│   ├── libSDL2main.la
│   ├── libSDL2.so -> libSDL2-2.0.so.0.14.0
│   ├── libSDL2_test.a
│   ├── libSDL2_test.la
│   └── pkgconfig
│       └── sdl2.pc
└── share
    └── aclocal
        └── sdl2.m4

9 directories, 90 files

5.设置环境变量

在/etc/profile文件中加入:

export PATH=$PATH:/home/fly/workSpace/SDL2/SDL2-2.0.14/_install/lib

#### Makefile创建软链接的命令
.PHONY:all link unlink mk_cmake_dir link_bin link_pc link_m4 link_cmake

SRCDIRS         = $(PWD)
DSTDIRS         = /usr
MKDIR           = mkdir -p
RM                      = rm -rf

FILE_BIN        = sdl2-config
FILE_M4         = sdl2.m4
FILE_PC         = sdl2.pc
DIR_CMAKE       = lib/cmake/SDL2


all:link link_bin link_pc link_m4 link_cmake

mk_cmake_dir:
        sudo $(MKDIR) /usr/lib/cmake

### 在/usr/include/目录下为SDL2创建头文件目录软链接
### 在/usr/lib目录下,为SDL2链接库创建软链接
link:
        @echo ">>>>make links for the files of include && lib. <<<<"
        sudo ln -s $(SRCDIRS)/include/SDL2/ $(DSTDIRS)/include/SDL2
        sudo ln -s $(SRCDIRS)/lib/libSDL2-2.0.so.0.14.0 $(DSTDIRS)/lib/libSDL2-2.0.so.0
        sudo ln -s $(SRCDIRS)/lib/libSDL2-2.0.so.0.14.0 $(DSTDIRS)/lib/libSDL2-2.0.so.0.14.0
        sudo ln -s $(SRCDIRS)/lib/libSDL2.a $(DSTDIRS)/lib/libSDL2.a
        sudo ln -s $(SRCDIRS)/lib/libSDL2.la $(DSTDIRS)/lib/libSDL2.la
        sudo ln -s $(SRCDIRS)/lib/libSDL2main.a $(DSTDIRS)/lib/libSDL2main.a
        sudo ln -s $(SRCDIRS)/lib/libSDL2main.la $(DSTDIRS)/lib/libSDL2main.la
        sudo ln -s $(SRCDIRS)/lib/libSDL2-2.0.so.0.14.0 $(DSTDIRS)/lib/libSDL2.so
        sudo ln -s $(SRCDIRS)/lib/libSDL2_test.a $(DSTDIRS)/lib/libSDL2_test.a
        sudo ln -s $(SRCDIRS)/lib/libSDL2_test.la $(DSTDIRS)/lib/libSDL2_test.la

link_bin:
        @echo ">>>>make links for the files of bin. <<<<"
        sudo ln -s $(SRCDIRS)/bin/sdl2-config $(DSTDIRS)/bin/sdl2-config

link_pc:
        @echo ">>>>make links for the files of pc. <<<<"
        sudo ln -s $(SRCDIRS)/lib/pkgconfig/sdl2.pc $(DSTDIRS)/lib/pkgconfig/sdl2.pc

link_m4:
        @echo ">>>>make links for the files of m4. <<<<"
        sudo ln -s $(SRCDIRS)/share/aclocal/sdl2.m4 $(DSTDIRS)/share/aclocal/sdl2.m4

link_cmake: mk_cmake_dir
        @echo ">>>>make links for the files of cmake. <<<<"
        sudo ln -s $(SRCDIRS)/lib/cmake/SDL2 $(DSTDIRS)/lib/cmake/SDL2

unlink:
        sudo $(RM) $(DSTDIRS)/include/SDL2
        sudo $(RM) $(DSTDIRS)/lib/libSDL2*
        sudo $(RM) $(DSTDIRS)/bin/sdl2-config
        sudo $(RM) $(DSTDIRS)/lib/pkgconfig/sdl2.pc
        sudo $(RM) $(DSTDIRS)/share/aclocal/sdl2.m4
        sudo $(RM) $(DSTDIRS)/lib/cmake/SDL2

6.测试SDL 

/*******************************************************************
 *   > File Name: sdl2_hello.c
 *   > Author: fly
 *   > Mail: [email protected]
 *   > Create Time: 2021-06-06  0/22  20:47:10 +0800
 *================================================================*/

#include 
#include 

int main(int argc, char* argv[])
{
    /* 启动SDL */
    if(SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("SDL_Init Error.\n");
        return 1;
    }

    /* 打开窗口 */
    SDL_Window *win = SDL_CreateWindow("Hello World", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
    if(win == NULL)
    {
        printf("SDL_CreateWindow Error.\n");
        SDL_Quit();
        return 1;
    }

    /* 创建渲染器 */
    SDL_Renderer *ren = SDL_CreateRenderer(win , -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if(ren == NULL)
    {
        SDL_DestroyWindow(win);
        printf("SDL_CreateRenderer Error.\n");
        return 1;
    }

    /* 加载位图图像 */
    SDL_Surface *bmp = SDL_LoadBMP("hello.bmp");
    if(bmp == NULL)
    {
        SDL_DestroyRenderer(ren);
        SDL_DestroyWindow(win);
        printf("SDL_LoadBMP Error.\n");
        SDL_Quit();
        return 1;
    }

    SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
    SDL_FreeSurface(bmp);
    if (tex == NULL){
    	SDL_DestroyRenderer(ren);
    	SDL_DestroyWindow(win);
    	printf("SDL_CreateTextureFromSurface Error.\n");
    	SDL_Quit();
    	return 1;
    }

    //A sleepy rendering loop, wait for 3 seconds and render and present the screen each time
    for (int i = 0; i < 3; ++i){
    	//First clear the renderer
    	SDL_RenderClear(ren);
    	//Draw the texture
    	SDL_RenderCopy(ren, tex, NULL, NULL);
    	//Update the screen
    	SDL_RenderPresent(ren);
    	//Take a quick break after all that hard work
    	SDL_Delay(1000);
    }

    SDL_DestroyTexture(tex);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

    return 0;
}

Makefile:

CC      = gcc
C_SRC   = ${wildcard *.c}
BIN = ${patsubst %.c, %, $(C_SRC)}

INCDIRS :=
LIBDIRS :=

INCDIRS += /home/fly/workSpace/SDL2/SDL2-2.0.14/_install/include/SDL2
LIBDIRS += /home/fly/workSpace/SDL2/SDL2-2.0.14/_install/lib

CFLAGS  = -g -Wall -I$(INCDIRS) -L$(LIBDIRS) -lSDL2 -lpthread -lm -ldl

all:$(BIN)

$(BIN):%:%.c
        $(CC) -o $@ $^ $(CFLAGS)

clean:
        $(RM) a.out $(BIN)

.PHONY: all clean

Ubuntu中测试效果:

SDL2的使用 | Linux系统下的编译与安装、使用_第1张图片

======================================================================

7.参考

1.Linux下编译安装SDL2

2.SDL中文教程 - 游戏编程入门

3.SDL学习代码GIT仓库

4.使用SDL1.2版本的 同学注意了,SDL2.0版本的变动

5.TwinklebearDev SDL 2.0 教程

6.我的SDL2学习GIT仓库

你可能感兴趣的:(22.SDL2,linux)