コード:
//
#include "stdafx.h"
#include "[2012 12 10] DxLib骨組み.h"
class CBlock
{
public:
int m_x, m_y;
void Set( int x, int y );
CBlock();
void Draw();
};
typedef struct
{
POINT camera; // カメラ
POINT player; // プレイヤー
int image; // 背景画像
int image2; // プレイヤー画像
vector<CBlock> vBlock; // ブロックコンテナ
} Param_t;
static Param_t g_tParam;
CBlock::CBlock() { m_x = m_y = 0; }
void CBlock::Set( int x, int y )
{
m_x = x;
m_y = y;
}
void CBlock::Draw()
{
int x = m_x + g_tParam.camera.x;
int y = m_y + g_tParam.camera.y;
DrawBox( x-16, y-16, x+16, y+16, GetColor(0,0,128), TRUE );
}
//======================================================================================
void 初期化()
{
memset( &g_tParam, 0, sizeof(g_tParam) );
g_tParam.image = LoadGraph( _T("yandere.jpg") );
g_tParam.image2 = LoadGraph( _T("プレイヤー1.bmp") );
}
//======================================================================================
void 描画()
{
int w, h;
GetGraphSize( g_tParam.image, &w, &h );
// 背景描画
int x = 320 - w/2;
int y = 240 - h/2;
x = x - g_tParam.camera.x;
y = y - g_tParam.camera.y;
DrawGraph( x, y, g_tParam.image, TRUE );
// プレイヤーの描画
x = g_tParam.player.x - g_tParam.camera.x + 320;
y = 240;
DrawGraph( x, y, g_tParam.image2, TRUE );
// ブロックの描画
vector<CBlock>::iterator p;
p = g_tParam.vBlock.begin();
while( p != g_tParam.vBlock.end() )
{
p->Draw();
p++;
}
// デバック情報
DrawFormatString( 0, 20, GetColor(255,0,0), _T("Camera:(%d,%d)"), g_tParam.camera.x, g_tParam.camera.y );
DrawFormatString( 0, 40, GetColor(255,0,0), _T("Player:(%d,%d)"), g_tParam.player.x, g_tParam.player.y );
}
//======================================================================================
void 動作()
{
if( 1 == CheckHitKey(KEY_INPUT_UP) )
{
g_tParam.camera.y -= 10;
g_tParam.player.y -= 10;
}
if( 1 == CheckHitKey(KEY_INPUT_DOWN) )
{
g_tParam.camera.y += 10;
g_tParam.player.y += 10;
}
if( 1 == CheckHitKey(KEY_INPUT_LEFT) )
{
g_tParam.camera.x -= 10;
g_tParam.player.x -= 10;
}
if( 1 == CheckHitKey(KEY_INPUT_RIGHT) )
{
g_tParam.camera.x += 10;
g_tParam.player.x += 10;
}
if( 1 == CheckHitKey(KEY_INPUT_SPACE) )
{
CBlock block;
block.Set( g_tParam.player.x, g_tParam.player.y );
g_tParam.vBlock.push_back( block );
}
}
//======================================================================================
int メインループ()
{
int result = 0;
static int mode = 0;
switch( mode )
{
case 0:
初期化();
mode = 10;
break;
case 10:
描画();
動作();
break;
}
return result;
}
int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode( TRUE );
if( DxLib_Init() == -1 ) // DXライブラリ初期化処理
{
return -1 ; // エラーが起きたら直ちに終了
}
// 描画先を裏にする
SetDrawScreen( DX_SCREEN_BACK );
// メインループ
while( ProcessMessage() == 0 && CheckHitKey(KEY_INPUT_ESCAPE)==0 )
{
// 画面をクリアする
ClearDrawScreen();
DrawString( 100, 100, _T("DxLib デフォルト"), GetColor(0,255,255), 0 );
if( メインループ() == -1 )
break;
ScreenFlip();
}
DxLib_End();
return 0;
}