I. Introduction
SDL (Simple DirectMedia Layer) is an open-source cross-platform multimedia development library written in C language. SDL provides several functions for controlling images, sounds, and outputs, developers can use the same or similar code to develop applications across multiple platforms (such as Linux, windows, and Mac OS X. Currently, SDL is mostly used for developing multimedia applications such as games, simulators, and media players.
Ii. Configuration
Sudo apt-Get install libsdl1.2-Dev
Additional package:
Sudo apt-Get install libsdl-image1.2-dev
Sudo apt-Get install libsdl-mixer1.2-dev
Sudo apt-Get install libsdl-ttf2.0-dev
Sudo apt-Get install libsdl-gfx1.2-dev
You can also download the latest source code on the official website for compilation.
The Installation Method for opensuse is as follows.
First install the OpenGL package:
sudo zypper in Mesa-devel freeglut-devel MesaGLw-develzypper in Mesa-devel freeglut-devel MesaGLw-devel
Then install SDL
sudo zypper in SDL-devel
Iii. Test
#include "SDL/SDL.h"int main( int argc, char* args[] ){ //Start SDL SDL_Init( SDL_INIT_EVERYTHING ); //Quit SDL SDL_Quit(); return 0; }
Compile command: G ++ main. C-o main-lsdl
If the program can be compiled and run, the SDL installation is successful.
4. Load a bitmap
SDL can easily load bitmap.
Copy a BMP image to the project folder and modify the Code as follows:
#include <stdio.h>#include <SDL/SDL.h>int main(){ //The images SDL_Surface* hello = NULL; SDL_Surface* screen = NULL; SDL_Init( SDL_INIT_EVERYTHING ); //Set up screen screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE ); //Load image hello = SDL_LoadBMP( "1.bmp" ); //Apply image to screen SDL_BlitSurface( hello, NULL, screen, NULL ); //Update Screen SDL_Flip( screen ); //Pause SDL_Delay( 5000 ); //Quit SDL SDL_Quit();//Free memory SDL_FreeSurface( hello ); //Quit SDL SDL_Quit(); return 0;}
Compile and run:
To load images in other formats, you need to add the sdl_image library.
Several functions are used to explain:
Sdl_init ():
Dynamically load and initialize the SDL library. This function includes a set of tags to indicate which parts need to be activated:
Sdl_init_audio
Sdl_init_video
Sdl_init_cdrom
Sdl_init_timer
Sdl_init_everything
Call sdl_quit () after use ().
Sdl_setvideomode ()
Set the video color depth and resolution
Sdl_loadbmp ()
Load BMP Images
Int sdl_blitsurface (sdl_surface * SRC, sdl_rect * srcrect, sdl_surface * DST, sdl_rect * dstrect );
All four parameters are pointers-two sdl_surface pointers and two sdl_rect pointers. SRC is the source plane, that is, the plane to which bits are sent; DST is the target plane, that is, the plane to which bits are sent to the source plane.
Int sdl_flip (sdl_surface * screen );
Swap screen Buffering
Refer:
SDL tutorial-http://lazyfoo.net/