/* Program to demonstrate how to open a window size and position it properly
 * New generation version using SDL rather than GLUT by Alfie Parthum */

#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "SDL.h"

#define SCREEN_BPP	16			/* bitdepth of colors */
#define TRUE 1
#define FALSE 0

int width=640, height=480;		/* Global Variables for Length and Width */
SDL_Surface *surface;			/* The almighty SDL surface upon which 
								 * all is written to */

void Quit(int ret) {
	SDL_Quit();
	exit(ret);
}

void ToggleFullScreen(void) {
	if(SDL_WM_ToggleFullScreen(surface) == 0) {
		fprintf(stderr, "Failed to Toggle Fullscreen mode: %s\n",
				SDL_GetError());
	}
}

/* Make everything right after a window resize */
int resizeWindow(int width, int height) {
	GLfloat ratio;				/* aspect ratio */
	if (height == 0) height = 1;	/* Protect against a divide by zero */

	ratio = (GLfloat)width / (GLfloat)height;
	glViewport(0, 0, (GLsizei)width, (GLsizei)height);
		/* Set up viewport */

	glMatrixMode(GL_PROJECTION);
		/* Change to projection matrix and set up viewing matrix*/
	glLoadIdentity();

	gluPerspective(45.0f, ratio, 0.1f, 100.0f);	/* Set perspective */

	glMatrixMode(GL_MODELVIEW);	/* Change model view (as opposed to proj)*/
	glLoadIdentity();			/* Reset the view */

	myinit();

	return(TRUE);
}

/* Declare initial window size, position, and display mode */
int main(int argc, char* argv[]) {
	int videoFlags;				/* Flags to pass to SDL_SetVideoMode */
	int done = FALSE;
	SDL_Event event;
	const SDL_VideoInfo *videoInfo;	/* Holds display info */

	printf("Creating window %d pixels wide and %d pixels high", width, height);
	printf("\nHit the F1 key to toggle between fullscreen and windowed mode");
	printf("\nPress ESC to quit.\n");

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {	/*initialize SDL */
		fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
		Quit(1);
	}
	
	videoInfo = SDL_GetVideoInfo();	/* Get some video info */
	if (!videoInfo) {
		fprintf(stderr, "Failed getting video info: %s\n", SDL_GetError());
		Quit(1);
	}
	videoFlags = SDL_OPENGL;	/* Enable OpenGL in SDL */
	videoFlags |= SDL_GL_DOUBLEBUFFER;	/* We want double buffering */
	videoFlags |= SDL_HWPALETTE;	/* Store the palette in hardware */
	videoFlags |= SDL_RESIZABLE;	/* Enable window resizing */
	if (videoInfo->hw_available) videoFlags |= SDL_HWSURFACE;
	else videoFlags |= SDL_SWSURFACE;
		/* Store surfaces in hardware if we can */
	if (videoInfo->blit_hw) videoFlags |= SDL_HWACCEL;
		/* If the hardware can do it, we want it to do our blitting */

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
		/* Set up OpenGL double buffering */
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, SCREEN_BPP);
		/* Size of depth buffer */
	SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
		/* Don't need to use stencil buffer */
	SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0);
		/* This and the next three lines set teh bpp for the accumulation
		 * buffer to 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);

	surface = SDL_SetVideoMode(width, height, SCREEN_BPP, videoFlags);
	if (!surface) {
		fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError());
		Quit(1);
	}

	myinit();					/* Call my initialization routine */

	display();					/* Call my display routine */
	
	while (!done) { /* main event loop */
		while (SDL_PollEvent(&event)) { /* look for events */
			switch (event.type) {	/* what kind of event is it? */
				case SDL_QUIT:	/* If user wishes to quit */
					done = TRUE;
					break;
				case SDL_KEYDOWN:	/* If a key has been pressed */
					KeyPressed(&event.key.keysym);	/* Handle it */
					break;
				case SDL_MOUSEBUTTONDOWN:
				case SDL_MOUSEMOTION:
					mouse(event);
					break;
				case SDL_VIDEORESIZE:
					surface = SDL_SetVideoMode(event.resize.w, event.resize.h,
							SCREEN_BPP, videoFlags);
					if (!surface) {
						fprintf(stderr, 
								"Could not get a surface after resize:%s\n",
								SDL_GetError());
						Quit(1);
					}
					resizeWindow(event.resize.w, event.resize.h);
					break;
				default:
					break;
			}
		}
		display();
	}
	Quit(0);
	return 0;					/* Shouldn't get here */
}

/*  Adapted for use in Supercomputer Applications 
 *  at TJHSST from the OpenGL Tutor by Mahesh Kumar
 *  Parts of this program might have been adopted from or inspired by 
 *  sample programs in 
 *  "OpenGL Programming Guide", 
 *  Copyright (c) 1993-1997, Silicon Graphics, Inc.
 *  "Interactive Computer Graphics"  
 *  Copyright (c) 1997, Addison-Wesley,
 *  and/or those written by Dr. Sumanta Guha guha@cs.uwm.edu
 *  and/or those written by Dr. Ichiro Suzuki suzuki@cs.uwm.edu */


