Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1.             #ifdef _WINDOWS
  2.     #include <GL/glew.h>
  3. #endif
  4. #include <SDL.h>
  5. #include <SDL_opengl.h>
  6. #include <SDL_image.h>
  7. #include "ShaderProgram.h"
  8. #include "Matrix.h"
  9.  
  10. #ifdef _WINDOWS
  11.     #define RESOURCE_FOLDER ""
  12. #else
  13.     #define RESOURCE_FOLDER "NYUCodebase.app/Contents/Resources/"
  14. #endif
  15.  
  16.  
  17.  
  18. GLuint LoadTexture(const char *image_path) {
  19.     SDL_Surface *surface = IMG_Load(image_path);
  20.     GLuint textureID;
  21.     glGenTextures(1, &textureID);
  22.     glBindTexture(GL_TEXTURE_2D, textureID);
  23.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGBA,
  24.         GL_UNSIGNED_BYTE, surface->pixels);
  25.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  26.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  27.     SDL_FreeSurface(surface);
  28.     return textureID;
  29. }
  30.  
  31.  
  32. SDL_Window* displayWindow;
  33.  
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37.     SDL_Init(SDL_INIT_VIDEO);
  38.     displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
  39.     SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
  40.     SDL_GL_MakeCurrent(displayWindow, context);
  41.     #ifdef _WINDOWS
  42.         glewInit();
  43.     #endif
  44.  
  45.     glViewport(0, 0, 640, 360);
  46.  
  47.    
  48.     ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
  49.  
  50.     GLuint emojiTexture = LoadTexture("emoji.png");
  51.     Matrix projectionMatrix;
  52.     Matrix modelMatrix;
  53.     Matrix viewMatrix;
  54.  
  55.  
  56.     projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
  57.  
  58.     glUseProgram(program.programID);
  59.  
  60.     SDL_Event event;
  61.     bool done = false;
  62.     while (!done) {
  63.         while (SDL_PollEvent(&event)) {
  64.             if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
  65.                 done = true;
  66.             }
  67.         }
  68.  
  69.         program.setModelMatrix(modelMatrix);
  70.         program.setProjectionMatrix(projectionMatrix);
  71.         program.setViewMatrix(viewMatrix);
  72.  
  73.  
  74.         glBindTexture(GL_TEXTURE_2D, emojiTexture);
  75.         float vertices[] = { -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5 };
  76.         glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
  77.         glEnableVertexAttribArray(program.positionAttribute);
  78.         float texCoords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
  79.         glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
  80.         glEnableVertexAttribArray(program.texCoordAttribute);
  81.         glDrawArrays(GL_TRIANGLES, 0, 6);
  82.         glDisableVertexAttribArray(program.positionAttribute);
  83.         glDisableVertexAttribArray(program.texCoordAttribute);
  84.  
  85.         glClear(GL_COLOR_BUFFER_BIT);
  86.         SDL_GL_SwapWindow(displayWindow);
  87.     }
  88.  
  89.     SDL_Quit();
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement