Technology development stories will be serialized

This is the first article in a series on audio and video basics, explaining what SDL is and why it’s used. It doesn’t seem to have anything to do with audio and video.

SDL introduction

SDL stands for “Simple DirectMedia Layer” and is a cross-platform multimedia library that runs on Mac, Windows, Linux, and more.

SDL provides a unified low-level access interface for audio, video, keyboard, mouse, joystick, and 3D hardware that enables playback of audio and video content across different systems without any knowledge of system-specific audio and video interfaces.

This cross-platform feature is the same as OpenGL, but the difference is that OpenGL is really cross-platform. It is an interface specification developed and maintained by the Khronos Group, and the specific implementation is done by the driver manufacturer. SDL encapsulates all relevant interfaces to be compatible with platforms, and then provides a unified interface externally.

Thus it can be seen that judgment is based on judgment. A cross-platform interface is written, regardless of implementation; The other is to write a good implementation before cross-platform.

Due to the cross-platform features of SDL, SDL can be used for audio and video playback operations when learning FFmpeg in the future, without the trouble of compiling SO, writing JNI and writing interface like FFmpeg on Android platform. In addition ffplay source code is also used in THE SDL play, can be used for reference.

SDL download and install

Downloading SDL on a Mac is simple and straightforward

brew install sdl2
Copy the code

Note that the download is SDL version 2.0 if you use the following command

brew install sdl
Copy the code

Download SDL version 1.0, the difference is the suffix of the number 2 after the version.

With the latest 2.0, I’m currently using version 2.0.10.

If it is Windows system, refer to the download configuration of other articles, there is no way to do without a computer.

The SDL download is located in the following directory on the MAC system, which will be used later.

/ usr/local/Cellar/sdl2/2.0.10

CLion new construction project

Now open CLion and create a new C++ project.

CLion is used here because it works. Autocompletion, code hints, breakpoint debugging, etc., are great, but there is no community free version. There is a 30-day free trial period after which you need to activate with an activation code.

Fortunately, it is compiled with CMake. If you download the project source code and configure the associated library and header file of CMake, you can also compile directly with the CMake command line, which will be discussed later.

C++ project associated with SDL library

The next step is to associate the SDL library with your C++ project so that you can reference the sdl-related header files in your project.

The SDL installation path mentioned earlier is as follows:

/usr/local/ Cellar/sdl2/2.0.10Copy the code

The directory is shown below:

Include is the path to the header file and lib is the path to the library.

The include_directories and link_directories commands are used.

Among them:

Include_directories add a header folder to the search path so that it can be loaded by include.

Link_directories add a library folder to the path so that it will be linked to at compile time.

The specific code is as follows:

Declare a variable SDL_DIR as the SDL installation pathset(SDL_DIR "/ usr/local/Cellar/sdl2 2.0.10." "Include_directories (${SDL_DIR}/include)Copy the code

The code declares a variable SDL_DIR as the installation path. If the path is different on your system, just change the path.

On a MAC, you can also set the directory to /usr/local, and all libraries are installed with an index in the lib and include directories.

Finally, associate the program we want to compile with the SDL library.

You can use the link_directories command to add directories for many libraries, but only the target_link_libraries command will determine which libraries are involved. If you add a folder path that doesn’t match a library, an error will be reported.

The implementation code is as follows:

target_link_libraries(av-beginner SDL2)
Copy the code

The target_link_libraries method links dynamic libraries in preference and can also display specified dynamic or static libraries. The suffix for dynamic libraries on MAC is dylib. In the image above you can see that libsdl2. dylib is actually an index. The real library is libsdl2-2.0.0. dylib, and the index ignores its version number.

Once the SDL library association is complete, you can start writing the actual code.

Code practice

The main code practice is to verify that our environment configuration is ok and run an SDL function to try it out.

#include <iostream>
#include <SDL2/SDL.h>
using namespace std;

int main(a){
    cout << "hello av-beginner" << endl;
    SDL_Init(SDL_INIT_EVERYTHING);
    return 0;
}
Copy the code

SDL_Init is an SDL initialization function that can be initialized selectively or completely, depending on the desired function.

If the program outputs normally and exits normally, the environment is configured and ready for functional development.

conclusion

The above is the audio and video basic learning serialized 001.

See the warehouse for specific codes:

Github.com/glumes/av-b…

The submission tag of this article is AV-beginner -001, you can switch to the corresponding source code to view.

My ability is limited. If there is anything wrong in this article, please add my wechat ezgluems to communicate ~~

Scan code attention, continue to update