Advertisement
kaenan

SDL & Glew - Draw tringle, rectangle

Nov 7th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2. #include "GL/glew.h"
  3.  
  4. #include <iostream>
  5.  
  6. bool processInput();
  7. void draw(SDL_Window *window);
  8. void draw_square_over(float x, float y, float size);
  9. void draw_square(float x, float y, float size);
  10. void draw_background();
  11.  
  12. int main(int argc, char* argv[])
  13. {
  14.     SDL_Init(SDL_INIT_EVERYTHING);
  15.  
  16.     SDL_Window *window = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_OPENGL);
  17.     if (!window)
  18.         return 1;
  19.     SDL_GLContext glContext = SDL_GL_CreateContext(window);
  20.     if (!glContext)
  21.         return 2;
  22.     GLenum error = glewInit();
  23.     if (error != GLEW_OK)
  24.         return 3;
  25.  
  26.     glClearDepth(1.0);
  27.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  28.  
  29.     bool run = true;
  30.     while (run)
  31.     {
  32.         run = processInput();
  33.         draw(window);
  34.     }
  35.  
  36.     SDL_GL_DeleteContext(glContext);
  37.     SDL_DestroyWindow(window);
  38.  
  39.     return 0;
  40. }
  41.  
  42. bool processInput()
  43. {
  44.     SDL_Event event;
  45.     while (SDL_PollEvent(&event))
  46.     {
  47.         switch (event.type)
  48.         {
  49.         case SDL_QUIT: return false;
  50.         case SDL_MOUSEMOTION:
  51.             std::cout << "Mouse moved to: " << event.motion.x << " " << event.motion.y << std::endl; return true;
  52.         }
  53.     }
  54.  
  55.     return true;
  56. }
  57.  
  58. inline void draw_triangle()
  59. {
  60.     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  61.  
  62.     glBegin(GL_TRIANGLES);
  63.     glColor3f(1.0f, 0.0f, 0.0f);    // red
  64.     glVertex2f(-1.f, -1.f);
  65.     glColor3f(0.0f, 1.0f, 0.0f);    // blue
  66.     glVertex2f(1.f, -1.f);
  67.     glColor3f(0.0f, 0.0f, 1.0f);    // green
  68.     glVertex2f(1.f, 1.f);
  69.     glEnd();
  70. }
  71.  
  72. inline void draw_background() {
  73.     draw_square_over(0.f, 0.f, 2.f);
  74. }
  75.  
  76. void draw_square(float x, float y, float size) {
  77.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  78.     draw_square_over(x, y, size);
  79. }
  80.  
  81. void draw_square_over(float x, float y, float size) {
  82.     struct {
  83.         float x, y;
  84.     } BL; // bottom-left
  85.  
  86.     BL.x = x - size/2;
  87.     BL.y = y - size/2;
  88.  
  89.     glBegin(GL_QUADS);
  90.     glColor3d(1, 0, 0);
  91.     glVertex3f(BL.x, BL.y, 0.0f);  // bottom-left
  92.     glColor3d(1, 1, 0);
  93.     glVertex3f(BL.x + size, BL.y, 0.0f);  // bottom-right
  94.     glColor3d(1, 1, 1);
  95.     glVertex3f(BL.x + size, BL.y + size, 0.0f);  // top-right
  96.     glColor3d(0, 1, 1);
  97.     glVertex3f(BL.x, BL.y + size, 0.0f);   // top-left
  98.     glEnd();
  99. }
  100.  
  101. void draw(SDL_Window *window)
  102. {
  103.     draw_background();
  104.     draw_square_over(0.f, 0.f, 0.2f);
  105.     SDL_GL_SwapWindow(window);
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement