Jade Dungeon

SDL图形库安装及配置

安装

sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev libsdl2-mixer-dev

如果要查看安装路径的话,使用

whereis SDL2

默认位置一般为/usr/include/SDL2

测试代码

在头文件的形式上,

  • visual studio为:#include "SDL.h"
  • linux中一般表示成路径形式 #include "SDL2/SDL.h"
//  gcc demo.c -o demo -lSDL2

#include "SDL2/SDL.h"
#include "stdio.h"

int main(int argc, char* argv[]) {
	SDL_Window   * window;
	SDL_Renderer * renderer;

	SDL_Init(SDL_INIT_VIDEO);
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		fprintf(stderr, "%s\n", SDL_GetError());
	}

	window = SDL_CreateWindow("SDL_RenderClear",
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
		512, 512, 0);

	renderer = SDL_CreateRenderer(window, -1, 0);
	SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
	
	SDL_RenderClear(  renderer);
	SDL_RenderPresent(renderer);
	SDL_Delay(5000);
	SDL_Quit();
	return 0;
}

编译:

gcc demo.c -o out/demo -lSDL2

运行:

out/demo

如果一切正常应该显示一个单色红色的窗口。