Introduction to SDL (1)

Introduction

SDL (Simple DirectMedia Layer) is a cross-platform development library designed to provide low-level access to audio, keyboard, mouse, joystick, and graphics hardware through OpenGL and Direct3D.

SDL supports Windows, Mac OS X, Linux, iOS and Android. Support for other platforms can be found in the source code. SDL is written in C and has bindings available for several other languages, including C#, GO, and Python, among others.

download

Open the githua SDL download page and download the compressed package according to your needs.

Since we are used to develop programs, we need to download the devel version. I use the stdio visual 2022 of the window. Here we download the SDL2-devel-2.26.5-VC.zip marked in the figure .

After the download is complete, place it in a favorite location and unzip it, as shown in the figure.

Build an SDL program

Next we create an SDL window program

Create a C++ console program

Configure header files and link libraries

Right-click the project to open the project properties page

1. Include the header file, add our SDL include folder in c/c++ ---> General ---> Attachment Include Directory ---> Edit

 2. Include the lib library, linker ---> general ---> additional library directory ---> edit, add the lib path of our SDL, here we need to choose the correct path according to whether the property of our project is x86 or x64, Here I am using x64

3. Configure dependencies, linker --> input...> additional dependencies ---> edit, add SDL2.lib and SDL2main.lib 

 4. Copy the corresponding dll dynamic library to the generated directory (I am x64/Debug here), and copy the x64 path dll in the lib directory to compile the x64 program

edit main function

1. Include the SDL header file

2. Use SDL_init to initialize SDL

3. Use SDL_CreateWindow to create a window, SDL_WINDOWPOS_UNDEFINED means the position is customized by the system

4. Use SDL_GetWindowSurface to get the drawing surface of the window

5. Use SDL_FillRect to fill the rectangular area with color

6.SDL_UpdateWindowSurface updates the drawing surface of the window, that is, presents

7. Release the application space and exit the program

Since then we have completed the first SDL program, the complete code is as follows


#include <iostream>

#include <sdl.h>
using namespace std;

const int SCREEN_WIDTH = 640;
const int  SCREEN_HEIGHT = 480;

int main(int argc, char* argv[])
{
	bool success = true;


	SDL_Window* gWindow = nullptr;

	SDL_Surface* gScreenSurface = NULL;

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		//cout << "video init Error" << SDL_GetError() << endl;

		printf("SDL could not initialize!SDL_Error: %s\n", SDL_GetError());
		success = false;
	}
	else
	{

		//Create window
		gWindow = SDL_CreateWindow("SDL First Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

		if (gWindow == nullptr)
		{
			printf("window could not be created!SDL_Error: %s\n", SDL_GetError());
			success = false;
		}
		else
		{
			//get window surface
			gScreenSurface = SDL_GetWindowSurface(gWindow);

			
			SDL_FillRect(gScreenSurface, nullptr, SDL_MapRGB(gScreenSurface->format, 0xff, 0xfd, 0xdd));


			//update the surface
			SDL_UpdateWindowSurface(gWindow);

			//hack to get window to stay up
            //这里使用SDL的事件队列,当用户点击关闭时,SDL事件队列会收到退出事件,然后这里退出循环,简单点可以直接注释掉这一段循环,使用SDL_Delay(2000); 2s后退出

			SDL_Event e;
			bool quit = false;

			while (quit == false)
			{
				while (SDL_PollEvent(&e))
				{
					if (e.type == SDL_QUIT)
						quit = true;
				}
			}
		}

	}

	//destory window
	SDL_DestroyWindow(gWindow);

	SDL_Quit();



	return success;
}


generate

Guess you like

Origin blog.csdn.net/yunxiaobaobei/article/details/130176626