src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 12 Aug 2007 07:02:28 +0000
changeset 2230 9b7d29d2432b
parent 2222 926294b2bb4e
child 2233 fb01ee9716bc
permissions -rw-r--r--
Optimized OpenGL renderer for Mac OS X. The SDL 1.2 API version of testsprite went from 120 FPS to 320 FPS. :)
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
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
    Copyright (C) 1997-2006 Sam Lantinga
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
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
    24
#if SDL_VIDEO_RENDER_OGL
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
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
    26
#include "SDL_video.h"
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
    27
#include "SDL_opengl.h"
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_sysvideo.h"
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
    29
#include "SDL_pixels_c.h"
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
    30
#include "SDL_rect_c.h"
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
    31
#include "SDL_yuv_sw_c.h"
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
/* OpenGL renderer implementation */
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    35
/* Details on optimizing the texture path on Mac OS X:
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    36
   http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/chapter_10_section_2.html
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    37
*/
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
    38
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    39
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
    40
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
1923
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
    42
static int GL_ActivateRenderer(SDL_Renderer * renderer);
1970
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
    43
static int GL_DisplayModeChanged(SDL_Renderer * renderer);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
2222
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
    45
static int GL_QueryTexturePixels(SDL_Renderer * renderer,
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
    46
                                 SDL_Texture * texture, void **pixels,
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
    47
                                 int *pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
static int GL_SetTexturePalette(SDL_Renderer * renderer,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
                                SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
                                const SDL_Color * colors, int firstcolor,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
                                int ncolors);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
static int GL_GetTexturePalette(SDL_Renderer * renderer,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
                                SDL_Texture * texture, SDL_Color * colors,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
                                int firstcolor, int ncolors);
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    55
static int GL_SetTextureColorMod(SDL_Renderer * renderer,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    56
                                 SDL_Texture * texture);
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    57
static int GL_SetTextureAlphaMod(SDL_Renderer * renderer,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    58
                                 SDL_Texture * texture);
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    59
static int GL_SetTextureBlendMode(SDL_Renderer * renderer,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    60
                                  SDL_Texture * texture);
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    61
static int GL_SetTextureScaleMode(SDL_Renderer * renderer,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    62
                                  SDL_Texture * texture);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
                            const SDL_Rect * rect, const void *pixels,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
                            int pitch);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
static int GL_LockTexture(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 * rect, int markDirty, void **pixels,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    68
                          int *pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
                            int numrects, const SDL_Rect * rects);
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    72
static int GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    73
                         Uint8 a, const SDL_Rect * rect);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
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
    75
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
static void GL_RenderPresent(SDL_Renderer * renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
static void GL_DestroyRenderer(SDL_Renderer * renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
SDL_RenderDriver GL_RenderDriver = {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
    GL_CreateRenderer,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
    {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
     "opengl",
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
    85
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
    86
      SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    87
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
    88
      SDL_TEXTUREMODULATE_ALPHA),
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
    89
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
    90
      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
    91
      SDL_TEXTUREBLENDMODE_MOD),
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
    92
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
    93
      SDL_TEXTURESCALEMODE_SLOW),
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
    94
     16,
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
     {
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
    96
      SDL_PIXELFORMAT_INDEX1LSB,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
    97
      SDL_PIXELFORMAT_INDEX1MSB,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
    98
      SDL_PIXELFORMAT_INDEX8,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
    99
      SDL_PIXELFORMAT_RGB332,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   100
      SDL_PIXELFORMAT_RGB444,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   101
      SDL_PIXELFORMAT_RGB555,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   102
      SDL_PIXELFORMAT_ARGB4444,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   103
      SDL_PIXELFORMAT_ARGB1555,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   104
      SDL_PIXELFORMAT_RGB565,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   105
      SDL_PIXELFORMAT_RGB24,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   106
      SDL_PIXELFORMAT_BGR24,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   107
      SDL_PIXELFORMAT_RGB888,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   108
      SDL_PIXELFORMAT_BGR888,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   109
      SDL_PIXELFORMAT_ARGB8888,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   110
      SDL_PIXELFORMAT_ABGR8888,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   111
      SDL_PIXELFORMAT_ARGB2101010},
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
     0,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
     0}
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
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
typedef struct
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
    SDL_GLContext context;
1970
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   119
    SDL_bool updateSize;
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   120
    SDL_bool GL_EXT_paletted_texture_supported;
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   121
    SDL_bool GL_ARB_texture_rectangle_supported;
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   122
    int blendMode;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   123
    int scaleMode;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   124
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   125
    /* OpenGL functions */
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   126
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   127
#include "SDL_glfuncs.h"
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   128
#undef SDL_PROC
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   129
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   130
    PFNGLCOLORTABLEEXTPROC glColorTableEXT;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
} GL_RenderData;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
typedef struct
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   134
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   135
    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
   136
    GLenum type;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   137
    GLfloat texw;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   138
    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
   139
    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
   140
    GLenum formattype;
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   141
    Uint8 *palette;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
    void *pixels;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
    int pitch;
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
   144
    SDL_DirtyRectList dirty;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
} GL_TextureData;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
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
   148
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
   149
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
   150
{
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
    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
   152
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
    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
   154
    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
   155
        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
   156
        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
   157
    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
   158
        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
   159
        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
   160
    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
   161
        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
   162
        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
   163
    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
   164
        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
   165
        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
   166
    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
   167
        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
   168
        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
   169
    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
   170
        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
   171
        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
   172
    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
   173
        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
   174
        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
   175
    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
   176
        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
   177
        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
   178
    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
   179
        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
   180
        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
   181
    }
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
   182
    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
   183
}
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
   184
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   185
static int
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   186
GL_LoadFunctions(GL_RenderData * data)
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   187
{
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   188
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   189
#define __SDL_NOGETPROCADDR__
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   190
#elif defined(__MINT__)
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   191
#define __SDL_NOGETPROCADDR__
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   192
#endif
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   193
#ifdef __SDL_NOGETPROCADDR__
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   194
#define SDL_PROC(ret,func,params) data->func=func;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   195
#else
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   196
#define SDL_PROC(ret,func,params) \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   197
    do { \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   198
        data->func = SDL_GL_GetProcAddress(#func); \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   199
        if ( ! data->func ) { \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   200
            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
   201
            return -1; \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   202
        } \
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   203
    } while ( 0 );
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   204
#endif /* __SDL_NOGETPROCADDR__ */
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   205
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   206
#include "SDL_glfuncs.h"
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   207
#undef SDL_PROC
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   208
    return 0;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   209
}
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   210
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
void
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
GL_AddRenderDriver(_THIS)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
{
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
   214
    if (_this->GL_CreateContext) {
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
        SDL_AddRenderDriver(0, &GL_RenderDriver);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   218
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   219
SDL_Renderer *
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   220
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
    SDL_Renderer *renderer;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
    GL_RenderData *data;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   224
    GLint value;
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   225
    int doublebuffer;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   227
    /* Render directly to the window, unless we're compositing */
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   228
#ifndef __MACOSX__
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   229
    if (flags & SDL_RENDERER_SINGLEBUFFER) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   230
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   231
    }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   232
#endif
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
1928
861bc36f0ab3 Fixed crash with multiple windows
Sam Lantinga <slouken@libsdl.org>
parents: 1927
diff changeset
   234
        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
   235
            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
   236
        }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
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
   239
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
    if (!renderer) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
        SDL_OutOfMemory();
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   244
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
   245
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   246
    if (!data) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   247
        GL_DestroyRenderer(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
        SDL_OutOfMemory();
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
1923
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   252
    renderer->ActivateRenderer = GL_ActivateRenderer;
1970
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   253
    renderer->DisplayModeChanged = GL_DisplayModeChanged;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
    renderer->CreateTexture = GL_CreateTexture;
2222
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   255
    renderer->QueryTexturePixels = GL_QueryTexturePixels;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
    renderer->SetTexturePalette = GL_SetTexturePalette;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
    renderer->GetTexturePalette = GL_GetTexturePalette;
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   258
    renderer->SetTextureColorMod = GL_SetTextureColorMod;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   259
    renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   260
    renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   261
    renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
    renderer->UpdateTexture = GL_UpdateTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
    renderer->LockTexture = GL_LockTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
    renderer->UnlockTexture = GL_UnlockTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
    renderer->DirtyTexture = GL_DirtyTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   266
    renderer->RenderFill = GL_RenderFill;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
    renderer->RenderCopy = GL_RenderCopy;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   268
    renderer->RenderPresent = GL_RenderPresent;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
    renderer->DestroyTexture = GL_DestroyTexture;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
    renderer->DestroyRenderer = GL_DestroyRenderer;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   271
    renderer->info = GL_RenderDriver.info;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   272
    renderer->window = window->id;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   273
    renderer->driverdata = data;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   274
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   275
    renderer->info.flags =
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   276
        (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   277
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   278
    if (GL_LoadFunctions(data) < 0) {
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   279
        GL_DestroyRenderer(renderer);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   280
        return NULL;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   281
    }
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   282
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
    data->context = SDL_GL_CreateContext(window->id);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
    if (!data->context) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
        GL_DestroyRenderer(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
        GL_DestroyRenderer(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
        return NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   293
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   294
        SDL_GL_SetSwapInterval(1);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   295
    } else {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   296
        SDL_GL_SetSwapInterval(0);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   297
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
    if (SDL_GL_GetSwapInterval() > 0) {
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   299
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   300
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   302
    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   303
        if (!doublebuffer) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   304
            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   305
        }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   306
    }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   307
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   308
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   309
    renderer->info.max_texture_width = value;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   310
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   311
    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
   312
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   313
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   314
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   315
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   316
    }
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   317
    if (SDL_GL_ExtensionSupported("GL_EXT_paletted_texture")) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   318
        data->GL_EXT_paletted_texture_supported = SDL_TRUE;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   319
        data->glColorTableEXT =
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   320
            (PFNGLCOLORTABLEEXTPROC) SDL_GL_GetProcAddress("glColorTableEXT");
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   321
    } else {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   322
        /* Don't advertise support for 8-bit indexed texture format */
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   323
        Uint32 i, j;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   324
        SDL_RendererInfo *info = &renderer->info;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   325
        for (i = 0, j = 0; i < info->num_texture_formats; ++i) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   326
            if (info->texture_formats[i] != SDL_PIXELFORMAT_INDEX8) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   327
                info->texture_formats[j++] = info->texture_formats[i];
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   328
            }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   329
        }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   330
        --info->num_texture_formats;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   331
    }
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
   332
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   333
    /* Set up parameters for rendering */
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   334
    data->blendMode = -1;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   335
    data->scaleMode = -1;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   336
    data->glDisable(GL_DEPTH_TEST);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   337
    data->glDisable(GL_CULL_FACE);
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   338
    if (data->GL_ARB_texture_rectangle_supported) {
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   339
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   340
    } else {
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   341
        data->glEnable(GL_TEXTURE_2D);
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   342
    }
1970
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   343
    data->updateSize = SDL_TRUE;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   344
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   345
    return renderer;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   346
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   347
1923
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   348
static int
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   349
GL_ActivateRenderer(SDL_Renderer * renderer)
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   350
{
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   351
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   352
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   353
1970
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   354
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   355
        return -1;
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   356
    }
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   357
    if (data->updateSize) {
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   358
        data->glMatrixMode(GL_PROJECTION);
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   359
        data->glLoadIdentity();
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   360
        data->glMatrixMode(GL_MODELVIEW);
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   361
        data->glLoadIdentity();
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   362
        data->glViewport(0, 0, window->w, window->h);
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   363
        data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0,
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   364
                      0.0, 1.0);
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   365
        data->updateSize = SDL_FALSE;
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   366
    }
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   367
    return 0;
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   368
}
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   369
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   370
static int
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   371
GL_DisplayModeChanged(SDL_Renderer * renderer)
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   372
{
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   373
    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
   374
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   375
    data->updateSize = SDL_TRUE;
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   376
    return 0;
1923
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   377
}
d4572b97b08f Switch OpenGL contexts when switching render contexts.
Sam Lantinga <slouken@libsdl.org>
parents: 1922
diff changeset
   378
1922
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   379
static __inline__ int
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   380
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
   381
{
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   382
    int value = 1;
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   383
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   384
    while (value < input) {
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   385
        value <<= 1;
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   386
    }
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   387
    return value;
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   388
}
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   389
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   390
static int
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   391
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   392
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   393
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   394
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
    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
   396
    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
   397
    GLenum format, type;
1922
4905cac7a4bd Fixed OpenGL blend modes, added power of 2 texture code
Sam Lantinga <slouken@libsdl.org>
parents: 1921
diff changeset
   398
    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
   399
    GLenum result;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   400
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
   401
    switch (texture->format) {
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   402
    case SDL_PIXELFORMAT_INDEX1LSB:
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   403
    case SDL_PIXELFORMAT_INDEX1MSB:
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
   404
        internalFormat = GL_RGB;
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
   405
        format = GL_COLOR_INDEX;
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
   406
        type = GL_BITMAP;
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
   407
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   408
    case SDL_PIXELFORMAT_INDEX8:
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   409
        if (!renderdata->GL_EXT_paletted_texture_supported) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   410
            SDL_SetError("Unsupported texture format");
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   411
            return -1;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   412
        }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   413
        internalFormat = GL_COLOR_INDEX8_EXT;
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
   414
        format = GL_COLOR_INDEX;
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
   415
        type = GL_UNSIGNED_BYTE;
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
   416
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   417
    case SDL_PIXELFORMAT_RGB332:
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
   418
        internalFormat = GL_R3_G3_B2;
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
   419
        format = GL_RGB;
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
        type = GL_UNSIGNED_BYTE_3_3_2;
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
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   422
    case SDL_PIXELFORMAT_RGB444:
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
   423
        internalFormat = GL_RGB4;
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
   424
        format = GL_RGB;
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
   425
        type = GL_UNSIGNED_SHORT_4_4_4_4;
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
   426
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   427
    case SDL_PIXELFORMAT_RGB555:
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
   428
        internalFormat = GL_RGB5;
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
   429
        format = GL_RGB;
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
   430
        type = GL_UNSIGNED_SHORT_5_5_5_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
   431
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   432
    case SDL_PIXELFORMAT_ARGB4444:
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
   433
        internalFormat = GL_RGBA4;
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
   434
        format = GL_BGRA;
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
   435
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
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
   436
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   437
    case SDL_PIXELFORMAT_ARGB1555:
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
   438
        internalFormat = GL_RGB5_A1;
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
   439
        format = GL_BGRA;
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
   440
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
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
   441
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   442
    case SDL_PIXELFORMAT_RGB565:
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
   443
        internalFormat = GL_RGB8;
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
   444
        format = GL_RGB;
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
   445
        type = GL_UNSIGNED_SHORT_5_6_5;
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
   446
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   447
    case SDL_PIXELFORMAT_RGB24:
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
   448
        internalFormat = GL_RGB8;
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
   449
        format = GL_RGB;
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
   450
        type = GL_UNSIGNED_BYTE;
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
   451
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   452
    case SDL_PIXELFORMAT_RGB888:
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
   453
        internalFormat = GL_RGB8;
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
   454
        format = GL_BGRA;
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
   455
        type = GL_UNSIGNED_BYTE;
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
   456
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   457
    case SDL_PIXELFORMAT_BGR24:
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
   458
        internalFormat = GL_RGB8;
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
   459
        format = GL_BGR;
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
   460
        type = GL_UNSIGNED_BYTE;
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
   461
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   462
    case SDL_PIXELFORMAT_BGR888:
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
   463
        internalFormat = GL_RGB8;
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
   464
        format = GL_RGBA;
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
   465
        type = GL_UNSIGNED_BYTE;
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
   466
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   467
    case SDL_PIXELFORMAT_ARGB8888:
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   468
#ifdef __MACOSX__
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   469
        internalFormat = GL_RGBA;
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   470
        format = GL_BGRA;
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   471
        type = GL_UNSIGNED_INT_8_8_8_8_REV;
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   472
#else
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
   473
        internalFormat = GL_RGBA8;
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
   474
        format = GL_BGRA;
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
   475
        type = GL_UNSIGNED_BYTE;
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   476
#endif
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
   477
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   478
    case SDL_PIXELFORMAT_ABGR8888:
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
   479
        internalFormat = GL_RGBA8;
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
   480
        format = GL_RGBA;
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
   481
        type = GL_UNSIGNED_BYTE;
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
   482
        break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   483
    case SDL_PIXELFORMAT_ARGB2101010:
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
   484
        internalFormat = GL_RGB10_A2;
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
   485
        format = GL_BGRA;
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
   486
        type = GL_UNSIGNED_INT_2_10_10_10_REV;
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
   487
        break;
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
   488
    default:
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
   489
        SDL_SetError("Unsupported texture 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
   490
        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
   491
    }
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
   492
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
   493
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   494
    if (!data) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   495
        SDL_OutOfMemory();
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   496
        return -1;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   497
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   498
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   499
    if (texture->format == SDL_PIXELFORMAT_INDEX8) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   500
        data->palette = (Uint8 *) SDL_malloc(3 * 256 * sizeof(Uint8));
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   501
        if (!data->palette) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   502
            SDL_OutOfMemory();
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   503
            SDL_free(data);
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   504
            return -1;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   505
        }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   506
        SDL_memset(data->palette, 0xFF, 3 * 256 * sizeof(Uint8));
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   507
    }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   508
2222
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   509
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   510
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   511
        data->pixels = SDL_malloc(texture->h * data->pitch);
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   512
        if (!data->pixels) {
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   513
            SDL_OutOfMemory();
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   514
            SDL_free(data);
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   515
            return -1;
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   516
        }
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   517
    }
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   518
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   519
    texture->driverdata = data;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   520
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   521
    renderdata->glGetError();
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   522
    renderdata->glGenTextures(1, &data->texture);
1926
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   523
    if (renderdata->GL_ARB_texture_rectangle_supported) {
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   524
        data->type = GL_TEXTURE_RECTANGLE_ARB;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   525
        texture_w = texture->w;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   526
        texture_h = texture->h;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   527
        data->texw = (GLfloat) texture->w;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   528
        data->texh = (GLfloat) texture->h;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   529
    } else {
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   530
        data->type = GL_TEXTURE_2D;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   531
        texture_w = power_of_2(texture->w);
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   532
        texture_h = power_of_2(texture->h);
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   533
        data->texw = (GLfloat) texture->w / texture_w;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   534
        data->texh = (GLfloat) texture->h / texture_h;
307355678142 Added SDL_GL_ExtensionSupported()
Sam Lantinga <slouken@libsdl.org>
parents: 1924
diff changeset
   535
    }
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
   536
    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
   537
    data->formattype = type;
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   538
    renderdata->glBindTexture(data->type, data->texture);
2230
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   539
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   540
                                GL_NEAREST);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   541
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   542
                                GL_NEAREST);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   543
    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
   544
                                GL_CLAMP_TO_EDGE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   545
    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
   546
                                GL_CLAMP_TO_EDGE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   547
#ifdef __MACOSX__
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   548
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   549
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   550
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   551
#ifndef STORAGE_CACHED_APPLE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   552
#define STORAGE_CACHED_APPLE                0x85BE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   553
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   554
#ifndef STORAGE_SHARED_APPLE
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   555
#define STORAGE_SHARED_APPLE                0x85BF
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   556
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   557
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   558
        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
   559
                                    GL_STORAGE_SHARED_APPLE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   560
    } else {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   561
        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
   562
                                    GL_STORAGE_CACHED_APPLE);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   563
    }
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   564
    if (data->pixels && internalFormat == GL_RGBA && format == GL_BGRA
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   565
        && type == GL_UNSIGNED_INT_8_8_8_8_REV && data->pixels) {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   566
        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
   567
        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
   568
                                 texture_h, 0, format, type, data->pixels);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   569
    } else
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   570
#endif
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   571
    {
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   572
        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
   573
                                 texture_h, 0, format, type, NULL);
9b7d29d2432b Optimized OpenGL renderer for Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 2222
diff changeset
   574
    }
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   575
    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
   576
    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
   577
        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
   578
        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
   579
    }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   580
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   581
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   582
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   583
static int
2222
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   584
GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   585
                      void **pixels, int *pitch)
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   586
{
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   587
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   588
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   589
    *pixels = data->pixels;
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   590
    *pitch = data->pitch;
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   591
    return 0;
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   592
}
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   593
926294b2bb4e Emphasized the separation between SDL_Surface and SDL_Texture
Sam Lantinga <slouken@libsdl.org>
parents: 1986
diff changeset
   594
static int
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   595
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   596
                     const SDL_Color * colors, int firstcolor, int ncolors)
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
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   599
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   600
    Uint8 *palette;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   601
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   602
    if (!data->palette) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   603
        SDL_SetError("Texture doesn't have a palette");
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   604
        return -1;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   605
    }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   606
    palette = data->palette + firstcolor * 3;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   607
    while (ncolors--) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   608
        *palette++ = colors->r;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   609
        *palette++ = colors->g;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   610
        *palette++ = colors->b;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   611
        ++colors;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   612
    }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   613
    renderdata->glBindTexture(data->type, data->texture);
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   614
    renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB,
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   615
                                GL_UNSIGNED_BYTE, data->palette);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   616
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   617
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   618
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   619
static int
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   620
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   621
                     SDL_Color * colors, int firstcolor, int ncolors)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   622
{
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   623
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   624
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   625
    Uint8 *palette;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   626
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   627
    if (!data->palette) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   628
        SDL_SetError("Texture doesn't have a palette");
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   629
        return -1;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   630
    }
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   631
    palette = data->palette + firstcolor * 3;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   632
    while (ncolors--) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   633
        colors->r = *palette++;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   634
        colors->g = *palette++;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   635
        colors->b = *palette++;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   636
        colors->unused = SDL_ALPHA_OPAQUE;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   637
        ++colors;
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   638
    }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   639
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   640
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   641
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
   642
static void
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   643
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   644
                   int pitch)
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
   645
{
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   646
    if (texture->format == SDL_PIXELFORMAT_INDEX1LSB) {
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   647
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   648
    } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) {
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   649
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 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
   650
    }
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   651
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   652
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   653
                              pitch / SDL_BYTESPERPIXEL(texture->format));
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
   654
}
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
   655
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   656
static int
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   657
GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   658
{
1986
f4c65e3bfaed Cleanup for the last checkin
Sam Lantinga <slouken@libsdl.org>
parents: 1985
diff changeset
   659
    return 0;
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   660
}
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   661
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   662
static int
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   663
GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   664
{
1986
f4c65e3bfaed Cleanup for the last checkin
Sam Lantinga <slouken@libsdl.org>
parents: 1985
diff changeset
   665
    return 0;
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   666
}
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   667
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   668
static int
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   669
GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   670
{
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   671
    switch (texture->blendMode) {
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   672
    case SDL_TEXTUREBLENDMODE_NONE:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   673
    case SDL_TEXTUREBLENDMODE_MASK:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   674
    case SDL_TEXTUREBLENDMODE_BLEND:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   675
    case SDL_TEXTUREBLENDMODE_ADD:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   676
    case SDL_TEXTUREBLENDMODE_MOD:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   677
        return 0;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   678
    default:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   679
        SDL_Unsupported();
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   680
        texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   681
        return -1;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   682
    }
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   683
}
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   684
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   685
static int
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   686
GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   687
{
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   688
    switch (texture->scaleMode) {
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   689
    case SDL_TEXTURESCALEMODE_NONE:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   690
    case SDL_TEXTURESCALEMODE_FAST:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   691
    case SDL_TEXTURESCALEMODE_SLOW:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   692
        return 0;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   693
    case SDL_TEXTURESCALEMODE_BEST:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   694
        SDL_Unsupported();
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   695
        texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   696
        return -1;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   697
    default:
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   698
        SDL_Unsupported();
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   699
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   700
        return -1;
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   701
    }
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   702
}
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   703
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   704
static int
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   705
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   706
                 const SDL_Rect * rect, const void *pixels, int pitch)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   707
{
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   708
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   709
    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
   710
    GLenum result;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   711
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   712
    renderdata->glGetError();
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   713
    SetupTextureUpdate(renderdata, texture, pitch);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   714
    renderdata->glBindTexture(data->type, data->texture);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   715
    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
   716
                                rect->h, data->format, data->formattype,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   717
                                pixels);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   718
    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
   719
    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
   720
        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
   721
        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
   722
    }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   723
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   724
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   725
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   726
static int
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   727
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   728
               const SDL_Rect * rect, int markDirty, void **pixels,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   729
               int *pitch)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   730
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   731
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   732
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
   733
    if (markDirty) {
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
   734
        SDL_AddDirtyRect(&data->dirty, rect);
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
   735
    }
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   736
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
   737
    *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
   738
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
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
   739
                  rect->x * SDL_BYTESPERPIXEL(texture->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
   740
    *pitch = data->pitch;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   741
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   742
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   743
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   744
static void
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   745
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   746
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   747
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   748
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   749
static void
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   750
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   751
                const SDL_Rect * rects)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   752
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   753
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   754
    int i;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   755
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   756
    for (i = 0; i < numrects; ++i) {
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
   757
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   758
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   759
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   760
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   761
static int
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   762
GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   763
              const SDL_Rect * rect)
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   764
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   765
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   766
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   767
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   768
    data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   769
                       (GLclampf) b * inv255f, (GLclampf) a * inv255f);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   770
    data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   771
    data->glClear(GL_COLOR_BUFFER_BIT);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   772
    data->glViewport(0, 0, window->w, window->h);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   773
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   774
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   775
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   776
static int
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   777
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
   778
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   779
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   780
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   781
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   782
    int minx, miny, maxx, maxy;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   783
    GLfloat minu, maxu, minv, maxv;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   784
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
   785
    if (texturedata->dirty.count > 0) {
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
   786
        SDL_DirtyRect *dirty;
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
   787
        void *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
   788
        int bpp = SDL_BYTESPERPIXEL(texture->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
   789
        int pitch = texturedata->pitch;
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
   790
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   791
        SetupTextureUpdate(data, texture, pitch);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   792
        data->glBindTexture(texturedata->type, texturedata->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
   793
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
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
   794
            SDL_Rect *rect = &dirty->rect;
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
   795
            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
   796
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
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
   797
                          rect->x * bpp);
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   798
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   799
                                  rect->w, rect->h, texturedata->format,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   800
                                  texturedata->formattype, 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
   801
        }
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
   802
        SDL_ClearDirtyRects(&texturedata->dirty);
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
   803
    }
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
   804
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   805
    minx = dstrect->x;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   806
    miny = dstrect->y;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   807
    maxx = dstrect->x + dstrect->w;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   808
    maxy = dstrect->y + dstrect->h;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   809
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   810
    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
   811
    minu *= texturedata->texw;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   812
    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
   813
    maxu *= texturedata->texw;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   814
    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
   815
    minv *= texturedata->texh;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   816
    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
   817
    maxv *= texturedata->texh;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   818
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   819
    data->glBindTexture(texturedata->type, texturedata->texture);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   820
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   821
    if (texture->modMode) {
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   822
        data->glColor4f((GLfloat) texture->r * inv255f,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   823
                        (GLfloat) texture->g * inv255f,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   824
                        (GLfloat) texture->b * inv255f,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   825
                        (GLfloat) texture->a * inv255f);
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   826
    } else {
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   827
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   828
    }
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   829
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   830
    if (texture->blendMode != data->blendMode) {
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   831
        switch (texture->blendMode) {
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   832
        case SDL_TEXTUREBLENDMODE_NONE:
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   833
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   834
            data->glDisable(GL_BLEND);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   835
            break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   836
        case SDL_TEXTUREBLENDMODE_MASK:
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   837
        case SDL_TEXTUREBLENDMODE_BLEND:
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   838
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   839
            data->glEnable(GL_BLEND);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   840
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   841
            break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   842
        case SDL_TEXTUREBLENDMODE_ADD:
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   843
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   844
            data->glEnable(GL_BLEND);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   845
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   846
            break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   847
        case SDL_TEXTUREBLENDMODE_MOD:
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   848
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   849
            data->glEnable(GL_BLEND);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   850
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   851
            break;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   852
        }
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   853
        data->blendMode = texture->blendMode;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   854
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   855
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   856
    if (texture->scaleMode != data->scaleMode) {
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   857
        switch (texture->scaleMode) {
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   858
        case SDL_TEXTURESCALEMODE_NONE:
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   859
        case SDL_TEXTURESCALEMODE_FAST:
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   860
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   861
                                  GL_NEAREST);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   862
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   863
                                  GL_NEAREST);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   864
            break;
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   865
        case SDL_TEXTURESCALEMODE_SLOW:
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1952
diff changeset
   866
        case SDL_TEXTURESCALEMODE_BEST:
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   867
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   868
                                  GL_LINEAR);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   869
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   870
                                  GL_LINEAR);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   871
            break;
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   872
        }
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1975
diff changeset
   873
        data->scaleMode = texture->scaleMode;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   874
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   875
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   876
    data->glBegin(GL_TRIANGLE_STRIP);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   877
    data->glTexCoord2f(minu, minv);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   878
    data->glVertex2i(minx, miny);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   879
    data->glTexCoord2f(maxu, minv);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   880
    data->glVertex2i(maxx, miny);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   881
    data->glTexCoord2f(minu, maxv);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   882
    data->glVertex2i(minx, maxy);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   883
    data->glTexCoord2f(maxu, maxv);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   884
    data->glVertex2i(maxx, maxy);
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   885
    data->glEnd();
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   886
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   887
    return 0;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   888
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   889
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   890
static void
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   891
GL_RenderPresent(SDL_Renderer * renderer)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   892
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   893
    SDL_GL_SwapWindow(renderer->window);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   894
}
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
static void
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   897
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   898
{
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   899
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   900
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   901
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   902
    if (!data) {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   903
        return;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   904
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   905
    if (data->texture) {
1927
aeb8263d377a OpenGL renderer is feature complete!
Sam Lantinga <slouken@libsdl.org>
parents: 1926
diff changeset
   906
        renderdata->glDeleteTextures(1, &data->texture);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   907
    }
1974
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   908
    if (data->palette) {
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   909
        SDL_free(data->palette);
70deaf574153 Added paletted OpenGL texture support.
Sam Lantinga <slouken@libsdl.org>
parents: 1970
diff changeset
   910
    }
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
   911
    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
   912
        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
   913
    }
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
   914
    SDL_FreeDirtyRects(&data->dirty);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   915
    SDL_free(data);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   916
    texture->driverdata = NULL;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   917
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   918
1975
ccef0d0c40c6 Added resize support for GDI and Direct3D renderers
Sam Lantinga <slouken@libsdl.org>
parents: 1974
diff changeset
   919
static void
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   920
GL_DestroyRenderer(SDL_Renderer * renderer)
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   921
{
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   922
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   923
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   924
    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
   925
        if (data->context) {
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
   926
            SDL_GL_MakeCurrent(0, NULL);
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
   927
            SDL_GL_DeleteContext(data->context);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   928
        }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   929
        SDL_free(data);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   930
    }
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   931
    SDL_free(renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   932
}
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   933
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents: 1928
diff changeset
   934
#endif /* SDL_VIDEO_RENDER_OGL */
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   935
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   936
/* vi: set ts=4 sw=4 expandtab: */