Advertisement
Guest User

SDL2 render image with android

a guest
Aug 27th, 2017
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.55 KB | None | 0 0
  1. /*
  2. Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
  3.  
  4. This software is provided 'as-is', without any express or implied
  5. warranty.  In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7.  
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely.
  11. */
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <math.h>
  16.  
  17. #ifdef __EMSCRIPTEN__
  18. #include <emscripten/emscripten.h>
  19. #endif
  20.  
  21. #include "SDL_test_common.h"
  22. #include "SDL_image.h"
  23. //#include <android/asset_manager.h>
  24.  
  25.  
  26. #if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__NACL__) \
  27.     || defined(__WINDOWS__) || defined(__LINUX__)
  28. #define HAVE_OPENGLES2
  29. #endif
  30.  
  31. #ifdef HAVE_OPENGLES2
  32.  
  33. #include "SDL_opengles2.h"
  34.  
  35. typedef struct GLES2_Context
  36. {
  37. #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
  38. #include "../src/render/opengles2/SDL_gles2funcs.h"
  39. #undef SDL_PROC
  40. } GLES2_Context;
  41.  
  42.  
  43. static SDLTest_CommonState *state;
  44. static SDL_GLContext *context = NULL;
  45. static int depth = 16;
  46. static GLES2_Context ctx;
  47. //static AAssetManager* asMgr = NULL;
  48.  
  49. static int LoadContext(GLES2_Context * data)
  50. {
  51. #if SDL_VIDEO_DRIVER_UIKIT
  52. #define __SDL_NOGETPROCADDR__
  53. #elif SDL_VIDEO_DRIVER_ANDROID
  54. #define __SDL_NOGETPROCADDR__
  55. #elif SDL_VIDEO_DRIVER_PANDORA
  56. #define __SDL_NOGETPROCADDR__
  57. #endif
  58.  
  59. #if defined __SDL_NOGETPROCADDR__
  60. #define SDL_PROC(ret,func,params) data->func=func;
  61. #else
  62. #define SDL_PROC(ret,func,params) \
  63.     do { \
  64.         data->func = SDL_GL_GetProcAddress(#func); \
  65.         if ( ! data->func ) { \
  66.             return SDL_SetError("Couldn't load GLES2 function %s: %s", #func, SDL_GetError()); \
  67.         } \
  68.     } while ( 0 );
  69. #endif /* __SDL_NOGETPROCADDR__ */
  70.  
  71. #include "../src/render/opengles2/SDL_gles2funcs.h"
  72. #undef SDL_PROC
  73.     return 0;
  74. }
  75.  
  76. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  77. static void
  78. quit(int rc)
  79. {
  80.     int i;
  81.  
  82.     if (context != NULL) {
  83.         for (i = 0; i < state->num_windows; i++) {
  84.             if (context[i]) {
  85.                 SDL_GL_DeleteContext(context[i]);
  86.             }
  87.         }
  88.  
  89.         SDL_free(context);
  90.     }
  91.  
  92.     SDLTest_CommonQuit(state);
  93.     exit(rc);
  94. }
  95.  
  96. #define GL_CHECK(x) \
  97.         x; \
  98.         { \
  99.           GLenum glError = ctx.glGetError(); \
  100.           if(glError != GL_NO_ERROR) { \
  101.             SDL_Log("glGetError() = %i (0x%.8x) at line %i\n", glError, glError, __LINE__); \
  102.             quit(1); \
  103.           } \
  104.         }
  105.  
  106. /*
  107. * Simulates desktop's glRotatef. The matrix is returned in column-major
  108. * order.
  109. */
  110. static void
  111. rotate_matrix(float angle, float x, float y, float z, float *r)
  112. {
  113.     float radians, c, s, c1, u[3], length;
  114.     int i, j;
  115.  
  116.     radians = (float)(angle * M_PI) / 180.0f;
  117.  
  118.     c = SDL_cosf(radians);
  119.     s = SDL_sinf(radians);
  120.  
  121.     c1 = 1.0f - SDL_cosf(radians);
  122.  
  123.     length = (float)SDL_sqrt(x * x + y * y + z * z);
  124.  
  125.     u[0] = x / length;
  126.     u[1] = y / length;
  127.     u[2] = z / length;
  128.  
  129.     for (i = 0; i < 16; i++) {
  130.         r[i] = 0.0;
  131.     }
  132.  
  133.     r[15] = 1.0;
  134.  
  135.     for (i = 0; i < 3; i++) {
  136.         r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
  137.         r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
  138.     }
  139.  
  140.     for (i = 0; i < 3; i++) {
  141.         for (j = 0; j < 3; j++) {
  142.             r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0f);
  143.         }
  144.     }
  145. }
  146.  
  147. /*
  148. * Simulates gluPerspectiveMatrix
  149. */
  150. static void
  151. perspective_matrix(float fovy, float aspect, float znear, float zfar, float *r)
  152. {
  153.     int i;
  154.     float f;
  155.  
  156.     f = 1.0f / SDL_tanf(fovy * 0.5f);
  157.  
  158.     for (i = 0; i < 16; i++) {
  159.         r[i] = 0.0;
  160.     }
  161.  
  162.     r[0] = f / aspect;
  163.     r[5] = f;
  164.     r[10] = (znear + zfar) / (znear - zfar);
  165.     r[11] = -1.0f;
  166.     r[14] = (2.0f * znear * zfar) / (znear - zfar);
  167.     r[15] = 0.0f;
  168. }
  169.  
  170. /*
  171. * Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
  172. * major. In-place multiplication is supported.
  173. */
  174. static void
  175. multiply_matrix(float *lhs, float *rhs, float *r)
  176. {
  177.     int i, j, k;
  178.     float tmp[16];
  179.  
  180.     for (i = 0; i < 4; i++) {
  181.         for (j = 0; j < 4; j++) {
  182.             tmp[j * 4 + i] = 0.0;
  183.  
  184.             for (k = 0; k < 4; k++) {
  185.                 tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
  186.             }
  187.         }
  188.     }
  189.  
  190.     for (i = 0; i < 16; i++) {
  191.         r[i] = tmp[i];
  192.     }
  193. }
  194.  
  195. /*
  196. * Create shader, load in source, compile, dump debug as necessary.
  197. *
  198. * shader: Pointer to return created shader ID.
  199. * source: Passed-in shader source code.
  200. * shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
  201. */
  202. void
  203. process_shader(GLuint *shader, const char * source, GLint shader_type)
  204. {
  205.     GLint status = GL_FALSE;
  206.     const char *shaders[1] = { NULL };
  207.     char buffer[1024];
  208.     GLsizei length;
  209.  
  210.     /* Create shader and load into GL. */
  211.     *shader = GL_CHECK(ctx.glCreateShader(shader_type));
  212.  
  213.     shaders[0] = source;
  214.  
  215.     GL_CHECK(ctx.glShaderSource(*shader, 1, shaders, NULL));
  216.  
  217.     /* Clean up shader source. */
  218.     shaders[0] = NULL;
  219.  
  220.     /* Try compiling the shader. */
  221.     GL_CHECK(ctx.glCompileShader(*shader));
  222.     GL_CHECK(ctx.glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
  223.  
  224.     /* Dump debug info (source and log) if compilation failed. */
  225.     if (status != GL_TRUE) {
  226.         ctx.glGetProgramInfoLog(*shader, sizeof(buffer), &length, &buffer[0]);
  227.         buffer[length] = '\0';
  228.         SDL_Log("Shader compilation failed: %s", buffer); fflush(stderr);
  229.         quit(-1);
  230.     }
  231. }
  232.  
  233. /* 3D data. Vertex range -0.5..0.5 in all axes.
  234. * Z -0.5 is near, 0.5 is far. */
  235. const float _vertices[] =
  236. {
  237.     /* Front face. */
  238.     /* Bottom left */
  239.     -0.5,  0.5, -0.5,
  240.     0.5, -0.5, -0.5,
  241.     -0.5, -0.5, -0.5,
  242.     /* Top right */
  243.     -0.5,  0.5, -0.5,
  244.     0.5,  0.5, -0.5,
  245.     0.5, -0.5, -0.5,
  246.     /* Left face */
  247.     /* Bottom left */
  248.     -0.5,  0.5,  0.5,
  249.     -0.5, -0.5, -0.5,
  250.     -0.5, -0.5,  0.5,
  251.     /* Top right */
  252.     -0.5,  0.5,  0.5,
  253.     -0.5,  0.5, -0.5,
  254.     -0.5, -0.5, -0.5,
  255.     /* Top face */
  256.     /* Bottom left */
  257.     -0.5,  0.5,  0.5,
  258.     0.5,  0.5, -0.5,
  259.     -0.5,  0.5, -0.5,
  260.     /* Top right */
  261.     -0.5,  0.5,  0.5,
  262.     0.5,  0.5,  0.5,
  263.     0.5,  0.5, -0.5,
  264.     /* Right face */
  265.     /* Bottom left */
  266.     0.5,  0.5, -0.5,
  267.     0.5, -0.5,  0.5,
  268.     0.5, -0.5, -0.5,
  269.     /* Top right */
  270.     0.5,  0.5, -0.5,
  271.     0.5,  0.5,  0.5,
  272.     0.5, -0.5,  0.5,
  273.     /* Back face */
  274.     /* Bottom left */
  275.     0.5,  0.5,  0.5,
  276.     -0.5, -0.5,  0.5,
  277.     0.5, -0.5,  0.5,
  278.     /* Top right */
  279.     0.5,  0.5,  0.5,
  280.     -0.5,  0.5,  0.5,
  281.     -0.5, -0.5,  0.5,
  282.     /* Bottom face */
  283.     /* Bottom left */
  284.     -0.5, -0.5, -0.5,
  285.     0.5, -0.5,  0.5,
  286.     -0.5, -0.5,  0.5,
  287.     /* Top right */
  288.     -0.5, -0.5, -0.5,
  289.     0.5, -0.5, -0.5,
  290.     0.5, -0.5,  0.5,
  291. };
  292.  
  293. const float _colors[] =
  294. {
  295.     /* Front face */
  296.     /* Bottom left */
  297.     1.0, 0.0, 0.0, /* red */
  298.     0.0, 0.0, 1.0, /* blue */
  299.     0.0, 1.0, 0.0, /* green */
  300.                    /* Top right */
  301.                    1.0, 0.0, 0.0, /* red */
  302.                    1.0, 1.0, 0.0, /* yellow */
  303.                    0.0, 0.0, 1.0, /* blue */
  304.                                   /* Left face */
  305.                                   /* Bottom left */
  306.                                   1.0, 1.0, 1.0, /* white */
  307.                                   0.0, 1.0, 0.0, /* green */
  308.                                   0.0, 1.0, 1.0, /* cyan */
  309.                                                  /* Top right */
  310.                                                  1.0, 1.0, 1.0, /* white */
  311.                                                  1.0, 0.0, 0.0, /* red */
  312.                                                  0.0, 1.0, 0.0, /* green */
  313.                                                                 /* Top face */
  314.                                                                 /* Bottom left */
  315.                                                                 1.0, 1.0, 1.0, /* white */
  316.                                                                 1.0, 1.0, 0.0, /* yellow */
  317.                                                                 1.0, 0.0, 0.0, /* red */
  318.                                                                                /* Top right */
  319.                                                                                1.0, 1.0, 1.0, /* white */
  320.                                                                                0.0, 0.0, 0.0, /* black */
  321.                                                                                1.0, 1.0, 0.0, /* yellow */
  322.                                                                                               /* Right face */
  323.                                                                                               /* Bottom left */
  324.                                                                                               1.0, 1.0, 0.0, /* yellow */
  325.                                                                                               1.0, 0.0, 1.0, /* magenta */
  326.                                                                                               0.0, 0.0, 1.0, /* blue */
  327.                                                                                                              /* Top right */
  328.                                                                                                              1.0, 1.0, 0.0, /* yellow */
  329.                                                                                                              0.0, 0.0, 0.0, /* black */
  330.                                                                                                              1.0, 0.0, 1.0, /* magenta */
  331.                                                                                                                             /* Back face */
  332.                                                                                                                             /* Bottom left */
  333.                                                                                                                             0.0, 0.0, 0.0, /* black */
  334.                                                                                                                             0.0, 1.0, 1.0, /* cyan */
  335.                                                                                                                             1.0, 0.0, 1.0, /* magenta */
  336.                                                                                                                                            /* Top right */
  337.                                                                                                                                            0.0, 0.0, 0.0, /* black */
  338.                                                                                                                                            1.0, 1.0, 1.0, /* white */
  339.                                                                                                                                            0.0, 1.0, 1.0, /* cyan */
  340.                                                                                                                                                           /* Bottom face */
  341.                                                                                                                                                           /* Bottom left */
  342.                                                                                                                                                           0.0, 1.0, 0.0, /* green */
  343.                                                                                                                                                           1.0, 0.0, 1.0, /* magenta */
  344.                                                                                                                                                           0.0, 1.0, 1.0, /* cyan */
  345.                                                                                                                                                                          /* Top right */
  346.                                                                                                                                                                          0.0, 1.0, 0.0, /* green */
  347.                                                                                                                                                                          0.0, 0.0, 1.0, /* blue */
  348.                                                                                                                                                                          1.0, 0.0, 1.0, /* magenta */
  349. };
  350.  
  351. const char* _shader_vert_src =
  352. " attribute vec4 av4position; "
  353. " attribute vec3 av3color; "
  354. " uniform mat4 mvp; "
  355. " varying vec3 vv3color; "
  356. " void main() { "
  357. "    vv3color = av3color; "
  358. "    gl_Position = mvp * av4position; "
  359. " } ";
  360.  
  361. const char* _shader_frag_src =
  362. " precision lowp float; "
  363. " varying vec3 vv3color; "
  364. " void main() { "
  365. "    gl_FragColor = vec4(vv3color, 1.0); "
  366. " } ";
  367.  
  368. typedef struct shader_data
  369. {
  370.     GLuint shader_program, shader_frag, shader_vert;
  371.  
  372.     GLint attr_position;
  373.     GLint attr_color, attr_mvp;
  374.  
  375.     int angle_x, angle_y, angle_z;
  376.  
  377. } shader_data;
  378.  
  379. static void
  380. Render(unsigned int width, unsigned int height, shader_data* data)
  381. {
  382.     float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
  383.  
  384.     /*
  385.     * Do some rotation with Euler angles. It is not a fixed axis as
  386.     * quaterions would be, but the effect is cool.
  387.     */
  388.     rotate_matrix((float)data->angle_x, 1.0f, 0.0f, 0.0f, matrix_modelview);
  389.     rotate_matrix((float)data->angle_y, 0.0f, 1.0f, 0.0f, matrix_rotate);
  390.  
  391.     multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
  392.  
  393.     rotate_matrix((float)data->angle_z, 0.0f, 1.0f, 0.0f, matrix_rotate);
  394.  
  395.     multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
  396.  
  397.     /* Pull the camera back from the cube */
  398.     matrix_modelview[14] -= 2.5;
  399.  
  400.     perspective_matrix(45.0f, (float)width / height, 0.01f, 100.0f, matrix_perspective);
  401.     multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
  402.  
  403.     GL_CHECK(ctx.glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
  404.  
  405.     data->angle_x += 3;
  406.     data->angle_y += 2;
  407.     data->angle_z += 1;
  408.  
  409.     if (data->angle_x >= 360) data->angle_x -= 360;
  410.     if (data->angle_x < 0) data->angle_x += 360;
  411.     if (data->angle_y >= 360) data->angle_y -= 360;
  412.     if (data->angle_y < 0) data->angle_y += 360;
  413.     if (data->angle_z >= 360) data->angle_z -= 360;
  414.     if (data->angle_z < 0) data->angle_z += 360;
  415.  
  416.     GL_CHECK(ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
  417.     GL_CHECK(ctx.glDrawArrays(GL_TRIANGLES, 0, 36));
  418. }
  419.  
  420. int done;
  421. Uint32 frames;
  422. shader_data *datas;
  423. SDL_Rect texr;
  424. SDL_Renderer *imgRenderer;
  425. void loop()
  426. {
  427.     SDL_Event event;
  428.     int i;
  429.     int status;
  430.  
  431.     /* Check for events */
  432.     ++frames;
  433.     /** Prepare textures */
  434.     static SDL_Texture *img = NULL;
  435.     static int w, h; // texture width & height
  436.               // load our image
  437.    
  438.     if (img == NULL) {
  439.         SDL_Log("Trying to load the image...");
  440.        
  441.         img = IMG_LoadTexture(imgRenderer, "background.jpg");
  442.        
  443.  
  444.         if (img != NULL)
  445.         {
  446.             SDL_Log("Image loaded ok %s\n", "background.jpg");
  447.             SDL_QueryTexture(img, NULL, NULL, &w, &h); // get the width and height of the texture
  448.                                                        // put the location where we want the texture to be drawn into a rectangle
  449.                                                        // I'm also scaling the texture 2x simply by setting the width and height
  450.             texr.x = w/2; texr.y = h/2; texr.w = w; texr.h = h;
  451.         }
  452.         else
  453.         {
  454.             SDL_Log("Error loading Image: %s\n", "background.jpg");
  455.             SDL_Log("SDL Error: %s", SDL_GetError());
  456.            
  457.         }
  458.     }
  459.    
  460.  
  461.     while (SDL_PollEvent(&event) && !done) {
  462.         switch (event.type) {
  463.         case SDL_WINDOWEVENT:
  464.             switch (event.window.event) {
  465.             case SDL_WINDOWEVENT_RESIZED:
  466.                 for (i = 0; i < state->num_windows; ++i) {
  467.                     if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
  468.                         int w, h;
  469.                         status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  470.                         if (status) {
  471.                             SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  472.                             break;
  473.                         }
  474.                         /* Change view port to the new window dimensions */
  475.                         SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
  476.                         ctx.glViewport(0, 0, w, h);
  477.                         state->window_w = event.window.data1;
  478.                         state->window_h = event.window.data2;
  479.                         /* Update window content */
  480.                         Render(event.window.data1, event.window.data2, &datas[i]);
  481.                        
  482.                         break;
  483.                     }
  484.                 }
  485.                 break;
  486.             }
  487.         }
  488.         SDLTest_CommonEvent(state, &event, &done);
  489.     }
  490.     if (!done) {
  491.         for (i = 0; i < state->num_windows; ++i) {
  492.             status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  493.             if (status) {
  494.                 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  495.  
  496.                 /* Continue for next window */
  497.                 continue;
  498.             }
  499.             //Render(state->window_w, state->window_h, &datas[i]);
  500.             //SDL_GL_SwapWindow(state->windows[i]);
  501.            
  502.             // clear the screen
  503.             SDL_RenderClear(state->renderers[i]);
  504.             SDL_RenderClear(imgRenderer);
  505.             // copy the texture to the rendering context
  506.             SDL_RenderCopy(imgRenderer, img, NULL, &texr);
  507.             SDL_RenderCopy(state->renderers[i], img, NULL, &texr);
  508.             SDL_GL_SwapWindow(state->windows[i]);
  509.             break;
  510.         }
  511.     }
  512. #ifdef __EMSCRIPTEN__
  513.     else {
  514.         emscripten_cancel_main_loop();
  515.     }
  516. #endif
  517. }
  518.  
  519. int
  520. main(int argc, char *argv[])
  521. {
  522.     int fsaa, accel;
  523.     int value;
  524.     int i;
  525.     SDL_DisplayMode mode;
  526.     Uint32 then, now;
  527.     int status;
  528.     shader_data *data;
  529.  
  530.     /* Initialize parameters */
  531.     fsaa = 0;
  532.     accel = 0;
  533.  
  534.     /* Initialize test framework */
  535.     //SDL_Init(SDL_INIT_VIDEO);
  536.     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  537.     if (!state) {
  538.         return 1;
  539.     }
  540.  
  541.     int flags = IMG_INIT_JPG;// | IMG_INIT_PNG;
  542.     if (IMG_Init(flags) != flags)
  543.     {
  544.         SDL_Log("Could not initialize IMG JPG");
  545.         SDL_Log("IMG Load error: %s", SDL_GetError());
  546.     }
  547.  
  548.     for (i = 1; i < argc;) {
  549.         int consumed;
  550.  
  551.         consumed = SDLTest_CommonArg(state, i);
  552.         if (consumed == 0) {
  553.             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
  554.                 ++fsaa;
  555.                 consumed = 1;
  556.             }
  557.             else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
  558.                 ++accel;
  559.                 consumed = 1;
  560.             }
  561.             else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
  562.                 i++;
  563.                 if (!argv[i]) {
  564.                     consumed = -1;
  565.                 }
  566.                 else {
  567.                     depth = SDL_atoi(argv[i]);
  568.                     consumed = 1;
  569.                 }
  570.             }
  571.             else {
  572.                 consumed = -1;
  573.             }
  574.         }
  575.         if (consumed < 0) {
  576.             SDL_Log("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
  577.                 SDLTest_CommonUsage(state));
  578.             quit(1);
  579.         }
  580.         i += consumed;
  581.     }
  582.  
  583.     /* Set OpenGL parameters */
  584.     state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
  585.     state->gl_red_size = 5;
  586.     state->gl_green_size = 5;
  587.     state->gl_blue_size = 5;
  588.     state->gl_depth_size = depth;
  589.     state->gl_major_version = 2;
  590.     state->gl_minor_version = 0;
  591.     state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
  592.  
  593.     if (fsaa) {
  594.         state->gl_multisamplebuffers = 1;
  595.         state->gl_multisamplesamples = fsaa;
  596.     }
  597.     if (accel) {
  598.         state->gl_accelerated = 1;
  599.     }
  600.     if (!SDLTest_CommonInit(state)) {
  601.         quit(2);
  602.         return 0;
  603.     }
  604.  
  605.     context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(context));
  606.     if (context == NULL) {
  607.         SDL_Log("Out of memory!\n");
  608.         quit(2);
  609.     }
  610.  
  611.     /* Create OpenGL ES contexts */
  612.     for (i = 0; i < state->num_windows; i++) {
  613.         context[i] = SDL_GL_CreateContext(state->windows[i]);
  614.         if (!context[i]) {
  615.             SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
  616.             quit(2);
  617.         }
  618.     }
  619.     //create image renderer
  620.     imgRenderer = SDL_CreateRenderer(state->windows[0], -1, SDL_RENDERER_ACCELERATED);
  621.  
  622.     /* Important: call this *after* creating the context */
  623.     if (LoadContext(&ctx) < 0) {
  624.         SDL_Log("Could not load GLES2 functions\n");
  625.         quit(2);
  626.         return 0;
  627.     }
  628.  
  629.  
  630.  
  631.     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
  632.         SDL_GL_SetSwapInterval(1);
  633.     }
  634.     else {
  635.         SDL_GL_SetSwapInterval(0);
  636.     }
  637.  
  638.     SDL_GetCurrentDisplayMode(0, &mode);
  639.     SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
  640.     SDL_Log("\n");
  641.     SDL_Log("Vendor     : %s\n", ctx.glGetString(GL_VENDOR));
  642.     SDL_Log("Renderer   : %s\n", ctx.glGetString(GL_RENDERER));
  643.     SDL_Log("Version    : %s\n", ctx.glGetString(GL_VERSION));
  644.     SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
  645.     SDL_Log("\n");
  646.  
  647.     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
  648.     if (!status) {
  649.         SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
  650.     }
  651.     else {
  652.         SDL_Log("Failed to get SDL_GL_RED_SIZE: %s\n",
  653.             SDL_GetError());
  654.     }
  655.     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
  656.     if (!status) {
  657.         SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
  658.     }
  659.     else {
  660.         SDL_Log("Failed to get SDL_GL_GREEN_SIZE: %s\n",
  661.             SDL_GetError());
  662.     }
  663.     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
  664.     if (!status) {
  665.         SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
  666.     }
  667.     else {
  668.         SDL_Log("Failed to get SDL_GL_BLUE_SIZE: %s\n",
  669.             SDL_GetError());
  670.     }
  671.     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
  672.     if (!status) {
  673.         SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
  674.     }
  675.     else {
  676.         SDL_Log("Failed to get SDL_GL_DEPTH_SIZE: %s\n",
  677.             SDL_GetError());
  678.     }
  679.     if (fsaa) {
  680.         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
  681.         if (!status) {
  682.             SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
  683.         }
  684.         else {
  685.             SDL_Log("Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
  686.                 SDL_GetError());
  687.         }
  688.         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
  689.         if (!status) {
  690.             SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
  691.                 value);
  692.         }
  693.         else {
  694.             SDL_Log("Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
  695.                 SDL_GetError());
  696.         }
  697.     }
  698.     if (accel) {
  699.         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
  700.         if (!status) {
  701.             SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
  702.         }
  703.         else {
  704.             SDL_Log("Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
  705.                 SDL_GetError());
  706.         }
  707.     }
  708.  
  709.     datas = (shader_data *)SDL_calloc(state->num_windows, sizeof(shader_data));
  710.  
  711.     /* Set rendering settings for each context */
  712.     for (i = 0; i < state->num_windows; ++i) {
  713.  
  714.         int w, h;
  715.         status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  716.         if (status) {
  717.             SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  718.  
  719.             /* Continue for next window */
  720.             continue;
  721.         }
  722.         SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
  723.         ctx.glViewport(0, 0, w, h);
  724.  
  725.         data = &datas[i];
  726.         data->angle_x = 0; data->angle_y = 0; data->angle_z = 0;
  727.  
  728.         /* Shader Initialization */
  729.         process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER);
  730.         process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER);
  731.  
  732.         /* Create shader_program (ready to attach shaders) */
  733.         data->shader_program = GL_CHECK(ctx.glCreateProgram());
  734.  
  735.         /* Attach shaders and link shader_program */
  736.         GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
  737.         GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
  738.         GL_CHECK(ctx.glLinkProgram(data->shader_program));
  739.  
  740.         /* Get attribute locations of non-fixed attributes like color and texture coordinates. */
  741.         data->attr_position = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av4position"));
  742.         data->attr_color = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av3color"));
  743.  
  744.         /* Get uniform locations */
  745.         data->attr_mvp = GL_CHECK(ctx.glGetUniformLocation(data->shader_program, "mvp"));
  746.  
  747.         GL_CHECK(ctx.glUseProgram(data->shader_program));
  748.  
  749.         /* Enable attributes for position, color and texture coordinates etc. */
  750.         GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_position));
  751.         GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_color));
  752.  
  753.         /* Populate attributes for position, color and texture coordinates etc. */
  754.         GL_CHECK(ctx.glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, _vertices));
  755.         GL_CHECK(ctx.glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, _colors));
  756.  
  757.         GL_CHECK(ctx.glEnable(GL_CULL_FACE));
  758.         GL_CHECK(ctx.glEnable(GL_DEPTH_TEST));
  759.     }
  760.    
  761.    
  762.     /* Main render loop */
  763.     frames = 0;
  764.     then = SDL_GetTicks();
  765.     done = 0;
  766.  
  767.     /** Get ready the assets manager from jni */
  768.     /*
  769.     if(asMgr!=NULL)
  770.     {
  771.        
  772.     }
  773.     AAssetDir* assetDir = AAssetManager_openDir(asMgr, "");
  774.     const char* filename = (const char*)NULL;
  775.     while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) {
  776.         AAsset* asset = AAssetManager_open(asMgr, filename, AASSET_MODE_STREAMING);
  777.         char buf[BUFSIZ];
  778.         int nb_read = 0;
  779.         FILE* out = fopen(filename, "r");
  780.         while ((nb_read = AAsset_read(asset, buf, BUFSIZ)) > 0)
  781.             fwrite(buf, nb_read, 1, out);
  782.         fclose(out);
  783.         AAsset_close(asset);
  784.     }
  785.     AAssetDir_close(assetDir);
  786.     */
  787.  
  788.  
  789. #ifdef __EMSCRIPTEN__
  790.     emscripten_set_main_loop(loop, 0, 1);
  791. #else
  792.     while (!done) {
  793.         loop();
  794.     }
  795. #endif
  796.  
  797.     /* Print out some timing information */
  798.     now = SDL_GetTicks();
  799.     if (now > then) {
  800.         SDL_Log("%2.2f frames per second\n",
  801.             ((double)frames * 1000) / (now - then));
  802.     }
  803. #if !defined(__ANDROID__) && !defined(__NACL__)  
  804.     quit(0);
  805. #endif    
  806.     return 0;
  807. }
  808.  
  809. #else /* HAVE_OPENGLES2 */
  810.  
  811. int
  812. main(int argc, char *argv[])
  813. {
  814.     SDL_Log("No OpenGL ES support on this system\n");
  815.     return 1;
  816. }
  817.  
  818. #endif /* HAVE_OPENGLES2 */
  819.  
  820. /* vi: set ts=4 sw=4 expandtab: */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement