1. 程式人生 > 其它 >mac下用clion進行sdl2遊戲開發de環境搭建

mac下用clion進行sdl2遊戲開發de環境搭建

1. 故事背景

想從unity轉unreal了,於是要使用c++進行開發。unreal引擎那麼大,每次開啟,我的小本都嗡嗡嗡的,想著不如用個輕量一些的引擎先開發吧,核心程式碼獨立出來,到時候如果真要移植到unreal也方便。

在sdl2/sfml中糾結了一下,最終選擇了文件相對較多的sdl2。本來試用了Xcode,但是實在是不習慣,感覺和JetBrains的編譯器差太遠了...決定還是用clion試一下。

但是到處看了各種參考資料,沒有一篇能完全滿足我的要求:

  • 1 在mac下開發
  • 2 使用c/c++進行開發
  • 3 用clion的CMake
  • 4 智慧提示SDL2庫方法

作為程式設計多年,但是c語言還停留在大學水平的開發,我還被迫學習了Cmake。

一番摸索完成後,決定自己寫一篇,記錄的同時,也造福其他需要的人。

話不多說,開始吧!

2. 利用homebrew安裝sdl2

依次執行下面的命令就好了

  • 安裝國內映象的Homebrew,一路往下YES就行
$ /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
  • 安裝sdl2
$ brew install sdl2_mixer 
  • 檢視下sdl2庫安裝情況
$ brew info sdl2

根據安裝的版本不同,可能得到不同的資訊,下面是我的

sdl2: stable 2.0.16 (bottled), HEAD
Low-level access to audio, keyboard, mouse, joystick, and graphics
https://www.libsdl.org/
/usr/local/Cellar/sdl2/2.0.16 (91 files, 5.4MB) *
  Poured from bottle on 2021-08-27 at 05:51:01
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/sdl2.rb
License: Zlib
==> Options
--HEAD
	Install HEAD version
==> Analytics
install: 107,393 (30 days), 219,217 (90 days), 957,010 (365 days)
install-on-request: 10,041 (30 days), 19,802 (90 days), 91,778 (365 days)
build-error: 0 (30 days)

其中,第4行的/usr/local/Cellar/sdl2/2.0.16很有用,是我們安裝sdl2庫的位置,下面會用到。

3. 建立clion專案

我們分C專案和CPP專案。

建立專案的時候,記得選C Executable或者C++ Executable哦。

3.1 C專案

  • CMakeLists.txt內容
cmake_minimum_required(VERSION 3.20)
project(SimpleWindow C)

set(CMAKE_C_STANDARD 11)
set(SDL_DIR /usr/local/Cellar/sdl2/2.0.16/)
include_directories(${SDL_DIR}/include/)
link_directories(${SDL_DIR}/lib/)

add_executable(SimpleWindow main.c)

target_link_libraries(SimpleWindow SDL2 SLD2_test SDL2main)
  • main.c內容
#include "stdio.h"
#include <SDL2/SDL.h>

const int WIDTH = 400, HEIGHT = 400;

int main() {
    if (SDL_Init(SDL_INIT_EVERYTHING)) {
        printf("Can not init video");
        return 1;
    }

    SDL_Window *win = SDL_CreateWindow(
            "Hello world",
            SDL_WINDOWPOS_CENTERED,
            SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT,
            SDL_WINDOW_ALLOW_HIGHDPI
    );

    if (win == NULL) {
        printf("Can not create window");
        return 1;
    }

    SDL_Event windowEvent;
    while(1) {
        if (SDL_PollEvent(&windowEvent)) {
            if (SDL_QUIT == windowEvent.type) {
                printf("SDL quit!!");
                break;
            }
        }
    }

    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}

都建立好了之後,點選執行,可以看到下面的視窗

3.2 CPP專案

  • CMakeLists.txt內容
cmake_minimum_required(VERSION 3.20)
project(SimpleWindow)

set(CMAKE_C_STANDARD 11)
set(SDL_DIR /usr/local/Cellar/sdl2/2.0.16/)
include_directories(${SDL_DIR}/include/)
link_directories(${SDL_DIR}/lib/)
add_executable(SimpleWindow main.cpp)
link_libraries(SDL2)
target_link_libraries(SimpleWindow SDL2  SDL2main)
  • main.cpp內容
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;

const int WIDTH = 400, HEIGHT = 400;

int main() {
    if (SDL_Init(SDL_INIT_EVERYTHING)) {
        cout << "SDL could not initialized with error: " <<  SDL_GetError() << endl;
        return 1;
    }

    SDL_Window *win = SDL_CreateWindow(
            "Hello world",
            SDL_WINDOWPOS_CENTERED,
            SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT,
            SDL_WINDOW_ALLOW_HIGHDPI
    );

    if (win == NULL) {
        cout << "SDL could not create window with error: " <<  SDL_GetError() << endl;
        return 1;
    }

    SDL_Event windowEvent;
    while(true) {
        if (SDL_PollEvent(&windowEvent)) {
            if (SDL_QUIT == windowEvent.type) {
                cout << "SDL quit!!" << endl;
                break;
            }
        }
    }

    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}

參考文章
mac安裝brew(親測)
SDL2 環境構建 (macOS + CLion + SDL2)
SDL2:第一個程式(Mac)

作者:亞楠老獵人 出處:https://www.cnblogs.com/laolieren/ 本部落格文章版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。