
#include <SDL/SDL.h>
#include <cairo/cairo.h>

#include <math.h>

int fps = 30;
int w = 320;
int h = 240;
float scale = 16.0;

int main(int argc, char **argv)
{
  SDL_Surface *sdl_surface;
  cairo_surface_t *surface;
  cairo_t *cr;

  SDL_Event event;
  Uint8 *keystate;
  Uint32 next_frame, now;

  float aspect = 1.0 * w / h;

  int running, hdtvtoggle;

  int i, j;

  SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK);
  SDL_ShowCursor(0);
  SDL_SetVideoMode(w, h, 32, 0);
  sdl_surface = SDL_GetVideoSurface();
  //joystick = SDL_JoystickOpen(0);

  /* Initialize Canvas */
  surface = cairo_image_surface_create_for_data(
              sdl_surface->pixels,
              CAIRO_FORMAT_RGB24,
              sdl_surface->w,
              sdl_surface->h,
              sdl_surface->pitch
            );
  cr = cairo_create(surface);
  cairo_surface_destroy(surface);

  hdtvtoggle = 0;
  
  /*
  cairo_translate(cr, w / 2.0, h / 2.0);
  cairo_scale(cr, 1, -1);
  cairo_scale(cr, h / scale, h / scale);
  cairo_rectangle(cr, -scale * aspect / 2.0, -scale / 2.0, scale * aspect, scale);
  cairo_clip(cr);
  */
  
  /* Initialize Delay */
  next_frame = 1024.0 / fps;

  /* Game Logic */
  running = 1;

  SDL_LockSurface(sdl_surface);

  while (running)
  {

    /* Render Frame */
    cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
    cairo_paint(cr);
    cairo_set_operator(cr, CAIRO_OPERATOR_OVER);

    // Object Translation

    cairo_move_to(cr, 0, 0);
    cairo_line_to(cr, 320, 0);
    cairo_line_to(cr, 320, 200);
    cairo_close_path(cr);

    cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
    cairo_fill(cr);

    /* Update Display */
    SDL_UnlockSurface(sdl_surface);
    SDL_Flip(sdl_surface);
    SDL_LockSurface(sdl_surface);

    /* Delay */
    now = SDL_GetTicks();

    if (now < next_frame)
    {
      SDL_Delay( next_frame - SDL_GetTicks() );
    }

    next_frame = next_frame + 1024.0 / fps;

    /* Game Logic */
    SDL_PumpEvents();

    while ( SDL_PollEvent(&event) )
    {
      switch (event.type)
      {
      case SDL_KEYDOWN:
        switch (event.key.keysym.sym)
        {
        case SDLK_h:
          cairo_identity_matrix (cr);
          cairo_translate(cr, w / 2.0, h / 2.0);

          if (hdtvtoggle)
          {
            hdtvtoggle = 0;
            cairo_scale(cr, 1, -1);
          }

          else
          {
            hdtvtoggle = 1;
            cairo_scale(cr, 9 / 10.0, -1);
          }

          cairo_scale(cr, h / scale, h / scale);
          cairo_reset_clip(cr);
          cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
          cairo_paint(cr);
          cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
          cairo_rectangle(cr,
                          -scale * aspect / 2.0, -scale / 2.0,
                          scale * aspect, scale);
          cairo_clip(cr);
          break;
        }

        break;
      }
    }

    keystate = SDL_GetKeyState(NULL);

    if (keystate[SDLK_q])
    {
      running = 0;
    }

    if (keystate[SDLK_f])
    {
      SDL_WM_ToggleFullScreen(sdl_surface);
    }

    if (keystate[SDLK_LEFT] || i)
    {
    }
    if (keystate[SDLK_RIGHT] || i)
    {
    }
    if (keystate[SDLK_UP] || i)
    {
    }

  }

  SDL_UnlockSurface(sdl_surface);

  cairo_destroy(cr);

  SDL_Quit();

  return 0;
}
