マップファイルが読み込まれない

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Qloeh
記事: 35
登録日時: 10年前

マップファイルが読み込まれない

#1

投稿記事 by Qloeh » 9年前

お久しぶりです。今は本を片手に書いてみて勉強中です。
DXライブラリを使用したゲームに近いものを作っていますが、分割したマップ画像が表示されない(シーン変更ボタンを押しても切り替わらない)という事態になっています。
色々とおかしなところだらけですが、根本的に間違っている所があると思いますので、そこを指摘して頂ければありがたいです。
ソースコードとヘッダーは以下の通りです。

コード:

#include"map.h"
_MAP _map;
_MAP_LIST _maplist;

int LoadListFile(const char*name, char list[][STR_MAX], int max);
//ファイルのパス読み込み
bool LoadMapList(){
	if ((_maplist.num = LoadListFile(MAP_LIST, _maplist.list, MAP_LIST_MAX)) == -1){
		return false;
	}
	return true;
}

bool LoadMapData(){
	int fh = 0;
	char pass[STR_MAX] = { NULL };
	char str[STR_MAX] = { NULL };
	sprintf_s(pass, "%s%s%s", MAP_ROOT, _maplist.list[_maplist.map_id], MAP_DATA);
	if ((fh = FileRead_open(pass)) == 0){
		return false;
	}
	FileRead_gets(str, STR_MAX, fh);
	_map._data.size_x = atoi(str);
	_map._data.size_y = atoi(str);
	_map._data.chipsize_x = atoi(str);
	_map._data.chipsize_y = atoi(str);
	if (FileRead_close(fh) == -1){
		return false;
	}
	return true;
}

bool LoadMapFieldData(){
	int fh = 0;
	char pass[STR_MAX] = { NULL };
	char buf[10];
	sprintf_s(pass, "%s%s%s", MAP_ROOT, _maplist.list[_maplist.map_id], MAP_IMAGE);
	if ((fh = FileRead_open(pass)) == 0){
		return false;
	}
	for (int i = 0; i < _map._data.size_y; i++){
		for (int j = 0; j < _map._data.size_x; j++){
			_map._field.mapdata[i][j] = (int)(buf[i] - '0');
		}
	}
	if (FileRead_close(fh) == -1){
		return false;
	}
	return true;
}

bool LoadMapGraph(){
	char pass[STR_MAX] = { NULL };
	sprintf_s(pass, "%s%s%s", MAP_ROOT, _maplist.list[_maplist.map_id], MAP_GRAPH);
	LoadDivGraph(pass, 192, 16, 12, _map._data.chipsize_x, _map._data.chipsize_y, _map._field.mapchip[0], false);
	if (_map._field.mapchip==false)return false;
	return true;
}

void DrawMap(){
	for (int i = 0; i <(_map._data.size_y/_map._data.chipsize_y); i++){
		for (int j = 0; j <( _map._data.size_x/_map._data.chipsize_x); j++){
			DrawGraph((i + _map._data.chipsize_x), (j + _map._data.chipsize_y), _map._field.mapdata[i][j],false);
		}
	}
}

void GameMap(){
	DrawMap();
}

コード:

#include"map.h"
_MAP _map;
_MAP_LIST _maplist;

int LoadListFile(const char*name, char list[][STR_MAX], int max);
//ファイルのパス読み込み
bool LoadMapList(){
	if ((_maplist.num = LoadListFile(MAP_LIST, _maplist.list, MAP_LIST_MAX)) == -1){
		return false;
	}
	return true;
}

bool LoadMapData(){
	int fh = 0;
	char pass[STR_MAX] = { NULL };
	char str[STR_MAX] = { NULL };
	sprintf_s(pass, "%s%s%s", MAP_ROOT, _maplist.list[_maplist.map_id], MAP_DATA);
	if ((fh = FileRead_open(pass)) == 0){
		return false;
	}
	FileRead_gets(str, STR_MAX, fh);
	_map._data.size_x = atoi(str);
	_map._data.size_y = atoi(str);
	_map._data.chipsize_x = atoi(str);
	_map._data.chipsize_y = atoi(str);
	if (FileRead_close(fh) == -1){
		return false;
	}
	return true;
}

bool LoadMapFieldData(){
	int fh = 0;
	char pass[STR_MAX] = { NULL };
	char buf[10];
	sprintf_s(pass, "%s%s%s", MAP_ROOT, _maplist.list[_maplist.map_id], MAP_IMAGE);
	if ((fh = FileRead_open(pass)) == 0){
		return false;
	}
	for (int i = 0; i < _map._data.size_y; i++){
		for (int j = 0; j < _map._data.size_x; j++){
			_map._field.mapdata[i][j] = (int)(buf[i] - '0');
		}
	}
	if (FileRead_close(fh) == -1){
		return false;
	}
	return true;
}

bool LoadMapGraph(){
	char pass[STR_MAX] = { NULL };
	sprintf_s(pass, "%s%s%s", MAP_ROOT, _maplist.list[_maplist.map_id], MAP_GRAPH);
	LoadDivGraph(pass, 192, 16, 12, _map._data.chipsize_x, _map._data.chipsize_y, _map._field.mapchip[0], false);
	if (_map._field.mapchip==false)return false;
	return true;
}

void DrawMap(){
	for (int i = 0; i <(_map._data.size_y/_map._data.chipsize_y); i++){
		for (int j = 0; j <( _map._data.size_x/_map._data.chipsize_x); j++){
			DrawGraph((i + _map._data.chipsize_x), (j + _map._data.chipsize_y), _map._field.mapdata[i][j],false);
		}
	}
}

void GameMap(){
	DrawMap();
}

コード:

#include"title.h"

char key[256];
void GameTitle(){
	DrawBox(0, 0, 640, 480, GetColor(255, 255, 255), true);
	DrawGraph(0, 0, g_titleimg, false);
	if (key[KEY_INPUT_Z] == 1){
		g_gamestate = GAME_MAP;
	}
}

コード:

#ifndef __MAP_H__
#define __MAP_H__

#include<DxLib.h>

#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480

#define MAP_LIST "Map\\map.txt"
#define MAP_ROOT "Map\\"
#define MAP_DATA "\\data.txt"
#define MAP_IMAGE "\\image.txt"

#define MAP_GRAPH "\\chip.png"
#define MAP_LIST_MAX 10
#define MAP_CHIP_NUM_X 20
#define MAP_CHIP_NUM_Y 15
#define MAP_CHIP_X_MAX 192
#define MAP_CHIP_Y_MAX 192
#define MAP_RAYER 4
#define STR_MAX 256
#define SCROLL_MAX_X 40
#define SCROLL_MAX_Y 30

struct MapData{
	int size_x;//マップのサイズX
	int size_y;//マップのサイズY

	int chipsize_x;
	int chipsize_y;
};

struct MapField{
	int back;
	int graphic;
	int chipset[32];
	int mapchip[5][192];
	int mapdata[MAP_CHIP_Y_MAX][MAP_CHIP_X_MAX];
};


struct MapObject{
	int field;
	int wall;
	int obg;
	int item;
};

struct _MAP{
	MapData _data;
	MapField _field;
	MapObject _object;
};

struct _MAP_LIST{
	 char list[MAP_LIST_MAX][STR_MAX];
	 int num;
	 int map_id;
};

extern _MAP_LIST _maplist;

bool LoadMapList();
bool LoasMapData();
bool LoadMapFieldData();
bool LoadMapGraph();
void DrawMap();
void GameMap();

#endif

コード:

#ifndef __MAIN_H__
#define __MAIN_H__
#include<DxLib.h>



#include"file.h"


#include"title.h"

#include"map.h"

#define STR_MAX 256
//グローバール
extern int g_lasttime;
extern int g_frametime;
enum GameState{
	GAME_TITLE, GAME_MAP, GAME_END, GAME_OVER
};
extern GameState g_gamestate;
extern int g_titleimg;
extern float g_hx;
extern float g_hy;
extern char key[256];
void DrawGameTitle();
void DrawGameMap();
void DrawGameEnd();
void DrawGameOver();

#endif

コード:

#ifndef __TITLE_H__
#define __TITLE_H__


#include<DxLib.h>


#include"main.h"

void GameTitle();

#endif
質問などありましたらどうぞ

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: マップファイルが読み込まれない

#2

投稿記事 by みけCAT » 9年前

とりあえず、
sprintf_s、_sprintf_s_l、swprintf_s、_swprintf_s_l
によると、sprintf_s関数の第二引数はバッファのサイズを指定しなければならないようです。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: マップファイルが読み込まれない

#3

投稿記事 by みけCAT » 9年前

C++ではアンダーバー(_)から始まるグローバルな識別子は予約されているので、使ってはいけません。
N3337 2.11 Identifiers さんが書きました: 3 In addition, some identifiers are reserved for use by C ++ implementations and standard libraries (17.6.4.3.2)
and shall not be used otherwise; no diagnostic is required.
N3337 17.6.4.3.2 Global names さんが書きました: 1 Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore __ or begins with an underscore followed by an uppercase
letter (2.12) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the
global namespace.
(http://www.open-std.org/jtc1/sc22/wg21/ ... /n3337.pdf)

具体的には、以下の識別子がNGですね。
  • _MAP
  • _map
  • _MAP_LIST
  • _maplist
  • __MAP_H__
  • __MAIN_H__
  • __TITLE_H__
また、
Qloeh さんが書きました:

コード:

void DrawMap(){
	for (int i = 0; i <(_map._data.size_y/_map._data.chipsize_y); i++){
		for (int j = 0; j <( _map._data.size_x/_map._data.chipsize_x); j++){
			DrawGraph((i + _map._data.chipsize_x), (j + _map._data.chipsize_y), _map._field.mapdata[i][j],false);
		}
	}
}
は、足し算ではなく掛け算を使う

コード:

void DrawMap(){
	for (int i = 0; i <(_map._data.size_y/_map._data.chipsize_y); i++){
		for (int j = 0; j <( _map._data.size_x/_map._data.chipsize_x); j++){
			DrawGraph((i * _map._data.chipsize_x), (j * _map._data.chipsize_y), _map._field.mapdata[i][j],false);
		}
	}
}
の間違いではないでしょうか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Qloeh
記事: 35
登録日時: 10年前

Re: マップファイルが読み込まれない

#4

投稿記事 by Qloeh » 9年前

そうですね。間違えてました。
早速修正してみます。キー入力の件も自己解決できそうです。有難う御座いました

閉鎖

“C言語何でも質問掲示板” へ戻る