#include "DxLib.h"
const char gStageData[] = "\
########\n\
# .. #\n\
# oo #\n\
# p #\n\
########";
const int gStageWidth = 8;
const int gStageHeight = 5;
int handle;
enum Object{
OBJ_SPACE,
OBJ_WALL,
OBJ_GOAL,
OBJ_BLOCK,
OBJ_BLOCK_ON_GOAL,
OBJ_MAN,
OBJ_MAN_ON_GOAL,
OBJ_UNKNOWN
};
void initialize(Object* state, int width, int height, const char* stageData);
void draw(const Object* state, int width, int height);
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
ChangeWindowMode(true);
DxLib_Init();
Object* state = new Object[gStageWidth*gStageHeight];
initialize(state, gStageWidth, gStageHeight, gStageData);
draw(state, gStageWidth, gStageHeight);
WaitKey();
DxLib_End();
return 0;
}
void initialize(Object* state, int width, int height, const char* stageData) {
const char* d = stageData;
int x = 0;
int y = 0;
while (*d != '\0') {
Object t;
switch (*d) {
case '#':t = OBJ_WALL;
case ' ':t = OBJ_SPACE;
case 'o':t = OBJ_BLOCK;
case 'O':t = OBJ_BLOCK_ON_GOAL;
case '.':t = OBJ_GOAL;
case 'p':t = OBJ_MAN;
case 'P':t = OBJ_MAN_ON_GOAL;
case '\n':
x = 0;
y++;
t = OBJ_UNKNOWN;
break;
default:t = OBJ_UNKNOWN;
}
d++;
if (t != OBJ_UNKNOWN) {
state[y*width + x] = t;
x++;
}
}
}
void draw(const Object* state, int width, int height) {
int x = 0;
int y = 0;
const Object* s = state;
for (int i = 0; i < gStageHeight; i++) {
for (int j = 0; j < gStageWidth; j++) {
switch (*s) {
case OBJ_SPACE:handle = LoadGraph("objspace.png");
case OBJ_WALL:handle = LoadGraph("objwall.png");
case OBJ_GOAL:handle = LoadGraph("objgoal.png");
case OBJ_BLOCK:handle = LoadGraph("objblock.png");
case OBJ_BLOCK_ON_GOAL:handle = LoadGraph("objblockongoal.png");
case OBJ_MAN:handle = LoadGraph("objman.png");
case OBJ_MAN_ON_GOAL:handle = LoadGraph("objmanongoal.png");
default:handle = LoadGraph("objblockongoal.png");
}
DrawGraph(x, y, handle, true);
x = x + 50;
s++;
}
x = 0;
y = y + 50;
}
}
最初にgStageData(マップ)を読み込んでinitialize関数で一つ一つのデータ(enum Object)を配列に格納していきます。
その後draw関数でObjectの値によって異なる画像を表示するようにしたのですがswitchの部分でdefaultの部分だけしか通過せず
objblockongoal.pngばかりが表示されてしまいます。どうすればいいのでしょうか?