0
|
1 /*
|
|
2 SDL - Simple DirectMedia Layer
|
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
|
|
4
|
|
5 This library is free software; you can redistribute it and/or
|
|
6 modify it under the terms of the GNU Library General Public
|
|
7 License as published by the Free Software Foundation; either
|
|
8 version 2 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 Library General Public License for more details.
|
|
14
|
|
15 You should have received a copy of the GNU Library General Public
|
|
16 License along with this library; if not, write to the Free
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18
|
|
19 Sam Lantinga
|
|
20 slouken@devolution.com
|
|
21 */
|
|
22
|
|
23 #ifdef SAVE_RCSID
|
|
24 static char rcsid =
|
|
25 "@(#) $Id$";
|
|
26 #endif
|
|
27
|
|
28 #include <stdlib.h>
|
|
29 #include <Ph.h>
|
|
30 #include <photon/Pg.h>
|
|
31
|
|
32 #include "SDL_error.h"
|
|
33 #include "SDL_endian.h"
|
|
34 #include "SDL_ph_image_c.h"
|
|
35
|
|
36 //printf("%s:%s:%d\n", __FILE__, __PRETTY_FUNCTION__, __LINE__ );
|
|
37
|
|
38 /* Various screen update functions available */
|
|
39 //static void ph_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
|
|
40 //static void ph_DummyUpdate(_THIS, int numrects, SDL_Rect *rects);
|
|
41
|
|
42 int ph_SetupImage(_THIS, SDL_Surface *screen)
|
|
43 {
|
|
44 int type = 0;
|
|
45
|
|
46 /* Determine image type */
|
|
47 switch(screen->format->BitsPerPixel)
|
|
48 {
|
|
49 case 8:{
|
|
50 type = Pg_IMAGE_PALETTE_BYTE;
|
|
51 }
|
|
52 break;
|
|
53 case 15:{
|
|
54 type = Pg_IMAGE_DIRECT_555;
|
|
55 }
|
|
56 break;
|
|
57 case 16:{
|
|
58 type = Pg_IMAGE_DIRECT_565;
|
|
59 }
|
|
60 break;
|
|
61
|
|
62 case 24:{
|
|
63 type = Pg_IMAGE_DIRECT_888;
|
|
64 }
|
|
65 break;
|
|
66
|
|
67 case 32:{
|
|
68 type = Pg_IMAGE_DIRECT_8888;
|
|
69 }
|
|
70 break;
|
|
71 default:{
|
|
72 /* should never get here */
|
|
73 fprintf(stderr,"error: unsupported bbp = %d\n",
|
|
74 screen->format->BitsPerPixel);
|
|
75 return -1;
|
|
76 }
|
|
77 break;
|
|
78 }
|
|
79
|
|
80 //using shared memory for speed (set last param to 1)
|
|
81 if ((SDL_Image = PhCreateImage( NULL, screen->w, screen->h, type, NULL, 0, 1 ))
|
|
82 == NULL)
|
|
83 {
|
|
84 fprintf(stderr,"error: PhCreateImage failed.\n");
|
|
85 return -1;
|
|
86 }
|
|
87
|
|
88 screen->pixels = SDL_Image->image;
|
|
89
|
|
90 this->UpdateRects = ph_NormalUpdate;
|
|
91
|
|
92 return 0;
|
|
93 }
|
|
94
|
|
95 int ph_SetupOCImage(_THIS, SDL_Surface *screen) //Offscreen context
|
|
96 {
|
|
97 int type = 0;
|
|
98
|
|
99 /* Determine image type */
|
|
100 switch(screen->format->BitsPerPixel)
|
|
101 {
|
|
102 case 8:{
|
|
103 type = Pg_IMAGE_PALETTE_BYTE;
|
|
104 }
|
|
105 break;
|
|
106 case 15:{
|
|
107 type = Pg_IMAGE_DIRECT_555;
|
|
108 }
|
|
109 break;
|
|
110 case 16:{
|
|
111 type = Pg_IMAGE_DIRECT_565;
|
|
112 }
|
|
113 break;
|
|
114
|
|
115 case 24:{
|
|
116 type = Pg_IMAGE_DIRECT_888;
|
|
117 }
|
|
118 break;
|
|
119
|
|
120 case 32:{
|
|
121 type = Pg_IMAGE_DIRECT_8888;
|
|
122 }
|
|
123 break;
|
|
124 default:{
|
|
125 /* should never get here */
|
|
126 fprintf(stderr,"error: unsupported bbp = %d\n",
|
|
127 screen->format->BitsPerPixel);
|
|
128 return -1;
|
|
129 }
|
|
130 break;
|
|
131 }
|
|
132
|
|
133 OCImage.FrameData0 = (FRAMEDATA *) malloc((size_t)(sizeof( FRAMEDATA)));
|
|
134 OCImage.FrameData1 = (FRAMEDATA *) malloc((size_t)(sizeof( FRAMEDATA)));
|
|
135
|
|
136 if(OCImage.direct_context == NULL)
|
|
137 OCImage.direct_context = PdCreateDirectContext();
|
|
138
|
|
139 OCImage.offscreen_context = PdCreateOffscreenContext(0,screen->w,screen->h, Pg_OSC_MEM_PAGE_ALIGN);
|
|
140
|
|
141 if (OCImage.offscreen_context == NULL)
|
|
142 {
|
|
143 printf("PdCreateOffscreenContext failed\n");
|
|
144 return -1;
|
|
145 }
|
|
146
|
|
147 OCImage.Stride = OCImage.offscreen_context->pitch;
|
|
148
|
|
149 if (OCImage.flags & SDL_DOUBLEBUF)
|
|
150 printf("hardware flag for doublebuf offscreen context\n");
|
|
151
|
|
152
|
|
153 OCImage.dc_ptr.ptr8 = (unsigned char *) PdGetOffscreenContextPtr(OCImage.offscreen_context);
|
|
154
|
|
155 OCImage.CurrentFrameData = OCImage.FrameData0;
|
|
156 OCImage.CurrentFrameData->Y = OCImage.dc_ptr.ptr8;
|
|
157 OCImage.CurrentFrameData->U = NULL;
|
|
158 OCImage.CurrentFrameData->V = NULL;
|
|
159 OCImage.current = 0;
|
|
160
|
|
161 if(OCImage.dc_ptr.ptr8 == NULL)
|
|
162 {
|
|
163 printf("PdGetOffscreenContextPtr failed\n");
|
|
164 return -1;
|
|
165 }
|
|
166
|
|
167 PhDCSetCurrent(OCImage.offscreen_context);
|
|
168
|
|
169 screen->pixels = OCImage.CurrentFrameData->Y;
|
|
170
|
|
171 this->UpdateRects = ph_OCUpdate;
|
|
172
|
|
173 return 0;
|
|
174 }
|
|
175
|
|
176
|
|
177 void ph_DestroyImage(_THIS, SDL_Surface *screen)
|
|
178 {
|
|
179
|
|
180
|
|
181 if(SDL_Image == NULL)
|
|
182 return;
|
|
183
|
|
184 if (OCImage.offscreen_context != NULL)
|
|
185 {
|
|
186
|
|
187 PhDCRelease(OCImage.offscreen_context);
|
|
188 OCImage.offscreen_context = NULL;
|
|
189 free(OCImage.FrameData0);
|
|
190 OCImage.FrameData0 = NULL;
|
|
191 free(OCImage.FrameData1);
|
|
192 OCImage.FrameData1 = NULL;
|
|
193 }
|
|
194
|
|
195 if (SDL_Image->image)
|
|
196 {
|
|
197 //free(SDL_Image->image);
|
|
198 //SDL_Image->image = NULL;
|
|
199 PhReleaseImage(SDL_Image);
|
|
200 SDL_Image = NULL;
|
|
201 }
|
|
202
|
|
203 if ( screen ) {
|
|
204 screen->pixels = NULL;
|
|
205 }
|
|
206
|
|
207 SDL_Image = NULL;
|
|
208 }
|
|
209
|
|
210 int ph_ResizeImage(_THIS, SDL_Surface *screen, Uint32 flags)
|
|
211 {
|
|
212
|
|
213 ph_DestroyImage(this, screen);
|
|
214
|
|
215 if( flags & SDL_HWSURFACE)
|
|
216 {
|
|
217 OCImage.flags = flags; //needed for SDL_DOUBLEBUF check
|
|
218 return ph_SetupOCImage(this,screen);
|
|
219 }
|
|
220 else if(flags & SDL_OPENGL) /* No image when using GL */
|
|
221 {
|
|
222 return 0;
|
|
223 }
|
|
224 else
|
|
225 {
|
|
226 return ph_SetupImage(this, screen);
|
|
227 }
|
|
228
|
|
229 }
|
|
230
|
|
231 int ph_AllocHWSurface(_THIS, SDL_Surface *surface)
|
|
232 {
|
|
233
|
|
234 return(-1);
|
|
235 }
|
|
236
|
|
237 void ph_FreeHWSurface(_THIS, SDL_Surface *surface)
|
|
238 {
|
|
239
|
|
240 return;
|
|
241 }
|
|
242
|
|
243 int ph_FlipHWSurface(_THIS, SDL_Surface *surface)
|
|
244 {
|
|
245
|
|
246 return(0);
|
|
247 }
|
|
248
|
|
249 int ph_LockHWSurface(_THIS, SDL_Surface *surface)
|
|
250 {
|
|
251
|
|
252 if ( (surface == SDL_VideoSurface) && blit_queued ) {
|
|
253 // XSync(GFX_Display, False);
|
|
254 PgFlush();
|
|
255 blit_queued = 0;
|
|
256 }
|
|
257 return(0);
|
|
258 }
|
|
259
|
|
260 void ph_UnlockHWSurface(_THIS, SDL_Surface *surface)
|
|
261 {
|
|
262
|
|
263 return;
|
|
264 }
|
|
265
|
|
266 static PhPoint_t ph_pos;
|
|
267 static PhRect_t ph_rect;
|
|
268 static int i;
|
|
269
|
|
270 void ph_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
|
|
271 {
|
|
272
|
|
273 for ( i=0; i<numrects; ++i )
|
|
274 {
|
|
275 if ( rects[i].w == 0 ) { /* Clipped? */
|
|
276 continue;
|
|
277 }
|
|
278
|
|
279 ph_pos.x = rects[i].x;
|
|
280 ph_pos.y = rects[i].y;
|
|
281 ph_rect.ul.x = rects[i].x;
|
|
282 ph_rect.ul.y = rects[i].y;
|
|
283 ph_rect.lr.x = rects[i].x + rects[i].w;
|
|
284 ph_rect.lr.y = rects[i].y + rects[i].h;
|
|
285
|
|
286 if (PgDrawPhImageRectmx( &ph_pos, SDL_Image, &ph_rect, 0 ) < 0)
|
|
287 {
|
|
288 fprintf(stderr,"error: PgDrawPhImageRectmx failed.\n");
|
|
289 }
|
|
290 }
|
|
291 if (PgFlush() < 0)
|
|
292 {
|
|
293 fprintf(stderr,"error: PgFlush failed.\n");
|
|
294 }
|
|
295 }
|
|
296 void ph_OCUpdate(_THIS, int numrects, SDL_Rect *rects)
|
|
297 {
|
|
298 PhPoint_t zero = {0};
|
|
299 PhRect_t src_rect;
|
|
300 PhRect_t dest_rect;
|
|
301
|
|
302 if(OCImage.direct_context == NULL)
|
|
303 {
|
|
304 return;
|
|
305 }
|
|
306
|
|
307 PgSetRegion(PtWidgetRid(window));
|
|
308 PgSetClipping(0,NULL);
|
|
309 PgWaitHWIdle();
|
|
310
|
|
311
|
|
312 for ( i=0; i<numrects; ++i )
|
|
313 {
|
|
314 if ( rects[i].w == 0 ) { /* Clipped? */
|
|
315 continue;
|
|
316 }
|
|
317
|
|
318 src_rect.ul.x=rects[i].x;
|
|
319 src_rect.ul.y=rects[i].y;
|
|
320 dest_rect.ul.x=rects[i].x;
|
|
321 dest_rect.ul.y=rects[i].y;
|
|
322
|
|
323 dest_rect.lr.x=src_rect.lr.x= rects[i].x +rects[i].w;
|
|
324 dest_rect.lr.y=src_rect.lr.y= rects[i].y +rects[i].h;
|
|
325
|
|
326 zero.x = zero.y = 0;
|
|
327 PgSetTranslation (&zero, 0);
|
|
328 PgSetRegion(PtWidgetRid(window));
|
|
329 PgSetClipping(0,NULL);
|
|
330 PgContextBlitArea(OCImage.offscreen_context, (PhArea_t *)(&src_rect), NULL, (PhArea_t *)(&dest_rect));
|
|
331
|
|
332 }
|
|
333 if (PgFlush() < 0)
|
|
334 {
|
|
335 fprintf(stderr,"error: PgFlush failed.\n");
|
|
336 }
|
|
337
|
|
338 //later used to toggling double buffer
|
|
339 if(OCImage.current == 0)
|
|
340 {
|
|
341 OCImage.CurrentFrameData = OCImage.FrameData0;
|
|
342 }
|
|
343 else
|
|
344 {
|
|
345 OCImage.CurrentFrameData = OCImage.FrameData1;
|
|
346 }
|
|
347 }
|
|
348
|