Skip to content

Instantly share code, notes, and snippets.

@yumetodo
Last active August 26, 2015 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yumetodo/792f568cc4ef1892488f to your computer and use it in GitHub Desktop.
Save yumetodo/792f568cc4ef1892488f to your computer and use it in GitHub Desktop.
DxLibraryのグラッフィクハンドルとサウンドハンドルのラッパー
#include "DxGraphicHandle.h"
#include "DxLib.h"
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
#include "DxHandleException.h"
#endif
DxGHandle::DxGHandle(const std::string & FileName) DxHANDLE_NOEXCEPT {
this->GrHandle = LoadGraph(FileName.c_str());
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == this->GrHandle) throw DxGHandle_runtime_error("画像の読み込みに失敗しました");
#endif
}
DxGHandle::DxGHandle(int SizeX, int SizeY) DxHANDLE_NOEXCEPT {
this->GrHandle = MakeGraph(SizeX, SizeY);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == this->GrHandle) throw DxGHandle_runtime_error("画像ハンドルの作成に失敗しました");
#endif
}
DxGHandle::DxGHandle(INT2_t size) DxHANDLE_NOEXCEPT {
this->GrHandle = MakeGraph(size.first, size.second);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == this->GrHandle) throw DxGHandle_runtime_error("画像ハンドルの作成に失敗しました");
#endif
}
DxGHandle::DxGHandle(DxGHandle_t GrHandle) DxHANDLE_NOEXCEPT {
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == GrHandle) throw DxGHandle_runtime_error("画像ハンドルの作成に失敗しました");
#endif
this->GrHandle = GrHandle;
}
int DxGHandle::DrawGraph(int x, int y, bool TransFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawGraph(x, y, this->GrHandle, TransFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawGraph(INT2_t p, bool TransFlag) {
const auto re = DxLib::DrawGraph(p.first, p.second, this->GrHandle, TransFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawTurnGraph(int x, int y, bool TransFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawTurnGraph(x, y, this->GrHandle, TransFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawTurnGraph(INT2_t p, bool TransFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawTurnGraph(p.first, p.second, this->GrHandle, TransFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawExtendGraph(int x1, int y1, int x2, int y2, bool TransFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawExtendGraph(x1, y1, x2, y2, this->GrHandle, TransFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の拡大縮小描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawExtendGraph(INT2_t p1, INT2_t p2, bool TransFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawExtendGraph(p1.first, p1.second, p2.first, p2.second, this->GrHandle, TransFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の拡大縮小描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawRotaGraph(int x, int y, double ExtRate, double Angle, bool TransFlag, bool TurnFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawRotaGraph(x, y, ExtRate, Angle, this->GrHandle, TransFlag, TurnFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の回転描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawRotaGraph(INT2_t p, double ExtRate, double Angle, bool TransFlag, bool TurnFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawRotaGraph(p.first, p.second, ExtRate, Angle, this->GrHandle, TransFlag, TurnFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の回転描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawRotaGraph(int x, int y, int cx, int cy, double ExtRate, double Angle, bool TransFlag, bool TurnFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawRotaGraph2(x, y, cx, cy, ExtRate, Angle, this->GrHandle, TransFlag, TurnFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の回転描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawRotaGraph(INT2_t p, INT2_t c, double ExtRate, double Angle, bool TransFlag, bool TurnFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawRotaGraph2(p.first, p.second, c.first, c.second, ExtRate, Angle, this->GrHandle, TransFlag, TurnFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の回転描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawRotaGraph(int x, int y, int cx, int cy, double ExtRateX, double ExtRateY, double Angle, bool TransFlag, int TurnFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawRotaGraph3(x, y, cx, cy, ExtRateX, ExtRateY, Angle, this->GrHandle, TransFlag, TurnFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の回転描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawRotaGraph(INT2_t p, INT2_t c, std::pair<double, double> ExtRate, double Angle, bool TransFlag, int TurnFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawRotaGraph3(p.first, p.second, c.first, c.second, ExtRate.first, ExtRate.second, Angle, this->GrHandle, TransFlag, TurnFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の回転描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawModiGraph(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, bool TransFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawModiGraph(x1, y1, x2, y2, x3, y3, x4, y4, this->GrHandle, TransFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の自由変形描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawModiGraph(std::array<INT2_t, 4> p, bool TransFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawModiGraph(p[0].first, p[0].second, p[1].first, p[1].second, p[2].first, p[2].second, p[3].first, p[3].second, this->GrHandle, TransFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の自由変形描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawRectGraph(int DestX, int DestY, int SrcX, int SrcY, int Width, int Height, bool TransFlag, bool TurnFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawRectGraph(DestX, DestY, SrcX, SrcY, Width, Height, this->GrHandle, TransFlag, TurnFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の部分描画に失敗しました");
#endif
return re;
}
int DxGHandle::DrawRectGraph(INT2_t Dest, INT2_t Src, INT2_t coord, bool TransFlag, bool TurnFlag) DxHANDLE_NOEXCEPT {
const auto re = DxLib::DrawRectGraph(Dest.first, Dest.second, Src.first, Src.second, coord.first, coord.second, this->GrHandle, TransFlag, TurnFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の部分描画に失敗しました");
#endif
return re;
}
DxGHandle DxGHandle::DerivationGraph(int SrcX, int SrcY, int Width, int Height) DxHANDLE_NOEXCEPT {
return DxGHandle(DxLib::DerivationGraph(SrcX, SrcY, Width, Height, this->GrHandle));
}
DxGHandle DxGHandle::DerivationGraph(INT2_t Src, INT2_t coord) DxHANDLE_NOEXCEPT {
return DxGHandle(DxLib::DerivationGraph(Src.first, Src.second, coord.first, coord.second, this->GrHandle));
}
int DxGHandle::GetGraphSize(int * SizeXBuf, int * SizeYBuf) DxHANDLE_NOEXCEPT {
const auto re = DxLib::GetGraphSize(this->GrHandle, SizeXBuf, SizeYBuf);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("画像の大きさの取得に失敗しました");
#endif
return re;
}
std::pair<int, int> DxGHandle::GetGraphSize() DxHANDLE_NOEXCEPT {
std::pair<int, int> re;
return (0 == this->GetGraphSize(&re.first, &re.second)) ? re : std::pair<int, int>();
}
#pragma once
#include <string>
#include <utility>
#include <array>
#ifndef DISABLE_DxHANDLE_WRAP_USE_EXCEPTION
#define DxHANDLE_WRAP_USE_EXCEPTION//throwする
#endif
#if (defined(_MSC_VER) && _MSC_VER > 1800) ||(defined(__clang__) && __clang__ >= 3) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif //_MSC_VER > 1800
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
#define DxHANDLE_NOEXCEPT
#else
#define DxHANDLE_NOEXCEPT NOEXCEPT
#endif //DxHANDLE_WRAP_USE_EXCEPTION
typedef std::pair<int, int> INT2_t;
class DxGHandle
{
public:
typedef int DxGHandle_t;
DxGHandle() NOEXCEPT : GrHandle(-1) {}
DxGHandle(const std::string& FileName) DxHANDLE_NOEXCEPT;
DxGHandle(int SizeX, int SizeY) DxHANDLE_NOEXCEPT;
explicit DxGHandle(INT2_t size) DxHANDLE_NOEXCEPT;
explicit DxGHandle(DxGHandle_t GrHandle) DxHANDLE_NOEXCEPT;
DxGHandle(const DxGHandle& o) NOEXCEPT : GrHandle(o.GrHandle) {}
explicit DxGHandle(DxGHandle&& o) NOEXCEPT : GrHandle(o.GrHandle) {}
int DrawGraph(int x, int y, bool TransFlag) DxHANDLE_NOEXCEPT ;
int DrawGraph(INT2_t p, bool TransFlag) DxHANDLE_NOEXCEPT ;
int DrawTurnGraph(int x, int y, bool TransFlag) DxHANDLE_NOEXCEPT ;
int DrawTurnGraph(INT2_t p, bool TransFlag) DxHANDLE_NOEXCEPT ;
int DrawExtendGraph(int x1, int y1, int x2, int y2, bool TransFlag) DxHANDLE_NOEXCEPT ;
int DrawExtendGraph(INT2_t p1, INT2_t p2, bool TransFlag) DxHANDLE_NOEXCEPT ;
int DrawRotaGraph(int x, int y, double ExtRate, double Angle, bool TransFlag, bool TurnFlag = false) DxHANDLE_NOEXCEPT ;
int DrawRotaGraph(INT2_t p, double ExtRate, double Angle, bool TransFlag, bool TurnFlag = false) DxHANDLE_NOEXCEPT ;
int DrawRotaGraph(int x, int y, int cx, int cy, double ExtRate, double Angle, bool TransFlag, bool TurnFlag = false) DxHANDLE_NOEXCEPT ;
int DrawRotaGraph(INT2_t p, INT2_t c, double ExtRate, double Angle, bool TransFlag, bool TurnFlag = false) DxHANDLE_NOEXCEPT ;
int DrawRotaGraph(int x, int y, int cx, int cy, double ExtRateX, double ExtRateY, double Angle, bool TransFlag, int TurnFlag = false) DxHANDLE_NOEXCEPT ;
int DrawRotaGraph(INT2_t p, INT2_t c, std::pair<double, double> ExtRate, double Angle, bool TransFlag, int TurnFlag = false) DxHANDLE_NOEXCEPT ;
int DrawModiGraph(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, bool TransFlag) DxHANDLE_NOEXCEPT ;
int DrawModiGraph(std::array<INT2_t, 4> p, bool TransFlag) DxHANDLE_NOEXCEPT ;
int DrawRectGraph(int DestX, int DestY, int SrcX, int SrcY, int Width, int Height, bool TransFlag, bool TurnFlag = false) DxHANDLE_NOEXCEPT ;
int DrawRectGraph(INT2_t Dest, INT2_t Src, INT2_t coord, bool TransFlag, bool TurnFlag = false) DxHANDLE_NOEXCEPT ;
DxGHandle DerivationGraph(int SrcX, int SrcY, int Width, int Height) DxHANDLE_NOEXCEPT ;
DxGHandle DerivationGraph(INT2_t Src, INT2_t coord) DxHANDLE_NOEXCEPT ;
int GetGraphSize(int *SizeXBuf, int *SizeYBuf) DxHANDLE_NOEXCEPT ;
INT2_t GetGraphSize() DxHANDLE_NOEXCEPT ;
DxGHandle_t get_raw() NOEXCEPT { return this->GrHandle; }
private:
DxGHandle_t GrHandle;
};
#pragma once
#include <stdexcept>
class DxGHandle_runtime_error : public std::runtime_error {
public:
explicit DxGHandle_runtime_error(const std::string& what_arg) : std::runtime_error("DxGHandle_runtime_error_runtime_error : " + what_arg) {}
explicit DxGHandle_runtime_error(const char* what_arg) : std::runtime_error(std::string("DxGHandle_runtime_error_runtime_error : ") + what_arg) {}
};
class DxSHandle_runtime_error : public std::runtime_error {
public:
explicit DxSHandle_runtime_error(const std::string& what_arg) : std::runtime_error("DxSHandle_runtime_error_runtime_error : " + what_arg) {}
explicit DxSHandle_runtime_error(const char* what_arg) : std::runtime_error(std::string("DxSHandle_runtime_error_runtime_error : ") + what_arg) {}
};
#include "DxSoundHandle.h"
#include "DxLib.h"
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
#include "DxHandleException.h"
#endif
DxSHandle::DxSHandle(const std::string & FileName) DxHANDLE_NOEXCEPT {
this->SHandle = LoadSoundMem(FileName.c_str());
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == this->SHandle) {
const auto ext = FileName.substr(FileName.find_first_of('.'), ((FileName.back() == '\\') ? FileName.length() - 2 : std::string::npos));
if ("mid" == ext || "MID" == ext) throw DxSHandle_runtime_error("midiファイルは利用できません。PlaySound関数を利用してください");
throw DxSHandle_runtime_error("音声の読み込みに失敗しました");
}
#endif
}
DxSHandle::DxSHandle(DxSHandle_t SHandle) DxHANDLE_NOEXCEPT {
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == SHandle) throw DxGHandle_runtime_error("音声ハンドルの作成に失敗しました");
#endif
this->SHandle = SHandle;
}
int DxSHandle::play(DxSoundMode PlayType, bool TopPositionFlag) DxHANDLE_NOEXCEPT {
const auto re = PlaySoundMem(this->SHandle, static_cast<int>(PlayType), TopPositionFlag);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("音声の再生に失敗しました");
#endif
return re;
}
int DxSHandle::play(DxSoundMode PlayType, uint8_t Volume, bool TopPositionFlag) DxHANDLE_NOEXCEPT {
this->ChangeNextPlayVolume(Volume);
return this->play(PlayType, TopPositionFlag);
}
int DxSHandle::play(DxSoundMode PlayType, uint8_t Volume, int16_t Pan, bool TopPositionFlag) DxHANDLE_NOEXCEPT {
this->ChangeNextPlayPan(Pan);
return this->play(PlayType, Volume, TopPositionFlag);
}
int DxSHandle::ChangeVolume(uint8_t Volume) DxHANDLE_NOEXCEPT {
const auto re = ChangeVolumeSoundMem(Volume, this->SHandle);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("音量を変更できませんでした");
#endif
return re;
}
int DxSHandle::ChangePan(int16_t Pan) DxHANDLE_NOEXCEPT {
const auto re = ChangePanSoundMem(Pan, this->SHandle);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("パンを変更できませんでした");
#endif
return 0;
}
int DxSHandle::ChangeNextPlayVolume(uint8_t Volume) DxHANDLE_NOEXCEPT {
const auto re = ChangeNextPlayVolumeSoundMem(Volume, this->SHandle);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("音量を変更できませんでした");
#endif
return re;
}
int DxSHandle::ChangeNextPlayPan(int16_t Pan) DxHANDLE_NOEXCEPT {
const auto re = ChangeNextPlayPanSoundMem(Pan, this->SHandle);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("パンを変更できませんでした");
#endif
return re;
}
int DxSHandle::stop() DxHANDLE_NOEXCEPT {
const auto re = StopSoundMem(this->SHandle);
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
if (-1 == re) throw DxGHandle_runtime_error("StopSoundMem関数で失敗しました");
#endif
return re;
}
int DxSHandle::is_during_playback() NOEXCEPT {
return CheckSoundMem(this->SHandle);
}
#pragma once
#include <string>
#include <cstdint>
#ifndef DISABLE_DxHANDLE_WRAP_USE_EXCEPTION
#define DxHANDLE_WRAP_USE_EXCEPTION//throwする
#endif
#if (defined(_MSC_VER) && _MSC_VER > 1800) ||(defined(__clang__) && __clang__ >= 3) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif //_MSC_VER > 1800
#ifdef DxHANDLE_WRAP_USE_EXCEPTION
#define DxHANDLE_NOEXCEPT
#else
#define DxHANDLE_NOEXCEPT NOEXCEPT
#endif //DxHANDLE_WRAP_USE_EXCEPTION
enum class DxSoundMode : int
{
NORMAL = 0,
BACK = 1,
LOOP = 3
};
class DxSHandle {
public:
typedef int DxSHandle_t;
DxSHandle() NOEXCEPT : SHandle(-1) {}
DxSHandle(const std::string& FileName) DxHANDLE_NOEXCEPT;
explicit DxSHandle(DxSHandle_t SHandle) DxHANDLE_NOEXCEPT;
DxSHandle(const DxSHandle& o) NOEXCEPT : SHandle(o.SHandle) {}
explicit DxSHandle(DxSHandle&& o) NOEXCEPT : SHandle(o.SHandle) {}
int play(DxSoundMode PlayType, bool TopPositionFlag = true) DxHANDLE_NOEXCEPT;
int play(DxSoundMode PlayType, uint8_t Volume, bool TopPositionFlag = true) DxHANDLE_NOEXCEPT;
int play(DxSoundMode PlayType, uint8_t Volume, int16_t Pan, bool TopPositionFlag = true) DxHANDLE_NOEXCEPT;
int ChangeVolume(uint8_t Volume) DxHANDLE_NOEXCEPT;
int ChangePan(int16_t Pan) DxHANDLE_NOEXCEPT;
int ChangeNextPlayVolume(uint8_t Volume) DxHANDLE_NOEXCEPT;
int ChangeNextPlayPan(int16_t Pan) DxHANDLE_NOEXCEPT;
int stop() DxHANDLE_NOEXCEPT;
int is_during_playback() NOEXCEPT;
DxSHandle_t get_raw() NOEXCEPT { return this->SHandle; }
private:
DxSHandle_t SHandle;
};