ページ 11

文字列の読み込み等について

Posted: 2012年6月19日(火) 21:49
by なお
初めて質問させていただきます。
DXlibを使い、テキストファイルを読み込み、Zキーが押されたら続きを表示するというプログラムを作ろうとしています。
しかし下記のソースコードを作ったところ、Zキーを押した後再びテキストファイルの最初の行に戻ってしまい、続きを表示することが出来ません。
テキストの続きを表示するのはどうしたらいいのでしょうか?

コード:

#include "DxLib.h"
#define FONTSIZE 18

int White = GetColor(255,255,255);
int Key[256];

int gpUpdateKey(){
	char tmpKey[256];
	GetHitKeyStateAll(tmpKey);
	for(int i=0;i<256;i++){
		if(tmpKey[i] != 0){
			Key[i]++;
		}else{
			Key[i]=0;
		}
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
	ChangeWindowMode(TRUE),DxLib_Init(),SetDrawScreen(DX_SCREEN_BACK);
	int i,FileHandle,command=0;
	char string[256];
	SetFontSize(FONTSIZE);

	while(ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 && gpUpdateKey()==0){
		if(Key[KEY_INPUT_Z]==1)
			command=1;
		FileHandle=FileRead_open("test.txt");
		if(command==0){
			for(i=0;i<15;i++){
				FileRead_gets(string,256,FileHandle);
				DrawString(25,i*20,string,White);
			}
		}
		if(command==1){
			ClearDrawScreen();
			for(i=0;i<15;i++){
				FileRead_gets(string,256,FileHandle);
				DrawString(25,i*20,string,White);
			}
		}
		if(Key[KEY_INPUT_ESCAPE]==1){
			FileRead_close(FileHandle);
			DxLib_End();
			return 0;
		}
	}
	FileRead_close(FileHandle);
	DxLib_End();
	return 0;
}

Re: 文字列の読み込み等について

Posted: 2012年6月19日(火) 21:57
by みけCAT
ループで毎回FileRead_openしてしまっていますね。
とりあえずZキーを押した時だけFileRead_openするようにしてください。
それだけでなく、FileRead_openとFileRead_closeが1:1で対応するように気をつけてください。

Re: 文字列の読み込み等について

Posted: 2012年6月19日(火) 22:09
by なお
完全に見落としてました
初歩的な間違いでしたが指摘してくださりありがとうございました

Re: 文字列の読み込み等について

Posted: 2012年6月19日(火) 22:12
by softya(ソフト屋)
次のように修正します。

(1)whileループ前にFileRead_openしてDxLib_End();前にFileRead_closeする様に変更。
(2)毎フレームFileRead_getsせずKEY_INPUT_ZでFileRead_getsして配列バッファに蓄えておくこと。
(3)毎フレームは蓄えてある配列バッファから表示。
(4)KEY_INPUT_ESCAPEではbreak;だけで良い。
(5)ClearDrawScreen();は一箇所に。

Re: 文字列の読み込み等について

Posted: 2012年6月20日(水) 18:35
by なお
お二方の意見を参考にこのように書き換えてみました
一応自分が考えていたテキストファイルを読み込んで表示するという機能は実装出来ました
ここは変更した方が良いなどという指摘がありましたら教えていただけますでしょうか
お願いします

コード:

#include "DxLib.h"
#include <string.h>
#define FONTSIZE 18

int White = GetColor(255,255,255),Black = GetColor(0,0,0);
int Key[256];

int gpUpdateKey(){
	char tmpKey[256];
	GetHitKeyStateAll(tmpKey);
	for(int i=0;i<256;i++){
		if(tmpKey[i] != 0){
			Key[i]++;
		}else{
			Key[i]=0;
		}
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
	ChangeWindowMode(TRUE),SetFontSize(FONTSIZE),DxLib_Init(),SetDrawScreen(DX_SCREEN_BACK);
	int FileHandle;
	char string[15][256];
	FileHandle=FileRead_open("test.txt");

	for(int i=0;i<15;i++){
		FileRead_gets(string[i],256,FileHandle);	
	}

	while(ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 && gpUpdateKey()==0){

		if(Key[KEY_INPUT_Z]==1){
			for(int i=0;i<15;i++){
				strcpy(string[i],"");
				FileRead_gets(string[i],256,FileHandle);
			}
		}
		for(int i=0;i<15;i++){
			DrawString(25,20*i,string[i],White);
		}
		if(Key[KEY_INPUT_ESCAPE]==1){
			break;
		}
	}
	FileRead_close(FileHandle);
	DxLib_End();
	return 0;
}