comparison src/video/dummy/SDL_nullrender.c @ 1677:5e4c5e095925 SDL-1.3

Added new style dummy renderer
author Sam Lantinga <slouken@libsdl.org>
date Tue, 13 Jun 2006 04:37:45 +0000
parents
children 90bf530ced8e
comparison
equal deleted inserted replaced
1676:e136f3ffdc1b 1677:5e4c5e095925
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_PresentCopy),
49 SDL_TextureBlendMode_None,
50 SDL_TextureScaleMode_None,
51 0,
52 {},
53 0,
54 0}
55 };
56
57 typedef struct
58 {
59 SDL_Surface *screen;
60 } SDL_DUMMY_RenderData;
61
62 SDL_Renderer *
63 SDL_DUMMY_CreateRenderer(SDL_Window * window, Uint32 flags)
64 {
65 SDL_DisplayMode *displayMode = &window->display->current_mode;
66 SDL_Renderer *renderer;
67 SDL_DUMMY_RenderData *data;
68 int i, n;
69 int bpp;
70 Uint32 Rmask, Gmask, Bmask, Amask;
71
72 if (!SDL_PixelFormatEnumToMasks
73 (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
74 SDL_SetError("Unknown display format");
75 return NULL;
76 }
77
78 renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
79 if (!renderer) {
80 SDL_OutOfMemory();
81 return NULL;
82 }
83 SDL_zerop(renderer);
84
85 data = (SDL_DUMMY_RenderData *) SDL_malloc(sizeof(*data));
86 if (!data) {
87 SDL_DUMMY_DestroyRenderer(renderer);
88 SDL_OutOfMemory();
89 return NULL;
90 }
91 SDL_zerop(data);
92
93 renderer->RenderReadPixels = SDL_DUMMY_RenderReadPixels;
94 renderer->RenderWritePixels = SDL_DUMMY_RenderWritePixels;
95 renderer->RenderPresent = SDL_DUMMY_RenderPresent;
96 renderer->DestroyRenderer = SDL_DUMMY_DestroyRenderer;
97 renderer->info = SDL_DUMMY_RenderDriver.info;
98 renderer->window = window;
99 renderer->driverdata = data;
100
101 data->screen =
102 SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
103 Bmask, Amask);
104 if (!data->screen) {
105 SDL_DUMMY_DestroyRenderer(renderer);
106 return NULL;
107 }
108
109 return renderer;
110 }
111
112 int
113 SDL_DUMMY_RenderReadPixels(SDL_Renderer * renderer, SDL_Rect * rect,
114 void *pixels, int pitch)
115 {
116 SDL_DUMMY_RenderData *data =
117 (SDL_DUMMY_RenderData *) renderer->driverdata;
118 SDL_Surface *surface = data->screen;
119 Uint8 *src, *dst;
120 int row;
121 size_t length;
122
123 src =
124 (Uint8 *) surface->pixels + rect->y * surface->pitch +
125 rect->x * surface->format->BytesPerPixel;
126 dst = (Uint8 *) pixels;
127 length = rect->w * surface->format->BytesPerPixel;
128 for (row = 0; row < rect->h; ++row) {
129 SDL_memcpy(dst, src, length);
130 src += surface->pitch;
131 dst += pitch;
132 }
133 return 0;
134 }
135
136 int
137 SDL_DUMMY_RenderWritePixels(SDL_Renderer * renderer, SDL_Rect * rect,
138 const void *pixels, int pitch)
139 {
140 SDL_DUMMY_RenderData *data =
141 (SDL_DUMMY_RenderData *) renderer->driverdata;
142 SDL_Surface *surface = data->screen;
143 Uint8 *src, *dst;
144 int row;
145 size_t length;
146
147 src = (Uint8 *) pixels;
148 dst =
149 (Uint8 *) surface->pixels + rect->y * surface->pitch +
150 rect->x * surface->format->BytesPerPixel;
151 length = rect->w * surface->format->BytesPerPixel;
152 for (row = 0; row < rect->h; ++row) {
153 SDL_memcpy(dst, src, length);
154 src += pitch;
155 dst += surface->pitch;
156 }
157 return 0;
158 }
159
160 void
161 SDL_DUMMY_RenderPresent(SDL_Renderer * renderer)
162 {
163 static int frame_number;
164 SDL_DUMMY_RenderData *data =
165 (SDL_DUMMY_RenderData *) renderer->driverdata;
166 SDL_Surface *surface = data->screen;
167
168 if (SDL_getenv("SDL_VIDEO_DUMMY_SAVE_FRAMES")) {
169 char file[128];
170 SDL_snprintf(file, sizeof(file), "SDL_screen-%8.8d.bmp",
171 ++frame_number);
172 SDL_SaveBMP(surface, file);
173 }
174 }
175
176 void
177 SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer)
178 {
179 SDL_DUMMY_RenderData *data =
180 (SDL_DUMMY_RenderData *) renderer->driverdata;
181
182 if (data) {
183 if (data->screen) {
184 SDL_FreeSurface(data->screen);
185 }
186 SDL_free(data);
187 }
188 SDL_free(renderer);
189 }
190
191 /* vi: set ts=4 sw=4 expandtab: */