main.cpp
#include "main_header.h"
//ファイル名
const char* const filename_char1 = "char.png";
//画像
int image[12];
ch_info ch;
int hantei[10][20] = {
{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,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,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,1},
{1,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,1},
{1,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},
};
void img::load_char(){
LoadDivGraph(filename_char1,12,3,4,32,48,image);
}
int img::field_can_or_cannot(int& x,int& y,int& muki){
//下向き
if(muki == 0){
if(hantei[y / 48 + 1][x / 32] == 1){
return 1;
}
}
//左向き
if(muki == 1){
if(hantei[y / 48][x / 32 - 1] == 1){
return 1;
}
}
//右向き
if(muki == 2){
if(hantei[y / 48][x / 32 + 1] == 1){
return 1;
}
}
//上向き
if(muki == 3){
if(hantei[y / 48 - 1][x / 32] == 1){
return 1;
}
}
return 0;
}
void img::move(char key[/url]){
if(ch.x % 32 == 0 && ch.y % 48 == 0){
ch.walking_flag = 1;
if(key[KEY_INPUT_DOWN] == 1){
ch.muki = 0;
}else if(key[KEY_INPUT_LEFT] == 1){
ch.muki = 1;
}else if(key[KEY_INPUT_RIGHT] == 1){
ch.muki = 2;
}else{
ch.walking_flag = 0;
}
}
if(ch.walking_flag == 1){
if(img::field_can_or_cannot(ch.x,ch.y,ch.muki) == 1){
ch.walking_flag = 0;
}
}
if(ch.walking_flag == 1){
if(ch.muki == 0){
ch.y++;
}else if(ch.muki == 1){
ch.x--;
}else if(ch.muki == 2){
ch.x++;
}else if(ch.muki == 3){
ch.y--;
}
}
ch.img = image[(ch.x % 32) / 11 + (ch.y % 48) / 16 + ch.muki * 3];
}
void img::draw(){
DrawGraph(ch.x,ch.y,ch.img,TRUE);
}
//メイン
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
char key[256];//キー
int func_state = 0;;//状態遷移
ChangeWindowMode(TRUE);
if(DxLib_Init() == -1) return -1;
SetDrawScreen(DX_SCREEN_BACK);
ch.x = 320;
ch.y = 240;
ch.walking_flag = 0;
ch.muki = 0;
while(true){
ClearDrawScreen();
GetHitKeyStateAll(key);
switch(func_state){
case 0:
img::load_char();
img::move(key);
break;
}
if(ProcessMessage() == -1) break;
ScreenFlip();
}
DxLib_End();
return 0;
}
main_header.h
#include "DxLib.h"
//画像関連
namespace img{
//キャラチップ読み込み
void load_char();
//フィールドあたり判定
int field_can_or_cannot(int& x,int& y,int& muki);
//キー操作
void move(char key[/url]);
//キャラクタードロー
void draw();
}
//キャラクター情報
struct character_info{
int x; //x座標
int y; //y座標
int img; //イメージ
int muki; //向き
int walking_flag; //歩けるかどうか。
};
//リネーム
typedef struct character_info ch_info;
なぜかメモリを馬鹿食いするのですが、設定が悪いんでしょうか。
指南のほどよろしくお願いします。
メモリを馬鹿食いする件について
Re:メモリを馬鹿食いする件について
switch(func_state){
case 0:
img::load_char();
img::move(key);
break;
}
ここで毎回画像のロードをしているから。
ロードは一回にしないと重たいよ。
case 0:
img::load_char();
img::move(key);
break;
}
ここで毎回画像のロードをしているから。
ロードは一回にしないと重たいよ。