SDL-1.2.14
[sdl_omap.git] / src / video / riscos / SDL_riscossprite.c
1 /*
2     SDL - Simple DirectMedia Layer
3     Copyright (C) 1997-2009 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@libsdl.org
21 */
22 #include "SDL_config.h"
23
24 /*
25      File added by Alan Buckley (alan_baa@hotmail.com) for RISC OS compatability
26          27 March 2003
27
28      Implements Sprite plotting code for wimp display.window
29 */
30
31 #include "kernel.h"
32 #include "swis.h"
33
34 #include "SDL_stdinc.h"
35 #include "SDL_riscosvideo.h"
36
37 extern void WIMP_ReadModeInfo(_THIS);
38
39 void WIMP_PaletteChanged(_THIS);
40
41
42 /* Create sprite buffer for screen */
43
44 unsigned char *WIMP_CreateBuffer(int width, int height, int bpp)
45 {
46         int size;
47         char sprite_name[12] = "display";
48         unsigned char *buffer;
49         _kernel_swi_regs regs;
50         int bytesPerPixel;
51         int bytesPerRow;
52         int offsetToSpriteData = 60;
53
54         switch(bpp)
55         {
56         case 32: bytesPerPixel = 4; break;
57         case 16: bytesPerPixel = 2; break;
58         case 8:
59             bytesPerPixel = 1;
60             offsetToSpriteData += 2048; /* Add in size of palette */
61             break;
62         default:
63                 return NULL;
64                 break;
65         }
66
67         bytesPerRow = bytesPerPixel * width;
68
69         if ((bytesPerRow & 3) != 0)
70         {
71                 bytesPerRow += 4 - (bytesPerRow & 3);
72         }
73         size = bytesPerRow * height;
74
75         buffer = SDL_malloc( (size_t) size + offsetToSpriteData );
76         if (!buffer) return NULL;
77
78    /* Initialise a sprite area */
79
80         *(unsigned int *)buffer         = size + offsetToSpriteData;
81         *(unsigned int *)(buffer + 8)   = 16;
82
83         regs.r[0] = 256+9;
84         regs.r[1] = (unsigned int)buffer;
85    _kernel_swi(OS_SpriteOp, &regs, &regs);
86
87         regs.r[0] = 256+15;
88         regs.r[1] = (unsigned int)buffer;
89         regs.r[2] = (unsigned int)&sprite_name;
90         regs.r[3] = 0; /* Palette flag: 0 = no palette */
91         regs.r[4] = width;
92         regs.r[5] = height;
93         if (bpp == 8)
94         {
95                 /* Use old style mode number */
96                 regs.r[6] = 28; /* 8bpp 90x90dpi */
97         } else
98         {
99                 regs.r[6] = (((bpp == 16) ? 5 : 6) << 27) /* Type 6 = 32bpp sprite, 5 = 16bpp sprite */
100                                         | (90 << 14) /* Vertical dpi */
101                                         | (90 << 1)  /* Horizontal dpi */
102                                         | 1; /* Marker to distinguish between mode selectors and sprite modes */
103         }
104    if (_kernel_swi(OS_SpriteOp, &regs, &regs) == NULL)
105    {
106        if (bpp == 8)
107        {
108           /* Modify sprite to take into account 256 colour palette */
109           int *sprite = (int *)(buffer + 16);
110           /* Adjust sprite offsets */
111           sprite[0] += 2048;
112           sprite[8] += 2048;
113           sprite[9] += 2048;
114           /* Adjust sprite area next free pointer */
115           (*(int *)(buffer+12)) += 2048;
116
117           /* Don't need to set up palette as SDL sets up the default
118              256 colour palette */
119 /*          {
120              int *pal = sprite + 11;
121              unsigned int j;
122              unsigned int entry;
123              for (j = 0; j < 255; j++)
124              {
125                 entry = (j << 24) | (j << 16) | (j << 8);
126                 *pal++ = entry;
127                 *pal++ = entry;
128              }
129           }
130 */
131        }
132    } else
133    {
134       SDL_free(buffer);
135       buffer = NULL;
136    }
137
138    return buffer;
139 }
140
141
142 /* Setup translation buffers for the sprite plotting */
143
144 void WIMP_SetupPlotInfo(_THIS)
145 {
146    _kernel_swi_regs regs;
147    int *sprite = ((int *)this->hidden->bank[1])+4;
148
149    regs.r[0] = (unsigned int)this->hidden->bank[1];
150    regs.r[1] = (unsigned int)sprite;
151    regs.r[2] = -1; /* Current mode */
152    regs.r[3] = -1; /* Current palette */
153    regs.r[4] = 0; /* Get size of buffer */
154    regs.r[5] = 1|2|16; /* R1 - pointer to sprite and can use full palette words */
155    regs.r[6] = 0;
156    regs.r[7] = 0;
157
158    if (this->hidden->pixtrans) SDL_free(this->hidden->pixtrans);
159    this->hidden->pixtrans = 0;
160
161    /* Get the size required for the buffer */
162    _kernel_swi(ColourTrans_GenerateTable, &regs, &regs);
163    if (regs.r[4])
164    {
165       this->hidden->pixtrans = SDL_malloc(regs.r[4]);
166     
167       regs.r[4] = (unsigned int)this->hidden->pixtrans;
168       /* Actually read the buffer */
169       _kernel_swi(ColourTrans_GenerateTable, &regs, &regs);
170    }
171 }
172
173 /* Plot the sprite in the given context */
174 void WIMP_PlotSprite(_THIS, int x, int y)
175 {
176    _kernel_swi_regs regs;
177    _kernel_oserror *err;
178
179    regs.r[0] =  52 + 512;
180    regs.r[1] = (unsigned int)this->hidden->bank[1];
181    regs.r[2] = (unsigned int)this->hidden->bank[1]+16;
182    regs.r[3] = x;
183    regs.r[4] = y;
184    regs.r[5] = 0|32; /* Overwrite screen and pixtrans contains wide colour entries */
185    regs.r[6] = 0; /* No scale factors i.e. 1:1 */
186    regs.r[7] = (int)this->hidden->pixtrans;
187
188    if ((err = _kernel_swi(OS_SpriteOp, &regs, &regs)) != 0)
189    {
190       int *p = (int *)this->hidden->pixtrans;
191       printf("OS_SpriteOp failed \n%s\n",err->errmess);
192       printf("pixtrans %d\n", (int)this->hidden->pixtrans);
193       printf("%x %x %x\n", p[0], p[1], p[2]);
194    }
195 }
196
197
198 /* Wimp mode has changes so update colour mapping and pixel sizes 
199    of windows and the sprites they plot */
200
201 void WIMP_ModeChanged(_THIS)
202 {
203         int oldXeig = this->hidden->xeig;
204         int oldYeig = this->hidden->yeig;
205
206         WIMP_ReadModeInfo(this);
207
208         if (oldXeig == this->hidden->xeig && oldYeig == this->hidden->yeig)
209         {
210                 /* Only need to update the palette */
211                 WIMP_PaletteChanged(this);
212         } else
213         {
214                 _kernel_swi_regs regs;
215                 int window_state[9];
216                 int extent[4];
217                 int currWidth, currHeight;
218                 int newWidth, newHeight;
219                 
220                 /* Need to resize windows and update the palette */
221                 WIMP_SetupPlotInfo(this);
222
223
224                 window_state[0] = this->hidden->window_handle;
225                 regs.r[1] = (unsigned int)window_state;
226                 _kernel_swi(Wimp_GetWindowState, &regs, &regs);
227                                                 
228                 currWidth = window_state[3] - window_state[1];
229                 currHeight = window_state[4] - window_state[2];
230                 
231                 newWidth = (currWidth >> oldXeig) << this->hidden->xeig;
232                 newHeight = (currHeight >> oldYeig) << this->hidden->yeig;
233                 /* Need to avoid extent getting too small for visible part
234                 of window */
235                 extent[0] = 0;
236                 if (currHeight <= newHeight)
237                 {
238                         extent[1] = -newHeight;
239                 } else
240                 {
241                         extent[1] = -currHeight;
242                 }
243                 if (currWidth <= newWidth)
244                 {
245                         extent[2] = newWidth;
246                 } else
247                 {
248                         extent[2] = currWidth;
249                 }
250                 extent[3] = 0;
251                 
252                 regs.r[0] = this->hidden->window_handle;
253                 regs.r[1] = (int)extent;
254                 _kernel_swi(Wimp_SetExtent, &regs, &regs);
255
256                 /*TODO: May need to set flag to resize window on next open */
257         }
258 }
259
260 /* Palette has changed so update palettes used for windows sprites */
261
262 void WIMP_PaletteChanged(_THIS)
263 {
264         WIMP_SetupPlotInfo(this);
265 }