如何在OCAML中使用TSDL检查按下的键?

时间:2019-01-24 11:11:34

标签: ocaml sdl

这是我想在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)

错误指出.()无法应用于大数组...

1 个答案:

答案 0 :(得分:0)

您需要使用.{}来索引Bigarray:

key_state.{Sdl.Scancode.up}