4. 基本的な関数を作る。
まず、プレイヤーの位置情報や、状態を管理するために、プレイヤーの情報を構造体で示します。
構造体の宣言を見てください。ここでは、xとyについてだけ、宣言されていることを確認していただけたらいいです。
typedef struct{
double x,y;
int status,counter;
int shot [11][15];
} BODY_player_t;
BODY_player_t Player;
次に、画像のハンドルを格納するために、
void img_sound_load();
という関数を作ります。ここで画像ファイル、音楽ファイルのハンドルを変数に格納します。
void initialization();
という関数で、先ほど宣言した構造体の初期化を行っています。
void SetColor();
という関数で、色のハンドルを作成しています。
void Background();
という関数で、背景を描画します。
void PlayerControl();
という関数で、プレイヤーを描画します。
void Background2();
という関数で、得点盤を描画します。
メイン関数ではこれらの作成した関数を順番に呼び出しているだけです。
//素材 ver 1.00以上必要。
#include "DxLib.h" /* グローバル宣言 */ int counter=0; int color_white; char Key[256]; //画像ファイルハンドル int img_background[2]; int img_player[4]; //構造体 typedef struct{ double x,y; int status,counter; int shot [11][15]; } BODY_player_t; BODY_player_t Player; /******************/ void img_sound_load(){ img_background[0] = LoadGraph("Sh/img/back/background0.png"); img_background[1] = LoadGraph("Sh/img/back/background1.png"); LoadDivGraph( "Sh/img/char/player.png" , 4 , 4 , 1 , 32 , 48 , img_player ) ; } void initialization(){ Player.x=200.0; Player.y=400.0; Player.counter=0; Player.status=0; } void SetColor(){ color_white = GetColor(255,255,255); //白色ハンドルを取得 } void Background(){ DrawGraph(0,0,img_background[0],FALSE); //背景を描画 } void PlayerControl(){ DrawGraph((int)Player.x,(int)Player.y,img_player[0],TRUE);//プレイヤーを描画 } void Background2(){ DrawGraph(420,0,img_background[1],FALSE); //得点盤を描画 } void FpsTimeFanction(){ static int FpsTime[2]={0,},FpsTime_i=0; static double Fps=0.0; if(FpsTime_i== 0) FpsTime[0]=GetNowCount(); //1周目の時間取得 if(FpsTime_i==49){ FpsTime[1]=GetNowCount(); //50周目の時間取得 Fps = 1000.0f / ((FpsTime[1] - FpsTime[0]) / 50.0f);//測定した値からfpsを計算 FpsTime_i=0; } else FpsTime_i++; //現在何周目かカウント if(Fps != 0) DrawFormatString(565,460,color_white,"FPS %.1f",Fps); //fpsを表示 return; } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){ int RefreshTime=0; ChangeWindowMode( TRUE ) ; if( DxLib_Init() == -1 ) return -1; SetDrawScreen( DX_SCREEN_BACK ) ; //裏画面を使用する。 img_sound_load(); //画像ファイルをロード initialization(); //初期化 SetColor(); //色を取得 while(ProcessMessage() == 0 && GetHitKeyStateAll(Key) == 0){ RefreshTime = GetNowCount(); //今の時間を取得 ClearDrawScreen(); //裏画面のデータを全て削除 Background(); //背景描画 PlayerControl(); //プレイヤー描画 Background2(); //得点盤描画 FpsTimeFanction(); //FPS計算 ScreenFlip() ; //裏画面データを表画面へ反映 counter++; if(Key[KEY_INPUT_ESCAPE]==1) break; //Escapeが押されたら終了 while(GetNowCount() - RefreshTime < 17); //1周の処理が17ミリ秒になるまで待つ } DxLib_End() ; return 0 ; }
実行画面
DXライブラリ著作権表示
DX Library Copyright (C) 2001-2006 Takumi
Yamada.