遊戲有很多輸入, 如鍵盤, 滑鼠, 手柄等. sdl庫將這些處理變得非常簡單, 我們這裡將這幾種輸入統一到一起. 這裡不講太多, 因為目前我們對輸入的處理就是檢測輸入裝置的狀態, 來更新遊戲物件的狀態.
看下**就行了, 然後對於遊戲中的物件怎麼使用輸入裝置的狀態, 自己發揮
// inputhandler.h
#ifndef inputhandler_h_included
#define inputhandler_h_included
#include
#include
"sdl.h"
#include
"vector2d.h"
enum mouse_buttons
;class
inputhandler
intxvalue
(int joy,
int stick)
;int
yvalue
(int joy,
int stick)
;bool
getbuttonstate
(int joy,
int buttonnumber)
bool
getmousebuttonstate
(int buttonnumber)
vector2d*
getmouseposition()
bool
iskeydown
(sdl_scancode key)
;void
update()
;void
clean()
;private
:inputhandler()
;~inputhandler()
// handle keyboard events
void
onkeydown()
;void
onkeyup()
;// handle mouse events
void
onmousemove
(sdl_event& event)
;void
onmousebuttondown
(sdl_event& event)
;void
onmousebuttonup
(sdl_event& event)
;// handle joysticks events
void
onjoystickaxismove
(sdl_event& event)
;void
onjoystickbuttondown
(sdl_event& event)
;void
onjoystickbuttonup
(sdl_event& event)
;private
:static inputhandler* s_pinstance;
vector2d* m_mouseposition;
const uint8* m_keystate;
std::vector> m_joysticks;
std::vector, vector2d*
>
> m_joystickvalues;
std::vectorbool
>
> m_buttonstates;
std::vector<
bool
> m_mousebuttonstates;
bool m_bjoysticksinitialized;
const
int m_joystickdeadzone =
10000;}
;typedef inputhandler theinputhandler;
#endif
// inputhandler_h_included
// inputhandler.cpp
#include
#include
"inputhandler.h"
#include
"game.h"
inputhandler* inputhandler::s_pinstance =
nullptr
;inputhandler::
inputhandler()
m_mouseposition =
newvector2d(0
,0);
}inputhandler* inputhandler::
instance()
return s_pinstance;
}void inputhandler::
initializejoysticks()
if(sdl_numjoysticks()
>0)
m_buttonstates.
push_back
(tempbuttons);}
else
}sdl_joystickeventstate
(sdl_enable)
; m_bjoysticksinitialized =
true
; std::cout <<
"initialised "
<< m_joysticks.
size()
<<
" joystick(s)\n";}
else
}void inputhandler::
clean()
}delete m_mouseposition;
delete s_pinstance;
}void inputhandler::
update()
}}int inputhandler::
xvalue
(int joy,
int stick)
else
if(stick ==2)
}return0;
}int inputhandler::
yvalue
(int joy,
int stick)
else
if(stick ==2)
}return0;
}bool inputhandler::
iskeydown
(sdl_scancode key)
void inputhandler::
onkeydown()
void inputhandler::
onkeyup()
void inputhandler::
onjoystickaxismove
(sdl_event& event)
else
if(event.jaxis.value <
-m_joystickdeadzone)
else}if
(event.jaxis.axis ==1)
else
if(event.jaxis.value <
-m_joystickdeadzone)
else
}// right stick move left or right
if(event.jaxis.axis ==3)
else
if(event.jaxis.value <
-m_joystickdeadzone)
else
}// right stick move up or down
if(event.jaxis.axis ==4)
else
if(event.jaxis.value <
-m_joystickdeadzone)
else}}
void inputhandler::
onjoystickbuttondown
(sdl_event& event)
void inputhandler::
onjoystickbuttonup
(sdl_event& event)
void inputhandler::
onmousebuttondown
(sdl_event& event)
if(event.button.button == sdl_button_middle)
if(event.button.button == sdl_button_right)
}void inputhandler::
onmousebuttonup
(sdl_event& event)
if(event.button.button == sdl_button_middle)
if(event.button.button == sdl_button_right)
}void inputhandler::
onmousemove
(sdl_event& event)
(9)處理型別
型別別名 是乙個名字,它是某種型別的同義詞。c 有兩種方法用於定義型別別名 一.使用關鍵字typedef typedef int iname iname a 1 可用iname定義變數 a是乙個整型變數二.使用別名宣告 using 來定義型別的別名 using iname int iname a 1...
React native 5 處理文字輸入
在上面的例子裡,我們把text儲存到state中,因為它會隨著時間變化。文字輸入方面還有很多其他的東西要處理。比如你可能想要在使用者輸入的時候進行驗證,在react的表單元件中的受限元件一節中有一些詳細的示例 注意react中的onchange對應的是rn中的onchangetext 此外你還需要看...
OSG學習筆記9 處理鍵盤事件
處理鍵盤事件 include stdafx.h include include include includeclass keyboardhandler public osgga guieventhandler 人機互動事件處理器 else else if ea.getkey 2 else if e...