ページ 11

キーボードモジュールの作成について

Posted: 2011年9月17日(土) 02:04
by ふぅ
初心者の書き込みで申し訳ないですが、よろしければお願いします。

C言語 ゲームプログラミングの館d.4のページを参考に、キーボードモジュールを作成しようとしたのですが、
ソースを打ち込みデバックを開始したところで

test.exe の 0x00000000 で初回の例外が発生しました: 0xC0000005: 場所 0x00000000 を読み込み中にアクセス違反が発生しました。
test.exe の 0x00000000 でハンドルされていない例外が発生しました: 0xC0000005: 場所 0x00000000 を読み込み中にアクセス違反が発生しました。
プログラム '[11736] test.exe: ネイティブ' はコード -1073741819 (0xc0000005) で終了しました。

とされ実行できませんでした・・・
お手隙の方いらっしゃいましたら原因をご教授願えればと思います・・・

(申し訳ないですが返信できるのが土曜日夕方以降になると思います・・・

以下ソースファイル

メイン関数
#include "DxLib.h"
#include "タイトル.h"
#include "キーボードモジュール.h"

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen( DX_SCREEN_BACK ); //ウィンドウモード変更と初期化と裏画面設定

Taitoru_Initialize();
while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 && DxLib_End()==0){

Keyboard_Update();
      Taitoru_Calc();
Taitoru_Graph();
}
DxLib_End(); // DXライブラリ終了処理
return 0;
}









※タイトル画面を表示するモジュール


#include "DxLib.h"
#include "タイトル.h"
#include "キーボードモジュール.h"

static int time;
static int d;
static int x ;
static int a[3];

static int toukado;
static int toukado2;
static int toukado3;

static int houkoukey;
static int Quit;
static int GameStart;
static int kontenyu;
static int taitoru;
static int rogo;
static int quit;
static int sitasen;

int r;

void Taitoru_Initialize(){
toukado = 0;
toukado2 = 0;
toukado3 = 0 ;
x = 65;
a[0] = 220;
a[1] = 290;
a[2] = 360;

time = 0 ;
d = 0;

houkoukey = LoadGraph("画像/方向キー右.png");
Quit = LoadGraph("画像/ゲーム終了.png");
GameStart = LoadGraph("画像/ゲーム開始ロゴ.png");
kontenyu=LoadGraph("画像/コンテニュー.png");
taitoru = LoadGraph("画像/タイトル.png");
rogo = LoadGraph("画像/ろご.png");
quit = LoadGraph("画像/ゲーム終了.png");
sitasen = LoadGraph("画像/下線.png");
}

void Taitoru_Calc(){
//演算フェイズ
if(Keyboard_Get(KEY_INPUT_UP) == 1)
{d = (d + 2 ) %3 ;
x = 120;
toukado = 255;
toukado2 = 255;
toukado3 = 255;}
if(Keyboard_Get(KEY_INPUT_DOWN) == 1)
{d = (d + 1) % 3 ;
x = 120;
toukado = 255;
toukado2 = 255;
toukado3 = 255;}

if(Keyboard_Get(KEY_INPUT_RETURN) == 1){
if(a[2]=360){DxLib_End();}}

if(x<=120){x++;}//ロゴの移動
if(toukado<=255){toukado = toukado + 3;}//ロゴを徐々に出現
if(time>=120){toukado2=toukado2+3;}
if(time>=150){toukado3=toukado3+3;}

}

void Taitoru_Graph(){
//描画フェイズ

DrawGraph(0,0,taitoru,TRUE);
SetDrawBlendMode(DX_BLENDMODE_ALPHA, toukado);
DrawGraph(x,85,rogo,TRUE);
SetDrawBlendMode(DX_BLENDMODE_ALPHA, toukado2);

DrawGraph(300,200,GameStart,TRUE);
DrawGraph(300,255,sitasen,TRUE);

DrawGraph(357,270,kontenyu,TRUE);
DrawGraph(300,325,sitasen,TRUE);

DrawGraph(470,340,quit,TRUE);
DrawGraph(300,395,sitasen,TRUE);

DrawGraph(263,a[d],houkoukey,TRUE);

SetDrawBlendMode(DX_BLENDMODE_NOBLEND, 0);

time++;}



上のヘッダファイル


#ifndef DEF_TAITORU_H //二重include防止

#define DEF_TAITORU_H

// 初期化をする
void Taitoru_Initialize();

// 動きを計算する
void Taitoru_Calc();

// 描画する
void Taitoru_Graph();


#endif








※キーボードモジュール


#include "DxLib.h"
#include "タイトル.h"


static int m_Key[256]; // キーの入力状態格納用変数

// キーの入力状態更新
void Keyboard_Update(){
char tmpKey[256];
// 現在のキーの入力状態を格納する
GetHitKeyStateAll( tmpKey ); // 全てのキーの入力状態を得る
for( int i=0; i<256; i++ ){
if( tmpKey != 0 ){ // i番のキーコードに対応するキーが押されていたら
m_Key++; // 加算
} else { // 押されていなければ
m_Key = 0; // 0にする
}
}
}

// KeyCodeのキーの入力状態を取得する
int Keyboard_Get( int KeyCode ){
return m_Key[ KeyCode ]; // KeyCodeの入力状態を返す
}

ヘッダファイル
#ifndef DEF_KEYBOARD_H //二重include防止

#define DEF_KEYBOARD_H

// キーの入力状態を更新する
void Keyboard_Update();

// 引数のキーコードのキーの入力状態を返す
int Keyboard_Get( int KeyCode );

#endif

Re: キーボードモジュールの作成について

Posted: 2011年9月17日(土) 06:26
by h2so5
コードを投稿するときはコードタグで囲ってください。
括弧も位置などに統一性がなく見にくいので修正しました。

本題ですが、ループ条件内にDxlib_End()があるのがおかしいです(13行目)

コード:

//メイン関数

#include "DxLib.h"
#include "タイトル.h"
#include "キーボードモジュール.h"

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){

	ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen( DX_SCREEN_BACK ); //ウィンドウモード変更と初期化と裏画面設定

	Taitoru_Initialize();
	
	while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 && DxLib_End()==0){

		Keyboard_Update();
		Taitoru_Calc();
		Taitoru_Graph();
		
	}
	
	DxLib_End(); // DXライブラリ終了処理
	
	return 0;
} 

//※タイトル画面を表示するモジュール

#include "DxLib.h"
#include "タイトル.h"
#include "キーボードモジュール.h"

static int time;
static int d;
static int x ;
static int a[3];

static int toukado;
static int toukado2;
static int toukado3;

static int houkoukey;
static int Quit;
static int GameStart;
static int kontenyu;
static int taitoru;
static int rogo;
static int quit;
static int sitasen;

int r;

void Taitoru_Initialize(){
	toukado = 0;
	toukado2 = 0;
	toukado3 = 0 ;
	x = 65;
	a[0] = 220;
	a[1] = 290;
	a[2] = 360;

	time = 0 ;
	d = 0;

	houkoukey = LoadGraph("画像/方向キー右.png");
	Quit = LoadGraph("画像/ゲーム終了.png");
	GameStart = LoadGraph("画像/ゲーム開始ロゴ.png");
	kontenyu=LoadGraph("画像/コンテニュー.png");
	taitoru = LoadGraph("画像/タイトル.png");
	rogo = LoadGraph("画像/ろご.png");
	quit = LoadGraph("画像/ゲーム終了.png");
	sitasen = LoadGraph("画像/下線.png");
}

void Taitoru_Calc(){

	//演算フェイズ
	if(Keyboard_Get(KEY_INPUT_UP) ==	1){
		d = (d + 2 ) %3 ;
		x = 120;
		toukado = 255;
		toukado2 = 255;
		toukado3 = 255;
	}
	
	if(Keyboard_Get(KEY_INPUT_DOWN) ==	1){
		d = (d + 1) % 3 ;
		x = 120;	
		toukado = 255;
		toukado2 = 255;
		toukado3 = 255;
	}

	if(Keyboard_Get(KEY_INPUT_RETURN) == 1){
		if(a[2]=360){
			DxLib_End();
		}
	}

	if(x<=120){x++;}//ロゴの移動
	if(toukado<=255){toukado = toukado + 3;}//ロゴを徐々に出現
	if(time>=120){toukado2=toukado2+3;}
	if(time>=150){toukado3=toukado3+3;}

}

void Taitoru_Graph(){
	//描画フェイズ

	DrawGraph(0,0,taitoru,TRUE);
	SetDrawBlendMode(DX_BLENDMODE_ALPHA, toukado);	
	DrawGraph(x,85,rogo,TRUE);
	SetDrawBlendMode(DX_BLENDMODE_ALPHA, toukado2);

	DrawGraph(300,200,GameStart,TRUE);
	DrawGraph(300,255,sitasen,TRUE);

	DrawGraph(357,270,kontenyu,TRUE);
	DrawGraph(300,325,sitasen,TRUE);

	DrawGraph(470,340,quit,TRUE);
	DrawGraph(300,395,sitasen,TRUE);

	DrawGraph(263,a[d],houkoukey,TRUE);

	SetDrawBlendMode(DX_BLENDMODE_NOBLEND, 0);

	time++;
}



//上のヘッダファイル


#ifndef DEF_TAITORU_H //二重include防止

#define DEF_TAITORU_H

// 初期化をする
void Taitoru_Initialize();

// 動きを計算する
void Taitoru_Calc();

// 描画する
void Taitoru_Graph();


#endif 




//※キーボードモジュール


#include "DxLib.h"
#include "タイトル.h"


static int m_Key[256]; // キーの入力状態格納用変数

// キーの入力状態更新
void Keyboard_Update(){

	char tmpKey[256];
	// 現在のキーの入力状態を格納する
	GetHitKeyStateAll( tmpKey ); // 全てのキーの入力状態を得る
	
	for( int i=0; i<256; i++ ){ 
		if( tmpKey[i] != 0 ){ // i番のキーコードに対応するキーが押されていたら
			m_Key[i]++; // 加算
		} else { // 押されていなければ
			m_Key[i] = 0; // 0にする
		}
	}
	
}

// KeyCodeのキーの入力状態を取得する
int Keyboard_Get( int KeyCode ){
	return m_Key[ KeyCode ]; // KeyCodeの入力状態を返す
} 

ヘッダファイル
#ifndef DEF_KEYBOARD_H //二重include防止

#define DEF_KEYBOARD_H

// キーの入力状態を更新する
void Keyboard_Update();

// 引数のキーコードのキーの入力状態を返す
int Keyboard_Get( int KeyCode );

#endif

Re: キーボードモジュールの作成について

Posted: 2011年9月17日(土) 18:33
by ふぅ
解決しました!
ありがとうございました。
Coadで囲むのに気が付かなかったので、次から気を付けるようにいたします。

すいませんでした。