Updated to latest source.
[AROS-Contrib.git] / SDL / SDL_cgxaccel.c
blob18cf95edd9d5c11288bcfc43ee728c8dd591f278
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000 Sam Lantinga
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.
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.
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
19 Sam Lantinga
20 slouken@libsdl.org
23 #ifdef SAVE_RCSID
24 static char rcsid =
25 "@(#) $Id$";
26 #endif
28 #include "SDL_error.h"
29 #include "SDL_endian.h"
30 #include "SDL_sysvideo.h"
31 #include "SDL_blit.h"
32 #include "SDL_video.h"
33 #include "SDL_cgxvideo.h"
35 #ifdef __AROS__
36 #include <stdlib.h>
37 #endif
39 static int CGX_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
40 SDL_Surface *dst, SDL_Rect *dstrect);
42 // These are needed to avoid register troubles with gcc -O2!
44 #if 1
46 * it was: defined(__SASC) || defined(__PPC__) || defined(MORPHOS)
48 * This is a workaround for an old gcc 2.7.x bug...
51 #define BMKBRP(a,b,c,d,e,f,g,h,i,j) BltMaskBitMapRastPort(a,b,c,d,e,f,g,h,i,j)
52 #define BBRP(a,b,c,d,e,f,g,h,i) BltBitMapRastPort(a,b,c,d,e,f,g,h,i)
53 #define BBB(a,b,c,d,e,f,g,h,i,j,k) BltBitMap(a,b,c,d,e,f,g,h,i,j,k)
54 #else
55 void BMKBRP(struct BitMap *a,long b, long c,struct RastPort *d,long e,long f,long g,long h,unsigned long i,APTR j)
56 {BltMaskBitMapRastPort(a,b,c,d,e,f,g,h,i,j);}
58 void BBRP(struct BitMap *a,long b, long c,struct RastPort *d,long e,long f,long g,long h,unsigned long i)
59 {BltBitMapRastPort(a,b,c,d,e,f,g,h,i);}
61 void BBB(struct BitMap *a,long b, long c,struct BitMap *d,long e,long f,long g,long h,unsigned long i,unsigned long j,PLANEPTR k)
62 {BltBitMap(a,b,c,d,e,f,g,h,i,j,k);}
63 #endif
65 int CGX_SetHWColorKey(_THIS,SDL_Surface *surface, Uint32 key)
67 if(surface->hwdata)
69 if(surface->hwdata->mask)
70 free(surface->hwdata->mask);
72 if((surface->hwdata->mask=malloc(RASSIZE(surface->w,surface->h))))
74 Uint32 pitch,ok=0;
75 APTR lock;
77 memset(surface->hwdata->mask,255,RASSIZE(surface->w,surface->h));
79 D(bug("Building colorkey mask: color: %ld, size: %ld x %ld, %ld bytes...Bpp:%ld\n",key,surface->w,surface->h,RASSIZE(surface->w,surface->h),surface->format->BytesPerPixel));
81 if((lock=LockBitMapTags(surface->hwdata->bmap,LBMI_BASEADDRESS,(ULONG)&surface->pixels,
82 LBMI_BYTESPERROW,(ULONG)&pitch,TAG_DONE)))
84 switch(surface->format->BytesPerPixel)
86 case 1:
88 unsigned char k=key;
89 register int i,j,t;
90 register unsigned char *dest=surface->hwdata->mask,*map=surface->pixels;
92 pitch-=surface->w;
94 for(i=0;i<surface->h;i++)
96 for(t=128,j=0;j<surface->w;j++)
98 if(*map==k)
99 *dest&=~t;
101 t>>=1;
103 if(t==0)
105 dest++;
106 t=128;
108 map++;
110 map+=pitch;
113 break;
114 case 2:
116 Uint16 k=key,*mapw;
117 register int i,j,t;
118 register unsigned char *dest=surface->hwdata->mask,*map=surface->pixels;
120 for(i=surface->h;i;--i)
122 mapw=(Uint16 *)map;
124 for(t=128,j=surface->w;j;--j)
126 if(*mapw==k)
127 *dest&=~t;
129 t>>=1;
131 if(t==0)
133 dest++;
134 t=128;
136 mapw++;
138 map+=pitch;
141 break;
142 case 4:
144 Uint32 *mapl;
145 register int i,j,t;
146 register unsigned char *dest=surface->hwdata->mask,*map=surface->pixels;
148 for(i=surface->h;i;--i)
150 mapl=(Uint32 *)map;
152 for(t=128,j=surface->w;j;--j)
154 if(*mapl==key)
155 *dest&=~t;
157 t>>=1;
159 if(t==0)
161 dest++;
162 t=128;
164 mapl++;
166 map+=pitch;
170 break;
171 default:
172 D(bug("Pixel mode non supported for color key..."));
173 free(surface->hwdata->mask);
174 surface->hwdata->mask=NULL;
175 ok=-1;
177 UnLockBitMap(lock);
178 D(bug("...Colorkey built!\n"));
179 return ok;
183 D(bug("HW colorkey not supported for this depth\n"));
185 return -1;
188 int CGX_CheckHWBlit(_THIS,SDL_Surface *src,SDL_Surface *dst)
190 // Doesn't support yet alpha blitting
192 if(src->hwdata&& !(src->flags & (SDL_SRCALPHA)))
194 D(bug("CheckHW blit... OK!\n"));
196 if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
197 if ( CGX_SetHWColorKey(this, src, src->format->colorkey) < 0 ) {
198 src->flags &= ~SDL_HWACCEL;
199 return -1;
203 src->flags|=SDL_HWACCEL;
204 src->map->hw_blit = CGX_HWAccelBlit;
205 return 1;
207 else
208 src->flags &= ~SDL_HWACCEL;
210 D(bug("CheckHW blit... NO!\n"));
212 return 0;
215 static int temprp_init=0;
216 static struct RastPort temprp;
218 static int CGX_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
219 SDL_Surface *dst, SDL_Rect *dstrect)
221 struct SDL_VideoDevice *this=src->hwdata->videodata;
223 // D(bug("Accel blit!\n"));
225 if(src->flags&SDL_SRCCOLORKEY && src->hwdata->mask)
227 if(dst==SDL_VideoSurface)
229 BMKBRP(src->hwdata->bmap,srcrect->x,srcrect->y,
230 SDL_RastPort,dstrect->x+SDL_Window->BorderLeft,dstrect->y+SDL_Window->BorderTop,
231 srcrect->w,srcrect->h,0xc0,src->hwdata->mask);
233 else if(dst->hwdata)
235 if(!temprp_init)
237 InitRastPort(&temprp);
238 temprp_init=1;
240 temprp.BitMap=(struct BitMap *)dst->hwdata->bmap;
242 BMKBRP(src->hwdata->bmap,srcrect->x,srcrect->y,
243 &temprp,dstrect->x,dstrect->y,
244 srcrect->w,srcrect->h,0xc0,src->hwdata->mask);
248 else if(dst==SDL_VideoSurface)
250 BBRP(src->hwdata->bmap,srcrect->x,srcrect->y,SDL_RastPort,dstrect->x+SDL_Window->BorderLeft,dstrect->y+SDL_Window->BorderTop,srcrect->w,srcrect->h,0xc0);
252 else if(dst->hwdata)
253 BBB(src->hwdata->bmap,srcrect->x,srcrect->y,dst->hwdata->bmap,dstrect->x,dstrect->y,srcrect->w,srcrect->h,0xc0,0xff,NULL);
255 return 0;
258 int CGX_FillHWRect(_THIS,SDL_Surface *dst,SDL_Rect *dstrect,Uint32 color)
260 if(dst==SDL_VideoSurface)
262 FillPixelArray(SDL_RastPort,dstrect->x+SDL_Window->BorderLeft,dstrect->y+SDL_Window->BorderTop,dstrect->w,dstrect->h,color);
264 else if(dst->hwdata)
266 if(!temprp_init)
268 InitRastPort(&temprp);
269 temprp_init=1;
272 temprp.BitMap=(struct BitMap *)dst->hwdata->bmap;
274 FillPixelArray(&temprp,dstrect->x,dstrect->y,dstrect->w,dstrect->h,color);
276 return 0;