Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h> // Header File For Windows
- #include <stdlib.h>
- #include <stdio.h>
- #include <gl\gl.h> // Header File For The OpenGL32 Library
- #include <gl\glu.h> // Header File For The GLu32 Library
- #include <GL/glut.h>
- //#include <GL\glaux.h> // or The Glaux Library
- #include "glaux.h"
- #include <iostream>
- #include <fstream>
- #define MAP_SIZEX 20
- #define MAP_SIZEY 20
- float offsetx = 0;
- float offsety = 0;
- GLuint texture[2];
- char map[MAP_SIZEX][MAP_SIZEY] = {
- {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
- {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
- };
- void draw_tiles();
- bool keys[256]; // Array Used For The Keyboard Routine
- bool active=TRUE; // Window Active Flag Set To TRUE By Default
- bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
- 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
- }
- int LoadGLTextures() // Load Bitmaps And Convert To Textures
- {
- int Status=FALSE; // Status Indicator
- AUX_RGBImageRec *TextureImage[2]; // Create Storage Space For The Texture
- memset(TextureImage,0,sizeof(void *)*1);
- if (TextureImage[0]=LoadBMP("grass.bmp"))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(1, &texture[0]); // Create The Texture
- // Typical Texture Generation Using Data From The Bitmap
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
- }
- if (TextureImage[1]=LoadBMP("dirt.bmp"))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(2, &texture[1]); // Create The Texture
- // Typical Texture Generation Using Data From The Bitmap
- glBindTexture(GL_TEXTURE_2D, texture[1]);
- glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
- }
- 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
- }
- if (TextureImage[1]) // If Texture Exists
- {
- if (TextureImage[1]->data) // If Texture Image Exists
- {
- free(TextureImage[1]->data); // Free The Texture Image Memory
- }
- free(TextureImage[1]); // Free The Image Structure
- }
- return Status; // Return The Status
- }
- void draw_tiles(GLvoid)
- {
- int tile;
- for (int y = 0; y < MAP_SIZEY; y++)
- {
- for (int x = 0; x < MAP_SIZEX; x++)
- {
- tile = map[y][x];
- glBindTexture(GL_TEXTURE_2D, texture[tile]);
- glBegin(GL_QUADS);
- // OFFSETS are how much the camera moves by when moves.
- // try setting offsets to how much the character moves form input of keys instead and the character should also control the amount of movement.
- glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x+offsetx), float(y + offsety), 0.0f);
- glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1 + offsetx), float(y + offsety), 0.0f);
- glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1 + offsetx), float(y + 1 + offsety), 0.0f);
- glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x + offsetx), float(y + 1 + offsety), 0.0f);
- glEnd();
- }
- }
- }
- GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
- {
- if (height==0) // Prevent A Divide By Zero By
- {
- height=1; // Making Height Equal One
- }
- glViewport(0,0,width,height); // Reset The Current Viewport
- glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
- glLoadIdentity(); // Reset The Projection Matrix
- // Calculate The Aspect Ratio Of The Window
- gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,20.0f);
- glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
- glLoadIdentity(); // Reset The Modelview Matrix
- }
- int init(GLvoid) // All Setup For OpenGL Goes Here
- {
- if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
- {
- return FALSE; // If Texture Didn't Load Return FALSE ( NEW )
- }
- glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
- glShadeModel(GL_SMOOTH); // Enable Smooth Shading
- glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
- glClearDepth(1.0f); // Depth Buffer Setup
- glEnable(GL_DEPTH_TEST); // Enables Depth Testing
- glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
- return TRUE; // Initialization Went OK
- }
- int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
- glLoadIdentity(); // Reset The Current Modelview Matrix
- gluLookAt(10.0f, 8.0f, 20.0f, 10.0f, 8.0f, 0.0f, 0.0f, 1.0f, 0.0f);
- glTranslatef(5.0f,2.0f,0.0f);
- draw_tiles();
- return true;
- }
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- DrawGLScene();
- //ReSizeGLScene(640,480);
- glutSwapBuffers();
- }
- void mySKeyboard(int key, int x, int y)
- {
- switch(key){
- case GLUT_KEY_LEFT: offsetx += 0.04f; break;
- case GLUT_KEY_RIGHT: offsetx -= 0.04f; break;
- case GLUT_KEY_UP: offsety -= 0.04f; break;
- case GLUT_KEY_DOWN: offsety += 0.04f; break;
- default: break;}
- }
- void update(){ glutPostRedisplay();}
- void main (int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(640, 480);
- glutCreateWindow("BasicPlatform");
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(ReSizeGLScene);
- glutSpecialFunc(mySKeyboard);
- glutIdleFunc(update);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement