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