キーについて
Posted: 2009年11月02日(月) 10:13
項目を選択してZを押したらその項目の色が変わるようにしたいのですがどうすればいいのでしょうか。
#include "test.h"
int Key[256];
void SetColor();
void Exit();
void Select();
void Change();
int a[ 6 ] ;
int b[ 6 ] ;
int c[ 6 ] ;
int y ;
int GetHitKeyStateAll_2(int KeyStateBuf[/url]){
char GetHitKeyStateAll_Key[256];
GetHitKeyStateAll( GetHitKeyStateAll_Key );
for(int i=0;i<256;i++){
if(GetHitKeyStateAll_Key==1) KeyStateBuf++;
else KeyStateBuf=0;
}
return 0;
}
enum Option{
OPTION_EXIT,
OPTION_SELECT,
};
Option state = OPTION_SELECT;
enum ColorId{
COLOR_RED,
COLOR_BLUE,
COLOR_YELLOW,
COLOR_GREEN,
COLOR_CYAN,
COLOR_BLACK,
COLOR_WHITE,
COLOR_MAX
};
int Color[COLOR_MAX];
typedef struct{
const int R;
const int G;
const int B;
}COLOR;
COLOR ColorList[COLOR_MAX] = {
{ 255, 0, 0},
{ 0, 0, 255},
{ 255, 255, 0},
{ 0, 255, 0},
{ 100, 255, 255},
{ 0, 0, 0},
{ 255, 255, 255}};
typedef struct{
int Pt_X;
int Pt_Y;
char* str;
ColorId c;
const Option next;
}CHOICES;
CHOICES choices[/url] = { {60 , 60, "・1", COLOR_WHITE, OPTION_SELECT},
{60 , 90, "・2", COLOR_WHITE, OPTION_SELECT},
{60 , 120, "・3", COLOR_WHITE, OPTION_SELECT},
{750 , 560, "EXIT", COLOR_WHITE, OPTION_EXIT}};
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
ChangeWindowMode( TRUE );
SetGraphMode( 800 , 600 , 32 ) ;
if(DxLib_Init() == -1 )
{
return -1;
}
SetDrawScreen( DX_SCREEN_BACK );
SetColor();
typedef void(*Func)();
Func fc[/url] = {Exit, Select};
// BMP画像のメモリへの分割読み込み
LoadDivGraph( "ball.bmp" , 6 , 6 , 1 , 100 , 100 , a ) ;
LoadDivGraph( "ball2.bmp" , 6 , 6 , 1 , 100 , 100 , b ) ;
LoadDivGraph( "ball3.bmp" , 6 , 6 , 1 , 100 , 100 , c ) ;
y = 0 ;
while(ProcessMessage()==0 && ClearDrawScreen()==0 && GetHitKeyStateAll_2(Key)==0 && Key[KEY_INPUT_ESCAPE]==0 ){
// アニメーションパターンナンバーを変更
y ++ ;
if( y == 6 ) y = 0 ;
// 一定時間待つ(『WaitTimer』使用)
WaitTimer( 100 ) ;
fc[state]();
ScreenFlip();
}
DxLib_End();
return 0;
}
void Exit(void)
{
DxLib_End();
}
void Select(void)
{
static int NowPt = 0;
static const int ChoiceNum = sizeof(choices)/sizeof(choices[0]);
DrawBox( 30, 50, 300, 200, Color[COLOR_WHITE], FALSE );// 四角形を描画(左上x座標,左上y座標,右下x座標,右下y座標)
DrawBox( 720, 50, 410, 400, Color[COLOR_WHITE], FALSE );// 四角形を描画(左上x座標,左上y座標,右下x座標,右下y座標)
DrawString( 45 , 50 - 32 , "WEAPON" , Color[COLOR_WHITE] );
DrawString( 430 , 50 - 32 , "SAMPLE" , Color[COLOR_WHITE] );
if(Key[KEY_INPUT_DOWN] == 1 || (Key[KEY_INPUT_DOWN] > 10 && Key[KEY_INPUT_DOWN] % 10 == 0)){
choices[NowPt].c = COLOR_WHITE;
NowPt = (NowPt + 1) % ChoiceNum;
}else if(Key[KEY_INPUT_UP] == 1 || (Key[KEY_INPUT_UP] > 10 && Key[KEY_INPUT_UP] % 10 == 0)){
choices[NowPt].c = COLOR_WHITE;
NowPt = (NowPt + ChoiceNum - 1) % ChoiceNum;
}
for(int i = 0; i < ChoiceNum; i++){
if(i == NowPt)
choices.c = COLOR_WHITE;
DrawString( choices.Pt_X , choices.Pt_Y , choices.str , Color[choices.c]);
}
DrawString( choices[NowPt].Pt_X - 20, choices[NowPt].Pt_Y , ">>" , Color[COLOR_BLUE]);
switch(NowPt)
{
// グラフィックの描画(『DrawGraph』使用)
case 0: DrawGraph( 410 , 50 , a[ y ] , FALSE ) ; break;
case 1: DrawGraph( 410 , 50 , b[ y ] , FALSE ) ; break;
case 2: DrawGraph( 410 , 50 , c[ y ] , FALSE ) ; break;
}
for(int i = 0; i < ChoiceNum; i++){
if(Key[KEY_INPUT_Z]==0){
state = choices[NowPt].next;
choices[NowPt].c = COLOR_RED;
DrawString( choices.Pt_X , choices.Pt_Y , choices[i].str , Color[choices[i].c]);
}
}
}
void SetColor(void)
{
for(int i = 0, count = sizeof(ColorList) / sizeof(ColorList[0]); i < count; i++)
Color[i] = GetColor(ColorList[i].R, ColorList[i].G, ColorList[i].B);
}「選択した項目の色を変える」だけなら、これで充分でしょう。
--------------------------------------------------------------------------------
#include "DxLib.h"
int Key[256];
int GetHitKeyStateAll_2(int KeyStateBuf[/url]){
char GetHitKeyStateAll_Key[256];
GetHitKeyStateAll( GetHitKeyStateAll_Key );
for(int i=0;i<256;i++){
if(GetHitKeyStateAll_Key==1) KeyStateBuf++;
else KeyStateBuf=0;
}
return 0;
}
typedef struct{
int x;
int y;
char* str;
}SELECT;
SELECT Select[/url] = { {100,100,"Menu 1"},
{100,120,"Menu 2"},
{100,140,"Menu 3"}};
void DrawSelect(void){
int white = GetColor( 255, 255, 255);
int Red = GetColor( 255, 0, 0);
static int count = sizeof(Select) / sizeof(Select[0]);
static int NowPt = 0;
if(Key[KEY_INPUT_UP] == 1){
NowPt = (NowPt + count - 1) % count;
}else if(Key[KEY_INPUT_DOWN] == 1){
NowPt = (NowPt + 1) % count;
}
for(int i = 0; i < count; i++){
if(Key[KEY_INPUT_Z] != 0 && i == NowPt ){
DrawString( Select.x, Select.y, Select.str, Red);
}else{
DrawString( Select.x, Select.y, Select.str, white);
}
}
DrawString( Select[NowPt].x - 40, Select[NowPt].y, "⇒", white);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
ChangeWindowMode( TRUE );
if(DxLib_Init() == -1 ) return -1;
SetDrawScreen( DX_SCREEN_BACK );
while(ProcessMessage()==0 && ClearDrawScreen()==0 && GetHitKeyStateAll_2(Key)==0 && Key[KEY_INPUT_ESCAPE]==0 ){
DrawSelect();
ScreenFlip();
}
DxLib_End();
return 0;
}
--------------------------------------------------------------------------------
あと、YSさんが提示したプログラムで上手くいかない原因は、
Zキーを押した時点でstateの値が変更され、Select関数が呼ばれない(選択肢を表示する処理が行われない)
からです。