src/render/opengles/SDL_render_gles.c
changeset 5355 cc2215cda170
parent 5333 5d3a2cd4e8c9
child 5402 5d102cd8aef3
equal deleted inserted replaced
5354:a606515be168 5355:cc2215cda170
    82 };
    82 };
    83 
    83 
    84 typedef struct
    84 typedef struct
    85 {
    85 {
    86     SDL_GLContext context;
    86     SDL_GLContext context;
    87     int blendMode;
    87     struct {
       
    88         Uint32 color;
       
    89         int blendMode;
       
    90         GLenum scaleMode;
       
    91         SDL_bool tex_coords;
       
    92     } current;
    88 
    93 
    89     SDL_bool useDrawTexture;
    94     SDL_bool useDrawTexture;
    90     SDL_bool GL_OES_draw_texture_supported;
    95     SDL_bool GL_OES_draw_texture_supported;
    91 } GLES_RenderData;
    96 } GLES_RenderData;
    92 
    97 
    98     GLfloat texh;
   103     GLfloat texh;
    99     GLenum format;
   104     GLenum format;
   100     GLenum formattype;
   105     GLenum formattype;
   101     void *pixels;
   106     void *pixels;
   102     int pitch;
   107     int pitch;
       
   108     GLenum scaleMode;
   103 } GLES_TextureData;
   109 } GLES_TextureData;
   104 
   110 
   105 static void
   111 static void
   106 GLES_SetError(const char *prefix, GLenum result)
   112 GLES_SetError(const char *prefix, GLenum result)
   107 {
   113 {
   150         SDL_CurrentContext = data->context;
   156         SDL_CurrentContext = data->context;
   151 
   157 
   152         GLES_UpdateViewport(renderer);
   158         GLES_UpdateViewport(renderer);
   153     }
   159     }
   154     return 0;
   160     return 0;
       
   161 }
       
   162 
       
   163 /* This is called if we need to invalidate all of the SDL OpenGL state */
       
   164 static void
       
   165 GLES_ResetState(SDL_Renderer *renderer)
       
   166 {
       
   167     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
       
   168 
       
   169     if (SDL_CurrentContext == data->context) {
       
   170         GLES_UpdateViewport(renderer);
       
   171     } else {
       
   172         GLES_ActivateRenderer(renderer);
       
   173     }
       
   174 
       
   175     data->current.color = 0;
       
   176     data->current.blendMode = -1;
       
   177     data->current.scaleMode = 0;
       
   178     data->current.tex_coords = SDL_FALSE;
       
   179 
       
   180     glDisable(GL_DEPTH_TEST);
       
   181     glDisable(GL_CULL_FACE);
       
   182 
       
   183     glMatrixMode(GL_MODELVIEW);
       
   184     glLoadIdentity();
       
   185 
       
   186     glEnableClientState(GL_VERTEX_ARRAY);
       
   187     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   155 }
   188 }
   156 
   189 
   157 SDL_Renderer *
   190 SDL_Renderer *
   158 GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
   191 GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
   159 {
   192 {
   232     renderer->info.max_texture_width = value;
   265     renderer->info.max_texture_width = value;
   233     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   266     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   234     renderer->info.max_texture_height = value;
   267     renderer->info.max_texture_height = value;
   235 
   268 
   236     /* Set up parameters for rendering */
   269     /* Set up parameters for rendering */
   237     data->blendMode = -1;
   270     GLES_ResetState(renderer);
   238     glDisable(GL_DEPTH_TEST);
   271 
   239     glDisable(GL_CULL_FACE);
       
   240     glMatrixMode(GL_MODELVIEW);
       
   241     glLoadIdentity();
       
   242 
       
   243     glEnableClientState(GL_VERTEX_ARRAY);
       
   244     //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
       
   245     
       
   246     return renderer;
   272     return renderer;
   247 }
   273 }
   248 
   274 
   249 static void
   275 static void
   250 GLES_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   276 GLES_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   317     data->texw = (GLfloat) texture->w / texture_w;
   343     data->texw = (GLfloat) texture->w / texture_w;
   318     data->texh = (GLfloat) texture->h / texture_h;
   344     data->texh = (GLfloat) texture->h / texture_h;
   319 
   345 
   320     data->format = format;
   346     data->format = format;
   321     data->formattype = type;
   347     data->formattype = type;
       
   348     data->scaleMode = GL_LINEAR;
   322     glBindTexture(data->type, data->texture);
   349     glBindTexture(data->type, data->texture);
   323     glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   350     glTexParameteri(data->type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   324                                 GL_LINEAR);
   351     glTexParameteri(data->type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   325     glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
       
   326                                 GL_LINEAR);
       
   327     glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
       
   328                                 GL_CLAMP_TO_EDGE);
       
   329     glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
       
   330                                 GL_CLAMP_TO_EDGE);
       
   331 
   352 
   332     glTexImage2D(data->type, 0, internalFormat, texture_w,
   353     glTexImage2D(data->type, 0, internalFormat, texture_w,
   333                              texture_h, 0, format, type, NULL);
   354                              texture_h, 0, format, type, NULL);
   334     glDisable(GL_TEXTURE_2D);
   355     glDisable(GL_TEXTURE_2D);
   335 
   356 
   451              (GLfloat) renderer->viewport.h,
   472              (GLfloat) renderer->viewport.h,
   452              (GLfloat) 0, 0.0, 1.0);
   473              (GLfloat) 0, 0.0, 1.0);
   453     return 0;
   474     return 0;
   454 }
   475 }
   455 
   476 
   456 static int
   477 static void
   457 GLES_RenderClear(SDL_Renderer * renderer)
   478 GLES_SetColor(GLES_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   458 {
   479 {
   459     GLES_ActivateRenderer(renderer);
   480     Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
   460 
   481 
   461     glClearColor((GLfloat) renderer->r * inv255f,
   482     if (color != data->current.color) {
   462                  (GLfloat) renderer->g * inv255f,
   483         glColor4f((GLfloat) r * inv255f,
   463                  (GLfloat) renderer->b * inv255f,
   484                         (GLfloat) g * inv255f,
   464                  (GLfloat) renderer->a * inv255f);
   485                         (GLfloat) b * inv255f,
   465 
   486                         (GLfloat) a * inv255f);
   466     glClear(GL_COLOR_BUFFER_BIT);
   487         data->current.color = color;
   467 
   488     }
   468     return 0;
       
   469 }
   489 }
   470 
   490 
   471 static void
   491 static void
   472 GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
   492 GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
   473 {
   493 {
   474     if (blendMode != data->blendMode) {
   494     if (blendMode != data->current.blendMode) {
   475         switch (blendMode) {
   495         switch (blendMode) {
   476         case SDL_BLENDMODE_NONE:
   496         case SDL_BLENDMODE_NONE:
   477             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   497             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   478             glDisable(GL_BLEND);
   498             glDisable(GL_BLEND);
   479             break;
   499             break;
   491             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   511             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   492             glEnable(GL_BLEND);
   512             glEnable(GL_BLEND);
   493             glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   513             glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   494             break;
   514             break;
   495         }
   515         }
   496         data->blendMode = blendMode;
   516         data->current.blendMode = blendMode;
   497     }
   517     }
       
   518 }
       
   519 
       
   520 static void
       
   521 GLES_SetTexCoords(GLES_RenderData * data, SDL_bool enabled)
       
   522 {
       
   523     if (enabled != data->current.tex_coords) {
       
   524         if (enabled) {
       
   525             glEnableClientState(GL_TEXTURE_COORD_ARRAY);
       
   526         } else {
       
   527             glDisableClientState(GL_TEXTURE_COORD_ARRAY);
       
   528         }
       
   529         data->current.tex_coords = enabled;
       
   530     }
       
   531 }
       
   532 
       
   533 static void
       
   534 GLES_SetDrawingState(SDL_Renderer * renderer)
       
   535 {
       
   536     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
       
   537 
       
   538     GLES_ActivateRenderer(renderer);
       
   539 
       
   540     GLES_SetColor(data, (GLfloat) renderer->r,
       
   541                         (GLfloat) renderer->g,
       
   542                         (GLfloat) renderer->b,
       
   543                         (GLfloat) renderer->a);
       
   544 
       
   545     GLES_SetBlendMode(data, renderer->blendMode);
       
   546 
       
   547     GLES_SetTexCoords(data, SDL_FALSE);
       
   548 }
       
   549 
       
   550 static int
       
   551 GLES_RenderClear(SDL_Renderer * renderer)
       
   552 {
       
   553     GLES_ActivateRenderer(renderer);
       
   554 
       
   555     glClearColor((GLfloat) renderer->r * inv255f,
       
   556                  (GLfloat) renderer->g * inv255f,
       
   557                  (GLfloat) renderer->b * inv255f,
       
   558                  (GLfloat) renderer->a * inv255f);
       
   559 
       
   560     glClear(GL_COLOR_BUFFER_BIT);
       
   561 
       
   562     return 0;
   498 }
   563 }
   499 
   564 
   500 static int
   565 static int
   501 GLES_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   566 GLES_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   502                       int count)
   567                       int count)
   503 {
   568 {
   504     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   569     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   505     int i;
   570     int i;
   506     GLshort *vertices;
   571     GLshort *vertices;
   507 
   572 
   508     GLES_ActivateRenderer(renderer);
   573     GLES_SetDrawingState(renderer);
   509 
       
   510     GLES_SetBlendMode(data, renderer->blendMode);
       
   511 
       
   512     glColor4f((GLfloat) renderer->r * inv255f,
       
   513                     (GLfloat) renderer->g * inv255f,
       
   514                     (GLfloat) renderer->b * inv255f,
       
   515                     (GLfloat) renderer->a * inv255f);
       
   516 
   574 
   517     vertices = SDL_stack_alloc(GLshort, count*2);
   575     vertices = SDL_stack_alloc(GLshort, count*2);
   518     for (i = 0; i < count; ++i) {
   576     for (i = 0; i < count; ++i) {
   519         vertices[2*i+0] = (GLshort)points[i].x;
   577         vertices[2*i+0] = (GLshort)points[i].x;
   520         vertices[2*i+1] = (GLshort)points[i].y;
   578         vertices[2*i+1] = (GLshort)points[i].y;
   532 {
   590 {
   533     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   591     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   534     int i;
   592     int i;
   535     GLshort *vertices;
   593     GLshort *vertices;
   536 
   594 
   537     GLES_ActivateRenderer(renderer);
   595     GLES_SetDrawingState(renderer);
   538 
       
   539     GLES_SetBlendMode(data, renderer->blendMode);
       
   540 
       
   541     glColor4f((GLfloat) renderer->r * inv255f,
       
   542                     (GLfloat) renderer->g * inv255f,
       
   543                     (GLfloat) renderer->b * inv255f,
       
   544                     (GLfloat) renderer->a * inv255f);
       
   545 
   596 
   546     vertices = SDL_stack_alloc(GLshort, count*2);
   597     vertices = SDL_stack_alloc(GLshort, count*2);
   547     for (i = 0; i < count; ++i) {
   598     for (i = 0; i < count; ++i) {
   548         vertices[2*i+0] = (GLshort)points[i].x;
   599         vertices[2*i+0] = (GLshort)points[i].x;
   549         vertices[2*i+1] = (GLshort)points[i].y;
   600         vertices[2*i+1] = (GLshort)points[i].y;
   567                      int count)
   618                      int count)
   568 {
   619 {
   569     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   620     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   570     int i;
   621     int i;
   571 
   622 
   572     GLES_ActivateRenderer(renderer);
   623     GLES_SetDrawingState(renderer);
   573 
       
   574     GLES_SetBlendMode(data, renderer->blendMode);
       
   575 
       
   576     glColor4f((GLfloat) renderer->r * inv255f,
       
   577                     (GLfloat) renderer->g * inv255f,
       
   578                     (GLfloat) renderer->b * inv255f,
       
   579                     (GLfloat) renderer->a * inv255f);
       
   580 
   624 
   581     for (i = 0; i < count; ++i) {
   625     for (i = 0; i < count; ++i) {
   582         const SDL_Rect *rect = &rects[i];
   626         const SDL_Rect *rect = &rects[i];
   583         GLshort minx = rect->x;
   627         GLshort minx = rect->x;
   584         GLshort maxx = rect->x + rect->w;
   628         GLshort maxx = rect->x + rect->w;
   612     GLfloat minu, maxu, minv, maxv;
   656     GLfloat minu, maxu, minv, maxv;
   613 
   657 
   614     GLES_ActivateRenderer(renderer);
   658     GLES_ActivateRenderer(renderer);
   615 
   659 
   616     glEnable(GL_TEXTURE_2D);
   660     glEnable(GL_TEXTURE_2D);
   617     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
       
   618 
   661 
   619     glBindTexture(texturedata->type, texturedata->texture);
   662     glBindTexture(texturedata->type, texturedata->texture);
   620 
   663 
       
   664     if (texturedata->scaleMode != data->current.scaleMode) {
       
   665         glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
       
   666                         texturedata->scaleMode);
       
   667         glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
       
   668                         texturedata->scaleMode);
       
   669         data->current.scaleMode = texturedata->scaleMode;
       
   670     }
       
   671 
   621     if (texture->modMode) {
   672     if (texture->modMode) {
   622         glColor4f((GLfloat) texture->r * inv255f,
   673         GLES_SetColor(data, texture->r, texture->g, texture->b, texture->a);
   623                         (GLfloat) texture->g * inv255f,
       
   624                         (GLfloat) texture->b * inv255f,
       
   625                         (GLfloat) texture->a * inv255f);
       
   626     } else {
   674     } else {
   627         glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   675         GLES_SetColor(data, 255, 255, 255, 255);
   628     }
   676     }
   629 
   677 
   630     GLES_SetBlendMode(data, texture->blendMode);
   678     GLES_SetBlendMode(data, texture->blendMode);
       
   679 
       
   680     GLES_SetTexCoords(data, SDL_TRUE);
   631 
   681 
   632     if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   682     if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   633         /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   683         /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   634         GLint cropRect[4];
   684         GLint cropRect[4];
   635         int w, h;
   685         int w, h;
   683 
   733 
   684         glVertexPointer(2, GL_SHORT, 0, vertices);
   734         glVertexPointer(2, GL_SHORT, 0, vertices);
   685         glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   735         glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   686         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   736         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   687     }
   737     }
   688 	
       
   689     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
       
   690     glDisable(GL_TEXTURE_2D);
   738     glDisable(GL_TEXTURE_2D);
   691 
   739 
   692     return 0;
   740     return 0;
   693 }
   741 }
   694 
   742