Advertisement
Benjamin_Loison

Parallel texture registration and rendering OpenGL

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