0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DXLibを利用した2Dゲーム作成に必要な関数等のメモ

Last updated at Posted at 2025-09-29

DXLibの環境構築(Windows Visual Studio 2022)

  • Visual Studioを起動して「新しいプロジェクトの作成(N)」を選択する。
    1.「Windowsデスクトップウィザード」を選択
    2.「プロジェクト名」「場所」を入力、「ソリューションとプロジェクトを同じディレクトリに配置する(D)」にチェックを入れて「作成」を選択
    3.「アプリケーションの種類」で「デスクトップアプリケーション(.exe)を選択して、「空のプロジェクト」にチェックを入れて「OK」を選択
    4.VisualStudio IDEが起動される。ソリューションエキスプローラ―が表示されない場合は ctl + alt + l で表示
  • 「ソリューションエクスプローラー」の「ソースファイル」で右クリックして表示されるメニューから「追加」を選択、「新しい項目」を選択
    image.png
    「C++ファイル(.cpp)」を選択、「名前」を %プロジェクト名%.cpp にして「追加」を選択する。
    image.png
  • 「プロジェクト」メニューの「プロジェクト名のプロパティ」を選択してプロパティーページを表示する。
    image.png
    image.png
  • [ ]
  • [ ]

DXLibリファレンス

Windowsにタイトルをつける

 SetWindowText("タイトル")

Windowの大きさとカラービット数の指定

SetGraphMode(WIDTH, HEIGHT, 32);

背景色の指定

SetBackgroundColor(0, 0, 0); 

画面のちらつき防止、裏で書いて全部書き終わったら表示

SetDrawScreen(DX_SCREEN_BACK);

while (1) {
    ClearDrawScreen();  // 画面をクリアする

    ・・・
  
    ScreenFlip();  // 裏画面の内容を表画面に反映させる
    WaitTimer(10);  // 一定時間待つ
}

画像を読み込み

img = LoadGraph("image/image.png");

画像を画面に描画する

DrawGraph(X, Y, img, FALSE);
int DrawGraph( int x, int y, int GrHandle, int TransFlag ) ;
// TransFlag : 画像の透明度を有効にするかどうか( TRUE:有効にする FALSE:無効にする )

図形を描画する

int DrawLine( int x1 , int y1 , int x2 , int y2 , unsigned  int Color ) ;
int DrawBox( int x1 , int y1 , int x2 , int y2 ,unsigned int Color , int FillFlag ) ;
int DrawCircle( int x , int y , int r , unsigned int Color, int FillFlag ) ;
int DrawOval( int x , int y , int rx , int ry , unsigned int Color , int FillFlag ) ;
int DrawTriangle( int x1, int y1, int x2, int y2,int x3, int y3, unsigned int Color , int FillFlag ) ;
int DrawPixel( int x , int y , unsigned int Color ) ;

Soundの読み込み

int bgm = LoadSoundMem("sound/bgm.mp3");
int se = LoadSoundMem("sound/se.mp3");

Soundの音量設定

se > bgm

ChangeVolumeSoundMem(128, bgm);
ChangeVolumeSoundMem(256, se);

Sound再生停止

PlaySoundMem(bgm, DX_PLAYTYPE_LOOP);
StopSoundMem(bgm);
PlaySoundMem(se, DX_PLAYTYPE_BACK);

文字の大きさ当の変更

SetFontSize(50);

文字の描画

DrawString(X, Y, "文字列", 0x00ff00);  
int DrawString( int x , int y , char *String , unsigned int Color ) ;

影を付けた文字列を描画する。

// 影を付けた文字列を描画する
void drawText(int x, int y, int col, const char* txt, int val, int siz)
{
    SetFontSize(siz);
    DrawFormatString(x + 2, y + 2, 0x000000, txt, val);
    DrawFormatString(x, y, col, txt, val);
}
drawText(10, 10, 0x00ffff, "SCORE %d", score, 30);

#ヒットチェック、画面の外に出たかを判定

if(hx>x && hy >y) {
}

キーボードからの入力

if (CheckHitKey(KEY_INPUT_ESCAPE) == 1) break;
int CheckHitKey( int KeyCode ) ;

ジョイパッド入力

例 パッド1の上ボタンが押されているか調べる

if( ( GetJoypadInputState( DX_INPUT_PAD1 ) & PAD_INPUT_UP ) == 0 )
{
    // 押されていない
}
else
{
    // 押されている
}
int GetJoypadInputState( int InputType ) ;
PAD_INPUT_DOWN  // ↓チェックマスク(下キー or テンキーの2キー)
PAD_INPUT_LEFT  // ←チェックマスク(左キー or テンキーの4キー)
PAD_INPUT_RIGHT // →チェックマスク(右キー or テンキーの6キー)
PAD_INPUT_UP    // ↑チェックマスク(上キー or テンキーの8キー)
PAD_INPUT_1     // 1ボタンチェックマスク(Zキー)
PAD_INPUT_2     // 2ボタンチェックマスク(Xキー)
PAD_INPUT_3     // 3ボタンチェックマスク(Cキー)
PAD_INPUT_4     // 4ボタンチェックマスク(Aキー)
PAD_INPUT_5     // 5ボタンチェックマスク(Sキー)
PAD_INPUT_6     // 6ボタンチェックマスク(Dキー)
PAD_INPUT_7     // 7ボタンチェックマスク(Qキー)
PAD_INPUT_8     // 8ボタンチェックマスク(Wキー)
PAD_INPUT_9     // 9ボタンチェックマスク(ESCキー)
PAD_INPUT_10    // 10ボタンチェックマスク(スペースキー)

画面の遷移

enum { TITLE, PLAY, GAMEOVER, GAMEEND };
int scene = TITLE;

while (1) // メインループ
{
    switch (scene)
    {
    case TITLE:
        // TITLEの処理
        break;
    case PLAY:
        // Playの処理
        break;
    case GAMEOVER:
        // Playの処理
        break;
    case GAMEEND:
        // Playの処理
        break;            
    }

任意の場所でシーンを変更する。

scene = GAMEOVER ;
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?