Linux ubuntu sdl2 のインストール方法

Linux ubuntu sdl2 のインストール方法

Ubuntu で SDL2 をインストールするには、以下の手順で進めるのが簡単です。通常の開発に必要なライブラリもまとめて入れるのがオススメです。


目次

✅ SDL2 本体のインストール

端末で以下を実行:

sudo apt update
sudo apt install libsdl2-dev

これで SDL2 の開発用ライブラリ(sdl2-config なども含む)が入ります。


✅ SDL2-CS(C#用バインディング)の導入方法

SDL2 を C# から使うには SDL2-CS というバインディングが必要です。

1. SDL2-CS を取得

SDL2-CS をクローン or ダウンロード:

git clone https://github.com/flibitijibibo/SDL2-CS.git

中にある SDL2.dll(Linuxでは SDL2-CS/SDL2.cs)を自分のプロジェクトにコピーして使います。

2. SDL2.dll ではなく SDL2.cs を使う(Mono)

Linux/Mono 環境では通常、SDL2.dll ではなく SDL2.cs を直接プロジェクトに追加します:

cp SDL2-CS/SDL2.cs ./your_project/

3. コンパイル時に一緒にビルド

mcs -out:YourApp.exe Program.cs WindowManager.cs SDL2.cs

✅ 実行時に SDL2 ライブラリ本体が必要

以下の共有ライブラリがシステムに必要です(通常 libsdl2-dev を入れていればOK):

ldconfig -p | grep SDL2

もし入っていない場合:

sudo apt install libsdl2-2.0-0

✅ SDL2 のインストール確認

以下で SDL2 のバージョンが見れればOK:

sdl2-config --version

必要なら SDL_image、SDL_ttf、SDL_mixer など追加ライブラリのインストール方法も案内できます!

Linux ubuntu sdl2 のインストール方法

Ubuntu で SDL2 の追加ライブラリ(画像、フォント、音声関連)を使うためのインストール方法を以下にまとめました。


🎨 SDL2_image(画像読み込み用)

対応形式:PNG, JPG, BMP など

sudo apt install libsdl2-image-dev

🔤 SDL2_ttf(TrueType フォント描画)

sudo apt install libsdl2-ttf-dev

🔊 SDL2_mixer(音楽・効果音再生)

対応形式:WAV, MP3, OGG, MOD など

sudo apt install libsdl2-mixer-dev

✅ インストールされたことを確認するには

それぞれ pkg-config で確認できます:

pkg-config --modversion SDL2_image
pkg-config --modversion SDL2_ttf
pkg-config --modversion SDL2_mixer

🧪 依存ライブラリも自動で入る?

apt install で入るパッケージは、必要な依存(libpng, freetype, ogg など)も一緒にインストールされます。


🧰 C# から使うには?

C# から使いたい場合は、SDL2_image.cs などの C# バインディングが必要になります。
公式の SDL2-CS には入ってないので、自分で P/Invoke で定義するか、SDL2-CS を拡張したラッパーを使う方法になります。


もし C# から SDL_image や SDL_ttf を使うコード例が欲しければ、それも作りますよ!

C# から SDL_image や SDL_ttf を使うコード例

OK!それぞれ C# から SDL2_imageSDL2_ttf を使う最小構成の例を出します。
以下では SDL2-CShttps://github.com/flibitijibibo/SDL2-CS) を利用し、P/Invoke で SDL_image/ttf をバインドしています。


🔹 SDL_image を使って画像を表示する C# の例

1. SDL2_image バインディング定義(SDL_image.cs

using System;
using System.Runtime.InteropServices;

public static class SDL_image
{
    public const string NativeLibName = "SDL2_image";

    [DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
    public static extern int IMG_Init(int flags);

    [DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
    public static extern void IMG_Quit();

    [DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr IMG_LoadTexture(IntPtr renderer, string file);

    public const int IMG_INIT_PNG = 0x00000002;
}

2. 画像表示コード(例:ImageExample.cs

using System;
using SDL2;

class ImageExample
{
    static void Main()
    {
        SDL.SDL_Init(SDL.SDL_INIT_VIDEO);
        SDL_image.IMG_Init(SDL_image.IMG_INIT_PNG);

        IntPtr window = SDL.SDL_CreateWindow("Image Example", 100, 100, 800, 600, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN);
        IntPtr renderer = SDL.SDL_CreateRenderer(window, -1, 0);

        IntPtr texture = SDL_image.IMG_LoadTexture(renderer, "sample.png");
        if (texture == IntPtr.Zero)
        {
            Console.WriteLine("Failed to load image: " + SDL.SDL_GetError());
            return;
        }

        bool running = true;
        SDL.SDL_Event e;

        while (running)
        {
            while (SDL.SDL_PollEvent(out e) != 0)
            {
                if (e.type == SDL.SDL_EventType.SDL_QUIT)
                    running = false;
            }

            SDL.SDL_RenderClear(renderer);
            SDL.SDL_RenderCopy(renderer, texture, IntPtr.Zero, IntPtr.Zero);
            SDL.SDL_RenderPresent(renderer);
        }

        SDL.SDL_DestroyTexture(texture);
        SDL.SDL_DestroyRenderer(renderer);
        SDL.SDL_DestroyWindow(window);
        SDL_image.IMG_Quit();
        SDL.SDL_Quit();
    }
}

🔸 SDL_ttf を使ってテキストを描画する C# の例

1. SDL_ttf.cs バインディング定義

using System;
using System.Runtime.InteropServices;

public static class SDL_ttf
{
    public const string NativeLibName = "SDL2_ttf";

    [DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
    public static extern int TTF_Init();

    [DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr TTF_OpenFont(string file, int ptsize);

    [DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
    public static extern void TTF_CloseFont(IntPtr font);

    [DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr TTF_RenderText_Blended(IntPtr font, string text, SDL.SDL_Color fg);

    [DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
    public static extern void TTF_Quit();
}

2. フォントを描画するコード例

using System;
using SDL2;

class TtfExample
{
    static void Main()
    {
        SDL.SDL_Init(SDL.SDL_INIT_VIDEO);
        SDL_ttf.TTF_Init();

        IntPtr window = SDL.SDL_CreateWindow("TTF Example", 100, 100, 800, 600, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN);
        IntPtr renderer = SDL.SDL_CreateRenderer(window, -1, 0);

        IntPtr font = SDL_ttf.TTF_OpenFont("font.ttf", 32); // フォントファイル必要!
        if (font == IntPtr.Zero)
        {
            Console.WriteLine("Failed to load font: " + SDL.SDL_GetError());
            return;
        }

        SDL.SDL_Color color = new SDL.SDL_Color { r = 255, g = 255, b = 255, a = 255 };
        IntPtr surface = SDL_ttf.TTF_RenderText_Blended(font, "Hello SDL_ttf!", color);
        IntPtr texture = SDL.SDL_CreateTextureFromSurface(renderer, surface);

        SDL.SDL_FreeSurface(surface);

        bool running = true;
        SDL.SDL_Event e;

        while (running)
        {
            while (SDL.SDL_PollEvent(out e) != 0)
            {
                if (e.type == SDL.SDL_EventType.SDL_QUIT)
                    running = false;
            }

            SDL.SDL_RenderClear(renderer);
            SDL.SDL_RenderCopy(renderer, texture, IntPtr.Zero, IntPtr.Zero);
            SDL.SDL_RenderPresent(renderer);
        }

        SDL.SDL_DestroyTexture(texture);
        SDL_ttf.TTF_CloseFont(font);
        SDL.SDL_DestroyRenderer(renderer);
        SDL.SDL_DestroyWindow(window);
        SDL_ttf.TTF_Quit();
        SDL.SDL_Quit();
    }
}

💡 注意点

  • font.ttfsample.png はカレントディレクトリに置いてください。
  • バインディングファイル(SDL_image.cs, SDL_ttf.cs)をプロジェクトに追加する必要があります。
  • mcs でビルド時は全ファイルをまとめて指定:
mcs -out:App.exe ImageExample.cs SDL2.cs SDL_image.cs

必要なら SDL_mixer の例や、Makefile、.csproj(.NET Core用)構成も作れますよ!

Makefile、.csproj

了解です!
以下に SDL2 + SDL_image + SDL_ttf を使った C# アプリのための Makefile.csproj(.NET Core/SDKスタイル)両方用意しました。


✅ 1. Makefile(Mono / mcs 向け)

Mono を使う場合の Makefile です。SDL2、SDL_image、SDL_ttf に必要な .cs ファイルも含めています。

# ファイル名: Makefile

CSFILES = Program.cs SDL2.cs SDL_image.cs SDL_ttf.cs
OUT = App.exe

all:
	mcs -out:$(OUT) $(CSFILES)

run:
	mono $(OUT)

clean:
	rm -f *.exe *.dll

使い方:

make        # ビルド
make run    # 実行
make clean  # 実行ファイル削除

✅ 2. .csproj(.NET Core / .NET 6+ 向け)

.NET SDK スタイルのプロジェクトファイル(dotnet run を使いたい場合)。

📄 SdlApp.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="SDL2.cs" />
    <Compile Include="SDL_image.cs" />
    <Compile Include="SDL_ttf.cs" />
  </ItemGroup>

</Project>

✅ 実行手順

dotnet new console -n SdlApp
cd SdlApp

# 既存の Program.cs を差し替え、SDL2.cs などを追加

dotnet run

SDL2 系の .so ライブラリ(libSDL2.so, libSDL2_image.so, libSDL2_ttf.so)は Linux にインストール済みである必要があります(apt install で入れていればOK)。


🔚 必要なら…

  • SDL_mixer.cs の .cs バインディング
  • Linux 用 .desktop ファイル(アプリ化)
  • .NET での NuGet パッケージ方式の SDL2 利用方法

なども追加できます。どの開発環境をメインに使う予定ですか?(Mono / .NET 6 / .NET 8 など)

Linux 用 .desktop ファイル(アプリ化)

いいですね!Linux で自作アプリ(SDL2など含むC#アプリ)を .desktop ファイルで アプリケーションメニューから起動可能にする 方法を紹介します。


.desktop ファイルとは?

.desktop ファイルは Linux の GUI ランチャーが使うショートカットです。
スタートメニューやアプリ検索で表示されるようになります。


🔧 1. 例:SDL2 C# アプリの .desktop ファイル

📄 ~/.local/share/applications/sdl_app.desktop

[Desktop Entry]
Type=Application
Name=My SDL2 App
Exec=mono /home/youruser/Projects/SdlApp/App.exe
Icon=/home/youruser/Projects/SdlApp/icon.png
Terminal=false
Categories=Game;Application;

✅ ポイント

  • Exec= には実行ファイルのフルパス(Monoの場合は mono を明記)
  • Icon= に 48×48〜512×512 の .png 画像ファイルを指定
  • Terminal=false でコンソールが開かないように

🛠 2. ファイルの設置と実行権限

chmod +x ~/.local/share/applications/sdl_app.desktop

または

gio set ~/.local/share/applications/sdl_app.desktop "metadata::trusted" yes

📌 注意

  • .desktop ファイルのパスは必ず ~/.local/share/applications/ に入れることでメニューに出てきます。
  • 実行権限がないと表示されません(Nautilus だと「信頼されていないアプリ」扱いになります)。

🖼 アイコン画像を用意しよう(任意)

例:

convert logo.svg -resize 256x256 icon.png

または手動で用意して icon.png としてプロジェクトに保存。


📚 カテゴリー(Categories)の一例

カテゴリー内容
Gameゲーム関連
Graphicsグラフィック系アプリ
Utilityユーティリティ
Development開発ツール
AudioVideoメディア系

✅ 完了後:メニュー検索から起動

例:GNOME / KDE の「アクティビティ」や「スタートメニュー」で「My SDL2 App」と検索すれば表示されます。


必要であれば:

  • .desktop ファイルをインストーラーや make install で自動配置
  • Flatpak / AppImage 化(さらに配布用にしたい場合)

も案内できます!どういう形で使いたいか教えてくれたら最適な方法を提案します。

よかったらシェアしてね!
目次