初心者です。サンプル改造で質問です。
Posted: 2012年12月28日(金) 11:49
DXライブラリを使っています。
http://homepage2.nifty.com/natupaji/DxL ... m.html#N21
こちらのサンプルプログラムを改造して
学習プログラムを作ろうとしています。
一問一答形式で
ログが残って入力中の文字もでるこのプログラムを改造し作ろうと思いました。
一応われなりにいろいろ変えてみましたがエラーは出ないのにデバッグすると起動後入力領域と文字出力領域の線も引かずにプログラムが強制的に落とされます。
どこが原因なのか全くわかりません教えてください。
コードは以下↓です。
#include "DxLib.h"
#include <string.h>
#define CHAT_LINENUM 20 // チャット中の文字列を表示する行数#define MAX_STRLENGTH 80 // チャットで1行で入力できる文字数
#define INPUT_LINE 21 // チャットで入力領域となる画面上の行位置
#define FONT_SIZE 16 // フォントのサイズ
int InputHandle ; // 入力ハンドル
int StringY ; // 文字列表示領域の次に文字列を表示する時の行位置
char ScreenString[ CHAT_LINENUM ][ MAX_STRLENGTH + 1 ] ; // 画面に表示中のチャット文字列
int ScreenStringAdd( char *AddString ) ; // チャット文字列を追加する
int ScreenStringDraw( void ) ; // チャットの現在の状態を画面に表示する
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode( TRUE );
SetWindowPos( GetMainWindowHandle() , HWND_TOPMOST, 0, 0, 300, 300, (SWP_SHOWWINDOW |SWP_NOSIZE) );
char Key ;
// DXライブラリ初期化
if( DxLib_Init() == -1 )
{
return -1 ;
}
// 入力領域と文字出力領域との境界線を引く
DrawLine( 0 , CHAT_LINENUM * FONT_SIZE , 640 , CHAT_LINENUM * FONT_SIZE , GetColor( 255 , 255 , 255 ) ) ;
// 文字列入力ハンドルを作成する
InputHandle = MakeKeyInput( 80 , FALSE , FALSE , FALSE ) ;
// 作成した入力ハンドルをアクティブにする
SetActiveKeyInput( InputHandle ) ;
/*
// どちらのキーが押されるか監視する
{
while( !ProcessMessage() )
{
if( CheckHitKey( KEY_INPUT_Z ) )
{
Key = 'Z' ;
break ;
}
if( CheckHitKey( KEY_INPUT_X ) )
{
Key = 'X' ;
break ;
}
}
}
*/
int i;
char q[100][255];
char ans[100][255];
FILE *fin;
if ((fin = fopen("test.txt", "r ")) == NULL){
ScreenStringAdd("開かないぞゴラァ");
}
for (i = 0; i<100; i++) {
fgets(q,100,fin); // 一行ずつ読み込む
fgets(ans,100,fin);
}
char buff[256];
ScreenStringAdd("表示される日本語の英語表記を入力してください。\n文字は全て小文字でスペースなどを含まないでください。");
for(int i = 0 ; i < 100 ; i++)
{
ScreenStringAdd(">");//問題
// 文字列入力
{
// 文字列の入力が終っている場合は送信する
if( CheckKeyInput( InputHandle ) == 1 )
{
int StrLength ;
// 入力された文字列を取得する
GetKeyInputString( buff, InputHandle ) ;
// 自分のとこにも表示する
ScreenStringAdd( buff ) ;
// 入力文字列を初期化する
SetKeyInputString( "" , InputHandle ) ;
// 再度インプットハンドルをアクティブにする
SetActiveKeyInput( InputHandle ) ;
}
// 画面に入力中の文字列を描画する
DrawBox( 0 , INPUT_LINE * FONT_SIZE + 2 , 640 , 480 , 0 , TRUE ) ;
DrawKeyInputString( 0 , INPUT_LINE * FONT_SIZE + 2 , InputHandle ) ;
DrawKeyInputModeString( 640 , 480 ) ;
}
//fgets(buff,100,stdin);//文字を受け取る
buff[strlen(buff) - 1] = '\0';//改行(\n)を消去
ans[strlen(ans) - 1] = '\0';//改行(\n)を消去
ScreenStringAdd(ans);
ScreenStringAdd(buff);
if(strcmp(buff,ans) ==0)//比較
ScreenStringAdd("Good!!");//正解
else{
ScreenStringAdd("Bad…");//はずれ
ScreenStringAdd(ans);
}
}
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
// チャット文字列を追加する
int ScreenStringAdd( char *AddString )
{
// 格納する行を一つ進める
StringY ++ ;
// もし表示領域下いっぱいに来ていた場合はスクロールさせる
if( StringY == CHAT_LINENUM )
{
int i ;
for( i = 1 ; i < CHAT_LINENUM ; i ++ )
lstrcpy( ScreenString[ i - 1 ] , ScreenString[ i ] ) ;
ScreenString[ i - 1 ][ 0 ] = '\0' ;
StringY -- ;
}
// 文字列を格納する
lstrcpy( ScreenString[ StringY ] , AddString ) ;
// 画面の内容を描画する
ScreenStringDraw() ;
// 終了
return 0 ;
}
// チャットの現在の状態を画面に表示する
int ScreenStringDraw( void )
{
int i ;
// 文字列表示域を黒で塗りつぶす
DrawBox( 0 , 0 , 640 , CHAT_LINENUM * FONT_SIZE , 0 , TRUE ) ;
// すべてのチャット文字列を描画する
for( i = 0 ; i < CHAT_LINENUM ; i ++ )
DrawString( 0 , i * FONT_SIZE , ScreenString[ i ] , GetColor( 255 , 255 , 255 ) ) ;
// 終了
return 0 ;
}
よろしくお願いします。
ちなみにcode /codeとすると
http://homepage2.nifty.com/natupaji/DxL ... m.html#N21
こちらのサンプルプログラムを改造して
学習プログラムを作ろうとしています。
一問一答形式で
ログが残って入力中の文字もでるこのプログラムを改造し作ろうと思いました。
一応われなりにいろいろ変えてみましたがエラーは出ないのにデバッグすると起動後入力領域と文字出力領域の線も引かずにプログラムが強制的に落とされます。
どこが原因なのか全くわかりません教えてください。
コードは以下↓です。
#include "DxLib.h"
#include <string.h>
#define CHAT_LINENUM 20 // チャット中の文字列を表示する行数#define MAX_STRLENGTH 80 // チャットで1行で入力できる文字数
#define INPUT_LINE 21 // チャットで入力領域となる画面上の行位置
#define FONT_SIZE 16 // フォントのサイズ
int InputHandle ; // 入力ハンドル
int StringY ; // 文字列表示領域の次に文字列を表示する時の行位置
char ScreenString[ CHAT_LINENUM ][ MAX_STRLENGTH + 1 ] ; // 画面に表示中のチャット文字列
int ScreenStringAdd( char *AddString ) ; // チャット文字列を追加する
int ScreenStringDraw( void ) ; // チャットの現在の状態を画面に表示する
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode( TRUE );
SetWindowPos( GetMainWindowHandle() , HWND_TOPMOST, 0, 0, 300, 300, (SWP_SHOWWINDOW |SWP_NOSIZE) );
char Key ;
// DXライブラリ初期化
if( DxLib_Init() == -1 )
{
return -1 ;
}
// 入力領域と文字出力領域との境界線を引く
DrawLine( 0 , CHAT_LINENUM * FONT_SIZE , 640 , CHAT_LINENUM * FONT_SIZE , GetColor( 255 , 255 , 255 ) ) ;
// 文字列入力ハンドルを作成する
InputHandle = MakeKeyInput( 80 , FALSE , FALSE , FALSE ) ;
// 作成した入力ハンドルをアクティブにする
SetActiveKeyInput( InputHandle ) ;
/*
// どちらのキーが押されるか監視する
{
while( !ProcessMessage() )
{
if( CheckHitKey( KEY_INPUT_Z ) )
{
Key = 'Z' ;
break ;
}
if( CheckHitKey( KEY_INPUT_X ) )
{
Key = 'X' ;
break ;
}
}
}
*/
int i;
char q[100][255];
char ans[100][255];
FILE *fin;
if ((fin = fopen("test.txt", "r ")) == NULL){
ScreenStringAdd("開かないぞゴラァ");
}
for (i = 0; i<100; i++) {
fgets(q,100,fin); // 一行ずつ読み込む
fgets(ans,100,fin);
}
char buff[256];
ScreenStringAdd("表示される日本語の英語表記を入力してください。\n文字は全て小文字でスペースなどを含まないでください。");
for(int i = 0 ; i < 100 ; i++)
{
ScreenStringAdd(">");//問題
// 文字列入力
{
// 文字列の入力が終っている場合は送信する
if( CheckKeyInput( InputHandle ) == 1 )
{
int StrLength ;
// 入力された文字列を取得する
GetKeyInputString( buff, InputHandle ) ;
// 自分のとこにも表示する
ScreenStringAdd( buff ) ;
// 入力文字列を初期化する
SetKeyInputString( "" , InputHandle ) ;
// 再度インプットハンドルをアクティブにする
SetActiveKeyInput( InputHandle ) ;
}
// 画面に入力中の文字列を描画する
DrawBox( 0 , INPUT_LINE * FONT_SIZE + 2 , 640 , 480 , 0 , TRUE ) ;
DrawKeyInputString( 0 , INPUT_LINE * FONT_SIZE + 2 , InputHandle ) ;
DrawKeyInputModeString( 640 , 480 ) ;
}
//fgets(buff,100,stdin);//文字を受け取る
buff[strlen(buff) - 1] = '\0';//改行(\n)を消去
ans[strlen(ans) - 1] = '\0';//改行(\n)を消去
ScreenStringAdd(ans);
ScreenStringAdd(buff);
if(strcmp(buff,ans) ==0)//比較
ScreenStringAdd("Good!!");//正解
else{
ScreenStringAdd("Bad…");//はずれ
ScreenStringAdd(ans);
}
}
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
// チャット文字列を追加する
int ScreenStringAdd( char *AddString )
{
// 格納する行を一つ進める
StringY ++ ;
// もし表示領域下いっぱいに来ていた場合はスクロールさせる
if( StringY == CHAT_LINENUM )
{
int i ;
for( i = 1 ; i < CHAT_LINENUM ; i ++ )
lstrcpy( ScreenString[ i - 1 ] , ScreenString[ i ] ) ;
ScreenString[ i - 1 ][ 0 ] = '\0' ;
StringY -- ;
}
// 文字列を格納する
lstrcpy( ScreenString[ StringY ] , AddString ) ;
// 画面の内容を描画する
ScreenStringDraw() ;
// 終了
return 0 ;
}
// チャットの現在の状態を画面に表示する
int ScreenStringDraw( void )
{
int i ;
// 文字列表示域を黒で塗りつぶす
DrawBox( 0 , 0 , 640 , CHAT_LINENUM * FONT_SIZE , 0 , TRUE ) ;
// すべてのチャット文字列を描画する
for( i = 0 ; i < CHAT_LINENUM ; i ++ )
DrawString( 0 , i * FONT_SIZE , ScreenString[ i ] , GetColor( 255 , 255 , 255 ) ) ;
// 終了
return 0 ;
}
よろしくお願いします。
ちなみにcode /codeとすると
#include "DxLib.h"
#include <string.h>
#define CHAT_LINENUM 20 // チャット中の文字列を表示する行数
#define MAX_STRLENGTH 80 // チャットで1行で入力できる文字数
#define INPUT_LINE 21 // チャットで入力領域となる画面上の行位置
#define FONT_SIZE 16 // フォントのサイズ
int InputHandle ; // 入力ハンドル
int StringY ; // 文字列表示領域の次に文字列を表示する時の行位置
char ScreenString[ CHAT_LINENUM ][ MAX_STRLENGTH + 1 ] ; // 画面に表示中のチャット文字列
int ScreenStringAdd( char *AddString ) ; // チャット文字列を追加する
int ScreenStringDraw( void ) ; // チャットの現在の状態を画面に表示する
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode( TRUE );
SetWindowPos( GetMainWindowHandle() , HWND_TOPMOST, 0, 0, 300, 300, (SWP_SHOWWINDOW |SWP_NOSIZE) );
char Key ;
// DXライブラリ初期化
if( DxLib_Init() == -1 )
{
return -1 ;
}
// 入力領域と文字出力領域との境界線を引く
DrawLine( 0 , CHAT_LINENUM * FONT_SIZE , 640 , CHAT_LINENUM * FONT_SIZE , GetColor( 255 , 255 , 255 ) ) ;
// 文字列入力ハンドルを作成する
InputHandle = MakeKeyInput( 80 , FALSE , FALSE , FALSE ) ;
// 作成した入力ハンドルをアクティブにする
SetActiveKeyInput( InputHandle ) ;
/*
// どちらのキーが押されるか監視する
{
while( !ProcessMessage() )
{
if( CheckHitKey( KEY_INPUT_Z ) )
{
Key = 'Z' ;
break ;
}
if( CheckHitKey( KEY_INPUT_X ) )
{
Key = 'X' ;
break ;
}
}
}
*/
int i;
char q[100][255];
char ans[100][255];
FILE *fin;
if ((fin = fopen("test.txt", "r ")) == NULL){
ScreenStringAdd("開かないぞゴラァ");
}
for (i = 0; i<100; i++) {
fgets(q[i],100,fin); /* 一行ずつ読み込む */
fgets(ans[i],100,fin);
}
char buff[256];
ScreenStringAdd("表示される日本語の英語表記を入力してください。\n文字は全て小文字でスペースなどを含まないでください。");
for(int i = 0 ; i < 100 ; i++)
{
ScreenStringAdd(">");//問題
// 文字列入力
{
// 文字列の入力が終っている場合は送信する
if( CheckKeyInput( InputHandle ) == 1 )
{
int StrLength ;
// 入力された文字列を取得する
GetKeyInputString( buff, InputHandle ) ;
// 自分のとこにも表示する
ScreenStringAdd( buff ) ;
// 入力文字列を初期化する
SetKeyInputString( "" , InputHandle ) ;
// 再度インプットハンドルをアクティブにする
SetActiveKeyInput( InputHandle ) ;
}
// 画面に入力中の文字列を描画する
DrawBox( 0 , INPUT_LINE * FONT_SIZE + 2 , 640 , 480 , 0 , TRUE ) ;
DrawKeyInputString( 0 , INPUT_LINE * FONT_SIZE + 2 , InputHandle ) ;
DrawKeyInputModeString( 640 , 480 ) ;
}
//fgets(buff,100,stdin);//文字を受け取る
buff[strlen(buff) - 1] = '\0';//改行(\n)を消去
ans[i][strlen(ans[i]) - 1] = '\0';//改行(\n)を消去
ScreenStringAdd(ans[i]);
ScreenStringAdd(buff);
if(strcmp(buff,ans[i]) ==0)//比較
ScreenStringAdd("Good!!");//正解
else{
ScreenStringAdd("Bad…");//はずれ
ScreenStringAdd(ans[i]);
}
}
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
// チャット文字列を追加する
int ScreenStringAdd( char *AddString )
{
// 格納する行を一つ進める
StringY ++ ;
// もし表示領域下いっぱいに来ていた場合はスクロールさせる
if( StringY == CHAT_LINENUM )
{
int i ;
for( i = 1 ; i < CHAT_LINENUM ; i ++ )
lstrcpy( ScreenString[ i - 1 ] , ScreenString[ i ] ) ;
ScreenString[ i - 1 ][ 0 ] = '\0' ;
StringY -- ;
}
// 文字列を格納する
lstrcpy( ScreenString[ StringY ] , AddString ) ;
// 画面の内容を描画する
ScreenStringDraw() ;
// 終了
return 0 ;
}
// チャットの現在の状態を画面に表示する
int ScreenStringDraw( void )
{
int i ;
// 文字列表示域を黒で塗りつぶす
DrawBox( 0 , 0 , 640 , CHAT_LINENUM * FONT_SIZE , 0 , TRUE ) ;
// すべてのチャット文字列を描画する
for( i = 0 ; i < CHAT_LINENUM ; i ++ )
DrawString( 0 , i * FONT_SIZE , ScreenString[ i ] , GetColor( 255 , 255 , 255 ) ) ;
// 終了
return 0 ;
}