src/video/riscos/SDL_riscossprite.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 05 Nov 2003 23:11:05 +0000
changeset 733 9557ac1857a3
parent 630 550bccdf04bd
child 769 b8d311d90021
permissions -rw-r--r--
Date: Mon, 13 Oct 2003 13:37:18 +0100
From: "alan buckley"
Subject: Modification for RISC OS SDL port (SDL_riscosevents.c)

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