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