Advertisement
Benjamin_Loison

Some LemnosLife render stuff

Jul 1st, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 KB | None | 0 0
  1. void renderEvent()
  2. {
  3.     // TODO: optimise opengl's options when needed (first initialize when changing gui screen)
  4.     size();
  5.     windowInitialWidth = windowWidth;
  6.     windowInitialHeight = windowHeight;
  7.     font = TTF_OpenFont(string(styles + name + ".ttf").c_str(), 100);
  8.     text = TTF_RenderText_Blended(font, nameUp.c_str(), {255, 255, 255});
  9.     glEnable(GL_CULL_FACE);
  10.     glCullFace(GL_BACK);
  11.     glFrontFace(GL_CCW);
  12.     glClearColor(0, 0, 0, 0);
  13.     glViewport(0, 0, windowWidth, windowHeight);
  14.  
  15.     glMatrixMode(GL_PROJECTION);
  16.     glLoadIdentity();
  17.     gluPerspective(70, (GLdouble)windowWidth / (GLdouble)windowHeight, 0.1, 1000);
  18.     SDL_GL_CreateContext(screen);
  19.  
  20.     gluOrtho2D(0, (GLdouble)windowWidth, 0, (GLdouble)windowHeight);
  21.  
  22.     glDisable(GL_DEPTH_TEST);
  23.     glEnable(GL_TEXTURE_2D);
  24.     glClear(GL_COLOR_BUFFER_BIT);
  25.     img = loadTexture(string(pictures + "background.jpg").c_str());
  26.     while(1)
  27.         for(int frame = 0; frame < fps; frame++)
  28.         {
  29.             renderScreen();
  30.             SDL_Delay(1000 / fps);
  31.             camera->animate(1000 / fps);
  32.         }
  33.  
  34. }
  35.  
  36. void renderScreen()
  37. {
  38.     if(rendering)
  39.         return;
  40.     rendering = true;
  41.     if(!game)
  42.     {
  43.         glBindTexture(GL_TEXTURE_2D, img);
  44.         glBegin(GL_QUADS);
  45.             glTexCoord2d(0, 0); glVertex2f(0, 0);
  46.             glTexCoord2d(0, 1); glVertex2f(0, windowHeight);
  47.             glTexCoord2d(1, 1); glVertex2f(windowWidth, windowHeight);
  48.             glTexCoord2d(1, 0); glVertex2f(windowWidth, 0);
  49.         glEnd();
  50.  
  51.         glEnable(GL_BLEND);
  52.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  53.         GLuint textureTexte;
  54.         glGenTextures(1, &textureTexte);
  55.         glBindTexture(GL_TEXTURE_2D, textureTexte);
  56.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  57.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  58.         GLenum codagePixel;
  59.         if(text->format->Rmask == 0x000000ff)
  60.             codagePixel = GL_RGBA;
  61.         else
  62.         {
  63.             #ifndef GL_BGRA
  64.                 #define GL_BGRA 0x80E1
  65.             #endif
  66.             codagePixel = GL_BGRA;
  67.         }
  68.         glTexImage2D(GL_TEXTURE_2D, 0, 4, text->w, text->h, 0, codagePixel, GL_UNSIGNED_BYTE, text->pixels);
  69.         glBegin(GL_QUADS);
  70.             glTexCoord2i(0, 0); glVertex2i(windowWidth/2 - resizeWidth / 1.6, windowHeight - resizeHeight / 0.85 + resizeHeight);
  71.             glTexCoord2i(0, 1); glVertex2i(windowWidth/2 - resizeWidth / 1.6, windowHeight - resizeHeight / 0.85 - resizeHeight);
  72.             glTexCoord2i(1, 1); glVertex2i(windowWidth/2 + resizeWidth / 1.6, windowHeight - resizeHeight / 0.85 - resizeHeight);
  73.             glTexCoord2i(1, 0); glVertex2i(windowWidth/2 + resizeWidth / 1.6, windowHeight - resizeHeight / 0.85 + resizeHeight);
  74.         glEnd();
  75.         glFlush();
  76.         SDL_GL_SwapWindow(screen);
  77.     }
  78.     else
  79.     {
  80.         if(first)
  81.         {
  82.             SDL_GL_CreateContext(screen);
  83.             glLoadIdentity();
  84.             glMatrixMode(GL_PROJECTION);
  85.             gluPerspective(70, (GLdouble)windowWidth / (GLdouble)windowHeight, 0.1, 1000);
  86.             glEnable(GL_DEPTH_TEST);
  87.             glEnable(GL_TEXTURE_2D);
  88.             initializeMapRender();
  89.             first = false;
  90.         }
  91.  
  92.         glEnable(GL_CULL_FACE);
  93.         glCullFace(GL_BACK);
  94.         glFrontFace(GL_CCW);
  95.         glClearColor(0, 0, 0, 0);
  96.         glViewport(0, 0, 1365, 704);
  97.         glMatrixMode(GL_PROJECTION);
  98.         glLoadIdentity();
  99.         gluPerspective(70.0, (GLdouble)windowWidth / (GLdouble)windowHeight, 0.1, 1000.0);
  100.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  101.         glEnable(GL_DEPTH_TEST);
  102.         glMatrixMode( GL_MODELVIEW );
  103.         glLoadIdentity();
  104.         glEnable(GL_TEXTURE_2D);
  105.  
  106.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  107.         glMatrixMode(GL_MODELVIEW);
  108.         glLoadIdentity();
  109.         camera->look();
  110.         renderMap();
  111.         drawAxis();
  112.         renderChat();
  113.         glFlush();
  114.         SDL_GL_SwapWindow(screen);
  115.     }
  116.     rendering = false;
  117. }
  118.  
  119. void drawText(string message, int a)
  120. {
  121.     SDL_Surface* text = TTF_RenderText_Blended(arial, message.c_str(), {255, 255, 255});
  122.     GLuint textTexture;
  123.     glGenTextures(1, &textTexture);
  124.     glBindTexture(GL_TEXTURE_2D, textTexture);
  125.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  126.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  127.     GLenum pixelCode = GL_RGBA;
  128.     glTexImage2D(GL_TEXTURE_2D, 0, 4, text->w, text->h, 0, pixelCode, GL_UNSIGNED_BYTE, text->pixels);
  129.     double width = text->w, height = text->h, posX = width / 2 + windowWidth / 100, posY = height / 2 + windowHeight / 100 + height * (a + 1);
  130.     resize(width, height);
  131.     SDL_FreeSurface(text);
  132.     glBindTexture(GL_TEXTURE_2D, textTexture);
  133.     glBegin(GL_QUADS);
  134.         glTexCoord2i(0, 0); glVertex2i(posX - resizeWidth - patch, posY + resizeHeight);
  135.         glTexCoord2i(0, 1); glVertex2i(posX - resizeWidth - patch, posY - resizeHeight);
  136.         glTexCoord2i(1, 1); glVertex2i(posX + resizeWidth - patch, posY - resizeHeight);
  137.         glTexCoord2i(1, 0); glVertex2i(posX + resizeWidth - patch, posY + resizeHeight);
  138.     glEnd();
  139. }
  140.  
  141. void prepareChat()
  142. {
  143.     glPushMatrix();
  144.     glLoadIdentity();
  145.     glMatrixMode(GL_PROJECTION);
  146.     glPushMatrix();
  147.     glLoadIdentity();
  148.     gluOrtho2D(0.0, (GLdouble)1365, 0.0, (GLdouble)704);
  149.     glDisable(GL_DEPTH_TEST);
  150.     glEnable(GL_TEXTURE_2D);
  151.     glEnable(GL_BLEND);
  152.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement