#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "SDL.h"
#include "SDL_thread.h"
#include "fastevents.h"
#include "SDLUtils.h"

static int doneYet = 0;
static int num = 0;

static int runThread(void *nothing)
{
  int i;
  int val;
  SDL_Event ev;

  ev.type = SDL_USEREVENT;
  ev.user.code = 0;
  ev.user.data1 = 0;
  ev.user.data2 = 0;

  i = 0;
  while (!doneYet)
  {
    ev.user.data1 = (void *)i;
    val = FE_PushEvent(&ev);
    num++;
    //printf("send=%d\n", num);

    i++;
  }

  return 0;
}

int main(int argc, char **argv)
{
  int count = 0;
  double rate = 0.0;
  Uint32 start = 0;
  Uint32 end = 0;
  Uint32 limit = (10 * 1000);
  SDL_Thread *thread = NULL;
  SDL_Event ev;

  if (2 <= argc)
  {
    limit = 1000 * atoi(argv[1]);
  }

  SDL_Init(SDL_INIT_EVENTTHREAD |
           SDL_INIT_VIDEO | 
           SDL_INIT_NOPARACHUTE);
  FE_Init();

  thread = SDL_CreateThread(runThread, NULL);

  start = SDL_GetTicks();
  while ((SDL_GetTicks() < (start + limit)) &&
         FE_WaitEvent(&ev))
  {
    num--;
    //printf("got=%d\n", num);
    //printf("time=%u\n", SDL_GetTicks());
    switch (ev.type)
    {
    case SDL_USEREVENT:
      //printf("count=%d data1=%d\n", count, (int)ev.user.data1);
      count++;
      break;

    case SDL_QUIT:
      printf("got here\n");
      exit(0); // this isn't supposed to happen
      break;

    default:
      printSDLEvent(&ev);
      break;
    }
  }
  doneYet = 1;

  while (FE_PollEvent(&ev)) // drain the que
  {
  }
  SDL_WaitThread(thread, NULL); // wait for it to die

  end = SDL_GetTicks();

  rate = ((double)count) / (((double)(end - start)) / 1000.0);
  printf("events/second=%f\n", rate);

  FE_Quit();
  SDL_Quit();
  exit(0);
}
