Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Includes **/
- #include "include/graphics.h"
- /** Datum **/
- int programStatus=0;
- GLuint Tex;
- /** Functions **/
- void GraphicsInit(void)
- {
- Tex = loadBMP("bit.bmp");
- printf("loaded bit.bmp as texture #%d\n",Tex);
- const int
- windowWidth = 800,
- windowHeight = 600;
- if (glfwInit() != GL_TRUE)
- GraphicsShutDown(1);
- // 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
- if (glfwOpenWindow(windowWidth, windowHeight, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)
- != GL_TRUE)
- GraphicsShutDown(1);
- glfwSetWindowTitle("Chase Hero!!!");
- glClearColor(0, 0, 0, 0);
- glMatrixMode(GL_PROJECTION);
- glOrtho(0, 512, 512, 0, 0, 1); // Define 2D Coordinates, 512 x 512, no depth
- glfwSetKeyCallback(keyboardCallback); // Callback for keyboard stuff
- }
- void GraphicsShutDown(int return_code)
- {
- glfwTerminate();
- exit(return_code);
- }
- void GraphicsMainLoop(void)
- {
- // Draw the figure
- Draw();
- // Swap back and front buffers
- glfwSwapBuffers();
- }
- void GLFWCALL keyboardCallback(int key, int action)
- {
- if(key == GLFW_KEY_ESC && action == GLFW_PRESS)
- programStatus = 1;
- else if(key == GLFW_KEY_LEFT && action == GLFW_PRESS)
- programStatus = 2;
- else if(key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
- programStatus = 3;
- else if(key == GLFW_KEY_UP && action == GLFW_PRESS)
- programStatus = 4;
- else if(key == GLFW_KEY_DOWN && action == GLFW_PRESS)
- programStatus = 5;
- else
- programStatus = 0;
- }
- void Draw(void)
- {
- int i, j;
- glClear(GL_COLOR_BUFFER_BIT); // Clear all the buffer!
- for(i=0; i<10; i++)
- {
- for(j=0; j<10; j++)
- {
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, Tex);
- glBegin(GL_QUADS);
- {
- if((i+j)&1)
- glColor3f(1,1,1);
- else if(programStatus == 5)
- glColor3f(1,0,0);
- else if(programStatus == 4)
- glColor3f(0,0,1);
- else if(programStatus == 3)
- glColor3f(0,1,1);
- else if(programStatus == 2)
- glColor3f(1,1,0);
- else
- glColor3f(0,1,0);
- glTexCoord2f(0, 0);
- glVertex2d(i*32+128,j*32+128);
- glTexCoord2f(1, 0);
- glVertex2d(i*32+160,j*32+128);
- glTexCoord2f(1, 1);
- glVertex2d(i*32+160,j*32+160);
- glTexCoord2f(0, 1);
- glVertex2d(i*32+128,j*32+160);
- }
- glEnd();
- glDisable(GL_TEXTURE_2D);
- }
- }
- }
- GLuint loadBMP(const char * imagepath)
- {
- /** Bitmap File layout **/
- unsigned char header[54]; // Bitmap has 54 byte header
- unsigned int dataPos; // Position of the actual data
- unsigned int width, height; // Sizes
- unsigned int imageSize; // width*height*3 (24 bit color)
- unsigned char * data; // Actual RGB data
- /** Loading the bitmap data into memory **/
- FILE * file = fopen(imagepath,"rb");
- if(!file)
- {
- fprintf(stderr, "404 - File %s not found", imagepath);
- return 0;
- }
- if(fread(header, 1, 54, file) != 54) // If the header isn't there
- {
- fprintf(stderr, "%s not vaild BMP data", imagepath);
- return 0;
- }
- if(header[0]!='B'||header[1]!='M')
- {
- fprintf(stderr, "%s not vaild BMP data", imagepath);
- return 0;
- }
- dataPos = *(int*)&(header[0x0A]);
- imageSize = *(int*)&(header[0x22]);
- width = *(int*)&(header[0x12]);
- height = *(int*)&(header[0x16]);
- // Some BMP files are misformatted, lazy software, time to guess shit.
- if(imageSize == 0) // Image size missing
- imageSize = width * height * 3; // Assume 24 Bit Color
- if(dataPos == 0) // No pointer to the actual pixel data
- dataPos = 54; // Data usually is right after header
- // NOTE: Those four lines are precautionary checks only.
- data = (unsigned char*)malloc(imageSize); // reserve memory for bitmap data.
- fread(data, 1, imageSize, file); // Read actual RGB data.
- fclose(file); // Done reading the file, close it.
- /** Actual GL stuff here **/
- // Create one OpenGL approved Texture
- GLuint textureID;
- glGenTextures(1, &textureID);
- // Bind the texture to a number
- glBindTexture(GL_TEXTURE_2D, textureID);
- printf("%d: %d x %d\n", textureID, width, height);
- // Pass the image to VRAM
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB8, GL_UNSIGNED_BYTE, data);
- free(data);
- return textureID;
- }
Add Comment
Please, Sign In to add comment