SDL (一) Mac 編譯SDL庫,VSCode調試,Xcode調試

在這裏插入圖片描述
一,下載編譯(SDL2 pkg):

// 0 下載編譯SDL2
  
  [下載最新的SDL2 安裝包](https://www.libsdl.org/download-2.0.php)
  
  解壓: tar -zvxf SDL2-2.0.9.tar.gz
  

// 1  --prefix=[make install輸出目錄] 
./configure --prefix=/usr/local  

//2  make install
sudo make -j 8 && make install

// 3下載pkg 包  [添加鏈接描述](https://pkgconfig.freedesktop.org/releases/)

// 4 解壓安裝
tar -zvxf pkg-config-0.29.2.tar.gz
cd pkg-config-0.29.2
./configure --with-internal-glib
make
make install


二, SDL創建窗口

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

int main(int argc, char *argv[])
{
  printf("Hello SDL !\n");
  int quit = 1;
  SDL_Event event;
  SDL_Window *sdl_window = NULL;
  SDL_Renderer *renderer = NULL;

  SDL_Init(SDL_INIT_VIDEO);

  sdl_window = SDL_CreateWindow("Hello SDL",
                                SDL_WINDOWPOS_UNDEFINED,
                                SDL_WINDOWPOS_UNDEFINED,
                                500,
                                500,
                                SDL_WINDOW_SHOWN);
  if (!sdl_window)
  {
    printf("Create window failed! \n");
    goto __FAIL;
  }
  renderer = SDL_CreateRenderer(sdl_window, -1, 0);
  if (!renderer)
  {
    printf("Create renderer failed! \n");
    goto __FAIL;
  }
  SDL_SetRenderDrawColor(renderer, 123, 123, 123, 255);

  SDL_RenderClear(renderer);

  SDL_RenderPresent(renderer);
  do
  {

    SDL_WaitEvent(&event);
    switch (event.type)
    {
    case SDL_QUIT:
      quit = 0;
      break;
    default:
      SDL_Log("event type is %d \n", event.type);
    }
  } while (quit);

__FAIL:
  if (renderer)
  {
    SDL_DestroyRenderer(renderer);
  }
  if (sdl_window)
  {
    SDL_DestroyWindow(sdl_window);
  }
  SDL_Quit();
  return 0;
}

三, 編譯運行

// 1 編譯
clang -g -o helloSDL 文件名.c `pkg-config --cflags --libs sdl2`
或者
clang -g -o helloSDL helloSDL.c -lsdl2

// 2 運行
./helloSDL

四, vscode 調試

{
  "version": "2.0.0",
  "tasks": [{
    "label": "Build with Clang",
    "type": "shell",
    "command": "clang",
    "args": [
      "-g",
      "-o",
      "helloSDL",
      "helloSDL.c",
      "-lsdl2",
    ],
    "group": {
      "kind": "build",
      "isDefault": true
    }
  }]
}
{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [{
    "name": "debug",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceFolder}/helloSDL",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "lldb",
    "logging": {
      "trace": true,
      "traceResponse": true,
      "engineLogging": true
    },
    "preLaunchTask": "Build with Clang",
  }]
}

用上這兩個配置就可以起飛了


5, XCode 調試

找到 SDL2的dylib 複製到工程中,即可使用 #include <SDL2/SDL.h> 引入頭文件 ,然後就可以調試了
在這裏插入圖片描述
在這裏插入圖片描述
DEMO地址

最後附上 雷神的學習資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章