src/video/nds/SDL_ndsrender.c
author Darren Alton <dalton@stevens.edu>
Thu, 10 Jul 2008 23:35:01 +0000
branchgsoc2008_nds
changeset 2678 3895761db26a
parent 2677 6386764eb222
child 2679 bc3e3e889f6d
permissions -rw-r--r--
Removed old touchscreen system, starting to implement mouse-based touchscreen. Also, more work on a non-software-rendering video driver
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@2678
    82
    {"nds", SDL_RENDERER_SINGLEBUFFER}
dalton@2678
    83
/*SDL_RENDERER_ values
dalton@2678
    84
SINGLEBUFFER   Render directly to the window, if possible
dalton@2678
    85
PRESENTCOPY    Present uses a copy from back buffer to the front buffer
dalton@2678
    86
PRESENTFLIP2   Present uses a flip, swapping back buffer and front buffer
dalton@2678
    87
PRESENTFLIP3   Present uses a flip, rotating two back buf.s and a front buf.
dalton@2678
    88
PRESENTDISCARD Present leaves the contents of the backbuffer undefined
dalton@2678
    89
PRESENTVSYNC   Present is synchronized with the refresh rate
dalton@2678
    90
ACCELERATED    The renderer uses hardware acceleration
dalton@2678
    91
*/
dalton@2670
    92
};
dalton@2670
    93
dalton@2670
    94
typedef struct
dalton@2670
    95
{
dalton@2670
    96
    int current_screen;
dalton@2677
    97
    u16* fb;
dalton@2677
    98
} NDS_RenderData;
dalton@2670
    99
dalton@2678
   100
typedef struct
dalton@2678
   101
{
dalton@2678
   102
    enum { NDSTX_BG, NDSTX_SPR } type;
dalton@2678
   103
    struct { int w, h, pitch, bpp; } dim;
dalton@2678
   104
    u16 *vram;
dalton@2678
   105
} NDS_TextureData;
dalton@2672
   106
dalton@2672
   107
dalton@2672
   108
/* this is mainly hackish testing/debugging stuff to get cleaned up soon
dalton@2672
   109
   anything named sdlds_blah shouldn't make it into the stable version
dalton@2672
   110
 */
dalton@2672
   111
dalton@2672
   112
u16
dalton@2673
   113
sdlds_rgb2bgr(u16 c)
dalton@2673
   114
{
dalton@2673
   115
/* hack to get the proper colors until I actually get BGR555 to work right */
dalton@2672
   116
    u16 Rmask = 0x7C00, Bmask = 0x001F, GAmask = 0x83E0, r, b;
dalton@2672
   117
    r = (c & Rmask) >> 10;
dalton@2672
   118
    b = (c & Bmask) << 10;
dalton@2672
   119
    return (c & GAmask) | r | b;
dalton@2672
   120
}
dalton@2672
   121
dalton@2672
   122
void
dalton@2672
   123
sdlds_surf2vram(SDL_Surface * s)
dalton@2672
   124
{
dalton@2673
   125
    if (s->w == 256) {
dalton@2677
   126
        u16 tmpbuf[0x20000];
dalton@2677
   127
        int i;
dalton@2677
   128
dalton@2673
   129
        dmaCopy((u8 *) (s->pixels) + 156 * sizeof(u16),
dalton@2677
   130
                tmpbuf, 256 * 192 * sizeof(u16));
dalton@2677
   131
    /* hack to fix the pixel format until I figure out why BGR doesn't work */
dalton@2677
   132
        for (i = 0; i < 256 * 192; ++i) {
dalton@2677
   133
            tmpbuf[i] = sdlds_rgb2bgr(tmpbuf[i]);
dalton@2677
   134
        }
dalton@2677
   135
        dmaCopy(tmpbuf, VRAM_A, 256 * 192 * sizeof(u16));
dalton@2672
   136
    }
dalton@2672
   137
}
dalton@2672
   138
dalton@2672
   139
void
dalton@2673
   140
sdlds_print_pixfmt_info(SDL_PixelFormat * f)
dalton@2673
   141
{
dalton@2673
   142
    if (!f)
dalton@2673
   143
        return;
dalton@2673
   144
    printf("bpp: %d\nRGBA: %x %x %x %x\n",
dalton@2673
   145
           f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask);
dalton@2672
   146
}
dalton@2672
   147
dalton@2672
   148
void
dalton@2673
   149
sdlds_print_surface_info(SDL_Surface * s)
dalton@2673
   150
{
dalton@2673
   151
    if (!s)
dalton@2673
   152
        return;
dalton@2673
   153
    printf("flags: %x\nsize: %dx%d, pitch: %d\nlocked: %d, refcount: %d\n",
dalton@2673
   154
           s->flags, s->w, s->h, s->pitch, s->locked, s->refcount);
dalton@2673
   155
    sdlds_print_pixfmt_info(s->format);
dalton@2672
   156
}
dalton@2672
   157
dalton@2678
   158
/* again the above shouldn't make it into the stable version */
dalton@2672
   159
dalton@2670
   160
SDL_Renderer *
dalton@2677
   161
NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
dalton@2670
   162
{
dalton@2670
   163
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2670
   164
    SDL_DisplayMode *displayMode = &display->current_mode;
dalton@2670
   165
    SDL_Renderer *renderer;
dalton@2677
   166
    NDS_RenderData *data;
dalton@2670
   167
    int i, n;
dalton@2672
   168
    int bpp = 15;
dalton@2673
   169
    Uint32 Rmask, Gmask, Bmask, Amask;
dalton@2673
   170
/*  Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
dalton@2672
   171
    Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000;
dalton@2673
   172
*/
dalton@2670
   173
dalton@2673
   174
    /* hard coded this to BGR555 for now */
dalton@2673
   175
    if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp,
dalton@2673
   176
                                    &Rmask, &Gmask, &Bmask, &Amask)) {
dalton@2673
   177
        SDL_SetError("Unknown display format");
dalton@2673
   178
        return NULL;
dalton@2673
   179
    }
dalton@2670
   180
dalton@2670
   181
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dalton@2670
   182
    if (!renderer) {
dalton@2670
   183
        SDL_OutOfMemory();
dalton@2670
   184
        return NULL;
dalton@2670
   185
    }
dalton@2670
   186
dalton@2677
   187
    data = (NDS_RenderData *) SDL_malloc(sizeof(*data));
dalton@2670
   188
    if (!data) {
dalton@2677
   189
        NDS_DestroyRenderer(renderer);
dalton@2670
   190
        SDL_OutOfMemory();
dalton@2670
   191
        return NULL;
dalton@2670
   192
    }
dalton@2670
   193
    SDL_zerop(data);
dalton@2670
   194
dalton@2677
   195
    renderer->RenderFill = NDS_RenderFill;
dalton@2677
   196
    renderer->RenderCopy = NDS_RenderCopy;
dalton@2677
   197
    renderer->RenderPresent = NDS_RenderPresent;
dalton@2677
   198
    renderer->DestroyRenderer = NDS_DestroyRenderer;
dalton@2677
   199
    renderer->info.name = NDS_RenderDriver.info.name;
dalton@2670
   200
    renderer->info.flags = 0;
dalton@2670
   201
    renderer->window = window->id;
dalton@2670
   202
    renderer->driverdata = data;
dalton@2677
   203
    Setup_SoftwareRenderer(renderer); /* TODO: well, "TODON'T" is more like it */
dalton@2670
   204
dalton@2670
   205
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
dalton@2670
   206
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
dalton@2670
   207
        n = 2;
dalton@2670
   208
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
dalton@2670
   209
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
dalton@2670
   210
        n = 3;
dalton@2670
   211
    } else {
dalton@2670
   212
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
dalton@2670
   213
        n = 1;
dalton@2678
   214
    }
dalton@2678
   215
    /*
dalton@2670
   216
    for (i = 0; i < n; ++i) {
dalton@2670
   217
        data->screens[i] =
dalton@2673
   218
            SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask,
dalton@2673
   219
                                 Amask);
dalton@2670
   220
        if (!data->screens[i]) {
dalton@2677
   221
            NDS_DestroyRenderer(renderer);
dalton@2670
   222
            return NULL;
dalton@2670
   223
        }
dalton@2670
   224
        SDL_SetSurfacePalette(data->screens[i], display->palette);
dalton@2673
   225
        sdlds_print_surface_info(data->screens[i]);
dalton@2677
   226
    }*/
dalton@2670
   227
dalton@2677
   228
    data->fb = (u16*)0x06020000;
dalton@2672
   229
dalton@2670
   230
    return renderer;
dalton@2670
   231
}
dalton@2670
   232
dalton@2670
   233
static int
dalton@2678
   234
NDS_ActivateRenderer(SDL_Renderer * renderer)
dalton@2678
   235
{
dalton@2678
   236
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2678
   237
    /* stub. TODO: figure out what needs to be done, if anything. */
dalton@2678
   238
    return 0;
dalton@2678
   239
}
dalton@2678
   240
dalton@2678
   241
static int
dalton@2678
   242
NDS_DisplayModeChanged(SDL_Renderer * renderer)
dalton@2678
   243
{
dalton@2678
   244
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2678
   245
    /* stub. TODO: figure out what needs to be done */
dalton@2678
   246
    return 0;
dalton@2678
   247
}
dalton@2678
   248
dalton@2678
   249
static int
dalton@2678
   250
NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   251
{
dalton@2678
   252
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2678
   253
dalton@2678
   254
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   255
        SDL_SetError("Unsupported texture format");
dalton@2678
   256
        return -1;
dalton@2678
   257
    } else {
dalton@2678
   258
        int bpp;
dalton@2678
   259
        Uint32 Rmask, Gmask, Bmask, Amask;
dalton@2678
   260
dalton@2678
   261
        if (!SDL_PixelFormatEnumToMasks
dalton@2678
   262
            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
dalton@2678
   263
            SDL_SetError("Unknown texture format");
dalton@2678
   264
            return -1;
dalton@2678
   265
        }
dalton@2678
   266
        /* TODO: appropriate checks for ABGR1555 */
dalton@2678
   267
        texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
dalton@2678
   268
        /* TODO: conditional statements on w/h to place it as bg/sprite */
dalton@2678
   269
    }
dalton@2678
   270
dalton@2678
   271
    if (!texture->driverdata) {
dalton@2678
   272
        return -1;
dalton@2678
   273
    }
dalton@2678
   274
    return 0;
dalton@2678
   275
}
dalton@2678
   276
dalton@2678
   277
static int
dalton@2678
   278
NDS_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   279
                      void **pixels, int *pitch)
dalton@2678
   280
{
dalton@2678
   281
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   282
        SDL_SetError("Unsupported texture format");
dalton@2678
   283
        return -1;
dalton@2678
   284
    } else {
dalton@2678
   285
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   286
dalton@2678
   287
        *pixels = txdat->vram;
dalton@2678
   288
        *pitch = txdat->dim.pitch;
dalton@2678
   289
        return 0;
dalton@2678
   290
    }
dalton@2678
   291
}
dalton@2678
   292
dalton@2678
   293
static int
dalton@2678
   294
NDS_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   295
                     const SDL_Color * colors, int firstcolor, int ncolors)
dalton@2678
   296
{
dalton@2678
   297
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   298
        SDL_SetError("YUV textures don't have a palette");
dalton@2678
   299
        return -1;
dalton@2678
   300
    } else {
dalton@2678
   301
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   302
        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
dalton@2678
   303
        return 0;
dalton@2678
   304
    }
dalton@2678
   305
}
dalton@2678
   306
dalton@2678
   307
static int
dalton@2678
   308
NDS_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   309
                     SDL_Color * colors, int firstcolor, int ncolors)
dalton@2678
   310
{
dalton@2678
   311
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   312
        SDL_SetError("YUV textures don't have a palette");
dalton@2678
   313
        return -1;
dalton@2678
   314
    } else {
dalton@2678
   315
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   316
        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
dalton@2678
   317
        return 0;
dalton@2678
   318
    }
dalton@2678
   319
}
dalton@2678
   320
dalton@2678
   321
static int
dalton@2678
   322
NDS_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   323
{
dalton@2678
   324
    /* stub. TODO: figure out what needs to be done, if anything */
dalton@2678
   325
    return 0;
dalton@2678
   326
}
dalton@2678
   327
dalton@2678
   328
static int
dalton@2678
   329
NDS_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   330
{
dalton@2678
   331
    /* stub. TODO: figure out what needs to be done, if anything */
dalton@2678
   332
    return 0;
dalton@2678
   333
}
dalton@2678
   334
dalton@2678
   335
static int
dalton@2678
   336
NDS_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   337
{
dalton@2678
   338
    /* stub. TODO: figure out what needs to be done, if anything */
dalton@2678
   339
    return 0;
dalton@2678
   340
}
dalton@2678
   341
dalton@2678
   342
static int
dalton@2678
   343
NDS_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   344
{
dalton@2678
   345
    /* stub. TODO: figure out what needs to be done.
dalton@2678
   346
       (NDS hardware scaling is nearest neighbor.) */
dalton@2678
   347
    return 0;
dalton@2678
   348
}
dalton@2678
   349
dalton@2678
   350
static int
dalton@2678
   351
NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   352
                 const SDL_Rect * rect, const void *pixels, int pitch)
dalton@2678
   353
{
dalton@2678
   354
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   355
        SDL_SetError("Unsupported texture format");
dalton@2678
   356
        return -1;
dalton@2678
   357
    } else {
dalton@2678
   358
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   359
        Uint8 *src, *dst;
dalton@2678
   360
        int row;
dalton@2678
   361
        size_t length;
dalton@2678
   362
        /* IMPORTANT! copy the new pixels into the sprite or bg. */
dalton@2678
   363
        src = (Uint8 *) pixels;
dalton@2678
   364
        dst =
dalton@2678
   365
            (Uint8 *) txdat->vram + rect->y * txdat->dim.pitch +
dalton@2678
   366
            rect->x * (txdat->dim.bpp/8);
dalton@2678
   367
        length = rect->w * (txdat->dim.bpp/8);
dalton@2678
   368
        for (row = 0; row < rect->h; ++row) {
dalton@2678
   369
            SDL_memcpy(dst, src, length);
dalton@2678
   370
            src += pitch;
dalton@2678
   371
            dst += surface->pitch;
dalton@2678
   372
        }
dalton@2678
   373
        return 0;
dalton@2678
   374
    }
dalton@2678
   375
}
dalton@2678
   376
dalton@2678
   377
static int
dalton@2678
   378
NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   379
               const SDL_Rect * rect, int markDirty, void **pixels,
dalton@2678
   380
               int *pitch)
dalton@2678
   381
{
dalton@2678
   382
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   383
        SDL_SetError("Unsupported texture format");
dalton@2678
   384
        return -1;
dalton@2678
   385
    } else {
dalton@2678
   386
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   387
dalton@2678
   388
        *pixels = (void *) ((u8 *)txdat->vram + rect->y * txdat->dim.pitch
dalton@2678
   389
                            + rect->x * (txdat->dim.bpp/8));
dalton@2678
   390
        *pitch = txdat->dim.pitch;
dalton@2678
   391
        return 0;
dalton@2678
   392
    }
dalton@2678
   393
}
dalton@2678
   394
dalton@2678
   395
static void
dalton@2678
   396
NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   397
{
dalton@2678
   398
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   399
        SDL_SetError("Unsupported texture format");
dalton@2678
   400
    }
dalton@2678
   401
}
dalton@2678
   402
dalton@2678
   403
static void
dalton@2678
   404
NDS_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   405
                int numrects, const SDL_Rect * rects)
dalton@2678
   406
{ /* stub */
dalton@2678
   407
}
dalton@2678
   408
dalton@2678
   409
static int
dalton@2677
   410
NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
dalton@2671
   411
                   Uint8 a, const SDL_Rect * rect)
dalton@2670
   412
{
dalton@2677
   413
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2670
   414
    SDL_Rect real_rect = *rect;
dalton@2677
   415
    u16 color;
dalton@2677
   416
    int i, j;
dalton@2670
   417
dalton@2677
   418
    color = RGB15(r>>3,g>>3,b>>3);
dalton@2677
   419
    for (i = real_rect.x; i < real_rect.x+real_rect.w; ++i) {
dalton@2677
   420
        for (j = real_rect.y; j < real_rect.y+real_rect.h; ++j) {
dalton@2677
   421
            data->fb[(j + real_rect.y) * 256 + i + real_rect.x] = 
dalton@2677
   422
                0x8000 | color;
dalton@2677
   423
        }
dalton@2677
   424
    }
dalton@2677
   425
    return 0;
dalton@2670
   426
}
dalton@2678
   427
dalton@2670
   428
static int
dalton@2677
   429
NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2671
   430
                   const SDL_Rect * srcrect, const SDL_Rect * dstrect)
dalton@2670
   431
{
dalton@2677
   432
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2670
   433
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
dalton@2670
   434
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2677
   435
dalton@2672
   436
#if 0
dalton@2670
   437
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2670
   438
        SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   439
        void *pixels =
dalton@2670
   440
            (Uint8 *) target->pixels + dstrect->y * target->pitch +
dalton@2670
   441
            dstrect->x * target->format->BytesPerPixel;
dalton@2670
   442
        return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
dalton@2670
   443
                                   srcrect, display->current_mode.format,
dalton@2670
   444
                                   dstrect->w, dstrect->h, pixels,
dalton@2670
   445
                                   target->pitch);
dalton@2670
   446
    } else {
dalton@2670
   447
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
dalton@2670
   448
        SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   449
        SDL_Rect real_srcrect = *srcrect;
dalton@2670
   450
        SDL_Rect real_dstrect = *dstrect;
dalton@2672
   451
        /*sdlds_print_surface_info(surface);
dalton@2673
   452
           sdlds_print_surface_info(target); */
dalton@2670
   453
        sdlds_surf2vram(surface);
dalton@2670
   454
        return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
dalton@2670
   455
    }
dalton@2677
   456
#endif
dalton@2672
   457
    /* copy it directly to vram */
dalton@2671
   458
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
dalton@2677
   459
    sdlds_surf2vram(surface);
dalton@2677
   460
    /*
dalton@2671
   461
    int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h;
dalton@2671
   462
    int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h;
dalton@2671
   463
    int si, sj, di, dj;
dalton@2671
   464
    for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) {
dalton@2671
   465
        for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) {
dalton@2677
   466
            data->fb[(dj + dy) * 256 + di + dx] = 0x8000 |
dalton@2677
   467
                ((u16 *) surface->pixels)[(sj + sy) * (surface->w) + si +
dalton@2673
   468
                                             sx];
dalton@2670
   469
        }
dalton@2677
   470
    }*/
dalton@2672
   471
    return 0;
dalton@2671
   472
}
dalton@2671
   473
dalton@2670
   474
dalton@2670
   475
static void
dalton@2677
   476
NDS_RenderPresent(SDL_Renderer * renderer)
dalton@2670
   477
{
dalton@2677
   478
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2677
   479
    /* Send the data to the display TODO */
dalton@2670
   480
dalton@2670
   481
    /* Update the flipping chain, if any */
dalton@2670
   482
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
dalton@2670
   483
        data->current_screen = (data->current_screen + 1) % 2;
dalton@2670
   484
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
dalton@2670
   485
        data->current_screen = (data->current_screen + 1) % 3;
dalton@2670
   486
    }
dalton@2670
   487
}
dalton@2670
   488
dalton@2670
   489
static void
dalton@2678
   490
NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   491
{
dalton@2678
   492
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   493
        SDL_SetError("Unsupported texture format");
dalton@2678
   494
    } else {
dalton@2678
   495
        /* TODO: free anything allocated for texture */
dalton@2678
   496
        /*SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
dalton@2678
   497
        SDL_FreeSurface(surface);*/
dalton@2678
   498
    }
dalton@2678
   499
}
dalton@2678
   500
dalton@2678
   501
static void
dalton@2677
   502
NDS_DestroyRenderer(SDL_Renderer * renderer)
dalton@2670
   503
{
dalton@2677
   504
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2678
   505
    /*SDL_Window *window = SDL_GetWindowFromID(renderer->window);
dalton@2678
   506
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);*/
dalton@2670
   507
    int i;
dalton@2670
   508
dalton@2670
   509
    if (data) {
dalton@2678
   510
        for (i = 0; i < SDL_arraysize(data->texture); ++i) {
dalton@2678
   511
            if (data->texture[i]) {
dalton@2678
   512
                DestroyTexture(data->renderer, data->texture[i]);
dalton@2670
   513
            }
dalton@2678
   514
        }
dalton@2678
   515
        if (data->surface.format) {
dalton@2678
   516
            SDL_SetSurfacePalette(&data->surface, NULL);
dalton@2678
   517
            SDL_FreeFormat(data->surface.format);
dalton@2678
   518
        }
dalton@2678
   519
        if (display->palette) {
dalton@2678
   520
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
dalton@2678
   521
                                data);
dalton@2678
   522
        }
dalton@2678
   523
        SDL_FreeDirtyRects(&data->dirty);
dalton@2670
   524
        SDL_free(data);
dalton@2670
   525
    }
dalton@2670
   526
    SDL_free(renderer);
dalton@2670
   527
}
dalton@2670
   528
dalton@2670
   529
/* vi: set ts=4 sw=4 expandtab: */