LoginSignup
7

More than 3 years have passed since last update.

楽にDxLibを使うためのテンプレ

Last updated at Posted at 2018-11-28

よく使う処理をテンプレにする

ループ処理

namespace System {
    bool Update() { return (DxLib::ScreenFlip() != -1 && DxLib::ClearDrawScreen() != -1 && DxLib::ProcessMessage() != -1); }
}

Update()関数で画面の更新・画面消去・メッセージ処理をします。
ループ処理をいちいち書くのは面倒なのでまとめました。

Siv3Dライクな書き方をしています。

前処理

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    //初期化前に書く処理
    DxLib::ChangeWindowMode(TRUE);

    //初期化
    if (DxLib::DxLib_Init() == -1) return -1;

    //初期化後に書く処理
    DxLib::SetDrawScreen(DX_SCREEN_BACK);

    Main();
    return DxLib::DxLib_End();
}

メイン関数(の役割を担う関数)をvoid Main()に移しました。
int WINAPI WinMain 関数はプログラム開始時の前処理を書く場所として使用します。

上記のサンプルコードでは最低限使用する関数のみが書かれています。
実際に使用するときは"画面サイズ変更"関数"や"タイトル変更"関数を加えます。

メイン処理

void Main() {

    //メインループ
    while (System::Update()) {}

}

メインの処理はvoid Main()に書きます。
while (System::Update())で簡単にメインループが使用できます。

テンプレ完成

Source.cpp
#include "DxLib.h"

//ループ
namespace System {
    bool Update() { return (DxLib::ScreenFlip() != -1 && DxLib::ClearDrawScreen() != -1 && DxLib::ProcessMessage() != -1); }
}

void Main();

//前処理を書く場所
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    //初期化前に書く処理
    DxLib::ChangeWindowMode(TRUE);

    //初期化
    if (DxLib::DxLib_Init() == -1) return -1;

    //初期化後に書く処理
    DxLib::SetDrawScreen(DX_SCREEN_BACK);

    Main();
    return DxLib::DxLib_End();
}

//メイン関数
void Main() {

    //メインループ
    while (System::Update()) {}

}
コメント無し版
#include "DxLib.h"

namespace System {
    bool Update() { return (DxLib::ScreenFlip() != -1 && DxLib::ClearDrawScreen() != -1 && DxLib::ProcessMessage() != -1); }
}

void Main();

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    DxLib::ChangeWindowMode(TRUE);
    if (DxLib::DxLib_Init() == -1) return -1;
    DxLib::SetDrawScreen(DX_SCREEN_BACK);
    Main();
    return DxLib::DxLib_End();
}

void Main() {

    while (System::Update()) {}

}
C++17サンプル
#include <DxLib.h>

namespace System {
    bool Update() { return (DxLib::ScreenFlip() != -1 && DxLib::ClearDrawScreen() != -1 && DxLib::ProcessMessage() != -1); }
}

void Main();

int WINAPI WinMain([[maybe_unused]]HINSTANCE hInstance, [[maybe_unused]]HINSTANCE hPrevInstance, [[maybe_unused]]LPSTR lpCmdLine, [[maybe_unused]]int nCmdShow)
{
    DxLib::SetOutApplicationLogValidFlag(FALSE);
    DxLib::ChangeWindowMode(TRUE);
    DxLib::SetGraphMode(512, 512, 32);
    if (DxLib::DxLib_Init() == -1) return -1;
    DxLib::SetDrawScreen(DX_SCREEN_BACK);
    DxLib::SetMainWindowText("Sample");
    Main();
    return DxLib::DxLib_End();
}

void Main() {

    while (System::Update()) {

    }

}
楽ちん版
#include "DxLib.h"

namespace System {
    bool Update() { return (DxLib::ScreenFlip() != -1 && DxLib::ClearDrawScreen() != -1 && DxLib::ProcessMessage() != -1); }
}

void Main();

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    DxLib::SetOutApplicationLogValidFlag(FALSE);
    DxLib::ChangeWindowMode(TRUE);
    DxLib::SetGraphMode(512, 512, 32);
    DxLib::SetMainWindowText("Sample");
    if (DxLib::DxLib_Init() == -1) return -1;
    DxLib::SetDrawScreen(DX_SCREEN_BACK);
    Main();
    return DxLib::DxLib_End();
}

void Main() {

    while (System::Update()) {



    }

}

ソースコードのライセンス

These codes are licensed under CC0.
CC0

ソースコードは自由に使用してください。

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
7