cmake 编译 SDL2 工程

使用 cmake 编译 SDL2 的用用, 在 MacOS 上使用:

安装 SDL2库 Link to heading

brew install sdl2

配置 CMakeList.txt 文件 Link to heading

cmake_minimum_required(VERSION 3.20)
project(test_sdl C)

set(CMAKE_C_STANDARD 99)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

add_executable(test_sdl main.c)

target_link_libraries(test_sdl ${SDL2_LIBRARIES})

HelloWorld 的代码 Link to heading

#include <stdio.h>
#include <SDL.h>

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 256

int main() {
    printf("Hello, World!\n");

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        printf("init sdl error\n");
    }
    SDL_Window *window = SDL_CreateWindow("Hello SDL world!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                          SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
    if (NULL == window) {
        printf("create window error\n");
    }

    SDL_Event windowEvent;
    while(1) {
        if (SDL_PollEvent(&windowEvent)) {
            if (SDL_QUIT == windowEvent.type) {
                printf("quit\n");
                break;
            }
        }
    }
    SDL_DestroyWindow(window);
    SDL_Quit();

    printf("Finish!\n");
    return 0;
}