#include #include #ifdef __linux__ #include #endif #ifdef _WIN32 #include #endif #include "libs/mainloop-api/mainloop.h" #include "graphics.h" #include "textures.h" #include "player.h" #include "clouds.h" #include "world.h" GLfloat aspect; GLsizei w_dimentions[2]; int skymap_size[2]; int skymap_id; int mspf=1000/30; int max_mspf=1000/10; int* sxy; void on_early() { // Make the display interval between updates shorter if(mspf > max_mspf) { mspf -= 1; } } void on_late() { // Increase the display update intervals mspf += 1; } int* desktopResolution() { // Setup the data structure int* data = new int[2](); #ifdef _WIN32 // Get the desktop RECT desktop; // Get the desktop window const HWND hDesktop = GetDesktopWindow(); // Process the size of the window GetWindowRect(hDesktop, &desktop); // Set these values data[0] = desktop.bottom; data[1] = desktop.right; #endif #ifdef __linux__ // Get the screen Display* d = XOpenDisplay(NULL); Screen* s = DefaultScreenOfDisplay(d); // Set the varibles data[0] = s->width; data[1] = s->height; #endif // Return the data return data; } void display() { // Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Set perspective view gluPerspective(100.0f, aspect, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Enable depth glEnable(GL_DEPTH_TEST); /*glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glFrustum(-1.0, 1.0, -1.0, 1.0, 10.0, 8.0);*/ // Set the viewport glViewport(0, 0, w_dimentions[0], w_dimentions[1]); // Load the skymap glBindTexture(GL_TEXTURE_2D, skymap_id); // Rotate the angle glRotated(player_angle[1], 1, 0, 0); glRotated(player_angle[0], 0, 1, 0); // Set the colour glColor4f(0.5f,0.7f,1.0f,1.0f); // Start drawing a cube glBegin(GL_QUADS); // Top glTexCoord2d(1/3.0,0); glVertex3i(-2, 1,-2); glTexCoord2d(0/3.0,0); glVertex3i( 2, 1,-2); glTexCoord2d(0/3.0,1); glVertex3i( 2, 1, 2); glTexCoord2d(1/3.0,1); glVertex3i(-2, 1, 2); // Sides glTexCoord2d(2/3.0,0); glVertex3i( 1,-1,-1); glTexCoord2d(1/3.0,0); glVertex3i( 1, 1,-1); glTexCoord2d(1/3.0,1); glVertex3i(-1, 1,-1); glTexCoord2d(2/3.0,1); glVertex3i(-1,-1,-1); glTexCoord2d(2/3.0,0); glVertex3i( 1,-1, 1); glTexCoord2d(1/3.0,0); glVertex3i( 1, 1, 1); glTexCoord2d(1/3.0,1); glVertex3i(-1, 1, 1); glTexCoord2d(2/3.0,1); glVertex3i(-1,-1, 1); glTexCoord2d(2/3.0,0); glVertex3i(-1,-1,-1); glTexCoord2d(1/3.0,0); glVertex3i(-1, 1,-1); glTexCoord2d(1/3.0,1); glVertex3i(-1, 1, 1); glTexCoord2d(2/3.0,1); glVertex3i(-1,-1, 1); glTexCoord2d(2/3.0,0); glVertex3i( 1,-1,-1); glTexCoord2d(1/3.0,0); glVertex3i( 1, 1,-1); glTexCoord2d(1/3.0,1); glVertex3i( 1, 1, 1); glTexCoord2d(2/3.0,1); glVertex3i( 1,-1, 1); // Bottom glTexCoord2d(3/3.0,0); glVertex3i(-2,-1,-2); glTexCoord2d(2/3.0,0); glVertex3i( 2,-1,-2); glTexCoord2d(2/3.0,1); glVertex3i( 2,-1, 2); glTexCoord2d(3/3.0,1); glVertex3i(-2,-1, 2); // Stop drawing the cubemap glEnd(); // Clear the depth glClear(GL_DEPTH_BUFFER_BIT); // Render the clouds clouds_render(); // Translate the position glTranslated(-player_pos[0],-player_pos[1],-player_pos[2]); // Load the worlds blocks world_render(); glPopMatrix(); // 2D view /*glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho(-9.0, 9.0, -9.0, 9.0, 0.0, 30.0); glLoadIdentity();*/ glLoadIdentity(); glMatrixMode(GL_PROJECTION); glDisable(GL_DEPTH_TEST); glLoadIdentity(); //glPopMatrix(); // Render the player player_render(); // Swap the front and back frame buffers glutSwapBuffers(); } void reshape(GLsizei width, GLsizei height) { // Prevent division by zero if (width < 1) width = 1; if (height < 1) height = 1; w_dimentions[0] = width; w_dimentions[1] = height; // Get the aspect aspect = GLfloat(width) / GLfloat(height); // Reload the identity glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Set the perspective gluPerspective(100.0f, aspect, 0.1f, 100.0f); } void graphics_reshape() { reshape(w_dimentions[0], w_dimentions[1]); } void render_event(int *args) { // Repost this event mainloopRegAction(render_event, mspf, 0); // Post Redisplay glutPostRedisplay(); } void display_event(int *args) { // Repost this event mainloopRegAction(display_event, 10, 0); // Do the glut mainloop event glutMainLoopEvent(); } void graphics_init(int argc, char *argv[]) { // Initialise OpenGL glutInit(&argc, argv); // Get the screen size sxy = desktopResolution(); // Set the window properties glutCreateWindow("Racing Game"); glutInitDisplayMode(GLUT_DOUBLE); glutReshapeWindow(sxy[0], sxy[1]); glutFullScreen(); // Display stuff glutDisplayFunc(display); glutReshapeFunc(reshape); // Enable some stuff glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); // Enable blending glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Load the skymap skymap_id = loadArchiveImageGL("textures/sky.rimg", 'N', skymap_size[0], skymap_size[1]); // Make these functions happen in the future mainloopRegAction(display_event, 10, 0); mainloopRegAction(render_event, 1000/60, 0); // Handle lag mainloopOnEarly(on_early); mainloopOnLate(on_late); }