src/video/dummy/SDL_nullrender.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 14 Jun 2006 06:14:26 +0000
branchSDL-1.3
changeset 1678 90bf530ced8e
parent 1677 5e4c5e095925
child 1682 7ae8018b2e5d
permissions -rw-r--r--
SDL_SetVideoMode() compiles now...
slouken@1677
     1
/*
slouken@1677
     2
    SDL - Simple DirectMedia Layer
slouken@1677
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1677
     4
slouken@1677
     5
    This library is free software; you can redistribute it and/or
slouken@1677
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1677
     7
    License as published by the Free Software Foundation; either
slouken@1677
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1677
     9
slouken@1677
    10
    This library is distributed in the hope that it will be useful,
slouken@1677
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1677
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1677
    13
    Lesser General Public License for more details.
slouken@1677
    14
slouken@1677
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1677
    16
    License along with this library; if not, write to the Free Software
slouken@1677
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1677
    18
slouken@1677
    19
    Sam Lantinga
slouken@1677
    20
    slouken@libsdl.org
slouken@1677
    21
*/
slouken@1677
    22
#include "SDL_config.h"
slouken@1677
    23
slouken@1677
    24
#include "SDL_video.h"
slouken@1677
    25
#include "../SDL_sysvideo.h"
slouken@1677
    26
slouken@1677
    27
slouken@1677
    28
/* SDL surface based renderer implementation */
slouken@1677
    29
slouken@1677
    30
static SDL_Renderer *SDL_DUMMY_CreateRenderer(SDL_Window * window,
slouken@1677
    31
                                              Uint32 flags);
slouken@1677
    32
static int SDL_DUMMY_CreateTexture(SDL_Renderer * renderer,
slouken@1677
    33
                                   SDL_Texture * texture);
slouken@1677
    34
static int SDL_DUMMY_RenderReadPixels(SDL_Renderer * renderer,
slouken@1677
    35
                                      SDL_Rect * rect, void *pixels,
slouken@1677
    36
                                      int pitch);
slouken@1677
    37
static int SDL_DUMMY_RenderWritePixels(SDL_Renderer * renderer,
slouken@1677
    38
                                       SDL_Rect * rect, const void *pixels,
slouken@1677
    39
                                       int pitch);
slouken@1677
    40
static void SDL_DUMMY_RenderPresent(SDL_Renderer * renderer);
slouken@1677
    41
static void SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer);
slouken@1677
    42
slouken@1677
    43
slouken@1677
    44
SDL_RenderDriver SDL_DUMMY_RenderDriver = {
slouken@1677
    45
    SDL_DUMMY_CreateRenderer,
slouken@1677
    46
    {
slouken@1677
    47
     "minimal",
slouken@1678
    48
     (SDL_Renderer_Minimal | SDL_Renderer_PresentDiscard |
slouken@1678
    49
      SDL_Renderer_PresentCopy),
slouken@1677
    50
     SDL_TextureBlendMode_None,
slouken@1677
    51
     SDL_TextureScaleMode_None,
slouken@1677
    52
     0,
slouken@1677
    53
     {},
slouken@1677
    54
     0,
slouken@1677
    55
     0}
slouken@1677
    56
};
slouken@1677
    57
slouken@1677
    58
typedef struct
slouken@1677
    59
{
slouken@1677
    60
    SDL_Surface *screen;
slouken@1677
    61
} SDL_DUMMY_RenderData;
slouken@1677
    62
slouken@1677
    63
SDL_Renderer *
slouken@1677
    64
SDL_DUMMY_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1677
    65
{
slouken@1677
    66
    SDL_DisplayMode *displayMode = &window->display->current_mode;
slouken@1677
    67
    SDL_Renderer *renderer;
slouken@1677
    68
    SDL_DUMMY_RenderData *data;
slouken@1677
    69
    int i, n;
slouken@1677
    70
    int bpp;
slouken@1677
    71
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1677
    72
slouken@1677
    73
    if (!SDL_PixelFormatEnumToMasks
slouken@1677
    74
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1677
    75
        SDL_SetError("Unknown display format");
slouken@1677
    76
        return NULL;
slouken@1677
    77
    }
slouken@1677
    78
slouken@1677
    79
    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
slouken@1677
    80
    if (!renderer) {
slouken@1677
    81
        SDL_OutOfMemory();
slouken@1677
    82
        return NULL;
slouken@1677
    83
    }
slouken@1677
    84
    SDL_zerop(renderer);
slouken@1677
    85
slouken@1677
    86
    data = (SDL_DUMMY_RenderData *) SDL_malloc(sizeof(*data));
slouken@1677
    87
    if (!data) {
slouken@1677
    88
        SDL_DUMMY_DestroyRenderer(renderer);
slouken@1677
    89
        SDL_OutOfMemory();
slouken@1677
    90
        return NULL;
slouken@1677
    91
    }
slouken@1677
    92
    SDL_zerop(data);
slouken@1677
    93
slouken@1677
    94
    renderer->RenderReadPixels = SDL_DUMMY_RenderReadPixels;
slouken@1677
    95
    renderer->RenderWritePixels = SDL_DUMMY_RenderWritePixels;
slouken@1677
    96
    renderer->RenderPresent = SDL_DUMMY_RenderPresent;
slouken@1677
    97
    renderer->DestroyRenderer = SDL_DUMMY_DestroyRenderer;
slouken@1677
    98
    renderer->info = SDL_DUMMY_RenderDriver.info;
slouken@1677
    99
    renderer->window = window;
slouken@1677
   100
    renderer->driverdata = data;
slouken@1677
   101
slouken@1677
   102
    data->screen =
slouken@1677
   103
        SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
slouken@1677
   104
                             Bmask, Amask);
slouken@1677
   105
    if (!data->screen) {
slouken@1677
   106
        SDL_DUMMY_DestroyRenderer(renderer);
slouken@1677
   107
        return NULL;
slouken@1677
   108
    }
slouken@1677
   109
slouken@1677
   110
    return renderer;
slouken@1677
   111
}
slouken@1677
   112
slouken@1677
   113
int
slouken@1677
   114
SDL_DUMMY_RenderReadPixels(SDL_Renderer * renderer, SDL_Rect * rect,
slouken@1677
   115
                           void *pixels, int pitch)
slouken@1677
   116
{
slouken@1677
   117
    SDL_DUMMY_RenderData *data =
slouken@1677
   118
        (SDL_DUMMY_RenderData *) renderer->driverdata;
slouken@1677
   119
    SDL_Surface *surface = data->screen;
slouken@1677
   120
    Uint8 *src, *dst;
slouken@1677
   121
    int row;
slouken@1677
   122
    size_t length;
slouken@1677
   123
slouken@1677
   124
    src =
slouken@1677
   125
        (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1677
   126
        rect->x * surface->format->BytesPerPixel;
slouken@1677
   127
    dst = (Uint8 *) pixels;
slouken@1677
   128
    length = rect->w * surface->format->BytesPerPixel;
slouken@1677
   129
    for (row = 0; row < rect->h; ++row) {
slouken@1677
   130
        SDL_memcpy(dst, src, length);
slouken@1677
   131
        src += surface->pitch;
slouken@1677
   132
        dst += pitch;
slouken@1677
   133
    }
slouken@1677
   134
    return 0;
slouken@1677
   135
}
slouken@1677
   136
slouken@1677
   137
int
slouken@1677
   138
SDL_DUMMY_RenderWritePixels(SDL_Renderer * renderer, SDL_Rect * rect,
slouken@1677
   139
                            const void *pixels, int pitch)
slouken@1677
   140
{
slouken@1677
   141
    SDL_DUMMY_RenderData *data =
slouken@1677
   142
        (SDL_DUMMY_RenderData *) renderer->driverdata;
slouken@1677
   143
    SDL_Surface *surface = data->screen;
slouken@1677
   144
    Uint8 *src, *dst;
slouken@1677
   145
    int row;
slouken@1677
   146
    size_t length;
slouken@1677
   147
slouken@1677
   148
    src = (Uint8 *) pixels;
slouken@1677
   149
    dst =
slouken@1677
   150
        (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1677
   151
        rect->x * surface->format->BytesPerPixel;
slouken@1677
   152
    length = rect->w * surface->format->BytesPerPixel;
slouken@1677
   153
    for (row = 0; row < rect->h; ++row) {
slouken@1677
   154
        SDL_memcpy(dst, src, length);
slouken@1677
   155
        src += pitch;
slouken@1677
   156
        dst += surface->pitch;
slouken@1677
   157
    }
slouken@1677
   158
    return 0;
slouken@1677
   159
}
slouken@1677
   160
slouken@1677
   161
void
slouken@1677
   162
SDL_DUMMY_RenderPresent(SDL_Renderer * renderer)
slouken@1677
   163
{
slouken@1677
   164
    static int frame_number;
slouken@1677
   165
    SDL_DUMMY_RenderData *data =
slouken@1677
   166
        (SDL_DUMMY_RenderData *) renderer->driverdata;
slouken@1677
   167
    SDL_Surface *surface = data->screen;
slouken@1677
   168
slouken@1677
   169
    if (SDL_getenv("SDL_VIDEO_DUMMY_SAVE_FRAMES")) {
slouken@1677
   170
        char file[128];
slouken@1678
   171
        SDL_snprintf(file, sizeof(file), "SDL_frame-%8.8d.bmp",
slouken@1677
   172
                     ++frame_number);
slouken@1677
   173
        SDL_SaveBMP(surface, file);
slouken@1677
   174
    }
slouken@1677
   175
}
slouken@1677
   176
slouken@1677
   177
void
slouken@1677
   178
SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer)
slouken@1677
   179
{
slouken@1677
   180
    SDL_DUMMY_RenderData *data =
slouken@1677
   181
        (SDL_DUMMY_RenderData *) renderer->driverdata;
slouken@1677
   182
slouken@1677
   183
    if (data) {
slouken@1677
   184
        if (data->screen) {
slouken@1677
   185
            SDL_FreeSurface(data->screen);
slouken@1677
   186
        }
slouken@1677
   187
        SDL_free(data);
slouken@1677
   188
    }
slouken@1677
   189
    SDL_free(renderer);
slouken@1677
   190
}
slouken@1677
   191
slouken@1677
   192
/* vi: set ts=4 sw=4 expandtab: */