如何在 OCAML 中使用 TSDL 检查按下的键?
how do I check the pressed down key using TSDL in OCAML?
这是我想用 OCAML 编写的一段代码,摘自 C++ 教程:
// Set texture based on current keystate
const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
if ( currentKeyStates[ SDL_SCANCODE_UP ] ) {
currentTexture = &gUpTexture;
} else if( currentKeyStates[ SDL_SCANCODE_DOWN ] ) {
currentTexture = &gDownTexture;
} else if( currentKeyStates[ SDL_SCANCODE_LEFT ] ) {
currentTexture = &gLeftTexture;
} else if( currentKeyStates[ SDL_SCANCODE_RIGHT ] ) {
currentTexture = &gRightTexture;
} else {
currentTexture = &gPressTexture;
}
此 C 代码使用 SDL 库。在 OCAML 中,我使用 TSDL 库,它是 SDL 库的精简绑定。
这是SDL_GetKeyBoardState
的签名
val get_keyboard_state : unit -> (int, Bigarray.int8_unsigned_elt) bigarray
我不太清楚该怎么做
currentKeyStates[SDL_SCANCODE_UP]
在 OCAML 中。
请帮忙?谢谢!
这是我迄今为止尝试过的方法:
let key_state = Sdl.get_keyboard_state () in
key_state.(Sdl.Scancode.up)
错误指出 .()
不能应用于双数组...
您需要使用 .{}
来索引 Bigarrays:
key_state.{Sdl.Scancode.up}
这是我想用 OCAML 编写的一段代码,摘自 C++ 教程:
// Set texture based on current keystate
const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
if ( currentKeyStates[ SDL_SCANCODE_UP ] ) {
currentTexture = &gUpTexture;
} else if( currentKeyStates[ SDL_SCANCODE_DOWN ] ) {
currentTexture = &gDownTexture;
} else if( currentKeyStates[ SDL_SCANCODE_LEFT ] ) {
currentTexture = &gLeftTexture;
} else if( currentKeyStates[ SDL_SCANCODE_RIGHT ] ) {
currentTexture = &gRightTexture;
} else {
currentTexture = &gPressTexture;
}
此 C 代码使用 SDL 库。在 OCAML 中,我使用 TSDL 库,它是 SDL 库的精简绑定。
这是SDL_GetKeyBoardState
的签名val get_keyboard_state : unit -> (int, Bigarray.int8_unsigned_elt) bigarray
我不太清楚该怎么做
currentKeyStates[SDL_SCANCODE_UP]
在 OCAML 中。
请帮忙?谢谢!
这是我迄今为止尝试过的方法:
let key_state = Sdl.get_keyboard_state () in
key_state.(Sdl.Scancode.up)
错误指出 .()
不能应用于双数组...
您需要使用 .{}
来索引 Bigarrays:
key_state.{Sdl.Scancode.up}