src/render/psp/SDL_render_psp.c
author Captain Lex
Sun, 17 Mar 2013 20:07:02 +0800
changeset 7009 161b7b6a5303
child 7037 3fedf1f25b94
permissions -rw-r--r--
Add PSP support
kimonline@7009
     1
/*
kimonline@7009
     2
  Simple DirectMedia Layer
kimonline@7009
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
kimonline@7009
     4
kimonline@7009
     5
  This software is provided 'as-is', without any express or implied
kimonline@7009
     6
  warranty.  In no event will the authors be held liable for any damages
kimonline@7009
     7
  arising from the use of this software.
kimonline@7009
     8
kimonline@7009
     9
  Permission is granted to anyone to use this software for any purpose,
kimonline@7009
    10
  including commercial applications, and to alter it and redistribute it
kimonline@7009
    11
  freely, subject to the following restrictions:
kimonline@7009
    12
kimonline@7009
    13
  1. The origin of this software must not be misrepresented; you must not
kimonline@7009
    14
     claim that you wrote the original software. If you use this software
kimonline@7009
    15
     in a product, an acknowledgment in the product documentation would be
kimonline@7009
    16
     appreciated but is not required.
kimonline@7009
    17
  2. Altered source versions must be plainly marked as such, and must not be
kimonline@7009
    18
     misrepresented as being the original software.
kimonline@7009
    19
  3. This notice may not be removed or altered from any source distribution.
kimonline@7009
    20
*/
kimonline@7009
    21
#include "SDL_config.h"
kimonline@7009
    22
kimonline@7009
    23
#if SDL_VIDEO_RENDER_PSP 
kimonline@7009
    24
kimonline@7009
    25
#include "SDL_hints.h"
kimonline@7009
    26
#include "../SDL_sysrender.h"
kimonline@7009
    27
kimonline@7009
    28
#include <pspkernel.h>
kimonline@7009
    29
#include <pspdisplay.h>
kimonline@7009
    30
#include <pspgu.h>
kimonline@7009
    31
#include <pspgum.h>
kimonline@7009
    32
#include <stdio.h>
kimonline@7009
    33
#include <string.h>
kimonline@7009
    34
#include <math.h>
kimonline@7009
    35
#include <pspge.h>
kimonline@7009
    36
#include <stdarg.h>
kimonline@7009
    37
#include <stdlib.h>
kimonline@7009
    38
#include <vram.h>
kimonline@7009
    39
kimonline@7009
    40
kimonline@7009
    41
kimonline@7009
    42
kimonline@7009
    43
/* PSP renderer implementation, based on the PGE  */
kimonline@7009
    44
kimonline@7009
    45
kimonline@7009
    46
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
kimonline@7009
    47
kimonline@7009
    48
kimonline@7009
    49
static SDL_Renderer *PSP_CreateRenderer(SDL_Window * window, Uint32 flags);
kimonline@7009
    50
static void PSP_WindowEvent(SDL_Renderer * renderer,
kimonline@7009
    51
                             const SDL_WindowEvent *event);
kimonline@7009
    52
static int PSP_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
kimonline@7009
    53
static int PSP_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
kimonline@7009
    54
                              const SDL_Rect * rect, const void *pixels,
kimonline@7009
    55
                              int pitch);
kimonline@7009
    56
static int PSP_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
kimonline@7009
    57
                            const SDL_Rect * rect, void **pixels, int *pitch);
kimonline@7009
    58
static void PSP_UnlockTexture(SDL_Renderer * renderer,
kimonline@7009
    59
                               SDL_Texture * texture);
kimonline@7009
    60
static int PSP_SetRenderTarget(SDL_Renderer * renderer,
kimonline@7009
    61
                                 SDL_Texture * texture);
kimonline@7009
    62
static int PSP_UpdateViewport(SDL_Renderer * renderer);
kimonline@7009
    63
static int PSP_RenderClear(SDL_Renderer * renderer);
kimonline@7009
    64
static int PSP_RenderDrawPoints(SDL_Renderer * renderer,
kimonline@7009
    65
                                 const SDL_FPoint * points, int count);
kimonline@7009
    66
static int PSP_RenderDrawLines(SDL_Renderer * renderer,
kimonline@7009
    67
                                const SDL_FPoint * points, int count);
kimonline@7009
    68
static int PSP_RenderFillRects(SDL_Renderer * renderer,
kimonline@7009
    69
                                const SDL_FRect * rects, int count);
kimonline@7009
    70
static int PSP_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
kimonline@7009
    71
                           const SDL_Rect * srcrect,
kimonline@7009
    72
                           const SDL_FRect * dstrect);
kimonline@7009
    73
static int PSP_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
kimonline@7009
    74
                    Uint32 pixel_format, void * pixels, int pitch);
kimonline@7009
    75
static int PSP_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
kimonline@7009
    76
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect,
kimonline@7009
    77
                         const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
kimonline@7009
    78
static void PSP_RenderPresent(SDL_Renderer * renderer);
kimonline@7009
    79
static void PSP_DestroyTexture(SDL_Renderer * renderer,
kimonline@7009
    80
                                SDL_Texture * texture);
kimonline@7009
    81
static void PSP_DestroyRenderer(SDL_Renderer * renderer);
kimonline@7009
    82
kimonline@7009
    83
/*
kimonline@7009
    84
SDL_RenderDriver PSP_RenderDriver = {
kimonline@7009
    85
    PSP_CreateRenderer,
kimonline@7009
    86
    {
kimonline@7009
    87
     "PSP",
kimonline@7009
    88
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
kimonline@7009
    89
     1,
kimonline@7009
    90
     {SDL_PIXELFORMAT_ABGR8888},
kimonline@7009
    91
     0,
kimonline@7009
    92
     0}
kimonline@7009
    93
};
kimonline@7009
    94
*/
kimonline@7009
    95
SDL_RenderDriver PSP_RenderDriver = {
kimonline@7009
    96
	.CreateRenderer = PSP_CreateRenderer,
kimonline@7009
    97
    .info = {
kimonline@7009
    98
		.name = "PSP",
kimonline@7009
    99
		.flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE,
kimonline@7009
   100
		.num_texture_formats = 4,
kimonline@7009
   101
		.texture_formats = { [0] = SDL_PIXELFORMAT_BGR565,
kimonline@7009
   102
							 					 [1] = SDL_PIXELFORMAT_ABGR1555,
kimonline@7009
   103
							 					 [2] = SDL_PIXELFORMAT_ABGR4444,
kimonline@7009
   104
							 					 [3] = SDL_PIXELFORMAT_ABGR8888,
kimonline@7009
   105
		},
kimonline@7009
   106
		.max_texture_width = 512,
kimonline@7009
   107
		.max_texture_height = 512,
kimonline@7009
   108
     }
kimonline@7009
   109
};
kimonline@7009
   110
kimonline@7009
   111
#define PSP_SCREEN_WIDTH	480
kimonline@7009
   112
#define PSP_SCREEN_HEIGHT	272
kimonline@7009
   113
kimonline@7009
   114
#define PSP_FRAME_BUFFER_WIDTH	512
kimonline@7009
   115
#define PSP_FRAME_BUFFER_SIZE	(PSP_FRAME_BUFFER_WIDTH*PSP_SCREEN_HEIGHT)
kimonline@7009
   116
kimonline@7009
   117
static unsigned int __attribute__((aligned(16))) DisplayList[262144];
kimonline@7009
   118
kimonline@7009
   119
																						 	
kimonline@7009
   120
#define COL5650(r,g,b,a)	((r>>3) | ((g>>2)<<5) | ((b>>3)<<11))
kimonline@7009
   121
#define COL5551(r,g,b,a)	((r>>3) | ((g>>3)<<5) | ((b>>3)<<10) | (a>0?0x7000:0))
kimonline@7009
   122
#define COL4444(r,g,b,a)	((r>>4) | ((g>>4)<<4) | ((b>>4)<<8) | ((a>>4)<<12))
kimonline@7009
   123
#define COL8888(r,g,b,a)	((r) | ((g)<<8) | ((b)<<16) | ((a)<<24))
kimonline@7009
   124
	
kimonline@7009
   125
kimonline@7009
   126
typedef struct
kimonline@7009
   127
{
kimonline@7009
   128
	void* 			frontbuffer ;
kimonline@7009
   129
	void* 			backbuffer ;
kimonline@7009
   130
	SDL_bool 		initialized ;
kimonline@7009
   131
	SDL_bool 		displayListAvail ;
kimonline@7009
   132
	unsigned int 	psm ;
kimonline@7009
   133
	unsigned int 	bpp ;
kimonline@7009
   134
	
kimonline@7009
   135
	SDL_bool    	vsync;	
kimonline@7009
   136
	unsigned int 	currentColor;
kimonline@7009
   137
	int				currentBlendMode;
kimonline@7009
   138
		
kimonline@7009
   139
} PSP_RenderData;
kimonline@7009
   140
kimonline@7009
   141
kimonline@7009
   142
typedef struct
kimonline@7009
   143
{
kimonline@7009
   144
	void				*data;								/**< Image data. */
kimonline@7009
   145
	unsigned int		size;								/**< Size of data in bytes. */
kimonline@7009
   146
	unsigned int		width;								/**< Image width. */
kimonline@7009
   147
	unsigned int		height;								/**< Image height. */
kimonline@7009
   148
	unsigned int		textureWidth;						/**< Texture width (power of two). */
kimonline@7009
   149
	unsigned int		textureHeight;						/**< Texture height (power of two). */
kimonline@7009
   150
	unsigned int		bits;								/**< Image bits per pixel. */
kimonline@7009
   151
	unsigned int		format;								/**< Image format - one of ::pgePixelFormat. */
kimonline@7009
   152
	unsigned int		pitch;										
kimonline@7009
   153
	SDL_bool			swizzled;							/**< Is image swizzled. */
kimonline@7009
   154
kimonline@7009
   155
} PSP_TextureData;
kimonline@7009
   156
kimonline@7009
   157
typedef struct
kimonline@7009
   158
{	
kimonline@7009
   159
	float	x, y, z;
kimonline@7009
   160
} VertV;
kimonline@7009
   161
kimonline@7009
   162
kimonline@7009
   163
typedef struct
kimonline@7009
   164
{
kimonline@7009
   165
	float	u, v;
kimonline@7009
   166
	float	x, y, z;
kimonline@7009
   167
	
kimonline@7009
   168
} VertTV;
kimonline@7009
   169
kimonline@7009
   170
kimonline@7009
   171
// Return next power of 2
kimonline@7009
   172
static int 
kimonline@7009
   173
TextureNextPow2(unsigned int w)
kimonline@7009
   174
{
kimonline@7009
   175
	if(w == 0)
kimonline@7009
   176
		return 0;
kimonline@7009
   177
kimonline@7009
   178
	unsigned int n = 2;
kimonline@7009
   179
kimonline@7009
   180
	while(w > n)
kimonline@7009
   181
		n <<= 1;
kimonline@7009
   182
kimonline@7009
   183
	return n;
kimonline@7009
   184
}
kimonline@7009
   185
kimonline@7009
   186
kimonline@7009
   187
static int
kimonline@7009
   188
GetScaleQuality(void)
kimonline@7009
   189
{
kimonline@7009
   190
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
kimonline@7009
   191
kimonline@7009
   192
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
kimonline@7009
   193
        return GU_NEAREST; // GU_NEAREST good for tile-map
kimonline@7009
   194
    } else {
kimonline@7009
   195
        return GU_LINEAR; // GU_LINEAR good for scaling
kimonline@7009
   196
    }
kimonline@7009
   197
}
kimonline@7009
   198
kimonline@7009
   199
static int
kimonline@7009
   200
PixelFormatToPSPFMT(Uint32 format)
kimonline@7009
   201
{
kimonline@7009
   202
    switch (format) {
kimonline@7009
   203
    case SDL_PIXELFORMAT_BGR565:
kimonline@7009
   204
        return GU_PSM_5650;
kimonline@7009
   205
    case SDL_PIXELFORMAT_ABGR1555:
kimonline@7009
   206
        return GU_PSM_5551;
kimonline@7009
   207
    case SDL_PIXELFORMAT_ABGR4444:
kimonline@7009
   208
        return GU_PSM_4444;
kimonline@7009
   209
    case SDL_PIXELFORMAT_ABGR8888:
kimonline@7009
   210
        return GU_PSM_8888;        
kimonline@7009
   211
    default:    	
kimonline@7009
   212
        return GU_PSM_8888;
kimonline@7009
   213
    }
kimonline@7009
   214
}
kimonline@7009
   215
kimonline@7009
   216
void 
kimonline@7009
   217
StartDrawing(SDL_Renderer * renderer)
kimonline@7009
   218
{	
kimonline@7009
   219
	PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata;
kimonline@7009
   220
	if(data->displayListAvail)
kimonline@7009
   221
		return;
kimonline@7009
   222
kimonline@7009
   223
	sceGuStart(GU_DIRECT, DisplayList);
kimonline@7009
   224
	data->displayListAvail = SDL_TRUE;
kimonline@7009
   225
}
kimonline@7009
   226
kimonline@7009
   227
kimonline@7009
   228
int 
kimonline@7009
   229
TextureSwizzle(PSP_TextureData *psp_texture)
kimonline@7009
   230
{
kimonline@7009
   231
	if(psp_texture->swizzled)
kimonline@7009
   232
		return 1;
kimonline@7009
   233
	
kimonline@7009
   234
	int bytewidth = psp_texture->textureWidth*(psp_texture->bits>>3);
kimonline@7009
   235
	int height = psp_texture->size / bytewidth;
kimonline@7009
   236
kimonline@7009
   237
	int rowblocks = (bytewidth>>4);
kimonline@7009
   238
	int rowblocksadd = (rowblocks-1)<<7;
kimonline@7009
   239
	unsigned int blockaddress = 0;
kimonline@7009
   240
	unsigned int *src = (unsigned int*) psp_texture->data;
kimonline@7009
   241
kimonline@7009
   242
	unsigned char *data = NULL;	
kimonline@7009
   243
	data = malloc(psp_texture->size);
kimonline@7009
   244
kimonline@7009
   245
	int j;
kimonline@7009
   246
kimonline@7009
   247
	for(j = 0; j < height; j++, blockaddress += 16)
kimonline@7009
   248
	{
kimonline@7009
   249
		unsigned int *block;
kimonline@7009
   250
kimonline@7009
   251
		block = (unsigned int*)&data[blockaddress];
kimonline@7009
   252
kimonline@7009
   253
		int i;
kimonline@7009
   254
kimonline@7009
   255
		for(i = 0; i < rowblocks; i++)
kimonline@7009
   256
		{
kimonline@7009
   257
			*block++ = *src++;
kimonline@7009
   258
			*block++ = *src++;
kimonline@7009
   259
			*block++ = *src++;
kimonline@7009
   260
			*block++ = *src++;
kimonline@7009
   261
			block += 28;
kimonline@7009
   262
		}
kimonline@7009
   263
kimonline@7009
   264
		if((j & 0x7) == 0x7)
kimonline@7009
   265
			blockaddress += rowblocksadd;
kimonline@7009
   266
	}
kimonline@7009
   267
kimonline@7009
   268
	free(psp_texture->data);	
kimonline@7009
   269
	psp_texture->data = data;
kimonline@7009
   270
	psp_texture->swizzled = SDL_TRUE;
kimonline@7009
   271
kimonline@7009
   272
	return 1;
kimonline@7009
   273
}
kimonline@7009
   274
int TextureUnswizzle(PSP_TextureData *psp_texture)
kimonline@7009
   275
{
kimonline@7009
   276
	if(!psp_texture->swizzled)
kimonline@7009
   277
		return 1;
kimonline@7009
   278
		
kimonline@7009
   279
	int blockx, blocky;
kimonline@7009
   280
	
kimonline@7009
   281
	int bytewidth = psp_texture->textureWidth*(psp_texture->bits>>3);
kimonline@7009
   282
	int height = psp_texture->size / bytewidth;
kimonline@7009
   283
	
kimonline@7009
   284
	int widthblocks = bytewidth/16;
kimonline@7009
   285
	int heightblocks = height/8;
kimonline@7009
   286
	
kimonline@7009
   287
	int dstpitch = (bytewidth - 16)/4;
kimonline@7009
   288
	int dstrow = bytewidth * 8;
kimonline@7009
   289
kimonline@7009
   290
	unsigned int *src = (unsigned int*) psp_texture->data;
kimonline@7009
   291
kimonline@7009
   292
	unsigned char *data = NULL;
kimonline@7009
   293
	
kimonline@7009
   294
	data = malloc(psp_texture->size);
kimonline@7009
   295
kimonline@7009
   296
	if(!data)
kimonline@7009
   297
		return 0;
kimonline@7009
   298
		
kimonline@7009
   299
	sceKernelDcacheWritebackAll();
kimonline@7009
   300
kimonline@7009
   301
	int j;
kimonline@7009
   302
	
kimonline@7009
   303
	unsigned char *ydst = (unsigned char *)data;
kimonline@7009
   304
kimonline@7009
   305
	for(blocky = 0; blocky < heightblocks; ++blocky)
kimonline@7009
   306
	{
kimonline@7009
   307
		unsigned char *xdst = ydst;
kimonline@7009
   308
		
kimonline@7009
   309
		for(blockx = 0; blockx < widthblocks; ++blockx)
kimonline@7009
   310
		{
kimonline@7009
   311
			unsigned int *block;
kimonline@7009
   312
kimonline@7009
   313
			block = (unsigned int*)xdst;
kimonline@7009
   314
kimonline@7009
   315
			for(j = 0; j < 8; ++j)
kimonline@7009
   316
			{
kimonline@7009
   317
				*(block++) = *(src++);
kimonline@7009
   318
				*(block++) = *(src++);
kimonline@7009
   319
				*(block++) = *(src++);
kimonline@7009
   320
				*(block++) = *(src++);
kimonline@7009
   321
				block += dstpitch;
kimonline@7009
   322
			}
kimonline@7009
   323
kimonline@7009
   324
			xdst += 16;
kimonline@7009
   325
		}
kimonline@7009
   326
		
kimonline@7009
   327
		ydst += dstrow;
kimonline@7009
   328
	}
kimonline@7009
   329
kimonline@7009
   330
	free(psp_texture->data);
kimonline@7009
   331
	
kimonline@7009
   332
	psp_texture->data = data;
kimonline@7009
   333
kimonline@7009
   334
	psp_texture->swizzled = SDL_FALSE;
kimonline@7009
   335
kimonline@7009
   336
	return 1;
kimonline@7009
   337
}
kimonline@7009
   338
kimonline@7009
   339
SDL_Renderer *
kimonline@7009
   340
PSP_CreateRenderer(SDL_Window * window, Uint32 flags)
kimonline@7009
   341
{
kimonline@7009
   342
kimonline@7009
   343
    SDL_Renderer *renderer;
kimonline@7009
   344
    PSP_RenderData *data;
kimonline@7009
   345
		int pixelformat;
kimonline@7009
   346
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
kimonline@7009
   347
    if (!renderer) {
kimonline@7009
   348
        SDL_OutOfMemory();
kimonline@7009
   349
        return NULL;
kimonline@7009
   350
    }
kimonline@7009
   351
kimonline@7009
   352
    data = (PSP_RenderData *) SDL_calloc(1, sizeof(*data));
kimonline@7009
   353
    if (!data) {
kimonline@7009
   354
        PSP_DestroyRenderer(renderer);
kimonline@7009
   355
        SDL_OutOfMemory();
kimonline@7009
   356
        return NULL;
kimonline@7009
   357
    }
kimonline@7009
   358
    
kimonline@7009
   359
		
kimonline@7009
   360
    renderer->WindowEvent = PSP_WindowEvent;
kimonline@7009
   361
    renderer->CreateTexture = PSP_CreateTexture;
kimonline@7009
   362
    renderer->UpdateTexture = PSP_UpdateTexture;
kimonline@7009
   363
    renderer->LockTexture = PSP_LockTexture;
kimonline@7009
   364
    renderer->UnlockTexture = PSP_UnlockTexture;
kimonline@7009
   365
    renderer->SetRenderTarget = PSP_SetRenderTarget;
kimonline@7009
   366
    renderer->UpdateViewport = PSP_UpdateViewport;
kimonline@7009
   367
    renderer->RenderClear = PSP_RenderClear;
kimonline@7009
   368
    renderer->RenderDrawPoints = PSP_RenderDrawPoints;
kimonline@7009
   369
    renderer->RenderDrawLines = PSP_RenderDrawLines;
kimonline@7009
   370
    renderer->RenderFillRects = PSP_RenderFillRects;
kimonline@7009
   371
    renderer->RenderCopy = PSP_RenderCopy;
kimonline@7009
   372
    renderer->RenderReadPixels = PSP_RenderReadPixels;
kimonline@7009
   373
    renderer->RenderCopyEx = PSP_RenderCopyEx;
kimonline@7009
   374
    renderer->RenderPresent = PSP_RenderPresent;
kimonline@7009
   375
    renderer->DestroyTexture = PSP_DestroyTexture;
kimonline@7009
   376
    renderer->DestroyRenderer = PSP_DestroyRenderer;
kimonline@7009
   377
    renderer->info = PSP_RenderDriver.info;
kimonline@7009
   378
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
kimonline@7009
   379
    renderer->driverdata = data;
kimonline@7009
   380
    renderer->window = window;
kimonline@7009
   381
    
kimonline@7009
   382
	if (data->initialized != SDL_FALSE)
kimonline@7009
   383
		return 0;
kimonline@7009
   384
	data->initialized = SDL_TRUE;
kimonline@7009
   385
	
kimonline@7009
   386
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
kimonline@7009
   387
        data->vsync = SDL_TRUE;
kimonline@7009
   388
    } else {
kimonline@7009
   389
        data->vsync = SDL_FALSE;
kimonline@7009
   390
    }
kimonline@7009
   391
    	
kimonline@7009
   392
	pixelformat=PixelFormatToPSPFMT(SDL_GetWindowPixelFormat(window));
kimonline@7009
   393
	switch(pixelformat)
kimonline@7009
   394
	{
kimonline@7009
   395
		case GU_PSM_4444:
kimonline@7009
   396
		case GU_PSM_5650:
kimonline@7009
   397
		case GU_PSM_5551:
kimonline@7009
   398
			data->frontbuffer = (unsigned int *)(PSP_FRAME_BUFFER_SIZE<<1);
kimonline@7009
   399
			data->backbuffer =  (unsigned int *)(0);
kimonline@7009
   400
			data->bpp = 2;
kimonline@7009
   401
			data->psm = pixelformat;
kimonline@7009
   402
			break;
kimonline@7009
   403
		default:
kimonline@7009
   404
			data->frontbuffer = (unsigned int *)(PSP_FRAME_BUFFER_SIZE<<2);
kimonline@7009
   405
			data->backbuffer =  (unsigned int *)(0);
kimonline@7009
   406
			data->bpp = 4;
kimonline@7009
   407
			data->psm = GU_PSM_8888;
kimonline@7009
   408
			break;
kimonline@7009
   409
	}
kimonline@7009
   410
	
kimonline@7009
   411
	sceGuInit();
kimonline@7009
   412
	// setup GU
kimonline@7009
   413
	sceGuStart(GU_DIRECT, DisplayList);
kimonline@7009
   414
	sceGuDrawBuffer(data->psm, data->frontbuffer, PSP_FRAME_BUFFER_WIDTH);
kimonline@7009
   415
	sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT, data->backbuffer, PSP_FRAME_BUFFER_WIDTH);
kimonline@7009
   416
kimonline@7009
   417
kimonline@7009
   418
	sceGuOffset(2048 - (PSP_SCREEN_WIDTH>>1), 2048 - (PSP_SCREEN_HEIGHT>>1));
kimonline@7009
   419
	sceGuViewport(2048, 2048, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
kimonline@7009
   420
kimonline@7009
   421
    data->frontbuffer = vabsptr(data->frontbuffer);
kimonline@7009
   422
    data->backbuffer = vabsptr(data->backbuffer);
kimonline@7009
   423
    
kimonline@7009
   424
	// Scissoring
kimonline@7009
   425
	sceGuScissor(0, 0, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
kimonline@7009
   426
	sceGuEnable(GU_SCISSOR_TEST);
kimonline@7009
   427
kimonline@7009
   428
	// Backface culling
kimonline@7009
   429
	sceGuFrontFace(GU_CCW);
kimonline@7009
   430
	sceGuEnable(GU_CULL_FACE);
kimonline@7009
   431
	
kimonline@7009
   432
	// Texturing
kimonline@7009
   433
	sceGuEnable(GU_TEXTURE_2D);
kimonline@7009
   434
	sceGuShadeModel(GU_SMOOTH);
kimonline@7009
   435
	sceGuTexWrap(GU_REPEAT, GU_REPEAT);
kimonline@7009
   436
	
kimonline@7009
   437
	// Blending
kimonline@7009
   438
	sceGuEnable(GU_BLEND);
kimonline@7009
   439
	sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
kimonline@7009
   440
kimonline@7009
   441
	sceGuTexFilter(GU_LINEAR,GU_LINEAR);
kimonline@7009
   442
	
kimonline@7009
   443
	sceGuFinish();
kimonline@7009
   444
	sceGuSync(0,0);	
kimonline@7009
   445
	sceDisplayWaitVblankStartCB();
kimonline@7009
   446
	sceGuDisplay(GU_TRUE);	
kimonline@7009
   447
		
kimonline@7009
   448
    return renderer;
kimonline@7009
   449
}
kimonline@7009
   450
kimonline@7009
   451
static void
kimonline@7009
   452
PSP_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
kimonline@7009
   453
{
kimonline@7009
   454
kimonline@7009
   455
}
kimonline@7009
   456
kimonline@7009
   457
kimonline@7009
   458
static int
kimonline@7009
   459
PSP_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
kimonline@7009
   460
{
kimonline@7009
   461
//		PSP_RenderData *renderdata = (PSP_RenderData *) renderer->driverdata;
kimonline@7009
   462
	PSP_TextureData* psp_texture = (PSP_TextureData*) SDL_calloc(1, sizeof(*psp_texture));;
kimonline@7009
   463
	
kimonline@7009
   464
	if(!psp_texture)
kimonline@7009
   465
		return -1;
kimonline@7009
   466
kimonline@7009
   467
	psp_texture->swizzled = SDL_FALSE;
kimonline@7009
   468
	psp_texture->width = texture->w;
kimonline@7009
   469
	psp_texture->height = texture->h;
kimonline@7009
   470
	psp_texture->textureHeight = TextureNextPow2(texture->h);
kimonline@7009
   471
	psp_texture->textureWidth = TextureNextPow2(texture->w);
kimonline@7009
   472
	psp_texture->format = PixelFormatToPSPFMT(texture->format);
kimonline@7009
   473
kimonline@7009
   474
	switch(psp_texture->format)
kimonline@7009
   475
	{
kimonline@7009
   476
		case GU_PSM_5650:
kimonline@7009
   477
		case GU_PSM_5551:
kimonline@7009
   478
		case GU_PSM_4444:
kimonline@7009
   479
			psp_texture->bits = 16;
kimonline@7009
   480
			break;
kimonline@7009
   481
			
kimonline@7009
   482
		case GU_PSM_8888:
kimonline@7009
   483
			psp_texture->bits = 32;
kimonline@7009
   484
			break;
kimonline@7009
   485
			
kimonline@7009
   486
		default:
kimonline@7009
   487
			return -1;
kimonline@7009
   488
	}
kimonline@7009
   489
kimonline@7009
   490
	psp_texture->pitch = psp_texture->textureWidth * SDL_BYTESPERPIXEL(texture->format);	
kimonline@7009
   491
	psp_texture->size = psp_texture->textureHeight*psp_texture->pitch;
kimonline@7009
   492
	psp_texture->data = SDL_calloc(1, psp_texture->size);
kimonline@7009
   493
	
kimonline@7009
   494
	if(!psp_texture->data)
kimonline@7009
   495
	{
kimonline@7009
   496
		SDL_OutOfMemory();
kimonline@7009
   497
		SDL_free(psp_texture);
kimonline@7009
   498
		return -1;
kimonline@7009
   499
	}
kimonline@7009
   500
    texture->driverdata = psp_texture;
kimonline@7009
   501
    
kimonline@7009
   502
    return 0;
kimonline@7009
   503
}
kimonline@7009
   504
kimonline@7009
   505
kimonline@7009
   506
void 
kimonline@7009
   507
TextureActivate(SDL_Texture * texture)
kimonline@7009
   508
{		
kimonline@7009
   509
	PSP_TextureData *psp_texture = (PSP_TextureData *) texture->driverdata;
kimonline@7009
   510
	int scaleMode = GetScaleQuality();
kimonline@7009
   511
	
kimonline@7009
   512
	// Swizzling is useless with small textures.
kimonline@7009
   513
    if (texture->w >= 16 || texture->h >= 16)
kimonline@7009
   514
    {
kimonline@7009
   515
		TextureSwizzle(psp_texture);	
kimonline@7009
   516
    }
kimonline@7009
   517
    
kimonline@7009
   518
	sceGuEnable(GU_TEXTURE_2D);
kimonline@7009
   519
	sceGuTexWrap(GU_REPEAT, GU_REPEAT);
kimonline@7009
   520
	sceGuTexMode(psp_texture->format, 0, 0, psp_texture->swizzled);
kimonline@7009
   521
	sceGuTexFilter(scaleMode, scaleMode); // GU_NEAREST good for tile-map
kimonline@7009
   522
										  // GU_LINEAR good for scaling	
kimonline@7009
   523
	sceGuTexImage(0, psp_texture->textureWidth, psp_texture->textureHeight, psp_texture->textureWidth, psp_texture->data);
kimonline@7009
   524
	sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
kimonline@7009
   525
}
kimonline@7009
   526
kimonline@7009
   527
kimonline@7009
   528
static int
kimonline@7009
   529
PSP_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
kimonline@7009
   530
                   const SDL_Rect * rect, const void *pixels, int pitch)
kimonline@7009
   531
{
kimonline@7009
   532
//	PSP_TextureData *psp_texture = (PSP_TextureData *) texture->driverdata;
kimonline@7009
   533
    const Uint8 *src;
kimonline@7009
   534
    Uint8 *dst;
kimonline@7009
   535
    int row, length,dpitch;
kimonline@7009
   536
    src = pixels;
kimonline@7009
   537
    
kimonline@7009
   538
    PSP_LockTexture(renderer, texture,rect,(void **)&dst, &dpitch);
kimonline@7009
   539
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
kimonline@7009
   540
    if (length == pitch && length == dpitch) {
kimonline@7009
   541
        SDL_memcpy(dst, src, length*rect->h);
kimonline@7009
   542
    } else {
kimonline@7009
   543
        for (row = 0; row < rect->h; ++row) {
kimonline@7009
   544
            SDL_memcpy(dst, src, length);
kimonline@7009
   545
            src += pitch;
kimonline@7009
   546
            dst += dpitch;
kimonline@7009
   547
        }
kimonline@7009
   548
    }
kimonline@7009
   549
    
kimonline@7009
   550
	sceKernelDcacheWritebackAll();
kimonline@7009
   551
    return 0;
kimonline@7009
   552
}
kimonline@7009
   553
kimonline@7009
   554
static int
kimonline@7009
   555
PSP_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
kimonline@7009
   556
                 const SDL_Rect * rect, void **pixels, int *pitch)
kimonline@7009
   557
{
kimonline@7009
   558
	PSP_TextureData *psp_texture = (PSP_TextureData *) texture->driverdata;
kimonline@7009
   559
kimonline@7009
   560
    *pixels =
kimonline@7009
   561
        (void *) ((Uint8 *) psp_texture->data + rect->y * psp_texture->pitch +
kimonline@7009
   562
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
kimonline@7009
   563
    *pitch = psp_texture->pitch;
kimonline@7009
   564
    return 0;
kimonline@7009
   565
}
kimonline@7009
   566
kimonline@7009
   567
static void
kimonline@7009
   568
PSP_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
kimonline@7009
   569
{
kimonline@7009
   570
	PSP_TextureData *psp_texture = (PSP_TextureData *) texture->driverdata;
kimonline@7009
   571
    SDL_Rect rect;
kimonline@7009
   572
kimonline@7009
   573
    /* We do whole texture updates, at least for now */
kimonline@7009
   574
    rect.x = 0;
kimonline@7009
   575
    rect.y = 0;
kimonline@7009
   576
    rect.w = texture->w;
kimonline@7009
   577
    rect.h = texture->h;
kimonline@7009
   578
    PSP_UpdateTexture(renderer, texture, &rect, psp_texture->data, psp_texture->pitch);
kimonline@7009
   579
}
kimonline@7009
   580
kimonline@7009
   581
static int
kimonline@7009
   582
PSP_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
kimonline@7009
   583
{
kimonline@7009
   584
kimonline@7009
   585
    return 0;
kimonline@7009
   586
}
kimonline@7009
   587
kimonline@7009
   588
static int
kimonline@7009
   589
PSP_UpdateViewport(SDL_Renderer * renderer)
kimonline@7009
   590
{
kimonline@7009
   591
kimonline@7009
   592
    return 0;
kimonline@7009
   593
}
kimonline@7009
   594
kimonline@7009
   595
kimonline@7009
   596
static void
kimonline@7009
   597
PSP_SetBlendMode(SDL_Renderer * renderer, int blendMode)
kimonline@7009
   598
{
kimonline@7009
   599
	PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata;				
kimonline@7009
   600
    if (blendMode != data-> currentBlendMode) {
kimonline@7009
   601
        switch (blendMode) {
kimonline@7009
   602
        case SDL_BLENDMODE_NONE:
kimonline@7009
   603
        		sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);        
kimonline@7009
   604
				sceGuDisable(GU_BLEND);
kimonline@7009
   605
            break;
kimonline@7009
   606
        case SDL_BLENDMODE_BLEND:
kimonline@7009
   607
        		sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA);
kimonline@7009
   608
				sceGuEnable(GU_BLEND);
kimonline@7009
   609
				sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0 );
kimonline@7009
   610
            break;
kimonline@7009
   611
        case SDL_BLENDMODE_ADD:
kimonline@7009
   612
        		sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA);
kimonline@7009
   613
				sceGuEnable(GU_BLEND);
kimonline@7009
   614
				sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_FIX, 0, 0x00FFFFFF );
kimonline@7009
   615
            break;
kimonline@7009
   616
        case SDL_BLENDMODE_MOD:
kimonline@7009
   617
        		sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA);
kimonline@7009
   618
				sceGuEnable(GU_BLEND);
kimonline@7009
   619
				sceGuBlendFunc( GU_ADD, GU_FIX, GU_SRC_COLOR, 0, 0);
kimonline@7009
   620
            break;
kimonline@7009
   621
        }
kimonline@7009
   622
        data->currentBlendMode = blendMode;
kimonline@7009
   623
    }
kimonline@7009
   624
}
kimonline@7009
   625
kimonline@7009
   626
kimonline@7009
   627
kimonline@7009
   628
static int
kimonline@7009
   629
PSP_RenderClear(SDL_Renderer * renderer)
kimonline@7009
   630
{					
kimonline@7009
   631
		//start list
kimonline@7009
   632
	StartDrawing(renderer);
kimonline@7009
   633
	int color = renderer->a << 24 | renderer->b << 16 | renderer->g << 8 | renderer->r;		
kimonline@7009
   634
	sceGuClearColor(color);
kimonline@7009
   635
	sceGuClearDepth(0);
kimonline@7009
   636
	sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT|GU_FAST_CLEAR_BIT);
kimonline@7009
   637
kimonline@7009
   638
    return 0;
kimonline@7009
   639
}
kimonline@7009
   640
kimonline@7009
   641
static int
kimonline@7009
   642
PSP_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
kimonline@7009
   643
                      int count)
kimonline@7009
   644
{
kimonline@7009
   645
	int color = renderer->a << 24 | renderer->b << 16 | renderer->g << 8 | renderer->r;
kimonline@7009
   646
	int i;
kimonline@7009
   647
	StartDrawing(renderer);		
kimonline@7009
   648
	VertV* vertices = (VertV*)sceGuGetMemory(count*sizeof(VertV)); 
kimonline@7009
   649
	
kimonline@7009
   650
 	for (i = 0; i < count; ++i) {
kimonline@7009
   651
			vertices[i].x = points[i].x;
kimonline@7009
   652
			vertices[i].y = points[i].y;
kimonline@7009
   653
			vertices[i].z = 0.0f;
kimonline@7009
   654
	}
kimonline@7009
   655
	sceGuDisable(GU_TEXTURE_2D);
kimonline@7009
   656
	sceGuColor(color);		
kimonline@7009
   657
	sceGuShadeModel(GU_FLAT);
kimonline@7009
   658
	sceGuDrawArray(GU_POINTS, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, vertices);
kimonline@7009
   659
	sceGuShadeModel(GU_SMOOTH);
kimonline@7009
   660
	sceGuEnable(GU_TEXTURE_2D);			
kimonline@7009
   661
kimonline@7009
   662
    return 0;
kimonline@7009
   663
}
kimonline@7009
   664
kimonline@7009
   665
static int
kimonline@7009
   666
PSP_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
kimonline@7009
   667
                     int count)
kimonline@7009
   668
{
kimonline@7009
   669
	int color = renderer->a << 24 | renderer->b << 16 | renderer->g << 8 | renderer->r;
kimonline@7009
   670
	int i;
kimonline@7009
   671
	StartDrawing(renderer);
kimonline@7009
   672
	VertV* vertices = (VertV*)sceGuGetMemory(count*sizeof(VertV)); 
kimonline@7009
   673
	
kimonline@7009
   674
	for (i = 0; i < count; ++i) {
kimonline@7009
   675
			vertices[i].x = points[i].x;
kimonline@7009
   676
			vertices[i].y = points[i].y;
kimonline@7009
   677
			vertices[i].z = 0.0f;
kimonline@7009
   678
	}
kimonline@7009
   679
kimonline@7009
   680
	sceGuDisable(GU_TEXTURE_2D);
kimonline@7009
   681
	sceGuColor(color);
kimonline@7009
   682
	sceGuShadeModel(GU_FLAT);
kimonline@7009
   683
	sceGuDrawArray(GU_LINE_STRIP, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, vertices);
kimonline@7009
   684
	sceGuShadeModel(GU_SMOOTH);
kimonline@7009
   685
	sceGuEnable(GU_TEXTURE_2D);
kimonline@7009
   686
	 
kimonline@7009
   687
    return 0;
kimonline@7009
   688
}
kimonline@7009
   689
kimonline@7009
   690
static int
kimonline@7009
   691
PSP_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects,
kimonline@7009
   692
                     int count)
kimonline@7009
   693
{
kimonline@7009
   694
	int color = renderer->a << 24 | renderer->b << 16 | renderer->g << 8 | renderer->r;
kimonline@7009
   695
	int i;	
kimonline@7009
   696
	StartDrawing(renderer);
kimonline@7009
   697
	
kimonline@7009
   698
	for (i = 0; i < count; ++i) {
kimonline@7009
   699
    	const SDL_FRect *rect = &rects[i];
kimonline@7009
   700
		VertV* vertices = (VertV*)sceGuGetMemory((sizeof(VertV)<<1));
kimonline@7009
   701
		vertices[0].x = rect->x;
kimonline@7009
   702
		vertices[0].y = rect->y;
kimonline@7009
   703
		vertices[0].z = 0.0f;
kimonline@7009
   704
kimonline@7009
   705
		vertices[1].x = rect->x + rect->w;
kimonline@7009
   706
		vertices[1].y = rect->y + rect->h;
kimonline@7009
   707
		vertices[1].z = 0.0f;
kimonline@7009
   708
		
kimonline@7009
   709
		sceGuDisable(GU_TEXTURE_2D);
kimonline@7009
   710
		sceGuColor(color);			
kimonline@7009
   711
		sceGuShadeModel(GU_FLAT);
kimonline@7009
   712
		sceGuDrawArray(GU_SPRITES, GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2, 0, vertices);
kimonline@7009
   713
		sceGuShadeModel(GU_SMOOTH);
kimonline@7009
   714
		sceGuEnable(GU_TEXTURE_2D);
kimonline@7009
   715
    }
kimonline@7009
   716
		
kimonline@7009
   717
    return 0;
kimonline@7009
   718
}
kimonline@7009
   719
kimonline@7009
   720
kimonline@7009
   721
#define PI   3.14159265358979f
kimonline@7009
   722
kimonline@7009
   723
#define radToDeg(x) ((x)*180.f/PI)
kimonline@7009
   724
#define degToRad(x) ((x)*PI/180.f)
kimonline@7009
   725
kimonline@7009
   726
float MathAbs(float x)
kimonline@7009
   727
{
kimonline@7009
   728
	float result;
kimonline@7009
   729
kimonline@7009
   730
	__asm__ volatile (
kimonline@7009
   731
		"mtv      %1, S000\n"
kimonline@7009
   732
		"vabs.s   S000, S000\n"
kimonline@7009
   733
		"mfv      %0, S000\n"
kimonline@7009
   734
	: "=r"(result) : "r"(x));
kimonline@7009
   735
kimonline@7009
   736
	return result;
kimonline@7009
   737
}
kimonline@7009
   738
kimonline@7009
   739
void MathSincos(float r, float *s, float *c)
kimonline@7009
   740
{
kimonline@7009
   741
	__asm__ volatile (
kimonline@7009
   742
		"mtv      %2, S002\n"
kimonline@7009
   743
		"vcst.s   S003, VFPU_2_PI\n"
kimonline@7009
   744
		"vmul.s   S002, S002, S003\n"
kimonline@7009
   745
		"vrot.p   C000, S002, [s, c]\n"
kimonline@7009
   746
		"mfv      %0, S000\n"
kimonline@7009
   747
		"mfv      %1, S001\n"
kimonline@7009
   748
	: "=r"(*s), "=r"(*c): "r"(r));
kimonline@7009
   749
}
kimonline@7009
   750
kimonline@7009
   751
void Swap(float *a, float *b)
kimonline@7009
   752
{
kimonline@7009
   753
	float n=*a;
kimonline@7009
   754
	*a = *b;
kimonline@7009
   755
	*b = n;
kimonline@7009
   756
}
kimonline@7009
   757
kimonline@7009
   758
static int
kimonline@7009
   759
PSP_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
kimonline@7009
   760
                const SDL_Rect * srcrect, const SDL_FRect * dstrect)
kimonline@7009
   761
{
kimonline@7009
   762
	float x, y, width, height;
kimonline@7009
   763
	float u0, v0, u1, v1;
kimonline@7009
   764
	unsigned char alpha;
kimonline@7009
   765
	
kimonline@7009
   766
	x = dstrect->x;
kimonline@7009
   767
	y = dstrect->y;
kimonline@7009
   768
	width = dstrect->w;
kimonline@7009
   769
	height = dstrect->h;
kimonline@7009
   770
kimonline@7009
   771
	u0 = srcrect->x;
kimonline@7009
   772
	v0 = srcrect->y;
kimonline@7009
   773
	u1 = srcrect->x + srcrect->w;
kimonline@7009
   774
	v1 = srcrect->y + srcrect->h;
kimonline@7009
   775
	
kimonline@7009
   776
	alpha = texture->a;
kimonline@7009
   777
	
kimonline@7009
   778
	StartDrawing(renderer);
kimonline@7009
   779
	TextureActivate(texture);
kimonline@7009
   780
	PSP_SetBlendMode(renderer, renderer->blendMode);
kimonline@7009
   781
	
kimonline@7009
   782
	if(alpha != 255)
kimonline@7009
   783
	{
kimonline@7009
   784
		sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
kimonline@7009
   785
		sceGuColor(GU_RGBA(255, 255, 255, alpha));
kimonline@7009
   786
	}else{
kimonline@7009
   787
		sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
kimonline@7009
   788
		sceGuColor(0xFFFFFFFF);
kimonline@7009
   789
	}
kimonline@7009
   790
		
kimonline@7009
   791
	if((MathAbs(u1) - MathAbs(u0)) < 64.0f)
kimonline@7009
   792
	{
kimonline@7009
   793
		VertTV* vertices = (VertTV*)sceGuGetMemory((sizeof(VertTV))<<1);
kimonline@7009
   794
kimonline@7009
   795
		vertices[0].u = u0;
kimonline@7009
   796
		vertices[0].v = v0;
kimonline@7009
   797
		vertices[0].x = x;
kimonline@7009
   798
		vertices[0].y = y; 
kimonline@7009
   799
		vertices[0].z = 0;
kimonline@7009
   800
kimonline@7009
   801
		vertices[1].u = u1;
kimonline@7009
   802
		vertices[1].v = v1;
kimonline@7009
   803
		vertices[1].x = x + width;
kimonline@7009
   804
		vertices[1].y = y + height;
kimonline@7009
   805
		vertices[1].z = 0;
kimonline@7009
   806
kimonline@7009
   807
		sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2, 0, vertices);
kimonline@7009
   808
	}
kimonline@7009
   809
	else
kimonline@7009
   810
	{
kimonline@7009
   811
		float start, end;
kimonline@7009
   812
		float curU = u0;
kimonline@7009
   813
		float curX = x;
kimonline@7009
   814
		float endX = x + width;
kimonline@7009
   815
		float slice = 64.0f;
kimonline@7009
   816
		float ustep = (u1 - u0)/width * slice;
kimonline@7009
   817
	
kimonline@7009
   818
		if(ustep < 0.0f)
kimonline@7009
   819
			ustep = -ustep;
kimonline@7009
   820
kimonline@7009
   821
		for(start = 0, end = width; start < end; start += slice)
kimonline@7009
   822
		{
kimonline@7009
   823
			VertTV* vertices = (VertTV*)sceGuGetMemory((sizeof(VertTV))<<1);
kimonline@7009
   824
kimonline@7009
   825
			float polyWidth = ((curX + slice) > endX) ? (endX - curX) : slice;
kimonline@7009
   826
			float sourceWidth = ((curU + ustep) > u1) ? (u1 - curU) : ustep;
kimonline@7009
   827
kimonline@7009
   828
			vertices[0].u = curU;
kimonline@7009
   829
			vertices[0].v = v0;
kimonline@7009
   830
			vertices[0].x = curX;
kimonline@7009
   831
			vertices[0].y = y; 
kimonline@7009
   832
			vertices[0].z = 0;
kimonline@7009
   833
kimonline@7009
   834
			curU += sourceWidth;
kimonline@7009
   835
			curX += polyWidth;
kimonline@7009
   836
kimonline@7009
   837
			vertices[1].u = curU;
kimonline@7009
   838
			vertices[1].v = v1;
kimonline@7009
   839
			vertices[1].x = curX;
kimonline@7009
   840
			vertices[1].y = (y + height);
kimonline@7009
   841
			vertices[1].z = 0;
kimonline@7009
   842
kimonline@7009
   843
			sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2, 0, vertices);
kimonline@7009
   844
		}
kimonline@7009
   845
	}
kimonline@7009
   846
	
kimonline@7009
   847
	if(alpha != 255)
kimonline@7009
   848
		sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
kimonline@7009
   849
    return 0;
kimonline@7009
   850
}
kimonline@7009
   851
kimonline@7009
   852
static int
kimonline@7009
   853
PSP_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
kimonline@7009
   854
                    Uint32 pixel_format, void * pixels, int pitch)
kimonline@7009
   855
kimonline@7009
   856
{
kimonline@7009
   857
		return 0;
kimonline@7009
   858
}
kimonline@7009
   859
kimonline@7009
   860
kimonline@7009
   861
static int
kimonline@7009
   862
PSP_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
kimonline@7009
   863
                const SDL_Rect * srcrect, const SDL_FRect * dstrect,
kimonline@7009
   864
                const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
kimonline@7009
   865
{
kimonline@7009
   866
	float x, y, width, height;
kimonline@7009
   867
	float u0, v0, u1, v1;
kimonline@7009
   868
	unsigned char alpha;
kimonline@7009
   869
	float centerx, centery;
kimonline@7009
   870
	
kimonline@7009
   871
	x = dstrect->x;
kimonline@7009
   872
	y = dstrect->y;
kimonline@7009
   873
	width = dstrect->w;
kimonline@7009
   874
	height = dstrect->h;
kimonline@7009
   875
kimonline@7009
   876
	u0 = srcrect->x;
kimonline@7009
   877
	v0 = srcrect->y;
kimonline@7009
   878
	u1 = srcrect->x + srcrect->w;
kimonline@7009
   879
	v1 = srcrect->y + srcrect->h;
kimonline@7009
   880
	
kimonline@7009
   881
    centerx = center->x;
kimonline@7009
   882
    centery = center->y;
kimonline@7009
   883
		
kimonline@7009
   884
	alpha = texture->a;
kimonline@7009
   885
	
kimonline@7009
   886
	StartDrawing(renderer);
kimonline@7009
   887
	TextureActivate(texture);
kimonline@7009
   888
	PSP_SetBlendMode(renderer, renderer->blendMode);
kimonline@7009
   889
	
kimonline@7009
   890
	if(alpha != 255)
kimonline@7009
   891
	{
kimonline@7009
   892
		sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
kimonline@7009
   893
		sceGuColor(GU_RGBA(255, 255, 255, alpha));
kimonline@7009
   894
	}else{
kimonline@7009
   895
		sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
kimonline@7009
   896
		sceGuColor(0xFFFFFFFF);
kimonline@7009
   897
	}
kimonline@7009
   898
kimonline@7009
   899
//		x += width * 0.5f;
kimonline@7009
   900
//		y += height * 0.5f;
kimonline@7009
   901
	x += centerx;
kimonline@7009
   902
	y += centery;		
kimonline@7009
   903
	
kimonline@7009
   904
	float c, s;
kimonline@7009
   905
	
kimonline@7009
   906
	MathSincos(degToRad(angle), &s, &c);
kimonline@7009
   907
	
kimonline@7009
   908
//		width *= 0.5f;
kimonline@7009
   909
//		height *= 0.5f;
kimonline@7009
   910
	width  -= centerx;
kimonline@7009
   911
	height -= centery;		
kimonline@7009
   912
	
kimonline@7009
   913
	
kimonline@7009
   914
	float cw = c*width;
kimonline@7009
   915
	float sw = s*width;
kimonline@7009
   916
	float ch = c*height;
kimonline@7009
   917
	float sh = s*height;
kimonline@7009
   918
kimonline@7009
   919
	VertTV* vertices = (VertTV*)sceGuGetMemory(sizeof(VertTV)<<2);
kimonline@7009
   920
kimonline@7009
   921
	vertices[0].u = u0;
kimonline@7009
   922
	vertices[0].v = v0;
kimonline@7009
   923
	vertices[0].x = x - cw + sh;
kimonline@7009
   924
	vertices[0].y = y - sw - ch;
kimonline@7009
   925
	vertices[0].z = 0;
kimonline@7009
   926
			
kimonline@7009
   927
	vertices[1].u = u0;
kimonline@7009
   928
	vertices[1].v = v1;
kimonline@7009
   929
	vertices[1].x = x - cw - sh;
kimonline@7009
   930
	vertices[1].y = y - sw + ch;
kimonline@7009
   931
	vertices[1].z = 0;
kimonline@7009
   932
	
kimonline@7009
   933
	vertices[2].u = u1;
kimonline@7009
   934
	vertices[2].v = v1;
kimonline@7009
   935
	vertices[2].x = x + cw - sh;
kimonline@7009
   936
	vertices[2].y = y + sw + ch;
kimonline@7009
   937
	vertices[2].z = 0;
kimonline@7009
   938
	
kimonline@7009
   939
	vertices[3].u = u1;
kimonline@7009
   940
	vertices[3].v = v0;
kimonline@7009
   941
	vertices[3].x = x + cw + sh;
kimonline@7009
   942
	vertices[3].y = y + sw - ch;
kimonline@7009
   943
	vertices[3].z = 0;
kimonline@7009
   944
	
kimonline@7009
   945
	if (flip & SDL_FLIP_HORIZONTAL) {
kimonline@7009
   946
				Swap(&vertices[0].v, &vertices[2].v);
kimonline@7009
   947
				Swap(&vertices[1].v, &vertices[3].v);
kimonline@7009
   948
	}
kimonline@7009
   949
	if (flip & SDL_FLIP_VERTICAL) {
kimonline@7009
   950
				Swap(&vertices[0].u, &vertices[2].u);
kimonline@7009
   951
				Swap(&vertices[1].u, &vertices[3].u);
kimonline@7009
   952
	}  	
kimonline@7009
   953
kimonline@7009
   954
	sceGuDrawArray(GU_TRIANGLE_FAN, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 4, 0, vertices);
kimonline@7009
   955
			
kimonline@7009
   956
	if(alpha != 255)
kimonline@7009
   957
		sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
kimonline@7009
   958
    return 0;
kimonline@7009
   959
}
kimonline@7009
   960
kimonline@7009
   961
static void
kimonline@7009
   962
PSP_RenderPresent(SDL_Renderer * renderer)
kimonline@7009
   963
{
kimonline@7009
   964
    PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata;
kimonline@7009
   965
	if(!data->displayListAvail)
kimonline@7009
   966
		return;
kimonline@7009
   967
	
kimonline@7009
   968
	data->displayListAvail = SDL_FALSE;
kimonline@7009
   969
	sceGuFinish();
kimonline@7009
   970
	sceGuSync(0,0);
kimonline@7009
   971
	
kimonline@7009
   972
//	if(data->vsync)
kimonline@7009
   973
		sceDisplayWaitVblankStart();
kimonline@7009
   974
			
kimonline@7009
   975
    data->backbuffer = data->frontbuffer;
kimonline@7009
   976
    data->frontbuffer = vabsptr(sceGuSwapBuffers());
kimonline@7009
   977
		
kimonline@7009
   978
}
kimonline@7009
   979
kimonline@7009
   980
static void
kimonline@7009
   981
PSP_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
kimonline@7009
   982
{
kimonline@7009
   983
	PSP_RenderData *renderdata = (PSP_RenderData *) renderer->driverdata;
kimonline@7009
   984
	PSP_TextureData *psp_texture = (PSP_TextureData *) texture->driverdata;
kimonline@7009
   985
	
kimonline@7009
   986
	if (renderdata == 0)
kimonline@7009
   987
		return;
kimonline@7009
   988
kimonline@7009
   989
	if(psp_texture == 0)
kimonline@7009
   990
		return;
kimonline@7009
   991
kimonline@7009
   992
	if(psp_texture->data != 0)
kimonline@7009
   993
	{
kimonline@7009
   994
		free(psp_texture->data);
kimonline@7009
   995
	}
kimonline@7009
   996
	free(texture);
kimonline@7009
   997
	texture->driverdata = NULL;
kimonline@7009
   998
}
kimonline@7009
   999
kimonline@7009
  1000
static void
kimonline@7009
  1001
PSP_DestroyRenderer(SDL_Renderer * renderer)
kimonline@7009
  1002
{
kimonline@7009
  1003
    PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata;
kimonline@7009
  1004
    if (data) {
kimonline@7009
  1005
		if (!data->initialized)
kimonline@7009
  1006
			return;
kimonline@7009
  1007
kimonline@7009
  1008
		StartDrawing(renderer);
kimonline@7009
  1009
		
kimonline@7009
  1010
		sceGuTerm();
kimonline@7009
  1011
//		vfree(data->backbuffer);
kimonline@7009
  1012
//		vfree(data->frontbuffer);
kimonline@7009
  1013
		
kimonline@7009
  1014
		data->initialized = SDL_FALSE;
kimonline@7009
  1015
		data->displayListAvail = SDL_FALSE;
kimonline@7009
  1016
        SDL_free(data);
kimonline@7009
  1017
    }
kimonline@7009
  1018
    SDL_free(renderer);
kimonline@7009
  1019
}
kimonline@7009
  1020
kimonline@7009
  1021
#endif /* SDL_VIDEO_RENDER_PSP */
kimonline@7009
  1022
kimonline@7009
  1023
/* vi: set ts=4 sw=4 expandtab: */
kimonline@7009
  1024