interkosmos/fortran-sdl2

Wrong result with SDLK_SCANCODE_MASK

Closed this issue · 8 comments

In a C program this statement

printf("%d %d\n",SDLK_SCANCODE_MASK,SDLK_LEFT);

prints

1073741824 1073741904

In Fortran, using SDL2.f90:

print *, SDLK_SCANCODE_MASK,SDLK_LEFT, ' --- pressing LEFT: ',event%key%key_sym%sym

prints

0          81  --- pressing LEFT:   1073741903

This causes


select case (event%key%key_sym%sym)

  case (SDLK_LEFT)

to fail: in Fortran, not only the above SDLK... constants values are wrong, but also what reports event%key%key_sym%sym (1073741903 instead of 1073741904) for LEFT arrow pressed is wrong..

BTW, maybe we need also SDL_GetMouseState...

In the C headers for SDL2, I find:

SDL_keycode.h:#define SDLK_SCANCODE_MASK (1<<30)

which corresponds to use a `left shift'.

In Fortran the left shift operator is ishft(i,shift) with shift > 0; shift < 0 is for the right shift operator.

See: ISHFT

So you should use

integer(kind=c_int), parameter :: SDLK_SCANCODE_MASK      = ishft(1, 30)

But this does not fix completely the issue. Now one gets:

print *, SDLK_SCANCODE_MASK,SDLK_LEFT, ' --- pressing LEFT: ',event%key%key_sym%sym

[...]

1073741824  1073741905  --- pressing LEFT:   1073741903

SDLK_SCANCODE_MASK is the same both for C and Fortran but not `SDLK_LEFT'... and 'event%key%key_sym%sym'..

Maybe we need to go to sleep..... (just the 03:17!)

Thank you for pointing this out. The reason why SDLK_LEFT does not match your C example is that the SDL_SCANCODE_* parameters are all increased by one, to be able to check the array returned by sdl_get_keyboard_state(). Fortran arrays start with index 1, whereas C arrays start with 0. For compatibility, I will decrease the SDL_SCANCODE_* by one to the original values, but this will make the use of sdl_get_keyboard_state() a little ugly.

@interkosmos wrote:

The reason why SDLK_LEFT does not match your C example..

No, you can leave things as now then if all is coherent. Thanks.

....Oops, I see you have already done the change..

How it works sdl_get_keyboard_state() now? OK this seems to work:

integer(C_UINT8_T), pointer :: keys(:) => null()
[...]
r%keys(0:) => sdl_get_keyboard_state()

Oops.. maybe we need SetWindowIcon and an example how to add an icon to our apps...

SDL_SetWindowIcon() has been added with commit 12a0879.

@angelog0

Oops.. maybe we need SetWindowIcon and an example how to add an icon to our apps...

Just for the record,

I have found this tutorial which would be interesting to translate in Fortran...

Maybe we need the SDL_WINDOWEVENT_XXXXX (SHOWN, HIDDEN, etc,) constants...

Enum has been added with commit ea9180c.