一例としてやってみました。
//- テストコード -//
#include "DxLib.h"
const int SCR_W = 640;
const int SCR_H = 480;
const int CHIP_SIZE = 32;
const int BLOCK_MAX_X = SCR_W / CHIP_SIZE;
const int BLOCK_MAX_Y = SCR_H / CHIP_SIZE;
const int MAP_CHIP_X = BLOCK_MAX_X * 2; // マップの大きさ(2画面分)
const int MAP_CHIP_Y = BLOCK_MAX_Y;
const int MAP_SIZE_X = MAP_CHIP_X * CHIP_SIZE;
const int MAP_SIZE_Y = MAP_CHIP_Y * CHIP_SIZE;
const int MAP_LIMIT_L = 0; // マップを移動する限界
const int MAP_LIMIT_R = MAP_SIZE_X - SCR_W;
const int SCR_LIMIT_L = CHIP_SIZE * 1; // キャラを移動する限界
const int SCR_LIMIT_R = SCR_W - CHIP_SIZE * 2;
const int MAP_MOVE_RATE = 1;
const int CHAR_MOVE_RATE = 5;
// マップデータ
const char MapData[MAP_CHIP_Y][MAP_CHIP_X] = {
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,
1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,
1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0, 0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,
1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0, 0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0, 0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,
1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0, 0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,
1,0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0, 0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,1,
1,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,0,0,0,0, 0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0, 0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
};
int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
{
ChangeWindowMode( TRUE );
SetWindowText( "DxLib:" DXLIB_VERSION_STR );
if ( DxLib_Init( ) == -1 ) return -1;
SetDrawScreen( DX_SCREEN_BACK );
int map_pos = MAP_LIMIT_L;
int char_x = (640 - CHIP_SIZE) / 2;
int char_y = (480 - CHIP_SIZE) / 2;
int char_limit_l = map_pos + SCR_LIMIT_L;
int char_limit_r = map_pos + SCR_LIMIT_R;
int BlockColor = GetColor( 255, 255, 255 );
int CharColor = GetColor( 255, 0, 0 );
while ( ProcessMessage( ) == 0 && CheckHitKey( KEY_INPUT_ESCAPE ) == 0 )
{
if ( map_pos < MAP_LIMIT_R ) {
map_pos += MAP_MOVE_RATE;
if ( map_pos > MAP_LIMIT_R ) // 補正
map_pos = MAP_LIMIT_R;
char_limit_l = map_pos + SCR_LIMIT_L;
char_limit_r = map_pos + SCR_LIMIT_R;
}
if ( CheckHitKey( KEY_INPUT_LEFT ) ) {
if ( char_x > char_limit_l )
char_x -= CHAR_MOVE_RATE;
}
if ( CheckHitKey( KEY_INPUT_RIGHT ) ) {
if ( char_x < char_limit_r )
char_x += CHAR_MOVE_RATE;
}
if ( char_x < char_limit_l ) char_x = char_limit_l; // 補正
if ( char_x > char_limit_r ) char_x = char_limit_r;
ClearDrawScreen( );
// マップ描画
int base_x = -(map_pos % CHIP_SIZE); // CHIP_SIZEで割り切れなければ画面外から描画開始
int max_x = BLOCK_MAX_X + (base_x ? 1 : 0); // CHIP_SIZEで割り切れなければ1マス多く描画する
int max_y = BLOCK_MAX_Y;
for ( int y = 0; y < max_y; y++ ) {
for ( int x = 0; x < max_x; x++ ) {
if ( MapData[y][x + (map_pos / CHIP_SIZE)] ) {
DrawBox( base_x + x * CHIP_SIZE, y * CHIP_SIZE,
base_x + x * CHIP_SIZE + CHIP_SIZE, y * CHIP_SIZE + CHIP_SIZE, BlockColor, TRUE );
}
}
}
// キャラ描画
DrawBox( char_x - map_pos, char_y, char_x - map_pos + CHIP_SIZE, char_y + CHIP_SIZE, CharColor, TRUE );
DrawFormatString( 0, 0, CharColor, "map_pos = %4d char_x = %4d base_x = %4d", map_pos, char_x, base_x );
ScreenFlip( );
}
DxLib_End( );
return 0;
}