From 1404fb09b583e99aa18ef937758149acbaecfa75 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 20 Apr 2015 07:34:06 +0200 Subject: [PATCH] Input: Detectar clicks. --- GameLib/Draw.c | 63 ++++++++++++++++++++++++++++++++++++++++------- GameLib/GameLib.c | 18 ++++++++++++++ GameLib/GameLib.h | 8 ++++++ GameLib/Input.c | 52 +++++++++++++++++++++++++++++++++++++- GameLib/Input.h | 25 +++++++++++++++++++ 5 files changed, 156 insertions(+), 10 deletions(-) diff --git a/GameLib/Draw.c b/GameLib/Draw.c index 0eca0b3..4394244 100644 --- a/GameLib/Draw.c +++ b/GameLib/Draw.c @@ -475,16 +475,50 @@ int Draw_LoopIteration(){ Input_SetKey(InputKey_Exit,1); } } - if(event.type==SDL_MOUSEBUTTONDOWN || - event.type==SDL_FINGERDOWN || - event.type==SDL_TOUCHBUTTONDOWN) - { + if(event.type==SDL_MOUSEMOTION){ + Input_SetPointerPosition( + event.motion.x/(float)_width, + event.motion.y/(float)_height); + } + if(event.type==SDL_MOUSEBUTTONDOWN){ + Input_SetPointerPosition( + event.button.x/(float)_width, + event.button.y/(float)_height); Input_SetPointerDown(1); } - if(event.type==SDL_MOUSEBUTTONUP || - event.type==SDL_FINGERUP || - event.type==SDL_TOUCHBUTTONUP) - { + if(event.type==SDL_FINGERMOTION){ + Input_SetPointerPosition( + event.tfinger.x, + event.tfinger.y); + } + if(event.type==SDL_FINGERDOWN){ + Input_SetPointerPosition( + event.tfinger.x, + event.tfinger.y); + Input_SetPointerDown(1); + } + if(event.type==SDL_TOUCHBUTTONDOWN){ + Input_SetPointerPosition( + event.tfinger.x, + event.tfinger.y); + Input_SetPointerDown(1); + } + if(event.type==SDL_MOUSEBUTTONUP){ + Input_SetPointerPosition( + event.button.x/(float)_width, + event.button.y/(float)_height); + Input_SetPointerDown(0); + } + if(event.type==SDL_FINGERUP){ + Input_SetPointerPosition( + event.tfinger.x, + event.tfinger.y); + Input_SetPointerDown(0); + } + if(event.type==SDL_TOUCHBUTTONUP){ + Input_SetPointerPosition( + event.tfinger.x, + event.tfinger.y); Input_SetPointerDown(0); } } @@ -504,10 +538,21 @@ int Draw_LoopIteration(){ } } } + if(event.type==SDL_MOUSEMOTION){ + Input_SetPointerPosition( + event.motion.x/(float)_width, + event.motion.y/(float)_height); + } if(event.type==SDL_MOUSEBUTTONDOWN){ + Input_SetPointerPosition( + event.button.x/(float)_width, + event.button.y/(float)_height); Input_SetPointerDown(1); } if(event.type==SDL_MOUSEBUTTONUP){ + Input_SetPointerPosition( + event.button.x/(float)_width, + event.button.y/(float)_height); Input_SetPointerDown(0); } } @@ -532,7 +577,7 @@ int Draw_LoopIteration(){ Input_Frame(); _proc_func(_data); _accTime-=proc_t_frame; - Input_SetKey(InputKey_Exit,0); + Input_PostFrame(); } } diff --git a/GameLib/GameLib.c b/GameLib/GameLib.c index 2a5e4a3..2687e39 100644 --- a/GameLib/GameLib.c +++ b/GameLib/GameLib.c @@ -624,3 +624,21 @@ void GameLib_EntitySetLight(Entity e,float r,float g,float b,float rad){ } } + +///////////////////////////// +// GameLib_ConvertScreenPositionToGamePosition +// +// +void GameLib_ConvertScreenPositionToGamePosition( + vec2 screenPos, vec2 gamePos) +{ + float f; + int game_pos[2]; + + game_pos[0]=_game_pos0[0]+f*(_game_pos1[0]-_game_pos0[0]); + game_pos[1]=_game_pos0[1]+f*(_game_pos1[1]-_game_pos0[1]); + + gamePos[0]=(screenPos[0]*_game_size[0])+game_pos[0]; + gamePos[1]=(screenPos[1]*_game_size[1])+game_pos[1]; +} + diff --git a/GameLib/GameLib.h b/GameLib/GameLib.h index 9d92f44..9fe483a 100644 --- a/GameLib/GameLib.h +++ b/GameLib/GameLib.h @@ -115,4 +115,12 @@ void GameLib_PlaySound(AudioSnd snd,int x,int y); void GameLib_EntitySetLight(Entity e,float r,float g,float b,float rad); +///////////////////////////// +// GameLib_ConvertScreenPositionToGamePosition +// +// +void GameLib_ConvertScreenPositionToGamePosition( + vec2 screenPos, vec2 gamePos); + + #endif diff --git a/GameLib/Input.c b/GameLib/Input.c index 7af8190..d9fc136 100644 --- a/GameLib/Input.c +++ b/GameLib/Input.c @@ -13,7 +13,14 @@ // Globals InputKeyStatus _keys[InputKey_Max]; + int _pointerDown=0; +float _pointerX=0; +float _pointerY=0; + +int _clicked=0; +float _clickedPositionX=0; +float _clickedPositionY=0; ///////////////////////////// @@ -56,6 +63,16 @@ void Input_Frame(){ } +///////////////////////////// +// Input_PostFrame +// +// Notify a frame update to the input subsystem. +void Input_PostFrame(){ + Input_SetKey(InputKey_Exit,0); + _clicked=0; +} + + ///////////////////////////// // Input_SetKey // @@ -82,14 +99,48 @@ InputKeyStatus Input_GetKey(InputKey key){ } +///////////////////////////// +// Input_SetPointerPosition +// +void Input_SetPointerPosition(float x, float y){ + _pointerX=x; + _pointerY=y; +} + + ///////////////////////////// // Input_SetPointerDown // void Input_SetPointerDown(int pointerDown){ + if(pointerDown==0 && _pointerDown==1){ + _clicked=1; + _clickedPositionX=_pointerX; + _clickedPositionY=_pointerY; + } _pointerDown=pointerDown; } +///////////////////////////// +// Input_GetPointerPosition +// +int Input_GetPointerPosition(vec2 pointer){ + pointer[0]=_pointerX; + pointer[1]=_pointerY; + return _pointerDown; +} + + +///////////////////////////// +// Input_GetClickedPosition +// +int Input_GetClickedPosition(vec2 clickPosition){ + clickPosition[0]=_clickedPositionX; + clickPosition[1]=_clickedPositionY; + return _clicked; +} + + ///////////////////////////// // Input_AnyKey // @@ -116,7 +167,6 @@ int Input_GetDir(vec2 dir){ float dlen; extern int _width,_height; - // Get mouse state buttons=SDL_GetMouseState(&mx,&my); if(buttons){ diff --git a/GameLib/Input.h b/GameLib/Input.h index ac9240b..e2a32a3 100644 --- a/GameLib/Input.h +++ b/GameLib/Input.h @@ -20,6 +20,13 @@ int Input_Init(); void Input_Frame(); +///////////////////////////// +// Input_PostFrame +// +// Notify a frame update end to the input subsystem. +void Input_PostFrame(); + + //////////////////////////////////////////////// // InputKey // ////////////// @@ -66,12 +73,30 @@ typedef enum { InputKeyStatus Input_GetKey(InputKey key); +///////////////////////////// +// Input_SetPointerPosition +// +void Input_SetPointerPosition(float x, float y); + + ///////////////////////////// // Input_SetPointerDown // void Input_SetPointerDown(int pointerDown); +///////////////////////////// +// Input_GetPointerPosition +// +int Input_GetPointerPosition(vec2 pointer); + + +///////////////////////////// +// Input_GetClickedPosition +// +int Input_GetClickedPosition(vec2 clickPosition); + + ///////////////////////////// // Input_AnyKey //