Skip to content

Instantly share code, notes, and snippets.

@Buravo46
Last active May 2, 2024 10:57
Show Gist options
  • Save Buravo46/10021981 to your computer and use it in GitHub Desktop.
Save Buravo46/10021981 to your computer and use it in GitHub Desktop.
【DXLib】画像をコマ送りアニメーションするプログラム。
#include "DxLib.h"
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode(TRUE);
int GHandle[ 4 ] ;
int i ;
if( DxLib_Init() == -1 ) // DXライブラリ初期化処理
{
return -1; // エラーが起きたら直ちに終了
}
// 描画先を裏画面に設定
SetDrawScreen(DX_SCREEN_BACK);
// BMP画像のメモリへの分割読み込み
// 画像のPass, フレーム数, 横のフレーム数, 縦のフレーム数, 画像の大きさ, 画像格納用ハンドル
LoadDivGraph( "images/sprite.png" , 4 , 4 , 1 , 32 , 32 , GHandle ) ;
// ロードしたグラフィックのアニメーション
i = 0 ;
// キーが押されるまでループ(キー判定には『CheckHitKeyAll』を使用)
while( CheckHitKeyAll() == 0 )
{
// メッセージ処理
if( ProcessMessage() == -1 ){
break ; // エラーが起きたらループから抜ける
}
// 画面を消す
ClearDrawScreen();
// 更新
// グラフィックの描画(『DrawGraph』使用)
DrawGraph( 0 , 0 , GHandle[ i ] , TRUE );
// アニメーションパターンナンバーを変更
i ++ ;
if( i == 4 ) i = 0 ;
// 一定時間待つ(『WaitTimer』使用)
WaitTimer( 100 ) ;
// 裏画面を表画面に反映
ScreenFlip();
}
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
#include "DxLib.h"
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen( DX_SCREEN_BACK ); //ウィンドウモード変更と初期化と裏画面設定
int GHandle[ 4 ] ;
int i ;
// BMP画像のメモリへの分割読み込み
// 画像のPass, フレーム数, 横のフレーム数, 縦のフレーム数, 画像の大きさ, 画像格納用ハンドル
LoadDivGraph( "images/sprite.png" , 4 , 4 , 1 , 32 , 32 , GHandle ) ;
// ロードしたグラフィックのアニメーション
i = 0 ;
// while( 裏画面を表画面に反映, メッセージ処理, 画面クリア )
while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 ){
// 更新
// グラフィックの描画(『DrawGraph』使用)
DrawGraph( 0 , 0 , GHandle[ i ] , TRUE );
// アニメーションパターンナンバーを変更
i ++ ;
if( i == 4 ) i = 0 ;
// 一定時間待つ(『WaitTimer』使用)
WaitTimer( 100 ) ;
}
DxLib_End(); // DXライブラリ終了処理
return 0;
}
#include "DxLib.h"
#include "Player.h"
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen( DX_SCREEN_BACK ); //ウィンドウモード変更と初期化と裏画面設定
Player_Initialize(); // 初期化
// while( 裏画面を表画面に反映, メッセージ処理, 画面クリア )
while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 ){
// 更新
Player_Update(); //計算
Player_Draw(); //描画
}
Player_Finalize(); // 終了処理
DxLib_End(); // DXライブラリ終了処理
return 0;
}
#include "DxLib.h"
#include "Player.h"
// このファイル内でしか使えないグローバル変数
// m_が付いていたらこのファイル内でのみ使えるグローバル変数という意味
static int m_Image; // 画像ハンドル
static int GHandle[4]; // 格納用画像ハンドル
static int m_frameIndex; // 表示する番号
// 初期化をする
void Player_Initialize(){
// BMP画像のメモリへの分割読み込み
// 画像のPass, フレーム数, 横のフレーム数, 縦のフレーム数, 画像の大きさ, 画像格納用ハンドル
m_Image = LoadDivGraph( "images/sprite.png" , 4 , 4 , 1 , 32 , 32 , GHandle ) ;;
}
// 動きを計算する
void Player_Update(){
// アニメーションパターンナンバーを変更
m_frameIndex++;
if(m_frameIndex == 4) m_frameIndex = 0 ;
// 一定時間待つ(『WaitTimer』使用)
WaitTimer(100) ;
}
// 描画する
void Player_Draw(){
// グラフィックの描画(『DrawGraph』使用)
DrawGraph( 0 , 0 , GHandle[m_frameIndex] , TRUE );
}
// 終了処理をする
void Player_Finalize(){
DeleteGraph( m_Image );
}
#ifndef DEF_PLAYER_H //二重include防止
#define DEF_PLAYER_H
// 関数のプロトタイプ宣言
// 初期化をする
void Player_Initialize();
// 動きを計算する
void Player_Update();
// 描画する
void Player_Draw();
// 終了処理をする
void Player_Finalize();
#endif
#include "DxLib.h"
#include "Player.h"
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen( DX_SCREEN_BACK ); //ウィンドウモード変更と初期化と裏画面設定
// 変数名PlayerというPlayer_t型変数を宣言
Player_t Player;
Player_Initialize(&Player); // 初期化
// while( 裏画面を表画面に反映, メッセージ処理, 画面クリア )
while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 ){
// 更新
Player_Update(&Player); //計算
Player_Draw(Player); //描画
}
Player_Finalize(Player); // 終了処理
DxLib_End(); // DXライブラリ終了処理
return 0;
}
#include "DxLib.h"
#include "Player.h"
// 初期化をする
void Player_Initialize(Player_t *Player){
// BMP画像のメモリへの分割読み込み
// 画像のPass, フレーム数, 横のフレーム数, 縦のフレーム数, 画像の大きさ, 画像格納用ハンドル
Player->m_Image = LoadDivGraph( "images/sprite.png" , 4 , 4 , 1 , 32 , 32 , Player->m_Handle );
Player->m_frameIndex = 0;
}
// 動きを計算する
void Player_Update(Player_t *Player){
// アニメーションパターンナンバーを変更
Player->m_frameIndex++;
if(Player->m_frameIndex == 4) Player->m_frameIndex = 0 ;
// 一定時間待つ(『WaitTimer』使用)
WaitTimer(100) ;
}
// 描画する
void Player_Draw(Player_t Player){
// グラフィックの描画(『DrawGraph』使用)
DrawGraph( 0 , 0 , Player.m_Handle[Player.m_frameIndex] , TRUE );
}
// 終了処理をする
void Player_Finalize(Player_t Player){
DeleteGraph(Player.m_Image );
}
#ifndef DEF_PLAYER_H //二重include防止
#define DEF_PLAYER_H
// 構造体
// typedefで定義した型名には_tを付ける慣習がある
typedef struct{
int m_Image; // 画像ハンドル
int m_frameIndex; // 表示する番号
int m_Handle[4]; // 格納用画像ハンドル
} Player_t;
// 関数のプロトタイプ宣言
// 値を変更する必要があるならばポインタにし、そうでないならポインタにしない。
// 初期化をする
void Player_Initialize(Player_t *Player);
// 動きを計算する
void Player_Update(Player_t *Player);
// 描画する
void Player_Draw(Player_t Player);
// 終了処理をする
void Player_Finalize(Player_t Player);
#endif
#include "DxLib.h"
#include "Player.h"
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen( DX_SCREEN_BACK ); //ウィンドウモード変更と初期化と裏画面設定
// クラスオブジェクトの作成
CPlayer Player;
Player.Initialize(); // 初期化
// while( 裏画面を表画面に反映, メッセージ処理, 画面クリア )
while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 ){
// 更新
Player.Update(); //計算
Player.Draw(); //描画
}
Player.Finalize(); // 終了処理
DxLib_End(); // DXライブラリ終了処理
return 0;
}
#include "DxLib.h"
#include "Player.h"
// 初期化をする
void CPlayer::Initialize(void){
// BMP画像のメモリへの分割読み込み
// 画像のPass, フレーム数, 横のフレーム数, 縦のフレーム数, 画像の大きさ, 画像格納用ハンドル
m_Image = LoadDivGraph( "images/sprite.png" , 4 , 4 , 1 , 32 , 32 , m_Handle );
m_frameIndex = 0;
}
// 動きを計算する
void CPlayer::Update(void){
// アニメーションパターンナンバーを変更
m_frameIndex++;
if(m_frameIndex == 4) m_frameIndex = 0 ;
// 一定時間待つ(『WaitTimer』使用)
WaitTimer(100) ;
}
// 描画する
void CPlayer::Draw(void){
// グラフィックの描画(『DrawGraph』使用)
DrawGraph( 0 , 0 , m_Handle[m_frameIndex] , TRUE );
}
// 終了処理をする
void CPlayer::Finalize(void){
DeleteGraph(m_Image );
}
#ifndef DEF_PLAYER_H //二重include防止
#define DEF_PLAYER_H
// クラス宣言
class CPlayer{
// 変数宣言
int m_Image; // 画像ハンドル
int m_frameIndex; // 表示する番号
int m_Handle[4]; // 格納用画像ハンドル
// 関数宣言
public:
// 初期化をする
void Initialize(void);
// 動きを計算する
void Update(void);
// 描画する
void Draw(void);
// 終了処理をする
void Finalize(void);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment