src/render/opengl/SDL_render_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 19 Feb 2011 21:51:21 -0800
changeset 5355 cc2215cda170
parent 5351 176cd91c3907
child 5381 79e60256dbf7
permissions -rw-r--r--
Added OpenGL state caching for decent speed improvement.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
5262
b530ef003506 Happy 2011! :)
Sam Lantinga <slouken@libsdl.org>
parents: 5233
diff changeset
     3
    Copyright (C) 1997-2011 Sam Lantinga
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
    Lesser General Public License for more details.
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
    License along with this library; if not, write to the Free Software
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
    Sam Lantinga
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
    slouken@libsdl.org
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
*/
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
#include "SDL_config.h"
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
5226
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
    24
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
    25
5233
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
    26
#include "SDL_hints.h"
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
    27
#include "SDL_log.h"
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
    28
#include "SDL_opengl.h"
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
    29
#include "../SDL_sysrender.h"
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
    30
#include "SDL_shaders_gl.h"
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
2246
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
    32
#ifdef __MACOSX__
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
    33
#include <OpenGL/OpenGL.h>
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
    34
#endif
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
    35
2778
38dfc890ee6b Preliminary support for YUV textures
Sam Lantinga <slouken@libsdl.org>
parents: 2328
diff changeset
    36
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
/* OpenGL renderer implementation */
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    39
/* Details on optimizing the texture path on Mac OS X:
5203
25ffd4e5255c Minor consistency cleanup and documentation link update.
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
    40
   http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    41
*/
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    42
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
    43
/* Used to re-create the window with OpenGL capability */
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
    44
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
    45
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    46
static const float inv255f = 1.0f / 255.0f;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    47
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
    49
static void GL_WindowEvent(SDL_Renderer * renderer,
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
    50
                           const SDL_WindowEvent *event);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
                            const SDL_Rect * rect, const void *pixels,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
                            int pitch);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    56
                          const SDL_Rect * rect, void **pixels, int *pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
    58
static int GL_UpdateViewport(SDL_Renderer * renderer);
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    59
static int GL_RenderClear(SDL_Renderer * renderer);
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    60
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    61
                               const SDL_Point * points, int count);
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    62
static int GL_RenderDrawLines(SDL_Renderer * renderer,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    63
                              const SDL_Point * points, int count);
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    64
static int GL_RenderFillRects(SDL_Renderer * renderer,
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
    65
                              const SDL_Rect * rects, int count);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    67
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
3431
c8fbb27627d9 Work in progress on OpenGL ReadPixels/WritePixels interface
Sam Lantinga <slouken@libsdl.org>
parents: 3393
diff changeset
    68
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
    69
                               Uint32 pixel_format, void * pixels, int pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
static void GL_RenderPresent(SDL_Renderer * renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
static void GL_DestroyRenderer(SDL_Renderer * renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
SDL_RenderDriver GL_RenderDriver = {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
    GL_CreateRenderer,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
    {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
     "opengl",
5203
25ffd4e5255c Minor consistency cleanup and documentation link update.
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
    79
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    80
     1,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    81
     {SDL_PIXELFORMAT_ARGB8888},
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
     0,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
     0}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
};
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
typedef struct
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
    SDL_GLContext context;
2233
fb01ee9716bc Test using glTextureRangeAPPLE
Sam Lantinga <slouken@libsdl.org>
parents: 2230
diff changeset
    89
    SDL_bool GL_ARB_texture_rectangle_supported;
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
    90
    struct {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
    91
        GL_Shader shader;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
    92
        Uint32 color;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
    93
        int blendMode;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
    94
        GLenum scaleMode;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
    95
    } current;
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
    96
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
    97
    /* OpenGL functions */
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
    98
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
5204
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
    99
#include "SDL_glfuncs.h"
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   100
#undef SDL_PROC
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   101
2233
fb01ee9716bc Test using glTextureRangeAPPLE
Sam Lantinga <slouken@libsdl.org>
parents: 2230
diff changeset
   102
    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
fb01ee9716bc Test using glTextureRangeAPPLE
Sam Lantinga <slouken@libsdl.org>
parents: 2230
diff changeset
   103
                                 const GLvoid * pointer);
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   104
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   105
    /* Multitexture support */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   106
    SDL_bool GL_ARB_multitexture_supported;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   107
    PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
5351
176cd91c3907 Fixed warning on Mac OS X 10.4
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
   108
    GLint num_texture_units;
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   109
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   110
    /* Shader support */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   111
    GL_ShaderContext *shaders;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   112
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
} GL_RenderData;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
typedef struct
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
    GLuint texture;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   118
    GLenum type;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
    GLfloat texw;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
    GLfloat texh;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   121
    GLenum format;
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   122
    GLenum formattype;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
    void *pixels;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    int pitch;
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   125
    int scaleMode;
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   126
    SDL_Rect locked_rect;
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   127
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   128
    /* YV12 texture support */
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   129
    SDL_bool yuv;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   130
    GLuint utexture;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   131
    GLuint vtexture;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
} GL_TextureData;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   134
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   135
static void
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   136
GL_SetError(const char *prefix, GLenum result)
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   137
{
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   138
    const char *error;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   139
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   140
    switch (result) {
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   141
    case GL_NO_ERROR:
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   142
        error = "GL_NO_ERROR";
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   143
        break;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   144
    case GL_INVALID_ENUM:
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   145
        error = "GL_INVALID_ENUM";
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   146
        break;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   147
    case GL_INVALID_VALUE:
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   148
        error = "GL_INVALID_VALUE";
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   149
        break;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   150
    case GL_INVALID_OPERATION:
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   151
        error = "GL_INVALID_OPERATION";
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   152
        break;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   153
    case GL_STACK_OVERFLOW:
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   154
        error = "GL_STACK_OVERFLOW";
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   155
        break;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   156
    case GL_STACK_UNDERFLOW:
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   157
        error = "GL_STACK_UNDERFLOW";
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   158
        break;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   159
    case GL_OUT_OF_MEMORY:
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   160
        error = "GL_OUT_OF_MEMORY";
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   161
        break;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   162
    case GL_TABLE_TOO_LARGE:
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   163
        error = "GL_TABLE_TOO_LARGE";
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   164
        break;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   165
    default:
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   166
        error = "UNKNOWN";
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   167
        break;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   168
    }
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   169
    SDL_SetError("%s: %s", prefix, error);
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   170
}
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   171
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   172
static int
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   173
GL_LoadFunctions(GL_RenderData * data)
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   174
{
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   175
#ifdef __SDL_NOGETPROCADDR__
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   176
#define SDL_PROC(ret,func,params) data->func=func;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   177
#else
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   178
#define SDL_PROC(ret,func,params) \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   179
    do { \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   180
        data->func = SDL_GL_GetProcAddress(#func); \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   181
        if ( ! data->func ) { \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   182
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   183
            return -1; \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   184
        } \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   185
    } while ( 0 );
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   186
#endif /* __SDL_NOGETPROCADDR__ */
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   187
5204
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   188
#include "SDL_glfuncs.h"
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   189
#undef SDL_PROC
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   190
    return 0;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   191
}
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   192
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   193
static SDL_GLContext SDL_CurrentContext = NULL;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   194
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   195
static int
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   196
GL_ActivateRenderer(SDL_Renderer * renderer)
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   197
{
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   198
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   199
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   200
    if (SDL_CurrentContext != data->context) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   201
        if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   202
            return -1;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   203
        }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   204
        SDL_CurrentContext = data->context;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   205
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   206
        GL_UpdateViewport(renderer);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   207
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   208
    return 0;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   209
}
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   210
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   211
/* This is called if we need to invalidate all of the SDL OpenGL state */
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   212
static void
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   213
GL_ResetState(SDL_Renderer *renderer)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   214
{
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   215
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   216
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   217
    if (SDL_CurrentContext == data->context) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   218
        GL_UpdateViewport(renderer);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   219
    } else {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   220
        GL_ActivateRenderer(renderer);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   221
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   222
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   223
    data->current.shader = SHADER_NONE;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   224
    data->current.color = 0;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   225
    data->current.blendMode = -1;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   226
    data->current.scaleMode = 0;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   227
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   228
    data->glDisable(GL_DEPTH_TEST);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   229
    data->glDisable(GL_CULL_FACE);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   230
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   231
    /*data->glEnable(GL_LINE_SMOOTH);*/
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   232
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   233
    data->glMatrixMode(GL_MODELVIEW);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   234
    data->glLoadIdentity();
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   235
}
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   236
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
SDL_Renderer *
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
    SDL_Renderer *renderer;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
    GL_RenderData *data;
5233
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   242
    const char *hint;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   243
    GLint value;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   244
    Uint32 window_flags;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   246
    window_flags = SDL_GetWindowFlags(window);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   247
    if (!(window_flags & SDL_WINDOW_OPENGL)) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   248
        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   249
            return NULL;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   250
        }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   253
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
    if (!renderer) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   255
        SDL_OutOfMemory();
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   259
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
    if (!data) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
        GL_DestroyRenderer(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
        SDL_OutOfMemory();
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   266
    renderer->WindowEvent = GL_WindowEvent;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
    renderer->CreateTexture = GL_CreateTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   268
    renderer->UpdateTexture = GL_UpdateTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
    renderer->LockTexture = GL_LockTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
    renderer->UnlockTexture = GL_UnlockTexture;
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   271
    renderer->UpdateViewport = GL_UpdateViewport;
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   272
    renderer->RenderClear = GL_RenderClear;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   273
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   274
    renderer->RenderDrawLines = GL_RenderDrawLines;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   275
    renderer->RenderFillRects = GL_RenderFillRects;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   276
    renderer->RenderCopy = GL_RenderCopy;
3431
c8fbb27627d9 Work in progress on OpenGL ReadPixels/WritePixels interface
Sam Lantinga <slouken@libsdl.org>
parents: 3393
diff changeset
   277
    renderer->RenderReadPixels = GL_RenderReadPixels;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   278
    renderer->RenderPresent = GL_RenderPresent;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   279
    renderer->DestroyTexture = GL_DestroyTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
    renderer->DestroyRenderer = GL_DestroyRenderer;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
    renderer->info = GL_RenderDriver.info;
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   282
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
    renderer->driverdata = data;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3607
diff changeset
   285
    data->context = SDL_GL_CreateContext(window);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
    if (!data->context) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
        GL_DestroyRenderer(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
    }
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3607
diff changeset
   290
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
        GL_DestroyRenderer(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   293
    }
5204
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   294
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   295
    if (GL_LoadFunctions(data) < 0) {
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   296
        GL_DestroyRenderer(renderer);
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   297
        return NULL;
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   298
    }
daa5463466c5 Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
Sam Lantinga <slouken@libsdl.org>
parents: 5203
diff changeset
   299
2246
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   300
#ifdef __MACOSX__
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   301
    /* Enable multi-threaded rendering */
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   302
    /* Disabled until Ryan finishes his VBO/PBO code...
2295
dbc6d1893869 Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
Bob Pendleton <bob@pendleton.com>
parents: 2275
diff changeset
   303
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
dbc6d1893869 Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
Bob Pendleton <bob@pendleton.com>
parents: 2275
diff changeset
   304
     */
2246
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   305
#endif
75daa0792bd1 Added code to enable multi-threaded OpenGL on Mac OS X, pending Ryan's PBO/VBO
Sam Lantinga <slouken@libsdl.org>
parents: 2237
diff changeset
   306
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   307
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   308
        SDL_GL_SetSwapInterval(1);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   309
    } else {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   310
        SDL_GL_SetSwapInterval(0);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   312
    if (SDL_GL_GetSwapInterval() > 0) {
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   313
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   314
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   315
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   316
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   317
    renderer->info.max_texture_width = value;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   318
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   319
    renderer->info.max_texture_height = value;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   320
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   321
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   322
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   323
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   324
    }
2233
fb01ee9716bc Test using glTextureRangeAPPLE
Sam Lantinga <slouken@libsdl.org>
parents: 2230
diff changeset
   325
    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
fb01ee9716bc Test using glTextureRangeAPPLE
Sam Lantinga <slouken@libsdl.org>
parents: 2230
diff changeset
   326
        data->glTextureRangeAPPLE =
fb01ee9716bc Test using glTextureRangeAPPLE
Sam Lantinga <slouken@libsdl.org>
parents: 2230
diff changeset
   327
            (void (*)(GLenum, GLsizei, const GLvoid *))
fb01ee9716bc Test using glTextureRangeAPPLE
Sam Lantinga <slouken@libsdl.org>
parents: 2230
diff changeset
   328
            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
fb01ee9716bc Test using glTextureRangeAPPLE
Sam Lantinga <slouken@libsdl.org>
parents: 2230
diff changeset
   329
    }
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   330
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   331
    /* Check for multitexture support */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   332
    if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   333
        data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   334
        if (data->glActiveTextureARB) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   335
            data->GL_ARB_multitexture_supported = SDL_TRUE;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   336
            data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   337
        }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   338
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   339
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   340
    /* Check for shader support */
5233
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   341
    hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS);
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   342
    if (!hint || *hint != '0') {
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   343
        data->shaders = GL_CreateShaderContext();
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   344
    }
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   345
    SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
ce4f91138031 Added a hint to control whether the OpenGL driver uses shaders.
Sam Lantinga <slouken@libsdl.org>
parents: 5230
diff changeset
   346
                data->shaders ? "ENABLED" : "DISABLED");
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   347
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   348
    /* We support YV12 textures using 3 textures and a shader */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   349
    if (data->shaders && data->num_texture_units >= 3) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   350
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   351
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   352
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents: 5227
diff changeset
   353
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   354
    /* Set up parameters for rendering */
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   355
    GL_ResetState(renderer);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   357
    return renderer;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   358
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   359
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   360
static void
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   361
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
1970
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   362
{
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   363
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   364
5276
8e421890cdb8 Fixed bug #1117
Sam Lantinga <slouken@libsdl.org>
parents: 5265
diff changeset
   365
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   366
        /* Rebind the context to the window area and update matrices */
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   367
        SDL_CurrentContext = NULL;
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   368
    }
1923
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   369
}
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   370
1922
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   371
static __inline__ int
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   372
power_of_2(int input)
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   373
{
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   374
    int value = 1;
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   375
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   376
    while (value < input) {
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   377
        value <<= 1;
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   378
    }
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   379
    return value;
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   380
}
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   381
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   382
static __inline__ SDL_bool
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   383
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   384
               GLint* internalFormat, GLenum* format, GLenum* type)
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   385
{
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   386
    switch (pixel_format) {
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   387
    case SDL_PIXELFORMAT_ARGB8888:
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   388
        *internalFormat = GL_RGBA8;
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   389
        *format = GL_BGRA;
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   390
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   391
        break;
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   392
    case SDL_PIXELFORMAT_YV12:
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   393
    case SDL_PIXELFORMAT_IYUV:
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   394
        *internalFormat = GL_LUMINANCE;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   395
        *format = GL_LUMINANCE;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   396
        *type = GL_UNSIGNED_BYTE;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   397
        break;
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   398
    default:
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   399
        return SDL_FALSE;
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   400
    }
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   401
    return SDL_TRUE;
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   402
}
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   403
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   404
static int
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   405
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   406
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   407
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   408
    GL_TextureData *data;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   409
    GLint internalFormat;
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   410
    GLenum format, type;
1922
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   411
    int texture_w, texture_h;
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   412
    GLenum result;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   413
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   414
    GL_ActivateRenderer(renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   415
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   416
    if (!convert_format(renderdata, texture->format, &internalFormat,
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   417
                        &format, &type)) {
4990
397e748d901a Improved error message for unsupported texture format
Sam Lantinga <slouken@libsdl.org>
parents: 4929
diff changeset
   418
        SDL_SetError("Texture format %s not supported by OpenGL",
397e748d901a Improved error message for unsupported texture format
Sam Lantinga <slouken@libsdl.org>
parents: 4929
diff changeset
   419
                     SDL_GetPixelFormatName(texture->format));
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   420
        return -1;
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   421
    }
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   422
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   423
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   424
    if (!data) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   425
        SDL_OutOfMemory();
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   426
        return -1;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   427
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   428
2222
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   429
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   430
        size_t size;
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   431
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   432
        size = texture->h * data->pitch;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   433
        if (texture->format == SDL_PIXELFORMAT_YV12 ||
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   434
            texture->format == SDL_PIXELFORMAT_IYUV) {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   435
            /* Need to add size for the U and V planes */
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   436
            size += (2 * (texture->h * data->pitch) / 4);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   437
        }
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   438
        data->pixels = SDL_malloc(size);
2222
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   439
        if (!data->pixels) {
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   440
            SDL_OutOfMemory();
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   441
            SDL_free(data);
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   442
            return -1;
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   443
        }
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   444
    }
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   445
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   446
    texture->driverdata = data;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   447
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   448
    renderdata->glGetError();
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   449
    renderdata->glGenTextures(1, &data->texture);
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   450
    if (renderdata->GL_ARB_texture_rectangle_supported) {
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   451
        data->type = GL_TEXTURE_RECTANGLE_ARB;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   452
        texture_w = texture->w;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   453
        texture_h = texture->h;
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   454
        data->texw = (GLfloat) texture_w;
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   455
        data->texh = (GLfloat) texture_h;
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   456
    } else {
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   457
        data->type = GL_TEXTURE_2D;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   458
        texture_w = power_of_2(texture->w);
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   459
        texture_h = power_of_2(texture->h);
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   460
        data->texw = (GLfloat) (texture->w) / texture_w;
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   461
        data->texh = (GLfloat) texture->h / texture_h;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   462
    }
2835
f38257b5d936 Initial pixel shader support for YUV textures in the GL renderer.
Ryan C. Gordon <icculus@icculus.org>
parents: 2833
diff changeset
   463
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   464
    data->format = format;
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   465
    data->formattype = type;
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   466
    data->scaleMode = GL_LINEAR;
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2858
diff changeset
   467
    renderdata->glEnable(data->type);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   468
    renderdata->glBindTexture(data->type, data->texture);
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   469
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   470
                                GL_CLAMP_TO_EDGE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   471
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   472
                                GL_CLAMP_TO_EDGE);
2840
53ca0d758b0a The previous checkin fixes the crash, so at least we're not overrunning
Sam Lantinga <slouken@libsdl.org>
parents: 2839
diff changeset
   473
#ifdef __MACOSX__
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   474
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   475
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   476
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   477
#ifndef STORAGE_CACHED_APPLE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   478
#define STORAGE_CACHED_APPLE                0x85BE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   479
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   480
#ifndef STORAGE_SHARED_APPLE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   481
#define STORAGE_SHARED_APPLE                0x85BF
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   482
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   483
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   484
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   485
                                    GL_STORAGE_SHARED_APPLE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   486
    } else {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   487
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   488
                                    GL_STORAGE_CACHED_APPLE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   489
    }
2295
dbc6d1893869 Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
Bob Pendleton <bob@pendleton.com>
parents: 2275
diff changeset
   490
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
dbc6d1893869 Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
Bob Pendleton <bob@pendleton.com>
parents: 2275
diff changeset
   491
        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   492
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   493
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   494
                                 texture_h, 0, format, type, data->pixels);
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   495
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   496
    else
2809
7e257c3a3bf0 Temporary fix for Mac OS X crash in textoverlay
Sam Lantinga <slouken@libsdl.org>
parents: 2808
diff changeset
   497
#endif
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   498
    {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   499
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   500
                                 texture_h, 0, format, type, NULL);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   501
    }
3041
20d65430e63c Fixed OpenGL state issue reported by Dmytro Bogovych
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   502
    renderdata->glDisable(data->type);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   503
    result = renderdata->glGetError();
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   504
    if (result != GL_NO_ERROR) {
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   505
        GL_SetError("glTexImage2D()", result);
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   506
        return -1;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   507
    }
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   508
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   509
    if (texture->format == SDL_PIXELFORMAT_YV12 ||
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   510
        texture->format == SDL_PIXELFORMAT_IYUV) {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   511
        data->yuv = SDL_TRUE;
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   512
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   513
        renderdata->glGenTextures(1, &data->utexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   514
        renderdata->glGenTextures(1, &data->vtexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   515
        renderdata->glEnable(data->type);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   516
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   517
        renderdata->glBindTexture(data->type, data->utexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   518
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   519
                                    GL_CLAMP_TO_EDGE);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   520
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   521
                                    GL_CLAMP_TO_EDGE);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   522
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   523
                                 texture_h/2, 0, format, type, NULL);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   524
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   525
        renderdata->glBindTexture(data->type, data->vtexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   526
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   527
                                    GL_CLAMP_TO_EDGE);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   528
        renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   529
                                    GL_CLAMP_TO_EDGE);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   530
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   531
                                 texture_h/2, 0, format, type, NULL);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   532
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   533
        renderdata->glDisable(data->type);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   534
    }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   535
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   536
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   537
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   538
static int
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   539
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   540
                 const SDL_Rect * rect, const void *pixels, int pitch)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   541
{
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   542
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   543
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   544
    GLenum result;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   545
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   546
    GL_ActivateRenderer(renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   547
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   548
    renderdata->glGetError();
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   549
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   550
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   551
                              (pitch / SDL_BYTESPERPIXEL(texture->format)));
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2858
diff changeset
   552
    renderdata->glEnable(data->type);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   553
    renderdata->glBindTexture(data->type, data->texture);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   554
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   555
                                rect->h, data->format, data->formattype,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   556
                                pixels);
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   557
    if (data->yuv) {
5265
48724afcdc6e Ah there, that fixed it. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5264
diff changeset
   558
        const void *top;
48724afcdc6e Ah there, that fixed it. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5264
diff changeset
   559
48724afcdc6e Ah there, that fixed it. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5264
diff changeset
   560
        renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
48724afcdc6e Ah there, that fixed it. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5264
diff changeset
   561
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   562
        /* Skip to the top of the next texture */
5265
48724afcdc6e Ah there, that fixed it. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5264
diff changeset
   563
        top = (const void*)((const Uint8*)pixels + (texture->h-rect->y) * pitch - rect->x);
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   564
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   565
        /* Skip to the correct offset into the next texture */
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   566
        pixels = (const void*)((const Uint8*)top + (rect->y / 2) * pitch + rect->x / 2);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   567
        if (texture->format == SDL_PIXELFORMAT_YV12) {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   568
            renderdata->glBindTexture(data->type, data->vtexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   569
        } else {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   570
            renderdata->glBindTexture(data->type, data->utexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   571
        }
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   572
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   573
                                    rect->w/2, rect->h/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   574
                                    data->format, data->formattype, pixels);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   575
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   576
        /* Skip to the top of the next texture */
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   577
        top = (const void*)((const Uint8*)top + (texture->h * pitch)/4);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   578
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   579
        /* Skip to the correct offset into the next texture */
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   580
        pixels = (const void*)((const Uint8*)top + (rect->y / 2) * pitch + rect->x / 2);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   581
        if (texture->format == SDL_PIXELFORMAT_YV12) {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   582
            renderdata->glBindTexture(data->type, data->utexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   583
        } else {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   584
            renderdata->glBindTexture(data->type, data->vtexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   585
        }
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   586
        renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   587
                                    rect->w/2, rect->h/2,
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   588
                                    data->format, data->formattype, pixels);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   589
    }
3041
20d65430e63c Fixed OpenGL state issue reported by Dmytro Bogovych
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   590
    renderdata->glDisable(data->type);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   591
    result = renderdata->glGetError();
1924
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   592
    if (result != GL_NO_ERROR) {
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   593
        GL_SetError("glTexSubImage2D()", result);
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   594
        return -1;
69217fdd2c0a If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Sam Lantinga <slouken@libsdl.org>
parents: 1923
diff changeset
   595
    }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   596
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   597
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   598
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   599
static int
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   600
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   601
               const SDL_Rect * rect, void **pixels, int *pitch)
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   602
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   603
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   604
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   605
    data->locked_rect = *rect;
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   606
    *pixels = 
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   607
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   608
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   609
    *pitch = data->pitch;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   610
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   611
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   612
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   613
static void
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   614
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   615
{
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   616
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   617
    const SDL_Rect *rect;
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   618
    void *pixels;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   619
5227
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   620
    rect = &data->locked_rect;
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   621
    pixels = 
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   622
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   623
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
9c0c4d767ef6 Reduce duplicated code in the texture update code paths
Sam Lantinga <slouken@libsdl.org>
parents: 5226
diff changeset
   624
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   625
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   626
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   627
static int
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   628
GL_UpdateViewport(SDL_Renderer * renderer)
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   629
{
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   630
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   631
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   632
    if (SDL_CurrentContext != data->context) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   633
        /* We'll update the viewport after we rebind the context */
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   634
        return 0;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   635
    }
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   636
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   637
    data->glViewport(renderer->viewport.x, renderer->viewport.y,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   638
                     renderer->viewport.w, renderer->viewport.h);
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   639
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   640
    data->glMatrixMode(GL_PROJECTION);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   641
    data->glLoadIdentity();
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   642
    data->glOrtho((GLdouble) 0,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   643
                  (GLdouble) renderer->viewport.w,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   644
                  (GLdouble) renderer->viewport.h,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   645
                  (GLdouble) 0, 0.0, 1.0);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   646
    return 0;
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   647
}
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   648
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5204
diff changeset
   649
static void
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   650
GL_SetShader(GL_RenderData * data, GL_Shader shader)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   651
{
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   652
    if (data->shaders && shader != data->current.shader) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   653
        GL_SelectShader(data->shaders, shader);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   654
        data->current.shader = shader;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   655
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   656
}
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   657
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   658
static void
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   659
GL_SetColor(GL_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   660
{
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   661
    Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   662
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   663
    if (color != data->current.color) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   664
        data->glColor4f((GLfloat) r * inv255f,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   665
                        (GLfloat) g * inv255f,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   666
                        (GLfloat) b * inv255f,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   667
                        (GLfloat) a * inv255f);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   668
        data->current.color = color;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   669
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   670
}
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   671
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   672
static void
5140
e743b9c3f6d6 Making the API simpler, the blend modes are "none, blend, add" and are supported by all renderers.
Sam Lantinga <slouken@libsdl.org>
parents: 5138
diff changeset
   673
GL_SetBlendMode(GL_RenderData * data, int blendMode)
2936
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   674
{
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   675
    if (blendMode != data->current.blendMode) {
2936
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   676
        switch (blendMode) {
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   677
        case SDL_BLENDMODE_NONE:
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   678
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   679
            data->glDisable(GL_BLEND);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   680
            break;
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   681
        case SDL_BLENDMODE_BLEND:
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   682
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   683
            data->glEnable(GL_BLEND);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   684
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   685
            break;
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   686
        case SDL_BLENDMODE_ADD:
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   687
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   688
            data->glEnable(GL_BLEND);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   689
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   690
            break;
5184
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5166
diff changeset
   691
        case SDL_BLENDMODE_MOD:
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5166
diff changeset
   692
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5166
diff changeset
   693
            data->glEnable(GL_BLEND);
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5166
diff changeset
   694
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5166
diff changeset
   695
            break;
2936
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   696
        }
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   697
        data->current.blendMode = blendMode;
2936
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   698
    }
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   699
}
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   700
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   701
static void
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   702
GL_SetDrawingState(SDL_Renderer * renderer)
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   703
{
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   704
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   705
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   706
    GL_ActivateRenderer(renderer);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   707
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   708
    GL_SetColor(data, (GLfloat) renderer->r,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   709
                      (GLfloat) renderer->g,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   710
                      (GLfloat) renderer->b,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   711
                      (GLfloat) renderer->a);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   712
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   713
    GL_SetBlendMode(data, renderer->blendMode);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   714
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   715
    GL_SetShader(data, SHADER_SOLID);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   716
}
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   717
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   718
static int
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   719
GL_RenderClear(SDL_Renderer * renderer)
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   720
{
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   721
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   722
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   723
    GL_ActivateRenderer(renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   724
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   725
    data->glClearColor((GLfloat) renderer->r * inv255f,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   726
                       (GLfloat) renderer->g * inv255f,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   727
                       (GLfloat) renderer->b * inv255f,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   728
                       (GLfloat) renderer->a * inv255f);
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   729
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   730
    data->glClear(GL_COLOR_BUFFER_BIT);
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   731
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   732
    return 0;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   733
}
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   734
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   735
static int
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   736
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   737
                    int count)
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2858
diff changeset
   738
{
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2858
diff changeset
   739
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   740
    int i;
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2858
diff changeset
   741
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   742
    GL_SetDrawingState(renderer);
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2858
diff changeset
   743
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   744
    data->glBegin(GL_POINTS);
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   745
    for (i = 0; i < count; ++i) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   746
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   747
    }
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   748
    data->glEnd();
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   749
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   750
    return 0;
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   751
}
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   752
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   753
static int
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   754
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   755
                   int count)
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   756
{
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   757
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   758
    int i;
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   759
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   760
    GL_SetDrawingState(renderer);
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2893
diff changeset
   761
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   762
    if (count > 2 && 
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   763
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   764
        data->glBegin(GL_LINE_LOOP);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   765
        /* GL_LINE_LOOP takes care of the final segment */
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   766
        --count;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   767
        for (i = 0; i < count; ++i) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   768
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   769
        }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   770
        data->glEnd();
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   771
    } else {
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
   772
#if defined(__APPLE__) || defined(__WIN32__)
4910
f205711f73d5 Fix and rename VS2010 project files, add tests to VS2010 solution, fix VS compiler warning
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 4905
diff changeset
   773
#else
4905
9779093454d2 This fixes SDL_renderer_gl so that it builds with c89.
Sam Lantinga <slouken@libsdl.org>
parents: 4455
diff changeset
   774
        int x1, y1, x2, y2;
4910
f205711f73d5 Fix and rename VS2010 project files, add tests to VS2010 solution, fix VS compiler warning
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 4905
diff changeset
   775
#endif
4905
9779093454d2 This fixes SDL_renderer_gl so that it builds with c89.
Sam Lantinga <slouken@libsdl.org>
parents: 4455
diff changeset
   776
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   777
        data->glBegin(GL_LINE_STRIP);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   778
        for (i = 0; i < count; ++i) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   779
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   780
        }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   781
        data->glEnd();
3474
1edb86163d62 Of COURSE that trick wouldn't work on all renderers. Fall back to something for now, hopefully figure out a better way to do this later.
Sam Lantinga <slouken@libsdl.org>
parents: 3473
diff changeset
   782
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   783
        /* The line is half open, so we need one more point to complete it.
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   784
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   785
         * If we have to, we can use vertical line and horizontal line textures
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   786
         * for vertical and horizontal lines, and then create custom textures
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   787
         * for diagonal lines and software render those.  It's terrible, but at
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   788
         * least it would be pixel perfect.
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   789
         */
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   790
        data->glBegin(GL_POINTS);
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
   791
#if defined(__APPLE__) || defined(__WIN32__)
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   792
        /* Mac OS X and Windows seem to always leave the second point open */
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   793
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
3474
1edb86163d62 Of COURSE that trick wouldn't work on all renderers. Fall back to something for now, hopefully figure out a better way to do this later.
Sam Lantinga <slouken@libsdl.org>
parents: 3473
diff changeset
   794
#else
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   795
        /* Linux seems to leave the right-most or bottom-most point open */
4905
9779093454d2 This fixes SDL_renderer_gl so that it builds with c89.
Sam Lantinga <slouken@libsdl.org>
parents: 4455
diff changeset
   796
        x1 = points[0].x;
9779093454d2 This fixes SDL_renderer_gl so that it builds with c89.
Sam Lantinga <slouken@libsdl.org>
parents: 4455
diff changeset
   797
        y1 = points[0].y;
9779093454d2 This fixes SDL_renderer_gl so that it builds with c89.
Sam Lantinga <slouken@libsdl.org>
parents: 4455
diff changeset
   798
        x2 = points[count-1].x;
9779093454d2 This fixes SDL_renderer_gl so that it builds with c89.
Sam Lantinga <slouken@libsdl.org>
parents: 4455
diff changeset
   799
        y2 = points[count-1].y;
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   800
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   801
        if (x1 > x2) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   802
            data->glVertex2f(0.5f + x1, 0.5f + y1);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   803
        } else if (x2 > x1) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   804
            data->glVertex2f(0.5f + x2, 0.5f + y2);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   805
        } else if (y1 > y2) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   806
            data->glVertex2f(0.5f + x1, 0.5f + y1);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   807
        } else if (y2 > y1) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   808
            data->glVertex2f(0.5f + x2, 0.5f + y2);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   809
        }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   810
#endif
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   811
        data->glEnd();
3474
1edb86163d62 Of COURSE that trick wouldn't work on all renderers. Fall back to something for now, hopefully figure out a better way to do this later.
Sam Lantinga <slouken@libsdl.org>
parents: 3473
diff changeset
   812
    }
3455
5a7b5760c875 Include the endpoint in the line we're drawing
Sam Lantinga <slouken@libsdl.org>
parents: 3454
diff changeset
   813
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   814
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   815
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   816
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   817
static int
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   818
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
2925
7e21f7662208 Swapped functions to match the other renderer files
Sam Lantinga <slouken@libsdl.org>
parents: 2922
diff changeset
   819
{
7e21f7662208 Swapped functions to match the other renderer files
Sam Lantinga <slouken@libsdl.org>
parents: 2922
diff changeset
   820
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   821
    int i;
2925
7e21f7662208 Swapped functions to match the other renderer files
Sam Lantinga <slouken@libsdl.org>
parents: 2922
diff changeset
   822
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   823
    GL_SetDrawingState(renderer);
2936
066384910f50 iPhone build compiles again (drawing routines need to be implemented)
Sam Lantinga <slouken@libsdl.org>
parents: 2927
diff changeset
   824
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   825
    for (i = 0; i < count; ++i) {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5276
diff changeset
   826
        const SDL_Rect *rect = &rects[i];
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   827
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   828
        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3533
diff changeset
   829
    }
2925
7e21f7662208 Swapped functions to match the other renderer files
Sam Lantinga <slouken@libsdl.org>
parents: 2922
diff changeset
   830
7e21f7662208 Swapped functions to match the other renderer files
Sam Lantinga <slouken@libsdl.org>
parents: 2922
diff changeset
   831
    return 0;
7e21f7662208 Swapped functions to match the other renderer files
Sam Lantinga <slouken@libsdl.org>
parents: 2922
diff changeset
   832
}
7e21f7662208 Swapped functions to match the other renderer files
Sam Lantinga <slouken@libsdl.org>
parents: 2922
diff changeset
   833
7e21f7662208 Swapped functions to match the other renderer files
Sam Lantinga <slouken@libsdl.org>
parents: 2922
diff changeset
   834
static int
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   835
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   836
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   837
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   838
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   839
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   840
    int minx, miny, maxx, maxy;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   841
    GLfloat minu, maxu, minv, maxv;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   842
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   843
    GL_ActivateRenderer(renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   844
5355
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   845
    data->glEnable(texturedata->type);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   846
    if (texturedata->yuv) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   847
        data->glActiveTextureARB(GL_TEXTURE2_ARB);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   848
        data->glBindTexture(texturedata->type, texturedata->vtexture);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   849
        if (texturedata->scaleMode != data->current.scaleMode) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   850
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   851
                                  texturedata->scaleMode);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   852
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   853
                                  texturedata->scaleMode);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   854
        }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   855
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   856
        data->glActiveTextureARB(GL_TEXTURE1_ARB);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   857
        data->glBindTexture(texturedata->type, texturedata->utexture);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   858
        if (texturedata->scaleMode != data->current.scaleMode) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   859
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   860
                                  texturedata->scaleMode);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   861
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   862
                                  texturedata->scaleMode);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   863
        }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   864
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   865
        data->glActiveTextureARB(GL_TEXTURE0_ARB);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   866
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   867
    data->glBindTexture(texturedata->type, texturedata->texture);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   868
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   869
    if (texturedata->scaleMode != data->current.scaleMode) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   870
        data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   871
                              texturedata->scaleMode);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   872
        data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   873
                              texturedata->scaleMode);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   874
        data->current.scaleMode = texturedata->scaleMode;
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   875
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   876
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   877
    if (texture->modMode) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   878
        GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   879
    } else {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   880
        GL_SetColor(data, 255, 255, 255, 255);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   881
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   882
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   883
    GL_SetBlendMode(data, texture->blendMode);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   884
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   885
    if (texturedata->yuv) {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   886
        GL_SetShader(data, SHADER_YV12);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   887
    } else {
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   888
        GL_SetShader(data, SHADER_RGB);
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   889
    }
cc2215cda170 Added OpenGL state caching for decent speed improvement.
Sam Lantinga <slouken@libsdl.org>
parents: 5351
diff changeset
   890
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   891
    minx = dstrect->x;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   892
    miny = dstrect->y;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   893
    maxx = dstrect->x + dstrect->w;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   894
    maxy = dstrect->y + dstrect->h;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   895
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   896
    minu = (GLfloat) srcrect->x / texture->w;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   897
    minu *= texturedata->texw;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   898
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   899
    maxu *= texturedata->texw;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   900
    minv = (GLfloat) srcrect->y / texture->h;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   901
    minv *= texturedata->texh;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   902
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   903
    maxv *= texturedata->texh;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   904
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   905
    data->glBegin(GL_TRIANGLE_STRIP);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   906
    data->glTexCoord2f(minu, minv);
3472
fdd2f2e9cd97 Fixed the coordinates for pixel coverage in blits
Sam Lantinga <slouken@libsdl.org>
parents: 3470
diff changeset
   907
    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   908
    data->glTexCoord2f(maxu, minv);
3472
fdd2f2e9cd97 Fixed the coordinates for pixel coverage in blits
Sam Lantinga <slouken@libsdl.org>
parents: 3470
diff changeset
   909
    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   910
    data->glTexCoord2f(minu, maxv);
3472
fdd2f2e9cd97 Fixed the coordinates for pixel coverage in blits
Sam Lantinga <slouken@libsdl.org>
parents: 3470
diff changeset
   911
    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   912
    data->glTexCoord2f(maxu, maxv);
3472
fdd2f2e9cd97 Fixed the coordinates for pixel coverage in blits
Sam Lantinga <slouken@libsdl.org>
parents: 3470
diff changeset
   913
    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   914
    data->glEnd();
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   915
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2858
diff changeset
   916
    data->glDisable(texturedata->type);
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2858
diff changeset
   917
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   918
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   919
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   920
3431
c8fbb27627d9 Work in progress on OpenGL ReadPixels/WritePixels interface
Sam Lantinga <slouken@libsdl.org>
parents: 3393
diff changeset
   921
static int
c8fbb27627d9 Work in progress on OpenGL ReadPixels/WritePixels interface
Sam Lantinga <slouken@libsdl.org>
parents: 3393
diff changeset
   922
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   923
                    Uint32 pixel_format, void * pixels, int pitch)
3431
c8fbb27627d9 Work in progress on OpenGL ReadPixels/WritePixels interface
Sam Lantinga <slouken@libsdl.org>
parents: 3393
diff changeset
   924
{
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   925
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3607
diff changeset
   926
    SDL_Window *window = renderer->window;
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   927
    GLint internalFormat;
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   928
    GLenum format, type;
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   929
    Uint8 *src, *dst, *tmp;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   930
    int w, h, length, rows;
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   931
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   932
    GL_ActivateRenderer(renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   933
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   934
    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   935
        /* FIXME: Do a temp copy to a format that is supported */
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   936
        SDL_SetError("Unsupported pixel format");
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   937
        return -1;
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   938
    }
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   939
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   940
    SDL_GetWindowSize(window, &w, &h);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   941
3446
0f969d273f65 First pass (untested) at RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3443
diff changeset
   942
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
0f969d273f65 First pass (untested) at RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3443
diff changeset
   943
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   944
                        (pitch / SDL_BYTESPERPIXEL(pixel_format)));
3433
ad845d9835aa Hmm, this isn't going to work, is it?
Sam Lantinga <slouken@libsdl.org>
parents: 3431
diff changeset
   945
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   946
    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   947
                       format, type, pixels);
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   948
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   949
    /* Flip the rows to be top-down */
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   950
    length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   951
    src = (Uint8*)pixels + (rect->h-1)*pitch;
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   952
    dst = (Uint8*)pixels;
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   953
    tmp = SDL_stack_alloc(Uint8, length);
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   954
    rows = rect->h / 2;
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   955
    while (rows--) {
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   956
        SDL_memcpy(tmp, dst, length);
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   957
        SDL_memcpy(dst, src, length);
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   958
        SDL_memcpy(src, tmp, length);
3447
294b770c1989 Fixed GL_RenderReadPixels() - thanks Ryan!
Sam Lantinga <slouken@libsdl.org>
parents: 3446
diff changeset
   959
        dst += pitch;
294b770c1989 Fixed GL_RenderReadPixels() - thanks Ryan!
Sam Lantinga <slouken@libsdl.org>
parents: 3446
diff changeset
   960
        src -= pitch;
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   961
    }
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3433
diff changeset
   962
    SDL_stack_free(tmp);
3440
e9502d56ae94 Added missing return values
Sam Lantinga <slouken@libsdl.org>
parents: 3435
diff changeset
   963
e9502d56ae94 Added missing return values
Sam Lantinga <slouken@libsdl.org>
parents: 3435
diff changeset
   964
    return 0;
3431
c8fbb27627d9 Work in progress on OpenGL ReadPixels/WritePixels interface
Sam Lantinga <slouken@libsdl.org>
parents: 3393
diff changeset
   965
}
c8fbb27627d9 Work in progress on OpenGL ReadPixels/WritePixels interface
Sam Lantinga <slouken@libsdl.org>
parents: 3393
diff changeset
   966
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   967
static void
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   968
GL_RenderPresent(SDL_Renderer * renderer)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   969
{
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   970
    GL_ActivateRenderer(renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   971
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   972
    SDL_GL_SwapWindow(renderer->window);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   973
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   974
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   975
static void
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   976
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   977
{
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   978
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   979
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   980
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   981
    GL_ActivateRenderer(renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   982
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   983
    if (!data) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   984
        return;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   985
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   986
    if (data->texture) {
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   987
        renderdata->glDeleteTextures(1, &data->texture);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   988
    }
5264
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   989
    if (data->yuv) {
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   990
        renderdata->glDeleteTextures(1, &data->utexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   991
        renderdata->glDeleteTextures(1, &data->vtexture);
7ace5f8f432f Initial pass at shader YV12 support - doesn't quite work yet.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   992
    }
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   993
    if (data->pixels) {
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   994
        SDL_free(data->pixels);
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
   995
    }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   996
    SDL_free(data);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   997
    texture->driverdata = NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   998
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   999
1975
ccef0d0c40c6 Added resize support for GDI and Direct3D renderers
Sam Lantinga <slouken@libsdl.org>
parents: 1974
diff changeset
  1000
static void
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1001
GL_DestroyRenderer(SDL_Renderer * renderer)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1002
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1003
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1004
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1005
    if (data) {
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
  1006
        if (data->context) {
2328
91e601d9df8b re: bug#563. checking in some commented out trace code and a fix so that the in testalpha.c the background only flashes when alpha == 255. The problem that is being
Bob Pendleton <bob@pendleton.com>
parents: 2295
diff changeset
  1007
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1919
diff changeset
  1008
            SDL_GL_DeleteContext(data->context);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1009
        }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1010
        SDL_free(data);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1011
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1012
    SDL_free(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1013
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1014
5226
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
  1015
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1016
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
  1017
/* vi: set ts=4 sw=4 expandtab: */