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