简体   繁体   中英

Why do SDL2 enumerations have high unexpected values?

I came across difficulties in debugging a SDL2 program: when I printed the values of enumerations such as SDL_EventType or SDL_Keycode , the values displayed were incredibly high and different from each other. For example, the event type SDL_KEYDOWN corresponded to 768 - and there aren't 769 elements in the enumeration SDL_EventType !

I made a little test to see if this strange behaviour was due to my program, but the behaviour was still there:

#include <stdio.h>
#include "SDL.h"

int main(int argc, char *argv[])
{
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Event event;
  SDL_Window *wind = SDL_CreateWindow("ee", 700, 100, 300, 300, 0);
  while (SDL_PollEvent == 1)
    SDL_PollEvent(&event);
  while (1)
  {
    SDL_PollEvent(&event);
    if (event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)
      break;
    fprintf(stderr, "type = %d\n", event.type);
    event.type = 0;
    SDL_Delay(15);
  }
  printf("%d\n", SDL_GetTicks());
  SDL_Quit();
    return 0;
}

Printing SDLK_DOWN , SDL_MOUSEMOTION or such keywords outputs the same result than the previous program, which is coherent. But why are these values not coherent with the enumeration type?

If you look at SDL_events.h then you'll see something like:

/* Window events */
SDL_WINDOWEVENT    = 0x200, /**< Window state change */
SDL_SYSWMEVENT,             /**< System specific event */

/* Keyboard events */
SDL_KEYDOWN        = 0x300, /**< Key pressed */
SDL_KEYUP,                  /**< Key released */

So they are just defined with such spread values. The rationale behind this is to allow adding new values without breaking backwards compatibility while keeping certain enumerator values grouped.

You seem to be misusing events. In your case:

if (event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)

However, event is basically a huge union and fields inside event.key are only valid when event.type is one of key-related events.

Maybe here lies your problem? To me that sounds like you misinterpreting what SDL_PollEvent returns.

EDIT: The person before me seems to be more on topic, however my point still stands. Admitedly, not exactly what you're asking about but you might still watch out for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM