src/video/riscos/SDL_riscosFullScreenVideo.c
author Cameron Cawley <ccawley2011@gmail.com>
Tue, 18 Jun 2019 23:55:01 +0100
branchSDL-1.2
changeset 12913 d07b4feae69c
parent 12786 4e73be7b4787
permissions -rw-r--r--
riscos: Improve error reporting
slouken@630
     1
/*
slouken@630
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 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@1312
    20
    slouken@libsdl.org
slouken@630
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@630
    23
slouken@630
    24
/*
slouken@1035
    25
     File added by Alan Buckley (alan_baa@hotmail.com) for RISC OS compatability
slouken@630
    26
	 27 March 2003
slouken@630
    27
slouken@1035
    28
     Implements RISC OS full screen display.
slouken@630
    29
*/
slouken@630
    30
slouken@630
    31
#include "SDL_video.h"
slouken@630
    32
#include "SDL_mouse.h"
slouken@1361
    33
#include "../SDL_sysvideo.h"
slouken@1361
    34
#include "../SDL_pixels_c.h"
slouken@1361
    35
#include "../../events/SDL_events_c.h"
slouken@630
    36
slouken@630
    37
#include "SDL_riscostask.h"
slouken@630
    38
#include "SDL_riscosvideo.h"
slouken@630
    39
#include "SDL_riscosevents_c.h"
slouken@630
    40
#include "SDL_riscosmouse_c.h"
slouken@630
    41
slouken@630
    42
#include "kernel.h"
slouken@630
    43
#include "swis.h"
slouken@630
    44
#include "unixlib/local.h"
slouken@630
    45
slouken@630
    46
/* Private structures */
slouken@630
    47
typedef struct tagScreenModeBlock
slouken@630
    48
{
slouken@630
    49
   int flags;  // mode selector flags, bit 0 = 1, bit 1-7 format specifier, 8-31 reserved
slouken@630
    50
   int x_pixels;
slouken@630
    51
   int y_pixels;
slouken@630
    52
   int pixel_depth;  // 2^pixel_depth = bpp,i.e. 0 = 1, 1 = 2, 4 = 16, 5 = 32
slouken@630
    53
   int frame_rate;   // -1 use first match
me@12786
    54
   int mode_vars[7]; // array of index, value pairs terminated by -1
slouken@630
    55
} SCREENMODEBLOCK;
slouken@630
    56
slouken@630
    57
slouken@630
    58
/* Helper functions */
me@12786
    59
static void FULLSCREEN_SetupBanks(_THIS);
slouken@630
    60
slouken@630
    61
/* SDL video device functions for fullscreen mode */
slouken@630
    62
static int FULLSCREEN_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
slouken@630
    63
static int FULLSCREEN_FlipHWSurface(_THIS, SDL_Surface *surface);
me@12786
    64
static void FULLSCREEN_SetWMCaption(_THIS, const char *title, const char *icon);
slouken@630
    65
slouken@1035
    66
/* UpdateRects variants */
slouken@1035
    67
static void FULLSCREEN_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
slouken@1035
    68
static void FULLSCREEN_UpdateRectsMemCpy(_THIS, int numrects, SDL_Rect *rects);
slouken@1035
    69
static void FULLSCREEN_UpdateRects8bpp(_THIS, int numrects, SDL_Rect *rects);
slouken@1035
    70
static void FULLSCREEN_UpdateRects16bpp(_THIS, int numrects, SDL_Rect *rects);
slouken@1035
    71
static void FULLSCREEN_UpdateRects32bpp(_THIS, int numrects, SDL_Rect *rects);
slouken@1035
    72
static void FULLSCREEN_UpdateRectsOS(_THIS, int numrects, SDL_Rect *rects);
slouken@1035
    73
slouken@630
    74
/* Local helper functions */
slouken@630
    75
static int cmpmodes(const void *va, const void *vb);
slouken@630
    76
static int FULLSCREEN_AddMode(_THIS, int bpp, int w, int h);
me@12786
    77
static void FULLSCREEN_SetWriteBank(int bank);
me@12786
    78
static void FULLSCREEN_SetDisplayBank(int bank);
slouken@630
    79
static void FULLSCREEN_DisableEscape();
slouken@630
    80
static void FULLSCREEN_EnableEscape();
slouken@630
    81
slouken@630
    82
SDL_Surface *FULLSCREEN_SetVideoMode(_THIS, SDL_Surface *current,
slouken@630
    83
				int width, int height, int bpp, Uint32 flags)
slouken@630
    84
{
slouken@630
    85
   _kernel_swi_regs regs;
slouken@630
    86
   int create_back_buffer = riscos_backbuffer;
slouken@630
    87
me@12786
    88
   const RISCOS_SDL_PixelFormat *fmt = FULLSCREEN_SetMode(width, height, bpp);
me@12786
    89
   if (fmt == NULL)
slouken@630
    90
   {
slouken@630
    91
	   return (NULL);
slouken@630
    92
   }
slouken@630
    93
slouken@630
    94
/* 	printf("Setting mode %dx%d\n", width, height); */
slouken@630
    95
slouken@630
    96
	/* Allocate the new pixel format for the screen */
me@12786
    97
	if ( ! SDL_ReallocFormat(current, fmt->sdl_bpp, fmt->rmask, fmt->gmask, fmt->bmask, 0) ) {
ccawley2011@12913
    98
		RISCOS_RestoreWimpMode();
slouken@630
    99
		return(NULL);
slouken@630
   100
	}
slouken@630
   101
slouken@630
   102
	/* Set up the new mode framebuffer */
slouken@630
   103
	current->w = width;
slouken@630
   104
	this->hidden->height = current->h = height;
slouken@630
   105
slouken@630
   106
   regs.r[0] = -1; /* -1 for current screen mode */
slouken@630
   107
slouken@630
   108
   /* Get screen width in bytes */
slouken@630
   109
   regs.r[1] = 6; // Screen Width in bytes
slouken@630
   110
   _kernel_swi(OS_ReadModeVariable, &regs, &regs);
slouken@630
   111
slouken@630
   112
   current->pitch = regs.r[2];
slouken@630
   113
slouken@630
   114
   if (flags & SDL_DOUBLEBUF)
slouken@630
   115
   {
slouken@630
   116
	   regs.r[0] = 2; /* Screen area */
slouken@630
   117
	   _kernel_swi(OS_ReadDynamicArea, &regs, &regs);
slouken@630
   118
	   
slouken@630
   119
	   /* Reg 1 has amount of memory currently used for display */
slouken@630
   120
	   regs.r[0] = 2; /* Screen area */
slouken@630
   121
	   regs.r[1] = (current->pitch * height * 2) - regs.r[1];
slouken@630
   122
	   if (_kernel_swi(OS_ChangeDynamicArea, &regs, &regs) != NULL)
slouken@630
   123
	   {
slouken@630
   124
		   /* Can't allocate enough screen memory for double buffer */
slouken@630
   125
		   flags &= ~SDL_DOUBLEBUF;
slouken@630
   126
	   }
slouken@630
   127
   }
me@12786
   128
me@12786
   129
   if (fmt->sdl_bpp == 8)
me@12786
   130
   {
me@12786
   131
      flags |= SDL_HWPALETTE;
me@12786
   132
   }
slouken@630
   133
   
slouken@630
   134
  	current->flags = flags | SDL_FULLSCREEN | SDL_HWSURFACE | SDL_PREALLOC;
slouken@630
   135
	
slouken@630
   136
slouken@630
   137
	/* Need to set display banks here for double buffering */
slouken@630
   138
	if (flags & SDL_DOUBLEBUF)
slouken@630
   139
	{
slouken@630
   140
	   FULLSCREEN_SetWriteBank(0);
slouken@630
   141
	   FULLSCREEN_SetDisplayBank(1);
slouken@630
   142
slouken@630
   143
         create_back_buffer = 0; /* Don't need a back buffer for a double buffered display */
slouken@630
   144
    }
slouken@630
   145
slouken@630
   146
    FULLSCREEN_SetupBanks(this);
slouken@630
   147
slouken@630
   148
    if (create_back_buffer)
slouken@630
   149
    {
slouken@630
   150
       /* If not double buffered we may need to create a memory
slouken@1035
   151
         ** back buffer to simulate processing on other OSes.
slouken@630
   152
         ** This is turned on by setting the enviromental variable
slouken@1035
   153
         ** SDL$<name>$BackBuffer >= 1
slouken@1035
   154
         */
ccawley2011@12913
   155
       if (riscos_backbuffer == 3) {
me@12786
   156
          this->hidden->bank[0] = WIMP_CreateBuffer(width, height, &fmt->ro);
ccawley2011@12913
   157
       } else {
slouken@1336
   158
          this->hidden->bank[0] = SDL_malloc(height * current->pitch);
ccawley2011@12913
   159
          if (this->hidden->bank[0] == 0)
ccawley2011@12913
   160
             SDL_OutOfMemory();
ccawley2011@12913
   161
       }
slouken@630
   162
       if (this->hidden->bank[0] == 0)
slouken@630
   163
       {
ccawley2011@12913
   164
           RISCOS_RestoreWimpMode();
slouken@630
   165
           return (NULL);
slouken@630
   166
       }
slouken@630
   167
       /* Surface updated in programs is now a software surface */
slouken@630
   168
       current->flags &= ~SDL_HWSURFACE;
slouken@630
   169
    }
slouken@630
   170
slouken@1035
   171
    /* Store address of allocated screen bank to be freed later */
slouken@1336
   172
    if (this->hidden->alloc_bank) SDL_free(this->hidden->alloc_bank);
slouken@1035
   173
    if (create_back_buffer)
slouken@1035
   174
    {
slouken@1035
   175
        this->hidden->alloc_bank = this->hidden->bank[0];
slouken@1035
   176
        if (riscos_backbuffer == 3)
slouken@1035
   177
        {
slouken@1035
   178
           this->hidden->bank[0] += 60; /* Start of sprite data */
slouken@1035
   179
           if (bpp == 8) this->hidden->bank[0] += 2048; /* 8bpp sprite have palette first */
slouken@1035
   180
        }
slouken@1035
   181
    } else
slouken@1035
   182
	  this->hidden->alloc_bank = 0;
slouken@630
   183
slouken@1035
   184
    // Clear both banks to black
slouken@1336
   185
    SDL_memset(this->hidden->bank[0], 0, height * current->pitch);
slouken@1336
   186
    SDL_memset(this->hidden->bank[1], 0, height * current->pitch);
slouken@630
   187
slouken@630
   188
 	   this->hidden->current_bank = 0;
slouken@630
   189
	   current->pixels = this->hidden->bank[0];
me@12786
   190
	   this->hidden->format = fmt;
slouken@630
   191
slouken@1035
   192
    /* Have to set the screen here, so SetDeviceMode will pick it up */
slouken@1035
   193
    this->screen = current;
slouken@1035
   194
slouken@630
   195
	/* Reset device functions for the wimp */
slouken@630
   196
	FULLSCREEN_SetDeviceMode(this);
slouken@630
   197
slouken@630
   198
/*	FULLSCREEN_DisableEscape(); */
slouken@630
   199
slouken@630
   200
	/* We're done */
slouken@630
   201
	return(current);
slouken@630
   202
}
slouken@630
   203
slouken@630
   204
/* Reset any device functions that have been changed because we have run in WIMP mode */
slouken@630
   205
void FULLSCREEN_SetDeviceMode(_THIS)
slouken@630
   206
{
slouken@1035
   207
	/* Update rects is different if we have a backbuffer */
slouken@1035
   208
slouken@1035
   209
	if (riscos_backbuffer && (this->screen->flags & SDL_DOUBLEBUF) == 0)
slouken@1035
   210
      {
slouken@1035
   211
	   switch(riscos_backbuffer)
slouken@1035
   212
         {
slouken@1035
   213
            case 2: /* ARM code full word copy */
slouken@1035
   214
               switch(this->screen->format->BytesPerPixel)
slouken@1035
   215
               {
slouken@1035
   216
                  case 1: /* 8bpp modes */
slouken@1035
   217
               	   this->UpdateRects = FULLSCREEN_UpdateRects8bpp;
slouken@1035
   218
                     break;
slouken@1035
   219
                  case 2: /* 15/16bpp modes */
slouken@1035
   220
               	   this->UpdateRects = FULLSCREEN_UpdateRects16bpp;
slouken@1035
   221
                     break;
slouken@1035
   222
                  case 4: /* 32 bpp modes */
slouken@1035
   223
               	   this->UpdateRects = FULLSCREEN_UpdateRects32bpp;
slouken@1035
   224
                     break;
slouken@1035
   225
slouken@1035
   226
                  default: /* Just default to the memcpy routine */
slouken@1035
   227
               	   this->UpdateRects = FULLSCREEN_UpdateRectsMemCpy;
slouken@1035
   228
                     break;
slouken@1035
   229
                }
slouken@1035
   230
               break;
slouken@1035
   231
slouken@1035
   232
            case 3: /* Use OS sprite plot routine */
slouken@1035
   233
               this->UpdateRects = FULLSCREEN_UpdateRectsOS;
slouken@1035
   234
               break;
slouken@1035
   235
slouken@1035
   236
            default: /* Old but safe memcpy */
slouken@1035
   237
               this->UpdateRects = FULLSCREEN_UpdateRectsMemCpy;
slouken@1035
   238
               break;
slouken@1035
   239
         }
slouken@1035
   240
      } else
slouken@1035
   241
	   this->UpdateRects = FULLSCREEN_UpdateRects; /* Default do nothing implementation */
slouken@1035
   242
slouken@630
   243
	this->SetColors   = FULLSCREEN_SetColors;
slouken@630
   244
slouken@630
   245
	this->FlipHWSurface = FULLSCREEN_FlipHWSurface;
slouken@630
   246
slouken@630
   247
	this->SetCaption = FULLSCREEN_SetWMCaption;
slouken@630
   248
	this->SetIcon = NULL;
slouken@630
   249
	this->IconifyWindow = NULL;
slouken@630
   250
	
slouken@630
   251
	this->ShowWMCursor = RISCOS_ShowWMCursor;
slouken@630
   252
	this->WarpWMCursor = FULLSCREEN_WarpWMCursor;
slouken@630
   253
slouken@630
   254
	this->PumpEvents = FULLSCREEN_PumpEvents;	
slouken@630
   255
}
slouken@630
   256
me@12786
   257
static void FULLSCREEN_CheckModeListEntry(_THIS, const int *blockInfo)
me@12786
   258
{
me@12786
   259
	if ((blockInfo[1] & 255) == 1) /* Old format mode entry */
me@12786
   260
	{
me@12786
   261
		switch(blockInfo[4])
me@12786
   262
		{
me@12786
   263
		case 3: /* 8 bits per pixel */
me@12786
   264
			FULLSCREEN_AddMode(this, 8, blockInfo[2], blockInfo[3]);
me@12786
   265
			break;
me@12786
   266
		case 4: /* 15 bits per pixel */
me@12786
   267
			FULLSCREEN_AddMode(this, 15, blockInfo[2], blockInfo[3]);
me@12786
   268
			break;
me@12786
   269
		case 5: /* 32 bits per pixel */
me@12786
   270
			FULLSCREEN_AddMode(this, 32, blockInfo[2], blockInfo[3]);
me@12786
   271
			break;
me@12786
   272
		}
me@12786
   273
	}
me@12786
   274
	else if ((blockInfo[1] & 255) == 3) /* Newer format mode entry */
me@12786
   275
	{
me@12786
   276
		/* Check it's RGB colourspace */
me@12786
   277
		if ((blockInfo[5] & 0x3000) != 0)
me@12786
   278
		{
me@12786
   279
			return;
me@12786
   280
		}
me@12786
   281
me@12786
   282
		switch(blockInfo[6])
me@12786
   283
		{
me@12786
   284
		case 3: /* 8 bits per pixel */
me@12786
   285
			FULLSCREEN_AddMode(this, 8, blockInfo[2], blockInfo[3]);
me@12786
   286
			break;
me@12786
   287
		case 4: /* 12, 15 or 16 bits per pixel */
me@12786
   288
			FULLSCREEN_AddMode(this, 15, blockInfo[2], blockInfo[3]);
me@12786
   289
			break;
me@12786
   290
		case 5: /* 32 bits per pixel */
me@12786
   291
			FULLSCREEN_AddMode(this, 32, blockInfo[2], blockInfo[3]);
me@12786
   292
			break;
me@12786
   293
		}
me@12786
   294
	}
me@12786
   295
}
me@12786
   296
slouken@630
   297
/* Query for the list of available video modes */
slouken@630
   298
void FULLSCREEN_BuildModeList(_THIS)
slouken@630
   299
{
slouken@630
   300
	_kernel_swi_regs regs;
slouken@630
   301
	char *enumInfo = NULL;
slouken@630
   302
	char *enum_ptr;
slouken@630
   303
	int *blockInfo;
slouken@630
   304
	int j;
slouken@630
   305
	int num_modes;
slouken@630
   306
slouken@630
   307
	/* Find out how much space we need */
slouken@630
   308
	regs.r[0] = 2; /* Reason code */
slouken@630
   309
	regs.r[2] = 0; /* Number of modes to skip */
slouken@630
   310
	regs.r[6] = 0; /* pointer to block or 0 for count */
slouken@630
   311
	regs.r[7] = 0; /* Size of block in bytes */
slouken@630
   312
	_kernel_swi(OS_ScreenMode, &regs, &regs);
slouken@630
   313
slouken@630
   314
    num_modes = -regs.r[2];
slouken@630
   315
slouken@630
   316
	/* Video memory should be in r[5] */
slouken@630
   317
	this->info.video_mem = regs.r[5]/1024;
slouken@630
   318
ccawley2011@12779
   319
	enumInfo = (char *)SDL_malloc(-regs.r[7]);
slouken@630
   320
	if (enumInfo == NULL)
slouken@630
   321
	{
slouken@630
   322
		SDL_OutOfMemory();
slouken@630
   323
		return;
slouken@630
   324
	}
slouken@630
   325
	/* Read mode information into block */
slouken@630
   326
	regs.r[2] = 0;
slouken@630
   327
	regs.r[6] = (int)enumInfo;
slouken@630
   328
	regs.r[7] = -regs.r[7];
slouken@630
   329
	_kernel_swi(OS_ScreenMode, &regs, &regs);
slouken@630
   330
slouken@630
   331
	enum_ptr = enumInfo;
slouken@630
   332
slouken@630
   333
	for (j =0; j < num_modes;j++)
slouken@630
   334
	{
slouken@630
   335
		blockInfo = (int *)enum_ptr;
me@12786
   336
		FULLSCREEN_CheckModeListEntry(this, blockInfo);
slouken@630
   337
slouken@630
   338
		enum_ptr += blockInfo[0];
slouken@630
   339
	}
slouken@630
   340
slouken@1336
   341
	SDL_free(enumInfo);
slouken@630
   342
		
slouken@630
   343
	/* Sort the mode lists */
slouken@630
   344
	for ( j=0; j<NUM_MODELISTS; ++j ) {
slouken@630
   345
		if ( SDL_nummodes[j] > 0 ) {
slouken@1338
   346
			SDL_qsort(SDL_modelist[j], SDL_nummodes[j], sizeof *SDL_modelist[j], cmpmodes);
slouken@630
   347
		}
slouken@630
   348
	}
slouken@630
   349
}
slouken@630
   350
slouken@630
   351
static int FULLSCREEN_FlipHWSurface(_THIS, SDL_Surface *surface)
slouken@630
   352
{
slouken@630
   353
   FULLSCREEN_SetDisplayBank(this->hidden->current_bank);
slouken@630
   354
   this->hidden->current_bank ^= 1;
slouken@630
   355
   FULLSCREEN_SetWriteBank(this->hidden->current_bank);
slouken@630
   356
   surface->pixels = this->hidden->bank[this->hidden->current_bank];
slouken@630
   357
slouken@1208
   358
   /* Wait for Vsync */
ccawley2011@12778
   359
   _kernel_osbyte(19, 0, 0);
slouken@1208
   360
slouken@630
   361
	return(0);
slouken@630
   362
}
slouken@630
   363
slouken@1035
   364
/* Nothing to do if we are writing direct to hardware */
slouken@630
   365
static void FULLSCREEN_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
slouken@630
   366
{
slouken@1035
   367
}
slouken@1035
   368
slouken@1035
   369
/* Safe but slower Memory copy from our allocated back buffer */
slouken@1035
   370
static void FULLSCREEN_UpdateRectsMemCpy(_THIS, int numrects, SDL_Rect *rects)
slouken@1035
   371
{
slouken@630
   372
      int j;
ccawley2011@12779
   373
      unsigned char *to, *from;
slouken@630
   374
      int pitch = this->screen->pitch;
slouken@630
   375
      int row;
slouken@630
   376
      int xmult = this->screen->format->BytesPerPixel;
slouken@630
   377
      for (j = 0; j < numrects; j++)
slouken@630
   378
      {
slouken@630
   379
         from = this->hidden->bank[0] + rects->x * xmult + rects->y * pitch;
slouken@630
   380
         to  = this->hidden->bank[1] + rects->x * xmult + rects->y * pitch;
slouken@630
   381
         for (row = 0; row < rects->h; row++)
slouken@630
   382
         {
slouken@1336
   383
             SDL_memcpy(to, from, rects->w * xmult);
slouken@630
   384
             from += pitch;
slouken@630
   385
             to += pitch;
slouken@630
   386
         }
slouken@630
   387
         rects++;
slouken@630
   388
      }
slouken@1035
   389
}
slouken@1035
   390
slouken@1035
   391
/* Use optimized assembler memory copy. Deliberately copies extra columns if
slouken@1035
   392
   necessary to ensure the rectangle is word aligned. */
slouken@1035
   393
static void FULLSCREEN_UpdateRects8bpp(_THIS, int numrects, SDL_Rect *rects)
slouken@1035
   394
{
slouken@1035
   395
   int j;
ccawley2011@12779
   396
   unsigned char *to, *from;
slouken@1035
   397
   int pitch = this->screen->pitch;
slouken@1035
   398
   int width_bytes;
slouken@1035
   399
   int src_skip_bytes;
slouken@1035
   400
slouken@1035
   401
   for (j = 0; j < numrects; j++)
slouken@1035
   402
   {
slouken@1035
   403
      from = this->hidden->bank[0] + rects->x + rects->y * pitch;
slouken@1035
   404
      to  = this->hidden->bank[1] + rects->x + rects->y * pitch;
slouken@1035
   405
      width_bytes = rects->w;
slouken@1035
   406
      if ((int)from & 3)
slouken@1035
   407
      {
slouken@1035
   408
         int extra = ((int)from & 3);
slouken@1035
   409
         from -= extra;
slouken@1035
   410
         to -= extra;
slouken@1035
   411
         width_bytes += extra;
slouken@1035
   412
      }
slouken@1035
   413
      if (width_bytes & 3) width_bytes += 4 - (width_bytes & 3);
slouken@1035
   414
      src_skip_bytes = pitch - width_bytes;
slouken@1035
   415
               
slouken@1035
   416
      RISCOS_Put32(to, (width_bytes >> 2), pitch, (int)rects->h, from, src_skip_bytes);
slouken@1035
   417
      rects++;
slouken@630
   418
   }
slouken@630
   419
}
slouken@630
   420
slouken@1035
   421
/* Use optimized assembler memory copy. Deliberately copies extra columns if
slouken@1035
   422
   necessary to ensure the rectangle is word aligned. */
slouken@1035
   423
static void FULLSCREEN_UpdateRects16bpp(_THIS, int numrects, SDL_Rect *rects)
slouken@1035
   424
{
slouken@1035
   425
   int j;
ccawley2011@12779
   426
   unsigned char *to, *from;
slouken@1035
   427
   int pitch = this->screen->pitch;
slouken@1035
   428
   int width_bytes;
slouken@1035
   429
   int src_skip_bytes;
slouken@1035
   430
slouken@1035
   431
   for (j = 0; j < numrects; j++)
slouken@1035
   432
   {
slouken@1035
   433
      from = this->hidden->bank[0] + (rects->x << 1) + rects->y * pitch;
slouken@1035
   434
      to  = this->hidden->bank[1] + (rects->x << 1) + rects->y * pitch;
slouken@1035
   435
      width_bytes = (((int)rects->w) << 1);
slouken@1035
   436
      if ((int)from & 3)
slouken@1035
   437
      {
slouken@1035
   438
         from -= 2;
slouken@1035
   439
         to -= 2;
slouken@1035
   440
         width_bytes += 2;
slouken@1035
   441
      }
slouken@1035
   442
      if (width_bytes & 3) width_bytes += 2;
slouken@1035
   443
      src_skip_bytes = pitch - width_bytes;
slouken@1035
   444
               
slouken@1035
   445
      RISCOS_Put32(to, (width_bytes >> 2), pitch, (int)rects->h, from, src_skip_bytes);
slouken@1035
   446
      rects++;
slouken@1035
   447
   }
slouken@1035
   448
}
slouken@1035
   449
slouken@1035
   450
/* Use optimized assembler memory copy. 32 bpp modes are always word aligned */
slouken@1035
   451
static void FULLSCREEN_UpdateRects32bpp(_THIS, int numrects, SDL_Rect *rects)
slouken@1035
   452
{
slouken@1035
   453
   int j;
ccawley2011@12779
   454
   unsigned char *to, *from;
slouken@1035
   455
   int pitch = this->screen->pitch;
slouken@1035
   456
   int width;
slouken@1035
   457
slouken@1035
   458
   for (j = 0; j < numrects; j++)
slouken@1035
   459
   {
slouken@1035
   460
      from = this->hidden->bank[0] + (rects->x << 2) + rects->y * pitch;
slouken@1035
   461
      to  = this->hidden->bank[1] + (rects->x << 2) + rects->y * pitch;
slouken@1035
   462
      width = (int)rects->w ;
slouken@1035
   463
               
slouken@1035
   464
      RISCOS_Put32(to, width, pitch, (int)rects->h, from, pitch - (width << 2));
slouken@1035
   465
      rects++;
slouken@1035
   466
   }
slouken@1035
   467
}
slouken@1035
   468
slouken@1035
   469
/* Use operating system sprite plots. Currently this is much slower than the
slouken@1035
   470
   other variants however accelerated sprite plotting can be seen on the horizon
slouken@1035
   471
   so this prepares for it. */
slouken@1035
   472
static void FULLSCREEN_UpdateRectsOS(_THIS, int numrects, SDL_Rect *rects)
slouken@1035
   473
{
slouken@1035
   474
   _kernel_swi_regs regs;
slouken@1035
   475
   _kernel_oserror *err;
slouken@1035
   476
   int j;
slouken@1035
   477
   int y;
slouken@1035
   478
slouken@1035
   479
   regs.r[0] = 28 + 512;
slouken@1035
   480
   regs.r[1] = (unsigned int)this->hidden->alloc_bank;
slouken@1035
   481
   regs.r[2] = (unsigned int)this->hidden->alloc_bank+16;
slouken@1035
   482
   regs.r[5] = 0;
slouken@1035
   483
slouken@1035
   484
   for (j = 0; j < numrects; j++)
slouken@1035
   485
   {
slouken@1035
   486
      y = this->screen->h - rects->y; /* top of clipping region */
slouken@1035
   487
      _kernel_oswrch(24); /* Set graphics clip region */
slouken@1035
   488
      _kernel_oswrch((rects->x << this->hidden->xeig) & 0xFF); /* left */
slouken@1035
   489
      _kernel_oswrch(((rects->x << this->hidden->xeig) >> 8) & 0xFF);
slouken@1035
   490
      _kernel_oswrch(((y - rects->h) << this->hidden->yeig) & 0xFF); /* bottom */
slouken@1035
   491
      _kernel_oswrch((((y - rects->h) << this->hidden->yeig)>> 8) & 0xFF);
slouken@1035
   492
      _kernel_oswrch(((rects->x + rects->w - 1) << this->hidden->xeig) & 0xFF); /* right */
slouken@1035
   493
      _kernel_oswrch((((rects->x + rects->w - 1)<< this->hidden->xeig) >> 8) & 0xFF);
slouken@1035
   494
      _kernel_oswrch(((y-1) << this->hidden->yeig) & 0xFF); /* top */
slouken@1035
   495
      _kernel_oswrch((((y-1) << this->hidden->yeig) >> 8) & 0xFF);
slouken@1035
   496
slouken@1035
   497
      regs.r[3] = 0;
slouken@1035
   498
      regs.r[4] = 0;
slouken@1035
   499
slouken@1035
   500
      if ((err = _kernel_swi(OS_SpriteOp, &regs, &regs)) != 0)
slouken@1035
   501
      {
slouken@1035
   502
         printf("OS_SpriteOp failed \n%s\n",err->errmess);
slouken@1035
   503
      }
slouken@1035
   504
slouken@1035
   505
      rects++;
slouken@1035
   506
slouken@1035
   507
      /* Reset to full screen clipping */
slouken@1035
   508
      _kernel_oswrch(24); /* Set graphics clip region */
slouken@1035
   509
      _kernel_oswrch(0); /* left */
slouken@1035
   510
      _kernel_oswrch(0);
slouken@1035
   511
      _kernel_oswrch(0); /* bottom */
slouken@1035
   512
      _kernel_oswrch(0);
slouken@1035
   513
      _kernel_oswrch(((this->screen->w-1) << this->hidden->xeig) & 0xFF); /* right */
slouken@1035
   514
      _kernel_oswrch((((this->screen->w-1) << this->hidden->xeig) >> 8) & 0xFF);
slouken@1035
   515
      _kernel_oswrch(((this->screen->h-1) << this->hidden->yeig) & 0xFF); /* top */
slouken@1035
   516
      _kernel_oswrch((((this->screen->h-1) << this->hidden->yeig) >> 8) & 0xFF);
slouken@1035
   517
   }
slouken@1035
   518
}
slouken@1035
   519
slouken@1035
   520
slouken@630
   521
int FULLSCREEN_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@630
   522
{
slouken@630
   523
	_kernel_swi_regs regs;
slouken@630
   524
	int palette[256];
slouken@630
   525
slouken@630
   526
	regs.r[0] = -1;
slouken@630
   527
	regs.r[1] = -1;
slouken@630
   528
	regs.r[2] = (int)palette;
slouken@630
   529
	regs.r[3] = 1024;
slouken@630
   530
	regs.r[4] = 0;
slouken@630
   531
	_kernel_swi(ColourTrans_ReadPalette, &regs, &regs);
slouken@630
   532
slouken@630
   533
	while(ncolors--)
slouken@630
   534
	{
slouken@630
   535
		palette[firstcolor] = ((colors->b) << 24) | ((colors->g) << 16) | ((colors->r) << 8);
slouken@630
   536
		firstcolor++;
slouken@630
   537
		colors++;
slouken@630
   538
	}
slouken@630
   539
slouken@630
   540
	regs.r[0] = -1;
slouken@630
   541
	regs.r[1] = -1;
slouken@630
   542
	regs.r[2] = (int)palette;
slouken@630
   543
	regs.r[3] = 0;
slouken@630
   544
	regs.r[4] = 0;
slouken@630
   545
	_kernel_swi(ColourTrans_WritePalette, &regs, &regs);
slouken@630
   546
slouken@630
   547
	return(1);
slouken@630
   548
}
slouken@630
   549
slouken@630
   550
slouken@630
   551
static int cmpmodes(const void *va, const void *vb)
slouken@630
   552
{
slouken@630
   553
    SDL_Rect *a = *(SDL_Rect **)va;
slouken@630
   554
    SDL_Rect *b = *(SDL_Rect **)vb;
slouken@966
   555
    if(a->w == b->w)
slouken@966
   556
        return b->h - a->h;
slouken@966
   557
    else
slouken@966
   558
        return b->w - a->w;
slouken@630
   559
}
slouken@630
   560
slouken@630
   561
static int FULLSCREEN_AddMode(_THIS, int bpp, int w, int h)
slouken@630
   562
{
slouken@630
   563
	SDL_Rect *mode;
slouken@630
   564
	int i, index;
slouken@630
   565
	int next_mode;
slouken@630
   566
slouken@630
   567
	/* Check to see if we already have this mode */
slouken@630
   568
	if ( bpp < 8 ) {  /* Not supported */
slouken@630
   569
		return(0);
slouken@630
   570
	}
slouken@630
   571
	index = ((bpp+7)/8)-1;
slouken@630
   572
	for ( i=0; i<SDL_nummodes[index]; ++i ) {
slouken@630
   573
		mode = SDL_modelist[index][i];
slouken@630
   574
		if ( (mode->w == w) && (mode->h == h) ) {
slouken@630
   575
			return(0);
slouken@630
   576
		}
slouken@630
   577
	}
slouken@630
   578
slouken@630
   579
	/* Set up the new video mode rectangle */
slouken@1336
   580
	mode = (SDL_Rect *)SDL_malloc(sizeof *mode);
slouken@630
   581
	if ( mode == NULL ) {
slouken@630
   582
		SDL_OutOfMemory();
slouken@630
   583
		return(-1);
slouken@630
   584
	}
slouken@630
   585
	mode->x = 0;
slouken@630
   586
	mode->y = 0;
slouken@630
   587
	mode->w = w;
slouken@630
   588
	mode->h = h;
slouken@630
   589
slouken@630
   590
	/* Allocate the new list of modes, and fill in the new mode */
slouken@630
   591
	next_mode = SDL_nummodes[index];
slouken@630
   592
	SDL_modelist[index] = (SDL_Rect **)
slouken@1336
   593
	       SDL_realloc(SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
slouken@630
   594
	if ( SDL_modelist[index] == NULL ) {
slouken@630
   595
		SDL_OutOfMemory();
slouken@630
   596
		SDL_nummodes[index] = 0;
slouken@1336
   597
		SDL_free(mode);
slouken@630
   598
		return(-1);
slouken@630
   599
	}
slouken@630
   600
	SDL_modelist[index][next_mode] = mode;
slouken@630
   601
	SDL_modelist[index][next_mode+1] = NULL;
slouken@630
   602
	SDL_nummodes[index]++;
slouken@630
   603
slouken@630
   604
	return(0);
slouken@630
   605
}
slouken@630
   606
slouken@630
   607
void FULLSCREEN_SetWriteBank(int bank)
slouken@630
   608
{
ccawley2011@12778
   609
	_kernel_osbyte(112, bank+1, 0);
slouken@630
   610
}
slouken@630
   611
slouken@630
   612
void FULLSCREEN_SetDisplayBank(int bank)
slouken@630
   613
{
ccawley2011@12778
   614
	_kernel_osbyte(113, bank+1, 0);
slouken@630
   615
}
slouken@630
   616
slouken@630
   617
slouken@630
   618
/** Disable special escape key processing */
slouken@630
   619
static void FULLSCREEN_DisableEscape()
slouken@630
   620
{
ccawley2011@12778
   621
	_kernel_osbyte(229, 1, 0);
slouken@630
   622
}
slouken@630
   623
slouken@630
   624
/** Enable special escape key processing */
slouken@630
   625
static void FULLSCREEN_EnableEscape()
slouken@630
   626
{
ccawley2011@12778
   627
	_kernel_osbyte(229, 0, 0);
slouken@630
   628
}
slouken@630
   629
slouken@630
   630
/** Store caption in case this is called before we create a window */
slouken@630
   631
void FULLSCREEN_SetWMCaption(_THIS, const char *title, const char *icon)
slouken@630
   632
{
slouken@1379
   633
	SDL_strlcpy(this->hidden->title, title, SDL_arraysize(this->hidden->title));
slouken@630
   634
}
slouken@630
   635
me@12786
   636
/* Try and set the exact screen mode specified */
me@12786
   637
static int FULLSCREEN_TrySetMode(int width,int height,const RISCOS_PixelFormat *fmt)
slouken@630
   638
{
slouken@630
   639
   SCREENMODEBLOCK smb;
slouken@630
   640
   _kernel_swi_regs regs;
slouken@630
   641
me@12786
   642
   /* To cope with dodgy validation by the OS or video drivers, check that the OS understands the corresponding sprite format */
me@12786
   643
   if (WIMP_IsSupportedSpriteFormat(fmt))
me@12786
   644
   {
me@12786
   645
      return -1;
me@12786
   646
   }
me@12786
   647
slouken@630
   648
   smb.flags = 1;
slouken@630
   649
   smb.x_pixels = width;
slouken@630
   650
   smb.y_pixels = height;
me@12786
   651
   smb.pixel_depth = fmt->log2bpp;
slouken@630
   652
   smb.frame_rate = -1;
me@12786
   653
   smb.mode_vars[0] = 3; /* NColour */
me@12786
   654
   smb.mode_vars[1] = fmt->ncolour;
me@12786
   655
   smb.mode_vars[2] = 0; /* ModeFlags */
me@12786
   656
   smb.mode_vars[3] = fmt->modeflags;
me@12786
   657
   smb.mode_vars[4] = -1;
slouken@630
   658
slouken@630
   659
   regs.r[0] = 0;
slouken@630
   660
   regs.r[1] = (int)&smb;
slouken@630
   661
me@12786
   662
   if (_kernel_swi(OS_ScreenMode, &regs, &regs) == 0)
slouken@630
   663
   {
me@12786
   664
      /* Turn cursor off*/
me@12786
   665
      _kernel_oswrch(23);_kernel_oswrch(1);_kernel_oswrch(0);
me@12786
   666
      _kernel_oswrch(0);_kernel_oswrch(0);_kernel_oswrch(0);
me@12786
   667
      _kernel_oswrch(0);_kernel_oswrch(0);_kernel_oswrch(0);
me@12786
   668
      _kernel_oswrch(0);_kernel_oswrch(0);
me@12786
   669
me@12786
   670
      return 0;
me@12786
   671
   }
me@12786
   672
   
me@12786
   673
   return -1;
me@12786
   674
}
me@12786
   675
me@12786
   676
/* Set screen mode
me@12786
   677
*
me@12786
   678
*  Returns ptr to pixel format if mode is set ok, otherwise 0
me@12786
   679
*/
me@12786
   680
me@12786
   681
const RISCOS_SDL_PixelFormat *FULLSCREEN_SetMode(int width, int height, int bpp)
me@12786
   682
{
me@12786
   683
   int pass;
me@12786
   684
   const RISCOS_SDL_PixelFormat *fmt;
me@12786
   685
me@12786
   686
   for(pass=0;pass<2;pass++)
me@12786
   687
   {
me@12786
   688
      for (fmt = RISCOS_SDL_PixelFormats;fmt->sdl_bpp;fmt++)
me@12786
   689
      {
me@12786
   690
         if (fmt->sdl_bpp != bpp)
me@12786
   691
         {
me@12786
   692
            continue;
me@12786
   693
         }
me@12786
   694
         
me@12786
   695
         if (!FULLSCREEN_TrySetMode(width,height,&fmt->ro))
me@12786
   696
         {
me@12786
   697
            return fmt;
me@12786
   698
         }
me@12786
   699
      }
me@12786
   700
me@12786
   701
      /* Fuzzy matching for 15bpp & 16bpp */
me@12786
   702
      if (bpp == 15) { bpp = 16; }
me@12786
   703
      else if (bpp == 16) { bpp = 15; }
me@12786
   704
      else { break; }
slouken@630
   705
   }
slouken@630
   706
me@12786
   707
   SDL_SetError("Couldn't set requested mode");
me@12786
   708
   return NULL;
slouken@630
   709
}
slouken@630
   710
slouken@630
   711
/* Get Start addresses for the screen banks */
slouken@630
   712
void FULLSCREEN_SetupBanks(_THIS)
slouken@630
   713
{
slouken@630
   714
   _kernel_swi_regs regs;
slouken@630
   715
   int block[5];
slouken@630
   716
   block[0] = 148; /* Write screen start */
slouken@630
   717
   block[1] = 149; /* Display screen start */
slouken@630
   718
   block[2] = 4;  /* X eig factor */
slouken@630
   719
   block[3] = 5;  /* Y eig factor */
slouken@630
   720
   block[4] = -1;  /* End of list of variables to request */
slouken@630
   721
slouken@630
   722
   regs.r[0] = (int)block;
slouken@630
   723
   regs.r[1] = (int)block;
slouken@630
   724
   _kernel_swi(OS_ReadVduVariables, &regs, &regs);
slouken@630
   725
slouken@630
   726
   this->hidden->bank[0] = (void *)block[0];
slouken@630
   727
   this->hidden->bank[1] = (void *)block[1];
slouken@630
   728
   this->hidden->xeig = block[2];
slouken@630
   729
   this->hidden->yeig = block[3];
slouken@630
   730
}
slouken@630
   731
slouken@630
   732
/* Toggle to full screen mode from the WIMP */
slouken@630
   733
slouken@630
   734
int FULLSCREEN_ToggleFromWimp(_THIS)
slouken@630
   735
{
slouken@630
   736
   int width = this->screen->w;
slouken@630
   737
   int height = this->screen->h;
slouken@630
   738
   int bpp = this->screen->format->BitsPerPixel;
slouken@630
   739
slouken@630
   740
   RISCOS_StoreWimpMode();
me@12786
   741
   if (!FULLSCREEN_TrySetMode(width, height, &this->hidden->format->ro))
slouken@630
   742
   {
ccawley2011@12779
   743
       unsigned char *buffer = this->hidden->alloc_bank; /* This is start of sprite data */
slouken@630
   744
       /* Support back buffer mode only */
slouken@1035
   745
       if (riscos_backbuffer == 0) riscos_backbuffer = 1;
slouken@630
   746
slouken@630
   747
       FULLSCREEN_SetupBanks(this);
slouken@630
   748
slouken@630
   749
       this->hidden->bank[0] = buffer + 60; /* Start of sprite data */
me@12786
   750
       if (bpp == 8)
me@12786
   751
       {
me@12786
   752
           /* Retain the SDL palette */
me@12786
   753
           _kernel_swi_regs regs;
me@12786
   754
           regs.r[0] = -1;
me@12786
   755
           regs.r[1] = -1;
me@12786
   756
           regs.r[2] = (int) this->hidden->bank[0];
me@12786
   757
           regs.r[3] = 0;
me@12786
   758
           regs.r[4] = 2; /* Flashing colours provided (source is sprite palette) */
me@12786
   759
           _kernel_swi(ColourTrans_WritePalette,&regs,&regs);
me@12786
   760
me@12786
   761
           this->hidden->bank[0] += 2048;
me@12786
   762
       }
slouken@630
   763
slouken@630
   764
	   this->hidden->current_bank = 0;
slouken@630
   765
	   this->screen->pixels = this->hidden->bank[0];
slouken@630
   766
slouken@630
   767
       /* Copy back buffer to screen memory */
slouken@1336
   768
       SDL_memcpy(this->hidden->bank[1], this->hidden->bank[0], width * height * this->screen->format->BytesPerPixel);
slouken@630
   769
slouken@630
   770
       FULLSCREEN_SetDeviceMode(this);
slouken@630
   771
       return 1;
slouken@630
   772
   } else
slouken@630
   773
      RISCOS_RestoreWimpMode();
slouken@630
   774
slouken@630
   775
   return 0;
slouken@630
   776
}