src/video/nds/SDL_ndsrender.c
author Darren Alton <dalton@stevens.edu>
Sun, 13 Jul 2008 04:28:54 +0000
branchgsoc2008_nds
changeset 2679 bc3e3e889f6d
parent 2678 3895761db26a
child 2680 4135aa9c5645
permissions -rw-r--r--
More NDS video driver work.
dalton@2670
     1
/*
dalton@2670
     2
    SDL - Simple DirectMedia Layer
dalton@2670
     3
    Copyright (C) 1997-2006 Sam Lantinga
dalton@2670
     4
dalton@2670
     5
    This library is free software; you can redistribute it and/or
dalton@2670
     6
    modify it under the terms of the GNU Lesser General Public
dalton@2670
     7
    License as published by the Free Software Foundation; either
dalton@2670
     8
    version 2.1 of the License, or (at your option) any later version.
dalton@2670
     9
dalton@2670
    10
    This library is distributed in the hope that it will be useful,
dalton@2670
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
dalton@2670
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dalton@2670
    13
    Lesser General Public License for more details.
dalton@2670
    14
dalton@2670
    15
    You should have received a copy of the GNU Lesser General Public
dalton@2670
    16
    License along with this library; if not, write to the Free Software
dalton@2670
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
dalton@2670
    18
dalton@2670
    19
    Sam Lantinga
dalton@2670
    20
    slouken@libsdl.org
dalton@2670
    21
*/
dalton@2670
    22
dalton@2670
    23
#include <stdio.h>
dalton@2670
    24
#include <stdlib.h>
dalton@2670
    25
#include <nds.h>
dalton@2670
    26
dalton@2670
    27
#include "SDL_config.h"
dalton@2670
    28
dalton@2670
    29
#include "SDL_video.h"
dalton@2670
    30
#include "../SDL_sysvideo.h"
dalton@2670
    31
#include "../SDL_yuv_sw_c.h"
dalton@2670
    32
#include "../SDL_renderer_sw.h"
dalton@2670
    33
dalton@2670
    34
dalton@2670
    35
/* SDL surface based renderer implementation */
dalton@2670
    36
dalton@2677
    37
static SDL_Renderer *NDS_CreateRenderer(SDL_Window * window, Uint32 flags);
dalton@2677
    38
static int NDS_ActivateRenderer(SDL_Renderer * renderer);
dalton@2677
    39
static int NDS_DisplayModeChanged(SDL_Renderer * renderer);
dalton@2677
    40
static int NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dalton@2677
    41
static int NDS_QueryTexturePixels(SDL_Renderer * renderer,
dalton@2677
    42
                                 SDL_Texture * texture, void **pixels,
dalton@2677
    43
                                 int *pitch);
dalton@2677
    44
static int NDS_SetTexturePalette(SDL_Renderer * renderer,
dalton@2677
    45
                                SDL_Texture * texture,
dalton@2677
    46
                                const SDL_Color * colors, int firstcolor,
dalton@2677
    47
                                int ncolors);
dalton@2677
    48
static int NDS_GetTexturePalette(SDL_Renderer * renderer,
dalton@2677
    49
                                SDL_Texture * texture, SDL_Color * colors,
dalton@2677
    50
                                int firstcolor, int ncolors);
dalton@2677
    51
static int NDS_SetTextureColorMod(SDL_Renderer * renderer,
dalton@2677
    52
                                 SDL_Texture * texture);
dalton@2677
    53
static int NDS_SetTextureAlphaMod(SDL_Renderer * renderer,
dalton@2677
    54
                                 SDL_Texture * texture);
dalton@2677
    55
static int NDS_SetTextureBlendMode(SDL_Renderer * renderer,
dalton@2677
    56
                                  SDL_Texture * texture);
dalton@2677
    57
static int NDS_SetTextureScaleMode(SDL_Renderer * renderer,
dalton@2677
    58
                                  SDL_Texture * texture);
dalton@2677
    59
static int NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2677
    60
                            const SDL_Rect * rect, const void *pixels,
dalton@2677
    61
                            int pitch);
dalton@2677
    62
static int NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2677
    63
                          const SDL_Rect * rect, int markDirty, void **pixels,
dalton@2677
    64
                          int *pitch);
dalton@2677
    65
static void NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dalton@2677
    66
static void NDS_DirtyTexture(SDL_Renderer * renderer,
dalton@2677
    67
                            SDL_Texture * texture, int numrects,
dalton@2677
    68
                            const SDL_Rect * rects);
dalton@2677
    69
static int NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g,
dalton@2671
    70
                              Uint8 b, Uint8 a, const SDL_Rect * rect);
dalton@2677
    71
static int NDS_RenderCopy(SDL_Renderer * renderer,
dalton@2671
    72
                              SDL_Texture * texture,
dalton@2671
    73
                              const SDL_Rect * srcrect,
dalton@2671
    74
                              const SDL_Rect * dstrect);
dalton@2677
    75
static void NDS_RenderPresent(SDL_Renderer * renderer);
dalton@2677
    76
static void NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dalton@2677
    77
static void NDS_DestroyRenderer(SDL_Renderer * renderer);
dalton@2670
    78
dalton@2670
    79
dalton@2677
    80
SDL_RenderDriver NDS_RenderDriver = {
dalton@2677
    81
    NDS_CreateRenderer,
dalton@2679
    82
    {   "nds", /* char* name */
dalton@2679
    83
        (SDL_RENDERER_SINGLEBUFFER|SDL_RENDERER_ACCELERATED), /* u32 flags */
dalton@2679
    84
        (SDL_TEXTUREMODULATE_NONE), /* u32 mod_modes */
dalton@2679
    85
        (SDL_TEXTUREBLENDMODE_NONE), /* u32 blend_modes */
dalton@2679
    86
        (SDL_TEXTURESCALEMODE_NONE), /* u32 scale_modes */
dalton@2679
    87
        3, /* u32 num_texture_formats */
dalton@2679
    88
        {
dalton@2679
    89
            SDL_PIXELFORMAT_INDEX8,
dalton@2679
    90
            SDL_PIXELFORMAT_RGB555,
dalton@2679
    91
            SDL_PIXELFORMAT_RGB565
dalton@2679
    92
        }, /* u32 texture_formats[20] */
dalton@2679
    93
        (256), /* int max_texture_width */
dalton@2679
    94
        (256), /* int max_texture_height */
dalton@2679
    95
    }
dalton@2670
    96
};
dalton@2670
    97
dalton@2670
    98
typedef struct
dalton@2670
    99
{
dalton@2679
   100
    bg_attribute *bg;
dalton@2679
   101
    u8 bg_taken[4];
dalton@2679
   102
    int sub;
dalton@2677
   103
} NDS_RenderData;
dalton@2670
   104
dalton@2678
   105
typedef struct
dalton@2678
   106
{
dalton@2678
   107
    enum { NDSTX_BG, NDSTX_SPR } type;
dalton@2679
   108
    int hw_index;
dalton@2678
   109
    struct { int w, h, pitch, bpp; } dim;
dalton@2678
   110
    u16 *vram;
dalton@2678
   111
} NDS_TextureData;
dalton@2672
   112
dalton@2672
   113
dalton@2672
   114
/* this is mainly hackish testing/debugging stuff to get cleaned up soon
dalton@2672
   115
   anything named sdlds_blah shouldn't make it into the stable version
dalton@2672
   116
 */
dalton@2672
   117
dalton@2672
   118
u16
dalton@2673
   119
sdlds_rgb2bgr(u16 c)
dalton@2673
   120
{
dalton@2673
   121
/* hack to get the proper colors until I actually get BGR555 to work right */
dalton@2672
   122
    u16 Rmask = 0x7C00, Bmask = 0x001F, GAmask = 0x83E0, r, b;
dalton@2672
   123
    r = (c & Rmask) >> 10;
dalton@2672
   124
    b = (c & Bmask) << 10;
dalton@2672
   125
    return (c & GAmask) | r | b;
dalton@2672
   126
}
dalton@2672
   127
dalton@2672
   128
void
dalton@2673
   129
sdlds_print_pixfmt_info(SDL_PixelFormat * f)
dalton@2673
   130
{
dalton@2673
   131
    if (!f)
dalton@2673
   132
        return;
dalton@2673
   133
    printf("bpp: %d\nRGBA: %x %x %x %x\n",
dalton@2673
   134
           f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask);
dalton@2672
   135
}
dalton@2672
   136
dalton@2672
   137
void
dalton@2673
   138
sdlds_print_surface_info(SDL_Surface * s)
dalton@2673
   139
{
dalton@2673
   140
    if (!s)
dalton@2673
   141
        return;
dalton@2673
   142
    printf("flags: %x\nsize: %dx%d, pitch: %d\nlocked: %d, refcount: %d\n",
dalton@2673
   143
           s->flags, s->w, s->h, s->pitch, s->locked, s->refcount);
dalton@2673
   144
    sdlds_print_pixfmt_info(s->format);
dalton@2672
   145
}
dalton@2672
   146
dalton@2678
   147
/* again the above shouldn't make it into the stable version */
dalton@2672
   148
dalton@2670
   149
SDL_Renderer *
dalton@2677
   150
NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
dalton@2670
   151
{
dalton@2670
   152
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2670
   153
    SDL_DisplayMode *displayMode = &display->current_mode;
dalton@2670
   154
    SDL_Renderer *renderer;
dalton@2677
   155
    NDS_RenderData *data;
dalton@2670
   156
    int i, n;
dalton@2672
   157
    int bpp = 15;
dalton@2673
   158
    Uint32 Rmask, Gmask, Bmask, Amask;
dalton@2673
   159
/*  Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
dalton@2672
   160
    Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000;
dalton@2673
   161
*/
dalton@2670
   162
dalton@2673
   163
    /* hard coded this to BGR555 for now */
dalton@2673
   164
    if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp,
dalton@2673
   165
                                    &Rmask, &Gmask, &Bmask, &Amask)) {
dalton@2673
   166
        SDL_SetError("Unknown display format");
dalton@2673
   167
        return NULL;
dalton@2673
   168
    }
dalton@2670
   169
dalton@2670
   170
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dalton@2670
   171
    if (!renderer) {
dalton@2670
   172
        SDL_OutOfMemory();
dalton@2670
   173
        return NULL;
dalton@2670
   174
    }
dalton@2670
   175
dalton@2677
   176
    data = (NDS_RenderData *) SDL_malloc(sizeof(*data));
dalton@2670
   177
    if (!data) {
dalton@2677
   178
        NDS_DestroyRenderer(renderer);
dalton@2670
   179
        SDL_OutOfMemory();
dalton@2670
   180
        return NULL;
dalton@2670
   181
    }
dalton@2670
   182
    SDL_zerop(data);
dalton@2670
   183
dalton@2677
   184
    renderer->RenderFill = NDS_RenderFill;
dalton@2677
   185
    renderer->RenderCopy = NDS_RenderCopy;
dalton@2677
   186
    renderer->RenderPresent = NDS_RenderPresent;
dalton@2677
   187
    renderer->DestroyRenderer = NDS_DestroyRenderer;
dalton@2677
   188
    renderer->info.name = NDS_RenderDriver.info.name;
dalton@2670
   189
    renderer->info.flags = 0;
dalton@2670
   190
    renderer->window = window->id;
dalton@2670
   191
    renderer->driverdata = data;
dalton@2679
   192
    renderer->CreateTexture = NDS_CreateTexture;
dalton@2679
   193
    renderer->QueryTexturePixels = NDS_QueryTexturePixels;
dalton@2679
   194
    renderer->SetTexturePalette = NDS_SetTexturePalette;
dalton@2679
   195
    renderer->GetTexturePalette = NDS_GetTexturePalette;
dalton@2679
   196
    renderer->SetTextureColorMod = NDS_SetTextureColorMod;
dalton@2679
   197
    renderer->SetTextureAlphaMod = NDS_SetTextureAlphaMod;
dalton@2679
   198
    renderer->SetTextureBlendMode = NDS_SetTextureBlendMode;
dalton@2679
   199
    renderer->SetTextureScaleMode = NDS_SetTextureScaleMode;
dalton@2679
   200
    renderer->UpdateTexture = NDS_UpdateTexture;
dalton@2679
   201
    renderer->LockTexture = NDS_LockTexture;
dalton@2679
   202
    renderer->UnlockTexture = NDS_UnlockTexture;
dalton@2679
   203
    renderer->DirtyTexture = NDS_DirtyTexture;
dalton@2679
   204
    renderer->DestroyTexture = NDS_DestroyTexture;
dalton@2670
   205
dalton@2679
   206
    renderer->info.mod_modes = NDS_RenderDriver.info.mod_modes;
dalton@2679
   207
    renderer->info.blend_modes = NDS_RenderDriver.info.blend_modes;
dalton@2679
   208
    renderer->info.scale_modes = NDS_RenderDriver.info.scale_modes;
dalton@2679
   209
    renderer->info.num_texture_formats =
dalton@2679
   210
        NDS_RenderDriver.info.num_texture_formats;
dalton@2679
   211
    SDL_memcpy(renderer->info.texture_formats,
dalton@2679
   212
               NDS_RenderDriver.info.texture_formats,
dalton@2679
   213
               sizeof(renderer->info.texture_formats));;
dalton@2679
   214
    renderer->info.max_texture_width = NDS_RenderDriver.info.max_texture_width;
dalton@2679
   215
    renderer->info.max_texture_height =
dalton@2679
   216
        NDS_RenderDriver.info.max_texture_height;
dalton@2670
   217
dalton@2679
   218
    /*data->fb = (u16*)0x06020000;*/
dalton@2679
   219
    data->bg = &BACKGROUND;
dalton@2679
   220
    data->bg_taken[2] = data->bg_taken[3] = 0;
dalton@2679
   221
    data->sub = 0;
dalton@2672
   222
dalton@2670
   223
    return renderer;
dalton@2670
   224
}
dalton@2670
   225
dalton@2670
   226
static int
dalton@2678
   227
NDS_ActivateRenderer(SDL_Renderer * renderer)
dalton@2678
   228
{
dalton@2678
   229
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2678
   230
    /* stub. TODO: figure out what needs to be done, if anything. */
dalton@2678
   231
    return 0;
dalton@2678
   232
}
dalton@2678
   233
dalton@2678
   234
static int
dalton@2678
   235
NDS_DisplayModeChanged(SDL_Renderer * renderer)
dalton@2678
   236
{
dalton@2678
   237
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2678
   238
    /* stub. TODO: figure out what needs to be done */
dalton@2678
   239
    return 0;
dalton@2678
   240
}
dalton@2678
   241
dalton@2678
   242
static int
dalton@2678
   243
NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   244
{
dalton@2678
   245
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2679
   246
    NDS_TextureData *txdat = NULL;
dalton@2678
   247
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   248
        SDL_SetError("Unsupported texture format");
dalton@2678
   249
        return -1;
dalton@2678
   250
    } else {
dalton@2678
   251
        int bpp;
dalton@2678
   252
        Uint32 Rmask, Gmask, Bmask, Amask;
dalton@2678
   253
dalton@2678
   254
        if (!SDL_PixelFormatEnumToMasks
dalton@2678
   255
            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
dalton@2678
   256
            SDL_SetError("Unknown texture format");
dalton@2678
   257
            return -1;
dalton@2678
   258
        }
dalton@2679
   259
        /* conditional statements on w/h to place it as bg/sprite */
dalton@2679
   260
        /*if(texture->w <= 64 && texture->h <= 64) {
dalton@2679
   261
            sprites not implemented yet.  elegant, I know.
dalton@2679
   262
        } else*/ if(texture->w <= 256 && texture->h <= 256) {
dalton@2679
   263
            int whichbg = -1;
dalton@2679
   264
            if(!data->bg_taken[2]) {
dalton@2679
   265
                whichbg = 2;
dalton@2679
   266
                data->bg->bg2_rotation.xdx = 0x100;
dalton@2679
   267
                data->bg->bg2_rotation.xdy = 0;
dalton@2679
   268
                data->bg->bg2_rotation.ydx = 0;
dalton@2679
   269
                data->bg->bg2_rotation.ydy = 0x100;
dalton@2679
   270
                data->bg->bg2_rotation.centerX = 0;
dalton@2679
   271
                data->bg->bg2_rotation.centerY = 0;
dalton@2679
   272
            } else if(!data->bg_taken[3]) {
dalton@2679
   273
                whichbg = 3;
dalton@2679
   274
                data->bg->bg3_rotation.xdx = 0x100;
dalton@2679
   275
                data->bg->bg3_rotation.xdy = 0;
dalton@2679
   276
                data->bg->bg3_rotation.ydx = 0;
dalton@2679
   277
                data->bg->bg3_rotation.ydy = 0x100;
dalton@2679
   278
                data->bg->bg3_rotation.centerX = 0;
dalton@2679
   279
                data->bg->bg3_rotation.centerY = 0;
dalton@2679
   280
            }
dalton@2679
   281
            if(whichbg >= 0) {
dalton@2679
   282
                data->bg->control[whichbg] = (bpp == 8) ?
dalton@2679
   283
                    BG_BMP8_256x256 : BG_BMP16_256x256;
dalton@2679
   284
                data->bg->scroll[whichbg].x = 0;
dalton@2679
   285
                data->bg->scroll[whichbg].y = 0;
dalton@2679
   286
                texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
dalton@2679
   287
                txdat = (NDS_TextureData*)texture->driverdata;
dalton@2679
   288
                txdat->type = NDSTX_BG;
dalton@2679
   289
                txdat->hw_index = whichbg;
dalton@2679
   290
                txdat->dim.w = texture->w;
dalton@2679
   291
                txdat->dim.h = texture->h;
dalton@2679
   292
                txdat->dim.pitch = 256 * (bpp/8);
dalton@2679
   293
                txdat->dim.bpp = bpp;
dalton@2679
   294
                txdat->vram = (u16*)(data->sub ?
dalton@2679
   295
                    BG_BMP_RAM_SUB(whichbg) : BG_BMP_RAM(whichbg));
dalton@2679
   296
            } else {
dalton@2679
   297
                SDL_SetError("Out of NDS backgrounds.");
dalton@2679
   298
            }
dalton@2679
   299
        } else {
dalton@2679
   300
            SDL_SetError("Texture too big for NDS hardware.");
dalton@2679
   301
        }
dalton@2678
   302
    }
dalton@2678
   303
dalton@2678
   304
    if (!texture->driverdata) {
dalton@2678
   305
        return -1;
dalton@2678
   306
    }
dalton@2678
   307
    return 0;
dalton@2678
   308
}
dalton@2678
   309
dalton@2678
   310
static int
dalton@2678
   311
NDS_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   312
                      void **pixels, int *pitch)
dalton@2678
   313
{
dalton@2678
   314
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   315
        SDL_SetError("Unsupported texture format");
dalton@2678
   316
        return -1;
dalton@2678
   317
    } else {
dalton@2678
   318
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   319
dalton@2678
   320
        *pixels = txdat->vram;
dalton@2678
   321
        *pitch = txdat->dim.pitch;
dalton@2678
   322
        return 0;
dalton@2678
   323
    }
dalton@2678
   324
}
dalton@2678
   325
dalton@2678
   326
static int
dalton@2678
   327
NDS_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   328
                     const SDL_Color * colors, int firstcolor, int ncolors)
dalton@2678
   329
{
dalton@2678
   330
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   331
        SDL_SetError("YUV textures don't have a palette");
dalton@2678
   332
        return -1;
dalton@2678
   333
    } else {
dalton@2678
   334
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   335
        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
dalton@2678
   336
        return 0;
dalton@2678
   337
    }
dalton@2678
   338
}
dalton@2678
   339
dalton@2678
   340
static int
dalton@2678
   341
NDS_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   342
                     SDL_Color * colors, int firstcolor, int ncolors)
dalton@2678
   343
{
dalton@2678
   344
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   345
        SDL_SetError("YUV textures don't have a palette");
dalton@2678
   346
        return -1;
dalton@2678
   347
    } else {
dalton@2678
   348
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   349
        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
dalton@2678
   350
        return 0;
dalton@2678
   351
    }
dalton@2678
   352
}
dalton@2678
   353
dalton@2678
   354
static int
dalton@2678
   355
NDS_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   356
{
dalton@2678
   357
    /* stub. TODO: figure out what needs to be done, if anything */
dalton@2678
   358
    return 0;
dalton@2678
   359
}
dalton@2678
   360
dalton@2678
   361
static int
dalton@2678
   362
NDS_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   363
{
dalton@2678
   364
    /* stub. TODO: figure out what needs to be done, if anything */
dalton@2678
   365
    return 0;
dalton@2678
   366
}
dalton@2678
   367
dalton@2678
   368
static int
dalton@2678
   369
NDS_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   370
{
dalton@2678
   371
    /* stub. TODO: figure out what needs to be done, if anything */
dalton@2678
   372
    return 0;
dalton@2678
   373
}
dalton@2678
   374
dalton@2678
   375
static int
dalton@2678
   376
NDS_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   377
{
dalton@2678
   378
    /* stub. TODO: figure out what needs to be done.
dalton@2678
   379
       (NDS hardware scaling is nearest neighbor.) */
dalton@2678
   380
    return 0;
dalton@2678
   381
}
dalton@2678
   382
dalton@2678
   383
static int
dalton@2678
   384
NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   385
                 const SDL_Rect * rect, const void *pixels, int pitch)
dalton@2678
   386
{
dalton@2678
   387
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   388
        SDL_SetError("Unsupported texture format");
dalton@2678
   389
        return -1;
dalton@2678
   390
    } else {
dalton@2678
   391
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   392
        Uint8 *src, *dst;
dalton@2678
   393
        int row;
dalton@2678
   394
        size_t length;
dalton@2678
   395
        /* IMPORTANT! copy the new pixels into the sprite or bg. */
dalton@2678
   396
        src = (Uint8 *) pixels;
dalton@2678
   397
        dst =
dalton@2678
   398
            (Uint8 *) txdat->vram + rect->y * txdat->dim.pitch +
dalton@2678
   399
            rect->x * (txdat->dim.bpp/8);
dalton@2678
   400
        length = rect->w * (txdat->dim.bpp/8);
dalton@2678
   401
        for (row = 0; row < rect->h; ++row) {
dalton@2678
   402
            SDL_memcpy(dst, src, length);
dalton@2678
   403
            src += pitch;
dalton@2679
   404
            dst += txdat->dim.pitch;
dalton@2678
   405
        }
dalton@2678
   406
        return 0;
dalton@2678
   407
    }
dalton@2678
   408
}
dalton@2678
   409
dalton@2678
   410
static int
dalton@2678
   411
NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   412
               const SDL_Rect * rect, int markDirty, void **pixels,
dalton@2678
   413
               int *pitch)
dalton@2678
   414
{
dalton@2678
   415
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   416
        SDL_SetError("Unsupported texture format");
dalton@2678
   417
        return -1;
dalton@2678
   418
    } else {
dalton@2678
   419
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   420
dalton@2678
   421
        *pixels = (void *) ((u8 *)txdat->vram + rect->y * txdat->dim.pitch
dalton@2678
   422
                            + rect->x * (txdat->dim.bpp/8));
dalton@2678
   423
        *pitch = txdat->dim.pitch;
dalton@2678
   424
        return 0;
dalton@2678
   425
    }
dalton@2678
   426
}
dalton@2678
   427
dalton@2678
   428
static void
dalton@2678
   429
NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   430
{
dalton@2678
   431
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   432
        SDL_SetError("Unsupported texture format");
dalton@2678
   433
    }
dalton@2678
   434
}
dalton@2678
   435
dalton@2678
   436
static void
dalton@2678
   437
NDS_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   438
                int numrects, const SDL_Rect * rects)
dalton@2678
   439
{ /* stub */
dalton@2678
   440
}
dalton@2678
   441
dalton@2678
   442
static int
dalton@2677
   443
NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
dalton@2671
   444
                   Uint8 a, const SDL_Rect * rect)
dalton@2670
   445
{
dalton@2677
   446
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2670
   447
    SDL_Rect real_rect = *rect;
dalton@2677
   448
    u16 color;
dalton@2677
   449
    int i, j;
dalton@2670
   450
dalton@2679
   451
    /* TODO: make a single-color sprite and stretch it.
dalton@2677
   452
    color = RGB15(r>>3,g>>3,b>>3);
dalton@2677
   453
    for (i = real_rect.x; i < real_rect.x+real_rect.w; ++i) {
dalton@2677
   454
        for (j = real_rect.y; j < real_rect.y+real_rect.h; ++j) {
dalton@2677
   455
            data->fb[(j + real_rect.y) * 256 + i + real_rect.x] = 
dalton@2677
   456
                0x8000 | color;
dalton@2677
   457
        }
dalton@2679
   458
    }*/
dalton@2677
   459
    return 0;
dalton@2670
   460
}
dalton@2678
   461
dalton@2670
   462
static int
dalton@2677
   463
NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2671
   464
                   const SDL_Rect * srcrect, const SDL_Rect * dstrect)
dalton@2670
   465
{
dalton@2677
   466
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2670
   467
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
dalton@2670
   468
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2677
   469
dalton@2672
   470
#if 0
dalton@2670
   471
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2670
   472
        SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   473
        void *pixels =
dalton@2670
   474
            (Uint8 *) target->pixels + dstrect->y * target->pitch +
dalton@2670
   475
            dstrect->x * target->format->BytesPerPixel;
dalton@2670
   476
        return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
dalton@2670
   477
                                   srcrect, display->current_mode.format,
dalton@2670
   478
                                   dstrect->w, dstrect->h, pixels,
dalton@2670
   479
                                   target->pitch);
dalton@2670
   480
    } else {
dalton@2670
   481
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
dalton@2670
   482
        SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   483
        SDL_Rect real_srcrect = *srcrect;
dalton@2670
   484
        SDL_Rect real_dstrect = *dstrect;
dalton@2670
   485
        return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
dalton@2670
   486
    }
dalton@2677
   487
#endif
dalton@2672
   488
    return 0;
dalton@2671
   489
}
dalton@2671
   490
dalton@2670
   491
dalton@2670
   492
static void
dalton@2677
   493
NDS_RenderPresent(SDL_Renderer * renderer)
dalton@2670
   494
{
dalton@2677
   495
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2677
   496
    /* Send the data to the display TODO */
dalton@2670
   497
dalton@2670
   498
    /* Update the flipping chain, if any */
dalton@2679
   499
    if (renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
dalton@2679
   500
        swiWaitForVBlank();
dalton@2670
   501
    }
dalton@2670
   502
}
dalton@2670
   503
dalton@2670
   504
static void
dalton@2678
   505
NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   506
{
dalton@2678
   507
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   508
        SDL_SetError("Unsupported texture format");
dalton@2678
   509
    } else {
dalton@2679
   510
        /* free anything else allocated for texture */
dalton@2679
   511
        SDL_free(texture->driverdata);
dalton@2678
   512
    }
dalton@2678
   513
}
dalton@2678
   514
dalton@2678
   515
static void
dalton@2677
   516
NDS_DestroyRenderer(SDL_Renderer * renderer)
dalton@2670
   517
{
dalton@2677
   518
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2678
   519
    /*SDL_Window *window = SDL_GetWindowFromID(renderer->window);
dalton@2678
   520
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);*/
dalton@2670
   521
    int i;
dalton@2670
   522
dalton@2670
   523
    if (data) {
dalton@2679
   524
        /* TODO: free anything relevant. */
dalton@2679
   525
        /*for (i = 0; i < SDL_arraysize(data->texture); ++i) {
dalton@2678
   526
            if (data->texture[i]) {
dalton@2678
   527
                DestroyTexture(data->renderer, data->texture[i]);
dalton@2670
   528
            }
dalton@2678
   529
        }
dalton@2678
   530
        if (data->surface.format) {
dalton@2678
   531
            SDL_SetSurfacePalette(&data->surface, NULL);
dalton@2678
   532
            SDL_FreeFormat(data->surface.format);
dalton@2678
   533
        }
dalton@2678
   534
        if (display->palette) {
dalton@2678
   535
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
dalton@2678
   536
                                data);
dalton@2678
   537
        }
dalton@2679
   538
        SDL_FreeDirtyRects(&data->dirty);*/
dalton@2670
   539
        SDL_free(data);
dalton@2670
   540
    }
dalton@2670
   541
    SDL_free(renderer);
dalton@2670
   542
}
dalton@2670
   543
dalton@2670
   544
/* vi: set ts=4 sw=4 expandtab: */