src/render/psp/SDL_render_psp.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Aug 2017 20:54:06 -0700
changeset 11229 436b07ff41b4
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Fixed bug 3297 - Horizontal and Vertical flip swapped on PSP

Littlefighter19

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