Advertisement
Benjamin_Loison

Parallel texture registration and rendering OpenGL

Aug 15th, 2017
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1.     #include <SDL.h>
  2.     #include <SDL_image.h>
  3.     #include <gl.h>
  4.     #include <glu.h>
  5.     #include <cstring>
  6.     #include <thread>
  7.     #include <iostream>
  8.     #define WINDOW_WIDTH 1365
  9.     #define WINDOW_HEIGHT 704
  10.     using namespace std;
  11.    
  12.     SDL_Window *screen;
  13.     SDL_GLContext ctx;
  14.     GLuint red = 0;
  15.    
  16.     SDL_Surface *flipSurface(SDL_Surface *surface)
  17.     {
  18.         int current_line, pitch;
  19.         SDL_Surface *fliped_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w,surface->h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
  20.         SDL_LockSurface(surface);
  21.         SDL_LockSurface(fliped_surface);
  22.         pitch = surface->pitch;
  23.         for(current_line = 0; current_line < surface->h; current_line++)
  24.             memcpy(&((unsigned char*)fliped_surface->pixels)[current_line*pitch], &((unsigned char*)surface->pixels)[(surface->h - 1  - current_line)*pitch], pitch);
  25.         SDL_UnlockSurface(fliped_surface);
  26.         SDL_UnlockSurface(surface);
  27.         return fliped_surface;
  28.     }
  29.    
  30.     GLuint loadTexture(const char *filename)
  31.     {
  32.         GLuint glID;
  33.         SDL_Surface *picture_surface, *gl_surface, *gl_fliped_surface;
  34.         Uint32 rmask, gmask, bmask, amask;
  35.         picture_surface = IMG_Load(filename);
  36.         if(picture_surface == NULL)
  37.             return 0;
  38.         #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  39.             rmask = 0xff000000;
  40.             gmask = 0x00ff0000;
  41.             bmask = 0x0000ff00;
  42.             amask = 0x000000ff;
  43.         #else
  44.             rmask = 0x000000ff;
  45.             gmask = 0x0000ff00;
  46.             bmask = 0x00ff0000;
  47.             amask = 0xff000000;
  48.         #endif
  49.         SDL_PixelFormat format = *(picture_surface->format);
  50.         format.BitsPerPixel = 32;
  51.         format.BytesPerPixel = 4;
  52.         format.Rmask = rmask;
  53.         format.Gmask = gmask;
  54.         format.Bmask = bmask;
  55.         format.Amask = amask;
  56.         gl_surface = SDL_ConvertSurface(picture_surface, &format, SDL_SWSURFACE);
  57.         gl_fliped_surface = flipSurface(gl_surface);
  58.         glGenTextures(1, &glID);
  59.         glBindTexture(GL_TEXTURE_2D, glID);
  60.         glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w, gl_fliped_surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, gl_fliped_surface->pixels);
  61.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  62.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  63.         SDL_FreeSurface(gl_fliped_surface);
  64.         SDL_FreeSurface(gl_surface);
  65.         SDL_FreeSurface(picture_surface);
  66.         return glID;
  67.     }
  68.    
  69.     void render(GLuint picture)
  70.     {
  71.         glBindTexture(GL_TEXTURE_2D, picture);
  72.         glBegin(GL_QUADS);
  73.             glTexCoord2d(0, 0); glVertex2f(-WINDOW_WIDTH, -WINDOW_HEIGHT);
  74.             glTexCoord2d(0, 1); glVertex2f(-WINDOW_WIDTH, WINDOW_HEIGHT);
  75.             glTexCoord2d(1, 1); glVertex2f(WINDOW_WIDTH, WINDOW_HEIGHT);
  76.             glTexCoord2d(1, 0); glVertex2f(WINDOW_WIDTH, -WINDOW_HEIGHT);
  77.         glEnd();
  78.     }
  79.    
  80.     void registerRed()
  81.     {
  82.         SDL_GL_MakeCurrent(screen, ctx);
  83.         red = loadTexture("red.jpg");
  84.         cout << endl << "Registered: " << red << endl << endl;
  85.     }
  86.    
  87.     int main(int argc, char** argv)
  88.     {
  89.         SDL_Init(SDL_INIT_VIDEO);
  90.         screen = SDL_CreateWindow("My App", 100, 100, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL);
  91.         SDL_Event event;
  92.         ctx = SDL_GL_CreateContext(screen);
  93.         bool running = true;
  94.         glEnable(GL_TEXTURE_2D);
  95.         GLuint blue = loadTexture("blue.jpg");
  96.         thread registerRedThread = thread(&registerRed);
  97.         while(running)
  98.         {
  99.             SDL_PollEvent(&event);
  100.             switch(event.type)
  101.             {
  102.                 case SDL_QUIT:
  103.                     {
  104.                         running = false;
  105.                         break;
  106.                     }
  107.             }
  108.    
  109.             cout << SDL_GetTicks() << endl;
  110.             glClear(GL_COLOR_BUFFER_BIT);
  111.    
  112.             render(blue);
  113.             if(red != 0)
  114.             {
  115.                 cout << "Rendering the red picture..." << endl;
  116.                 render(red);
  117.             }
  118.    
  119.             glFlush();
  120.             SDL_GL_SwapWindow(screen);
  121.    
  122.             SDL_Delay(1000);
  123.         }
  124.         registerRedThread.detach();
  125.         SDL_Quit();
  126.         return 0;
  127.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement