Created
November 29, 2011 21:36
-
-
Save orlp/1406628 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <string> | |
| #include <cstdio> | |
| #include <cmath> | |
| #include "GL/glfw.h" | |
| #include "SOIL.h" | |
| // function declarations | |
| void drawscene(); | |
| void updatedisplay(); | |
| class Texture { | |
| public: | |
| Texture() : | |
| texture(0), | |
| width(0), height(0), | |
| channels(0) { | |
| } | |
| Texture(const char *filename) { | |
| load_filename(filename); | |
| } | |
| void load_filename(const char *filename) { | |
| unsigned char *data = SOIL_load_image(filename, &width, &height, &channels, SOIL_LOAD_AUTO); | |
| texture = SOIL_create_OGL_texture(data, width, height, channels, 0, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MULTIPLY_ALPHA); | |
| SOIL_free_image_data(data); | |
| } | |
| public: | |
| GLuint texture; | |
| int width; | |
| int height; | |
| int channels; | |
| }; | |
| class Sprite { | |
| public: | |
| Sprite() : | |
| x(0), y(0), | |
| anchor_x(0), anchor_y(0), | |
| rotation(0.0), scale(0.0), | |
| texture(0) { | |
| } | |
| Sprite(GLuint tex) : | |
| x(0), y(0), | |
| anchor_x(0), anchor_y(0), | |
| rotation(0.0), scale(0.0), | |
| texture(tex) { | |
| } | |
| Sprite(const Sprite& sprite) : | |
| x(sprite.x), y(sprite.y), | |
| anchor_x(sprite.anchor_x), anchor_y(sprite.anchor_y), | |
| rotation(sprite.rotation), scale(sprite.rotation), | |
| texture(sprite.texture) { | |
| } | |
| void draw() { | |
| if (!texture) return; | |
| glBindTexture(GL_TEXTURE_2D, texture); | |
| glPushMatrix(); | |
| glTranslatef((GLfloat) x, (GLfloat) y, 0); | |
| glTranslatef((GLfloat) anchor_x, (GLfloat) anchor_y, 0); | |
| glRotatef((GLfloat) rotation, 0.0f, 0.0f, 1.0f); | |
| glScalef(scale, scale, 1.0f); | |
| glTranslatef((GLfloat) -anchor_x, (GLfloat) -anchor_y, 0); | |
| glBegin(GL_QUADS); | |
| glTexCoord2f(0.0f, 0.0f); | |
| glVertex2f(0.0f, 0.0f); | |
| glTexCoord2f(0.0f, 1.0f); | |
| glVertex2f(0.0f, 128.0f); | |
| glTexCoord2f(1.0f, 1.0f); | |
| glVertex2f(128.0f, 128.0f); | |
| glTexCoord2f(1.0f, 0.0f); | |
| glVertex2f(128.0f, 0.0f); | |
| glEnd(); | |
| glPopMatrix(); | |
| } | |
| int x; | |
| int y; | |
| int anchor_x; | |
| int anchor_y; | |
| float rotation; | |
| float scale; | |
| private: | |
| GLuint texture; | |
| }; | |
| Texture tex; | |
| int main(int argc, char **argv) { | |
| if (!glfwInit()) { | |
| fprintf(stderr, "Failed to initialize GLFW\n"); | |
| return 1; | |
| } | |
| if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 16, 0, GLFW_WINDOW)) { | |
| fprintf(stderr, "Failed to open GLFW window\n"); | |
| return 1; | |
| } | |
| // enable vsync (if available) | |
| glfwSwapInterval(1); | |
| // load textures | |
| tex.load_filename("tex.png"); | |
| //texture = SOIL_load_OGL_texture( | |
| // "tex.png", | |
| // SOIL_LOAD_AUTO, | |
| // SOIL_CREATE_NEW_ID, | |
| // SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT | |
| //); | |
| // check for an error during the texture loading | |
| //if (!texture) { | |
| // printf("SOIL loading error: '%s'\n", SOIL_last_result()); | |
| //} | |
| while (glfwGetWindowParam(GLFW_OPENED)) { | |
| updatedisplay(); | |
| drawscene(); | |
| } | |
| // if we get here something went wrong | |
| return 0; | |
| } | |
| // set up te display | |
| void updatedisplay() { | |
| int screen_width, screen_height; | |
| glfwGetWindowSize(&screen_width, &screen_height); | |
| if (screen_height <= 0) screen_height = 1; | |
| if (screen_width <= 0) screen_width = 1; | |
| glViewport(0, 0, screen_width, screen_height); | |
| glClearColor(1, 1, 1, 0.0f); | |
| glClear(GL_COLOR_BUFFER_BIT); | |
| glEnable(GL_TEXTURE_2D); | |
| glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| glEnable(GL_BLEND); | |
| glDisable(GL_DEPTH_TEST); | |
| glMatrixMode(GL_PROJECTION); | |
| glLoadIdentity(); | |
| glOrtho(0.0, screen_width, screen_height, 0.0, 0.0, 1.0); | |
| glMatrixMode(GL_MODELVIEW); | |
| glLoadIdentity(); | |
| // displacement trick for exact pixelization | |
| glTranslatef(0.375f, 0.375f, 0.0f); | |
| } | |
| // draw the scene in this function | |
| void drawscene() { | |
| static float delta = 0.0; | |
| delta += 0.1; | |
| Sprite sprite(tex.texture); | |
| sprite.x = 150; | |
| sprite.y = 150; | |
| sprite.anchor_x = 64; | |
| sprite.anchor_y = 64; | |
| sprite.rotation = delta * 2; | |
| sprite.scale = fmod(delta / 20, 2.0); | |
| sprite.draw(); | |
| glfwSwapBuffers(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment