/* -- Include the precompiled libraries -- */
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
#endif

#include <cstdlib>
#include "SDL.h"
#include "SDL_opengl.h"

int main(int argc, char **argv)
{
  if( SDL_Init(SDL_INIT_VIDEO) <0 )
  {
      printf("Unable to init SDL: %s\n", SDL_GetError());      
      return 1;
  }    
    
  atexit(SDL_Quit);

  //set up the video mode
  int videoflags = SDL_OPENGL;

  //videoflags |= SDL_FULLSCREEN; //remove this line if you do not want full screen

  //check for hardware support
  const SDL_VideoInfo * VideoInfo = SDL_GetVideoInfo();
  if(VideoInfo -> hw_available) 
  {
    videoflags |= SDL_HWSURFACE;
  }else{
    videoflags |= SDL_SWSURFACE;
  }
  if(VideoInfo -> blit_hw)
  {
    videoflags |= SDL_HWACCEL;
  }   

  //double buffer, no stencil, no accumulation buffer
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
  SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0);
  SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0);
  SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0);
  SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
  SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);

  SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, videoflags); //you can change the resolution here, this is 640x480
  if( screen == NULL )
  {
    printf("Unable to init SDL video mode: %s\n", SDL_GetError());   
    return 1;
  }

  glClear(GL_COLOR_BUFFER_BIT);

  glMatrixMode(GL_MODELVIEW);

  while(1) {

    SDL_PumpEvents();
    Uint8 *curr_keyboard = SDL_GetKeyState(0);
    if(curr_keyboard[SDLK_ESCAPE]) {
      exit(0);
    }
    
    glBegin(GL_TRIANGLES);
      glVertex3f(1,1,1);
      glVertex3f(0,1,1);
      glVertex3f(1,0,1);
    glEnd();

    SDL_GL_SwapBuffers();
  }

	return 0;
}