/*
*
* This code is (c) by Andrei Krotkov.
* The base fot this code was made through
* the tutorials at nehe.gamedev.net.
*
*/
#pragma comment(lib, "winmm.lib")
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fstream.h>
#include <mmsystem.h>
#include <time.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;
bool active=true;
bool fullscreen=false;
const float piover180 = 0.0174532925f;
const float onesix = .0625f;
int screennum=0;
DWORD ticks=0;
long int frames=0;
int fps=0;
GLuint texture[15];
GLfloat color[]={0,0,0,0};
int rot[9]={-18,-18,-18,-18,-18,-18,-18,-18,-18};
int dat[9]={0,0,0,0,0,0,0,0,0};
int timev=0;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void drawwheel(int num)
{
int x=0;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
for (int dir=0;dir<360;dir+=36)
{
if (x==num)
{
glColor3f(1,0,0);
}
else
{
glColor3f(1,1,1);
}
glTexCoord2f(.0625*x,1-.0625);
glVertex3f(cos(dir*piover180),sin(dir*piover180),.5);
glTexCoord2f(.0625*(x+1),1-.0625);
glVertex3f(cos(dir*piover180),sin(dir*piover180),-.5);
glTexCoord2f(.0625*(x+1),1);
glVertex3f(cos((dir+36)*piover180),sin((dir+36)*piover180),-.5);
glTexCoord2f(.0625*x,1);
glVertex3f(cos((dir+36)*piover180),sin((dir+36)*piover180),.5);
x++;
}
glEnd();
}
void drawcolon()
{
int x=0;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
for (int dir=0;dir<360;dir+=36)
{
if (x==0)
{
glColor3f(1,0,0);
}
else
{
glColor3f(1,1,1);
}
glTexCoord2f(.0625*11,1-.0625);
glVertex3f(cos(dir*piover180),sin(dir*piover180),.5);
glTexCoord2f(.0625*(11+1),1-.0625);
glVertex3f(cos(dir*piover180),sin(dir*piover180),-.5);
glTexCoord2f(.0625*(11+1),1);
glVertex3f(cos((dir+36)*piover180),sin((dir+36)*piover180),-.5);
glTexCoord2f(.0625*11,1);
glVertex3f(cos((dir+36)*piover180),sin((dir+36)*piover180),.5);
x++;
}
glEnd();
}
void drawpmam(bool pm)
{
int x=0;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
for (int dir=0;dir<360;dir+=36)
{
if (x==0)
{
glColor3f(1,0,0);
}
else
{
glColor3f(1,1,1);
}
if (pm)
{
glTexCoord2f(.0625*12,1-.0625);
glVertex3f(cos(dir*piover180),sin(dir*piover180),.5);
glTexCoord2f(.0625*(12+1),1-.0625);
glVertex3f(cos(dir*piover180),sin(dir*piover180),-.5);
glTexCoord2f(.0625*(12+1),1);
glVertex3f(cos((dir+36)*piover180),sin((dir+36)*piover180),-.5);
glTexCoord2f(.0625*12,1);
glVertex3f(cos((dir+36)*piover180),sin((dir+36)*piover180),.5);
}
else
{
glTexCoord2f(.0625*13,1-.0625);
glVertex3f(cos(dir*piover180),sin(dir*piover180),.5);
glTexCoord2f(.0625*(13+1),1-.0625);
glVertex3f(cos(dir*piover180),sin(dir*piover180),-.5);
glTexCoord2f(.0625*(13+1),1);
glVertex3f(cos((dir+36)*piover180),sin((dir+36)*piover180),-.5);
glTexCoord2f(.0625*13,1);
glVertex3f(cos((dir+36)*piover180),sin((dir+36)*piover180),.5);
}
x++;
}
glEnd();
}
int DrawGLScene(void)
{
// int hour10=0,hour1=0,min10=0,min1=0,sec10=0,sec1=0;
int hours=0, minutes=0, seconds=0;
bool pm=true;
timev=time(NULL);
timev%=86400;
if (timev<43200)
pm=false;
hours=timev/3600;
timev%=3600;
minutes=timev/60;
timev%=60;
seconds=timev;
hours-=4;
hours+=12;
hours%=12;
rot[1]=360-((seconds%10)*36+18);
dat[1]=seconds%10;
rot[2]=360-((seconds/10)*36+18+((seconds%10)/10.0)*36.0f);
dat[2]=seconds/10;
// rot[3] is a colon
rot[4]=360-((minutes%10)*36+18+((seconds/10)/6.0)*36.0f);
dat[4]=minutes%10;
rot[5]=360-((minutes/10)*36+18+((minutes%10)/10.0)*36.0f);
dat[5]=minutes/10;
// rot[6] is a colon
rot[7]=360-((hours%10)*36+18+((minutes/10)/6.0)*36.0f);
dat[7]=hours%10;
rot[8]=360-((hours/10)*36+18+((hours%10)/10.0)*36.0f);
dat[8]=hours/10;
if (screennum==0)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(20,0,4.5,
0,0,4.5,
0,1,0);
for (int x=0;x<9;x++)
{
glTranslatef(0,0,1);
glRotatef(rot[x],0,0,1);
if ((x!=3)&(x!=6)&&(x!=0))
{
drawwheel(dat[x]);
}
else
{
if (x!=0)
drawcolon();
else
drawpmam(pm);
}
glRotatef(rot[x],0,0,-1);
}
}
return true;
}
void ReSizeGLScene(GLsizei width, GLsizei height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
void LoadGLTextures() // Load Bitmaps And Convert To Textures
{
char input[200];
char buffer[200];
int num=0;
ifstream fin("graphics/textures.txt");
fin>>num;
for (int i=0;i<num;i++)
{
fin>>input;
wsprintf(buffer,"graphics/%s",input);
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=auxDIBImageLoad(buffer))
{
glGenTextures(1, &texture[i]); // Create Three Textures
glBindTexture(GL_TEXTURE_2D, texture[i]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}
if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}
free(TextureImage[0]); // Free The Image Structure
}
}
}
int InitGL(void)
{
LoadGLTextures();
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f,0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return true;
}
void KillGLWindow(void)
{
if (fullscreen)
{
ChangeDisplaySettings(NULL,0);
ShowCursor(true);
}
if (hRC)
{
if (!wglMakeCurrent(NULL,NULL))
{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
if (!wglDeleteContext(hRC))
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL;
}
if (hDC && !ReleaseDC(hWnd,hDC))
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL;
}
if (hWnd && !DestroyWindow(hWnd))
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL;
}
if (!UnregisterClass("OpenGL",hInstance))
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL;
}
}
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwExStyle;
DWORD dwStyle;
RECT WindowRect;
WindowRect.left=(long)0;
WindowRect.right=(long)width;
WindowRect.top=(long)0;
WindowRect.bottom=(long)height;
fullscreen=fullscreenflag;
hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL";
if (!RegisterClass(&wc))
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false;
}
if (fullscreen)
{
DEVMODE dmScreenSettings;
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = width;
dmScreenSettings.dmPelsHeight = height;
dmScreenSettings.dmBitsPerPel = bits;
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=false;
}
else
{
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return false;
}
}
}
if (fullscreen)
{
dwExStyle=WS_EX_APPWINDOW;
dwStyle=WS_POPUP;
ShowCursor(false);
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle=WS_OVERLAPPEDWINDOW;
}
AdjustWindowRectEx(&WindowRect, dwStyle, false, dwExStyle);
if (!(hWnd=CreateWindowEx( dwExStyle,
"OpenGL",
title,
dwStyle |
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN,
0, 0,
WindowRect.right-WindowRect.left,
WindowRect.bottom-WindowRect.top,
NULL,
NULL,
hInstance,
NULL)))
{
KillGLWindow();
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false;
}
static PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
bits,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
if (!(hDC=GetDC(hWnd)))
{
KillGLWindow();
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false;
}
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))
{
KillGLWindow();
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false;
}
if(!SetPixelFormat(hDC,PixelFormat,&pfd))
{
KillGLWindow();
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false;
}
if (!(hRC=wglCreateContext(hDC)))
{
KillGLWindow();
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false;
}
if(!wglMakeCurrent(hDC,hRC))
{
KillGLWindow();
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false;
}
ShowWindow(hWnd,SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
ReSizeGLScene(width, height);
if (!InitGL())
{
KillGLWindow();
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false;
}
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_ACTIVATE:
{
if (!HIWORD(wParam))
{
active=true;
}
else
{
active=false;
}
}
break;
case WM_SYSCOMMAND:
{
switch (wParam)
{
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}
case WM_SIZE:
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
return 0;
}
case WM_KEYDOWN:
{
}
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nShowCmd)
{
MSG msg;
BOOL done=false;
if (!CreateGLWindow("3D RPG DEMO",640,480,16,fullscreen))
{
return 0;
}
while(!done)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message==WM_QUIT)
{
done=true;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
if (active)
{
if (GetAsyncKeyState(VK_ESCAPE)!=0)
{
done=true;
}
else
{
DrawGLScene();
SwapBuffers(hDC);
}
}
}
}
KillGLWindow();
return (msg.wParam);
}