src/video/nds/SDL_ndsrender.c
author Darren Alton
Thu, 14 Aug 2008 04:33:55 +0000
branchgsoc2008_nds
changeset 2687 5166b19b6808
parent 2685 2190b873ff00
child 2688 71c56e900f8b
permissions -rw-r--r--
renderer: Added a separate copy of texture pixels in system RAM, copied into VRAM during RenderPresent. Still trying to fix rendering.
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@2685
    35
/* SDL NDS 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@2681
    90
            SDL_PIXELFORMAT_ABGR1555,
dalton@2681
    91
            SDL_PIXELFORMAT_BGR555,
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@2685
   102
    /* todo for sprites: pSpriteRotation and pSpriteEntry. pointers to OAM */
dalton@2679
   103
    int sub;
dalton@2677
   104
} NDS_RenderData;
dalton@2670
   105
dalton@2678
   106
typedef struct
dalton@2678
   107
{
dalton@2678
   108
    enum { NDSTX_BG, NDSTX_SPR } type;
dalton@2679
   109
    int hw_index;
dalton@2685
   110
    struct { int hdx, hdy, vdx, vdy, pitch, bpp; } dim;
dalton@2678
   111
    u16 *vram;
dalton@2687
   112
    u16 *system_ram_copy;
dalton@2687
   113
    int size;
dalton@2678
   114
} NDS_TextureData;
dalton@2672
   115
dalton@2672
   116
dalton@2672
   117
/* this is mainly hackish testing/debugging stuff to get cleaned up soon
dalton@2672
   118
   anything named sdlds_blah shouldn't make it into the stable version
dalton@2672
   119
 */
dalton@2672
   120
dalton@2672
   121
u16
dalton@2673
   122
sdlds_rgb2bgr(u16 c)
dalton@2673
   123
{
dalton@2673
   124
/* hack to get the proper colors until I actually get BGR555 to work right */
dalton@2672
   125
    u16 Rmask = 0x7C00, Bmask = 0x001F, GAmask = 0x83E0, r, b;
dalton@2672
   126
    r = (c & Rmask) >> 10;
dalton@2672
   127
    b = (c & Bmask) << 10;
dalton@2672
   128
    return (c & GAmask) | r | b;
dalton@2672
   129
}
dalton@2672
   130
dalton@2678
   131
/* again the above shouldn't make it into the stable version */
dalton@2672
   132
dalton@2670
   133
SDL_Renderer *
dalton@2677
   134
NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
dalton@2670
   135
{
dalton@2670
   136
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2670
   137
    SDL_DisplayMode *displayMode = &display->current_mode;
dalton@2670
   138
    SDL_Renderer *renderer;
dalton@2677
   139
    NDS_RenderData *data;
dalton@2670
   140
    int i, n;
dalton@2672
   141
    int bpp = 15;
dalton@2673
   142
    Uint32 Rmask, Gmask, Bmask, Amask;
dalton@2673
   143
/*  Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
dalton@2672
   144
    Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000;
dalton@2673
   145
*/
dalton@2680
   146
printf("+NDS_CreateRenderer\n");
dalton@2670
   147
dalton@2673
   148
    /* hard coded this to BGR555 for now */
dalton@2673
   149
    if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp,
dalton@2673
   150
                                    &Rmask, &Gmask, &Bmask, &Amask)) {
dalton@2673
   151
        SDL_SetError("Unknown display format");
dalton@2673
   152
        return NULL;
dalton@2673
   153
    }
dalton@2670
   154
dalton@2670
   155
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dalton@2670
   156
    if (!renderer) {
dalton@2670
   157
        SDL_OutOfMemory();
dalton@2670
   158
        return NULL;
dalton@2670
   159
    }
dalton@2670
   160
dalton@2677
   161
    data = (NDS_RenderData *) SDL_malloc(sizeof(*data));
dalton@2670
   162
    if (!data) {
dalton@2677
   163
        NDS_DestroyRenderer(renderer);
dalton@2670
   164
        SDL_OutOfMemory();
dalton@2670
   165
        return NULL;
dalton@2670
   166
    }
dalton@2670
   167
    SDL_zerop(data);
dalton@2670
   168
dalton@2677
   169
    renderer->RenderFill = NDS_RenderFill;
dalton@2677
   170
    renderer->RenderCopy = NDS_RenderCopy;
dalton@2677
   171
    renderer->RenderPresent = NDS_RenderPresent;
dalton@2677
   172
    renderer->DestroyRenderer = NDS_DestroyRenderer;
dalton@2677
   173
    renderer->info.name = NDS_RenderDriver.info.name;
dalton@2670
   174
    renderer->info.flags = 0;
dalton@2670
   175
    renderer->window = window->id;
dalton@2670
   176
    renderer->driverdata = data;
dalton@2679
   177
    renderer->CreateTexture = NDS_CreateTexture;
dalton@2679
   178
    renderer->QueryTexturePixels = NDS_QueryTexturePixels;
dalton@2679
   179
    renderer->SetTexturePalette = NDS_SetTexturePalette;
dalton@2679
   180
    renderer->GetTexturePalette = NDS_GetTexturePalette;
dalton@2679
   181
    renderer->SetTextureColorMod = NDS_SetTextureColorMod;
dalton@2679
   182
    renderer->SetTextureAlphaMod = NDS_SetTextureAlphaMod;
dalton@2679
   183
    renderer->SetTextureBlendMode = NDS_SetTextureBlendMode;
dalton@2679
   184
    renderer->SetTextureScaleMode = NDS_SetTextureScaleMode;
dalton@2679
   185
    renderer->UpdateTexture = NDS_UpdateTexture;
dalton@2679
   186
    renderer->LockTexture = NDS_LockTexture;
dalton@2679
   187
    renderer->UnlockTexture = NDS_UnlockTexture;
dalton@2679
   188
    renderer->DirtyTexture = NDS_DirtyTexture;
dalton@2679
   189
    renderer->DestroyTexture = NDS_DestroyTexture;
dalton@2670
   190
dalton@2679
   191
    renderer->info.mod_modes = NDS_RenderDriver.info.mod_modes;
dalton@2679
   192
    renderer->info.blend_modes = NDS_RenderDriver.info.blend_modes;
dalton@2679
   193
    renderer->info.scale_modes = NDS_RenderDriver.info.scale_modes;
dalton@2679
   194
    renderer->info.num_texture_formats =
dalton@2679
   195
        NDS_RenderDriver.info.num_texture_formats;
dalton@2679
   196
    SDL_memcpy(renderer->info.texture_formats,
dalton@2679
   197
               NDS_RenderDriver.info.texture_formats,
dalton@2679
   198
               sizeof(renderer->info.texture_formats));;
dalton@2679
   199
    renderer->info.max_texture_width = NDS_RenderDriver.info.max_texture_width;
dalton@2679
   200
    renderer->info.max_texture_height =
dalton@2679
   201
        NDS_RenderDriver.info.max_texture_height;
dalton@2670
   202
dalton@2679
   203
    /*data->fb = (u16*)0x06020000;*/
dalton@2679
   204
    data->bg = &BACKGROUND;
dalton@2679
   205
    data->bg_taken[2] = data->bg_taken[3] = 0;
dalton@2679
   206
    data->sub = 0;
dalton@2672
   207
dalton@2680
   208
printf("-NDS_CreateRenderer\n");
dalton@2670
   209
    return renderer;
dalton@2670
   210
}
dalton@2670
   211
dalton@2670
   212
static int
dalton@2678
   213
NDS_ActivateRenderer(SDL_Renderer * renderer)
dalton@2678
   214
{
dalton@2678
   215
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2678
   216
    /* stub. TODO: figure out what needs to be done, if anything. */
dalton@2680
   217
printf("!NDS_ActivateRenderer\n");
dalton@2678
   218
    return 0;
dalton@2678
   219
}
dalton@2678
   220
dalton@2678
   221
static int
dalton@2678
   222
NDS_DisplayModeChanged(SDL_Renderer * renderer)
dalton@2678
   223
{
dalton@2678
   224
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2678
   225
    /* stub. TODO: figure out what needs to be done */
dalton@2680
   226
printf("!NDS_DisplayModeChanged\n");
dalton@2678
   227
    return 0;
dalton@2678
   228
}
dalton@2678
   229
dalton@2678
   230
static int
dalton@2678
   231
NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   232
{
dalton@2678
   233
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2679
   234
    NDS_TextureData *txdat = NULL;
dalton@2682
   235
    int i;
dalton@2680
   236
printf("+NDS_CreateTexture\n");
dalton@2678
   237
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   238
        SDL_SetError("Unsupported texture format");
dalton@2678
   239
        return -1;
dalton@2678
   240
    } else {
dalton@2678
   241
        int bpp;
dalton@2678
   242
        Uint32 Rmask, Gmask, Bmask, Amask;
dalton@2678
   243
dalton@2678
   244
        if (!SDL_PixelFormatEnumToMasks
dalton@2678
   245
            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
dalton@2678
   246
            SDL_SetError("Unknown texture format");
dalton@2678
   247
            return -1;
dalton@2678
   248
        }
dalton@2679
   249
        /* conditional statements on w/h to place it as bg/sprite */
dalton@2679
   250
        /*if(texture->w <= 64 && texture->h <= 64) {
dalton@2679
   251
            sprites not implemented yet.  elegant, I know.
dalton@2679
   252
        } else*/ if(texture->w <= 256 && texture->h <= 256) {
dalton@2679
   253
            int whichbg = -1;
dalton@2679
   254
            if(!data->bg_taken[2]) {
dalton@2679
   255
                whichbg = 2;
dalton@2679
   256
            } else if(!data->bg_taken[3]) {
dalton@2679
   257
                whichbg = 3;
dalton@2679
   258
            }
dalton@2679
   259
            if(whichbg >= 0) {
dalton@2679
   260
                data->bg->control[whichbg] = (bpp == 8) ?
dalton@2679
   261
                    BG_BMP8_256x256 : BG_BMP16_256x256;
dalton@2679
   262
                data->bg->scroll[whichbg].x = 0;
dalton@2679
   263
                data->bg->scroll[whichbg].y = 0;
dalton@2679
   264
                texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
dalton@2679
   265
                txdat = (NDS_TextureData*)texture->driverdata;
dalton@2679
   266
                txdat->type = NDSTX_BG;
dalton@2679
   267
                txdat->hw_index = whichbg;
dalton@2685
   268
                txdat->dim.hdx = 0x100; txdat->dim.hdy = 0;
dalton@2685
   269
                txdat->dim.vdx = 0;     txdat->dim.vdy = 0x100;
dalton@2679
   270
                txdat->dim.pitch = 256 * (bpp/8);
dalton@2679
   271
                txdat->dim.bpp = bpp;
dalton@2679
   272
                txdat->vram = (u16*)(data->sub ?
dalton@2679
   273
                    BG_BMP_RAM_SUB(whichbg) : BG_BMP_RAM(whichbg));
dalton@2687
   274
                txdat->size = txdat->dim.pitch * texture->h;
dalton@2687
   275
                txdat->system_ram_copy = SDL_malloc(txdat->size);
dalton@2679
   276
            } else {
dalton@2679
   277
                SDL_SetError("Out of NDS backgrounds.");
dalton@2685
   278
                printf("ran out.\n");
dalton@2679
   279
            }
dalton@2679
   280
        } else {
dalton@2679
   281
            SDL_SetError("Texture too big for NDS hardware.");
dalton@2679
   282
        }
dalton@2678
   283
    }
dalton@2678
   284
dalton@2680
   285
printf("-NDS_CreateTexture\n");
dalton@2678
   286
    if (!texture->driverdata) {
dalton@2678
   287
        return -1;
dalton@2678
   288
    }
dalton@2678
   289
    return 0;
dalton@2678
   290
}
dalton@2678
   291
dalton@2678
   292
static int
dalton@2678
   293
NDS_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   294
                      void **pixels, int *pitch)
dalton@2678
   295
{
dalton@2680
   296
printf("+NDS_QueryTexturePixels\n");
dalton@2678
   297
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   298
        SDL_SetError("Unsupported texture format");
dalton@2678
   299
        return -1;
dalton@2678
   300
    } else {
dalton@2678
   301
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   302
dalton@2678
   303
        *pixels = txdat->vram;
dalton@2678
   304
        *pitch = txdat->dim.pitch;
dalton@2680
   305
printf("-NDS_QueryTexturePixels\n");
dalton@2678
   306
        return 0;
dalton@2678
   307
    }
dalton@2678
   308
}
dalton@2678
   309
dalton@2678
   310
static int
dalton@2685
   311
NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2685
   312
                 const SDL_Rect * rect, const void *pixels, int pitch)
dalton@2685
   313
{
dalton@2685
   314
printf("+NDS_UpdateTexture\n");
dalton@2685
   315
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2685
   316
        SDL_SetError("Unsupported texture format");
dalton@2685
   317
        return -1;
dalton@2685
   318
    } else {
dalton@2685
   319
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2685
   320
        Uint8 *src, *dst;
dalton@2685
   321
        int row;
dalton@2685
   322
        size_t length;
dalton@2685
   323
        /* IMPORTANT! copy the new pixels into the sprite or bg. */
dalton@2685
   324
        src = (Uint8 *) pixels;
dalton@2685
   325
        dst =
dalton@2687
   326
            (Uint8 *) txdat->system_ram_copy + rect->y * txdat->dim.pitch +
dalton@2685
   327
            rect->x * (txdat->dim.bpp/8);
dalton@2685
   328
        length = rect->w * (txdat->dim.bpp/8);
dalton@2685
   329
        for (row = 0; row < rect->h; ++row) {
dalton@2685
   330
            SDL_memcpy(dst, src, length);
dalton@2685
   331
            src += pitch;
dalton@2685
   332
            dst += txdat->dim.pitch;
dalton@2685
   333
        }
dalton@2685
   334
printf("-NDS_UpdateTexture\n");
dalton@2685
   335
        return 0;
dalton@2685
   336
    }
dalton@2685
   337
}
dalton@2685
   338
dalton@2685
   339
static int
dalton@2685
   340
NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2685
   341
               const SDL_Rect * rect, int markDirty, void **pixels,
dalton@2685
   342
               int *pitch)
dalton@2685
   343
{
dalton@2685
   344
printf("+NDS_LockTexture\n");
dalton@2685
   345
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2685
   346
        SDL_SetError("Unsupported texture format");
dalton@2685
   347
        return -1;
dalton@2685
   348
    } else {
dalton@2685
   349
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2685
   350
dalton@2685
   351
        if (markDirty) {
dalton@2685
   352
            printf("wanted to mark dirty\n");
dalton@2685
   353
            /*SDL_AddDirtyRect(&txdat->dirty, rect);*/
dalton@2685
   354
        }
dalton@2685
   355
dalton@2687
   356
        *pixels = (void *) ((u8 *)txdat->system_ram_copy + rect->y
dalton@2687
   357
                            * txdat->dim.pitch + rect->x * (txdat->dim.bpp/8));
dalton@2685
   358
        *pitch = txdat->dim.pitch;
dalton@2685
   359
        printf("  pixels = %08x\n", (u32)*pixels);
dalton@2685
   360
printf("-NDS_LockTexture\n");
dalton@2685
   361
        return 0;
dalton@2685
   362
    }
dalton@2685
   363
}
dalton@2685
   364
dalton@2685
   365
static void
dalton@2685
   366
NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2685
   367
{
dalton@2685
   368
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2685
   369
        SDL_SetError("Unsupported texture format");
dalton@2685
   370
    }
dalton@2685
   371
printf("+NDS_UnlockTexture\n-NDS_UnlockTexture\n");
dalton@2685
   372
}
dalton@2685
   373
dalton@2685
   374
static void
dalton@2685
   375
NDS_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2685
   376
                int numrects, const SDL_Rect * rects)
dalton@2685
   377
{ /* stub */
dalton@2685
   378
printf("!NDS_DirtyTexture\n");
dalton@2685
   379
}
dalton@2685
   380
dalton@2685
   381
static int
dalton@2685
   382
NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
dalton@2685
   383
                   Uint8 a, const SDL_Rect * rect)
dalton@2685
   384
{
dalton@2685
   385
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2685
   386
    SDL_Rect real_rect = *rect;
dalton@2685
   387
    u16 color;
dalton@2685
   388
    int i, j;
dalton@2685
   389
dalton@2685
   390
printf("+NDS_RenderFill\n");
dalton@2685
   391
    /* TODO: make a single-color sprite and stretch it.
dalton@2685
   392
    color = RGB15(r>>3,g>>3,b>>3);
dalton@2685
   393
    for (i = real_rect.x; i < real_rect.x+real_rect.w; ++i) {
dalton@2685
   394
        for (j = real_rect.y; j < real_rect.y+real_rect.h; ++j) {
dalton@2685
   395
            data->fb[(j + real_rect.y) * 256 + i + real_rect.x] = 
dalton@2685
   396
                0x8000 | color;
dalton@2685
   397
        }
dalton@2685
   398
    }*/
dalton@2685
   399
printf("-NDS_RenderFill\n");
dalton@2685
   400
    return 0;
dalton@2685
   401
}
dalton@2685
   402
dalton@2685
   403
static int
dalton@2685
   404
NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2685
   405
                   const SDL_Rect * srcrect, const SDL_Rect * dstrect)
dalton@2685
   406
{
dalton@2685
   407
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2685
   408
    NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2687
   409
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
dalton@2687
   410
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2685
   411
    int i;
dalton@2685
   412
    int bpp = SDL_BYTESPERPIXEL(texture->format);
dalton@2685
   413
    int pitch = txdat->dim.pitch;
dalton@2685
   414
printf("+NDS_RenderCopy\n");
dalton@2685
   415
    if(txdat->type == NDSTX_BG) {
dalton@2685
   416
        bg_rotation *tmpbg = (txdat->hw_index == 2) ?
dalton@2685
   417
            &(data->bg->bg2_rotation) : &(data->bg->bg3_rotation);
dalton@2685
   418
        tmpbg->xdx = txdat->dim.hdx;
dalton@2685
   419
        tmpbg->xdy = txdat->dim.hdy;
dalton@2685
   420
        tmpbg->ydx = txdat->dim.vdx;
dalton@2685
   421
        tmpbg->ydy = txdat->dim.vdy;
dalton@2685
   422
        tmpbg->centerX = 0;
dalton@2685
   423
        tmpbg->centerY = 0;
dalton@2685
   424
    } else {
dalton@2685
   425
        /* sprites not implemented yet */
dalton@2685
   426
    }
dalton@2685
   427
    printf("  txdat->hw_index = %d\n", txdat->hw_index);
dalton@2685
   428
printf("-NDS_RenderCopy\n");
dalton@2685
   429
    return 0;
dalton@2685
   430
}
dalton@2685
   431
dalton@2685
   432
dalton@2685
   433
static void
dalton@2685
   434
NDS_RenderPresent(SDL_Renderer * renderer)
dalton@2685
   435
{
dalton@2685
   436
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2687
   437
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
dalton@2687
   438
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2687
   439
    int i;
dalton@2687
   440
    /* Send the data to the display TODO :
dalton@2687
   441
       shouldn't it already be there at this point?
dalton@2685
   442
       I guess set the BG's and sprites "visible" flags here. */
dalton@2685
   443
printf("+NDS_RenderPresent\n");
dalton@2685
   444
dalton@2687
   445
    for(i = 0; i < 64; ++i) {
dalton@2687
   446
        SDL_Texture * tx = display->textures[i];
dalton@2687
   447
        NDS_TextureData * txdat = (NDS_TextureData*)tx->driverdata;
dalton@2687
   448
        SDL_memcpy(txdat->vram, txdat->system_ram_copy, txdat->size);
dalton@2687
   449
    }
dalton@2687
   450
dalton@2685
   451
    /* vsync for NDS */
dalton@2685
   452
    if (renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
dalton@2685
   453
        swiWaitForVBlank();
dalton@2685
   454
    }
dalton@2685
   455
printf("-NDS_RenderPresent\n");
dalton@2685
   456
}
dalton@2685
   457
dalton@2685
   458
static void
dalton@2685
   459
NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2685
   460
{
dalton@2685
   461
printf("+NDS_DestroyTexture\n");
dalton@2685
   462
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2685
   463
        SDL_SetError("Unsupported texture format");
dalton@2685
   464
    } else {
dalton@2685
   465
        /* free anything else allocated for texture */
dalton@2685
   466
        NDS_TextureData *txdat = texture->driverdata;
dalton@2685
   467
        /*SDL_FreeDirtyRects(&txdat->dirty);*/
dalton@2687
   468
        SDL_free(txdat->system_ram_copy);
dalton@2685
   469
        SDL_free(txdat);
dalton@2685
   470
    }
dalton@2685
   471
printf("-NDS_DestroyTexture\n");
dalton@2685
   472
}
dalton@2685
   473
dalton@2685
   474
static void
dalton@2685
   475
NDS_DestroyRenderer(SDL_Renderer * renderer)
dalton@2685
   476
{
dalton@2685
   477
    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
dalton@2685
   478
    /*SDL_Window *window = SDL_GetWindowFromID(renderer->window);
dalton@2685
   479
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);*/
dalton@2685
   480
    int i;
dalton@2685
   481
dalton@2685
   482
printf("+NDS_DestroyRenderer\n");
dalton@2685
   483
    if (data) {
dalton@2685
   484
        /* TODO: free anything relevant. */
dalton@2685
   485
        /*for (i = 0; i < SDL_arraysize(data->texture); ++i) {
dalton@2685
   486
            if (data->texture[i]) {
dalton@2685
   487
                DestroyTexture(data->renderer, data->texture[i]);
dalton@2685
   488
            }
dalton@2685
   489
        }
dalton@2685
   490
        if (data->surface.format) {
dalton@2685
   491
            SDL_SetSurfacePalette(&data->surface, NULL);
dalton@2685
   492
            SDL_FreeFormat(data->surface.format);
dalton@2685
   493
        }
dalton@2685
   494
        if (display->palette) {
dalton@2685
   495
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
dalton@2685
   496
                                data);
dalton@2685
   497
        }
dalton@2685
   498
        SDL_FreeDirtyRects(&data->dirty);*/
dalton@2685
   499
        SDL_free(data);
dalton@2685
   500
    }
dalton@2685
   501
    SDL_free(renderer);
dalton@2685
   502
printf("-NDS_DestroyRenderer\n");
dalton@2685
   503
}
dalton@2685
   504
dalton@2685
   505
static int
dalton@2678
   506
NDS_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   507
                     const SDL_Color * colors, int firstcolor, int ncolors)
dalton@2678
   508
{
dalton@2680
   509
printf("+NDS_SetTexturePalette\n");
dalton@2678
   510
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   511
        SDL_SetError("YUV textures don't have a palette");
dalton@2678
   512
        return -1;
dalton@2678
   513
    } else {
dalton@2678
   514
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2678
   515
        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
dalton@2680
   516
printf("-NDS_SetTexturePalette\n");
dalton@2678
   517
        return 0;
dalton@2678
   518
    }
dalton@2678
   519
}
dalton@2678
   520
dalton@2678
   521
static int
dalton@2678
   522
NDS_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2678
   523
                     SDL_Color * colors, int firstcolor, int ncolors)
dalton@2678
   524
{
dalton@2680
   525
printf("+NDS_GetTexturePalette\n");
dalton@2678
   526
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2678
   527
        SDL_SetError("YUV textures don't have a palette");
dalton@2678
   528
        return -1;
dalton@2678
   529
    } else {
dalton@2678
   530
        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
dalton@2680
   531
printf("-NDS_GetTexturePalette\n");
dalton@2678
   532
        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
dalton@2678
   533
        return 0;
dalton@2678
   534
    }
dalton@2678
   535
}
dalton@2678
   536
dalton@2678
   537
static int
dalton@2678
   538
NDS_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   539
{
dalton@2680
   540
printf("!NDS_SetTextureColorMod\n");
dalton@2678
   541
    /* stub. TODO: figure out what needs to be done, if anything */
dalton@2678
   542
    return 0;
dalton@2678
   543
}
dalton@2678
   544
dalton@2678
   545
static int
dalton@2678
   546
NDS_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   547
{
dalton@2680
   548
printf("!NDS_SetTextureAlphaMod\n");
dalton@2678
   549
    /* stub. TODO: figure out what needs to be done, if anything */
dalton@2678
   550
    return 0;
dalton@2678
   551
}
dalton@2678
   552
dalton@2678
   553
static int
dalton@2678
   554
NDS_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   555
{
dalton@2680
   556
printf("!NDS_SetTextureBlendMode\n");
dalton@2678
   557
    /* stub. TODO: figure out what needs to be done, if anything */
dalton@2678
   558
    return 0;
dalton@2678
   559
}
dalton@2678
   560
dalton@2678
   561
static int
dalton@2678
   562
NDS_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
dalton@2678
   563
{
dalton@2680
   564
printf("!NDS_SetTextureScaleMode\n");
dalton@2678
   565
    /* stub. TODO: figure out what needs to be done.
dalton@2678
   566
       (NDS hardware scaling is nearest neighbor.) */
dalton@2678
   567
    return 0;
dalton@2678
   568
}
dalton@2678
   569
dalton@2670
   570
/* vi: set ts=4 sw=4 expandtab: */