comparison src/video/dummy/SDL_nullrender.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents
children 0c49855a7a3e
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
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 const SDL_Rect * rect, void *pixels,
36 int pitch);
37 static int SDL_DUMMY_RenderWritePixels(SDL_Renderer * renderer,
38 const SDL_Rect * rect,
39 const void *pixels, 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 {0},
54 0,
55 0}
56 };
57
58 typedef struct
59 {
60 SDL_Surface *surface;
61 } SDL_DUMMY_RenderData;
62
63 SDL_Renderer *
64 SDL_DUMMY_CreateRenderer(SDL_Window * window, Uint32 flags)
65 {
66 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
67 SDL_DisplayMode *displayMode = &display->current_mode;
68 SDL_Renderer *renderer;
69 SDL_DUMMY_RenderData *data;
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->id;
100 renderer->driverdata = data;
101
102 data->surface =
103 SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
104 Bmask, Amask);
105 if (!data->surface) {
106 SDL_DUMMY_DestroyRenderer(renderer);
107 return NULL;
108 }
109 SDL_SetSurfacePalette(data->surface, display->palette);
110
111 return renderer;
112 }
113
114 int
115 SDL_DUMMY_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
116 void *pixels, int pitch)
117 {
118 SDL_DUMMY_RenderData *data =
119 (SDL_DUMMY_RenderData *) renderer->driverdata;
120 SDL_Surface *surface = data->surface;
121 Uint8 *src, *dst;
122 int row;
123 size_t length;
124
125 src =
126 (Uint8 *) surface->pixels + rect->y * surface->pitch +
127 rect->x * surface->format->BytesPerPixel;
128 dst = (Uint8 *) pixels;
129 length = rect->w * surface->format->BytesPerPixel;
130 for (row = 0; row < rect->h; ++row) {
131 SDL_memcpy(dst, src, length);
132 src += surface->pitch;
133 dst += pitch;
134 }
135 return 0;
136 }
137
138 int
139 SDL_DUMMY_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
140 const void *pixels, int pitch)
141 {
142 SDL_DUMMY_RenderData *data =
143 (SDL_DUMMY_RenderData *) renderer->driverdata;
144 SDL_Surface *surface = data->surface;
145 Uint8 *src, *dst;
146 int row;
147 size_t length;
148
149 src = (Uint8 *) pixels;
150 dst =
151 (Uint8 *) surface->pixels + rect->y * surface->pitch +
152 rect->x * surface->format->BytesPerPixel;
153 length = rect->w * surface->format->BytesPerPixel;
154 for (row = 0; row < rect->h; ++row) {
155 SDL_memcpy(dst, src, length);
156 src += pitch;
157 dst += surface->pitch;
158 }
159 return 0;
160 }
161
162 void
163 SDL_DUMMY_RenderPresent(SDL_Renderer * renderer)
164 {
165 static int frame_number;
166 SDL_DUMMY_RenderData *data =
167 (SDL_DUMMY_RenderData *) renderer->driverdata;
168 SDL_Surface *surface = data->surface;
169
170 if (SDL_getenv("SDL_VIDEO_DUMMY_SAVE_FRAMES")) {
171 char file[128];
172 SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
173 renderer->window, ++frame_number);
174 SDL_SaveBMP(surface, file);
175 }
176 }
177
178 void
179 SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer)
180 {
181 SDL_DUMMY_RenderData *data =
182 (SDL_DUMMY_RenderData *) renderer->driverdata;
183
184 if (data) {
185 if (data->surface) {
186 data->surface->format->palette = NULL;
187 SDL_FreeSurface(data->surface);
188 }
189 SDL_free(data);
190 }
191 SDL_free(renderer);
192 }
193
194 /* vi: set ts=4 sw=4 expandtab: */