src/video/nds/SDL_ndsrender.c
author Darren Alton <dalton@stevens.edu>
Thu, 19 Jun 2008 07:11:35 +0000
branchgsoc2008_nds
changeset 2673 24a6b3588eac
parent 2672 74ac057679d5
child 2677 6386764eb222
permissions -rw-r--r--
Cleaned up and added joypad support from 1.2.9 NDS port. Also some minor video driver bugfixes.
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@2670
    37
static SDL_Renderer *SDL_NDS_CreateRenderer(SDL_Window * window,
dalton@2671
    38
                                            Uint32 flags);
dalton@2670
    39
static int SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g,
dalton@2671
    40
                              Uint8 b, Uint8 a, const SDL_Rect * rect);
dalton@2670
    41
static int SDL_NDS_RenderCopy(SDL_Renderer * renderer,
dalton@2671
    42
                              SDL_Texture * texture,
dalton@2671
    43
                              const SDL_Rect * srcrect,
dalton@2671
    44
                              const SDL_Rect * dstrect);
dalton@2670
    45
static void SDL_NDS_RenderPresent(SDL_Renderer * renderer);
dalton@2670
    46
static void SDL_NDS_DestroyRenderer(SDL_Renderer * renderer);
dalton@2670
    47
dalton@2670
    48
dalton@2670
    49
SDL_RenderDriver SDL_NDS_RenderDriver = {
dalton@2670
    50
    SDL_NDS_CreateRenderer,
dalton@2671
    51
    {"nds", SDL_RENDERER_PRESENTCOPY}
dalton@2670
    52
/*   (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
dalton@2670
    53
      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
dalton@2670
    54
      SDL_RENDERER_PRESENTDISCARD),*/
dalton@2670
    55
};
dalton@2670
    56
dalton@2670
    57
typedef struct
dalton@2670
    58
{
dalton@2670
    59
    int current_screen;
dalton@2670
    60
    SDL_Surface *screens[3];
dalton@2670
    61
    int ultimate_answer;
dalton@2670
    62
} SDL_NDS_RenderData;
dalton@2670
    63
dalton@2672
    64
dalton@2672
    65
dalton@2672
    66
/* this is mainly hackish testing/debugging stuff to get cleaned up soon
dalton@2672
    67
   anything named sdlds_blah shouldn't make it into the stable version
dalton@2672
    68
 */
dalton@2672
    69
dalton@2672
    70
u16
dalton@2673
    71
sdlds_rgb2bgr(u16 c)
dalton@2673
    72
{
dalton@2673
    73
/* hack to get the proper colors until I actually get BGR555 to work right */
dalton@2672
    74
    u16 Rmask = 0x7C00, Bmask = 0x001F, GAmask = 0x83E0, r, b;
dalton@2672
    75
    r = (c & Rmask) >> 10;
dalton@2672
    76
    b = (c & Bmask) << 10;
dalton@2672
    77
    return (c & GAmask) | r | b;
dalton@2672
    78
}
dalton@2672
    79
dalton@2672
    80
void
dalton@2672
    81
sdlds_surf2vram(SDL_Surface * s)
dalton@2672
    82
{
dalton@2673
    83
    if (s->w == 256) {
dalton@2673
    84
        dmaCopy((u8 *) (s->pixels) + 156 * sizeof(u16),
dalton@2673
    85
                VRAM_A, 256 * 192 * sizeof(u16));
dalton@2672
    86
    }
dalton@2672
    87
}
dalton@2672
    88
dalton@2672
    89
void
dalton@2673
    90
sdlds_print_pixfmt_info(SDL_PixelFormat * f)
dalton@2673
    91
{
dalton@2673
    92
    if (!f)
dalton@2673
    93
        return;
dalton@2673
    94
    printf("bpp: %d\nRGBA: %x %x %x %x\n",
dalton@2673
    95
           f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask);
dalton@2672
    96
}
dalton@2672
    97
dalton@2672
    98
void
dalton@2673
    99
sdlds_print_surface_info(SDL_Surface * s)
dalton@2673
   100
{
dalton@2673
   101
    if (!s)
dalton@2673
   102
        return;
dalton@2673
   103
    printf("flags: %x\nsize: %dx%d, pitch: %d\nlocked: %d, refcount: %d\n",
dalton@2673
   104
           s->flags, s->w, s->h, s->pitch, s->locked, s->refcount);
dalton@2673
   105
    sdlds_print_pixfmt_info(s->format);
dalton@2672
   106
}
dalton@2672
   107
dalton@2672
   108
dalton@2672
   109
dalton@2670
   110
SDL_Renderer *
dalton@2670
   111
SDL_NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
dalton@2670
   112
{
dalton@2670
   113
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2670
   114
    SDL_DisplayMode *displayMode = &display->current_mode;
dalton@2670
   115
    SDL_Renderer *renderer;
dalton@2670
   116
    SDL_NDS_RenderData *data;
dalton@2670
   117
    int i, n;
dalton@2672
   118
    int bpp = 15;
dalton@2673
   119
    Uint32 Rmask, Gmask, Bmask, Amask;
dalton@2673
   120
/*  Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
dalton@2672
   121
    Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000;
dalton@2673
   122
*/
dalton@2670
   123
dalton@2672
   124
#if 0
dalton@2670
   125
    printf("SDL_NDS_CreateRenderer(window, 0x%x)\n", flags);
dalton@2672
   126
    printf(" window: (%d,%d), %dx%d\n",
dalton@2672
   127
           window->x, window->y, window->w, window->h);
dalton@2672
   128
#endif
dalton@2670
   129
dalton@2673
   130
    /* hard coded this to BGR555 for now */
dalton@2673
   131
    if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp,
dalton@2673
   132
                                    &Rmask, &Gmask, &Bmask, &Amask)) {
dalton@2673
   133
        SDL_SetError("Unknown display format");
dalton@2673
   134
        return NULL;
dalton@2673
   135
    }
dalton@2670
   136
dalton@2670
   137
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dalton@2670
   138
    if (!renderer) {
dalton@2670
   139
        SDL_OutOfMemory();
dalton@2670
   140
        return NULL;
dalton@2670
   141
    }
dalton@2670
   142
dalton@2670
   143
    data = (SDL_NDS_RenderData *) SDL_malloc(sizeof(*data));
dalton@2670
   144
    if (!data) {
dalton@2670
   145
        SDL_NDS_DestroyRenderer(renderer);
dalton@2670
   146
        SDL_OutOfMemory();
dalton@2670
   147
        return NULL;
dalton@2670
   148
    }
dalton@2670
   149
    SDL_zerop(data);
dalton@2670
   150
dalton@2670
   151
    renderer->RenderFill = SDL_NDS_RenderFill;
dalton@2670
   152
    renderer->RenderCopy = SDL_NDS_RenderCopy;
dalton@2670
   153
    renderer->RenderPresent = SDL_NDS_RenderPresent;
dalton@2670
   154
    renderer->DestroyRenderer = SDL_NDS_DestroyRenderer;
dalton@2670
   155
    renderer->info.name = SDL_NDS_RenderDriver.info.name;
dalton@2670
   156
    renderer->info.flags = 0;
dalton@2670
   157
    renderer->window = window->id;
dalton@2670
   158
    renderer->driverdata = data;
dalton@2670
   159
    Setup_SoftwareRenderer(renderer);
dalton@2670
   160
dalton@2670
   161
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
dalton@2670
   162
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
dalton@2670
   163
        n = 2;
dalton@2670
   164
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
dalton@2670
   165
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
dalton@2670
   166
        n = 3;
dalton@2670
   167
    } else {
dalton@2670
   168
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
dalton@2670
   169
        n = 1;
dalton@2670
   170
    }
dalton@2670
   171
    for (i = 0; i < n; ++i) {
dalton@2670
   172
        data->screens[i] =
dalton@2673
   173
            SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask,
dalton@2673
   174
                                 Amask);
dalton@2670
   175
        if (!data->screens[i]) {
dalton@2670
   176
            SDL_NDS_DestroyRenderer(renderer);
dalton@2670
   177
            return NULL;
dalton@2670
   178
        }
dalton@2670
   179
        SDL_SetSurfacePalette(data->screens[i], display->palette);
dalton@2673
   180
        sdlds_print_surface_info(data->screens[i]);
dalton@2670
   181
    }
dalton@2670
   182
dalton@2670
   183
    data->current_screen = 0;
dalton@2670
   184
    data->ultimate_answer = 42;
dalton@2672
   185
dalton@2670
   186
    return renderer;
dalton@2670
   187
}
dalton@2670
   188
dalton@2670
   189
static int
dalton@2670
   190
SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
dalton@2671
   191
                   Uint8 a, const SDL_Rect * rect)
dalton@2670
   192
{
dalton@2670
   193
    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
dalton@2670
   194
    SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   195
    Uint32 color;
dalton@2670
   196
    SDL_Rect real_rect = *rect;
dalton@2670
   197
dalton@2670
   198
    color = SDL_MapRGBA(target->format, r, g, b, a);
dalton@2670
   199
dalton@2670
   200
    return SDL_FillRect(target, &real_rect, color);
dalton@2670
   201
}
dalton@2670
   202
dalton@2670
   203
static int
dalton@2670
   204
SDL_NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2671
   205
                   const SDL_Rect * srcrect, const SDL_Rect * dstrect)
dalton@2670
   206
{
dalton@2671
   207
    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
dalton@2670
   208
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
dalton@2670
   209
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2672
   210
#if 0
dalton@2670
   211
    printf("SDL_NDS_RenderCopy(renderer, texture, srcrect, dstrect)\n");
dalton@2670
   212
    printf(" renderer: %s\n", renderer->info.name);
dalton@2670
   213
    printf(" texture: %dx%d\n", texture->w, texture->h);
dalton@2671
   214
    printf(" srcrect: (%d,%d), %dx%d\n", srcrect->x, srcrect->y, srcrect->w,
dalton@2671
   215
           srcrect->h);
dalton@2671
   216
    printf(" dstrect: (%d,%d), %dx%d\n", dstrect->x, dstrect->y, dstrect->w,
dalton@2671
   217
           dstrect->h);
dalton@2672
   218
#endif
dalton@2670
   219
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2670
   220
        SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   221
        void *pixels =
dalton@2670
   222
            (Uint8 *) target->pixels + dstrect->y * target->pitch +
dalton@2670
   223
            dstrect->x * target->format->BytesPerPixel;
dalton@2670
   224
        return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
dalton@2670
   225
                                   srcrect, display->current_mode.format,
dalton@2670
   226
                                   dstrect->w, dstrect->h, pixels,
dalton@2670
   227
                                   target->pitch);
dalton@2670
   228
    } else {
dalton@2670
   229
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
dalton@2670
   230
        SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   231
        SDL_Rect real_srcrect = *srcrect;
dalton@2670
   232
        SDL_Rect real_dstrect = *dstrect;
dalton@2672
   233
        /*sdlds_print_surface_info(surface);
dalton@2673
   234
           sdlds_print_surface_info(target); */
dalton@2670
   235
        sdlds_surf2vram(surface);
dalton@2670
   236
        return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
dalton@2670
   237
    }
dalton@2670
   238
#if 0
dalton@2672
   239
    /* copy it directly to vram */
dalton@2671
   240
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
dalton@2671
   241
    int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h;
dalton@2671
   242
    int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h;
dalton@2671
   243
    int si, sj, di, dj;
dalton@2671
   244
    for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) {
dalton@2671
   245
        for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) {
dalton@2672
   246
            VRAM_A[(dj + dy) * 256 + di + dx] =
dalton@2673
   247
                ((Uint16 *) surface->pixels)[(sj + sy) * (surface->w) + si +
dalton@2673
   248
                                             sx];
dalton@2670
   249
        }
dalton@2670
   250
    }
dalton@2672
   251
    return 0;
dalton@2672
   252
#endif
dalton@2671
   253
}
dalton@2671
   254
dalton@2670
   255
dalton@2670
   256
static void
dalton@2670
   257
SDL_NDS_RenderPresent(SDL_Renderer * renderer)
dalton@2670
   258
{
dalton@2671
   259
    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
dalton@2673
   260
    int i;
dalton@2672
   261
#if 0
dalton@2670
   262
    printf("SDL_NDS_RenderPresent(renderer)\n");
dalton@2670
   263
    printf(" renderer: %s\n", renderer->info.name);
dalton@2672
   264
#endif
dalton@2670
   265
    /* Send the data to the display */
dalton@2670
   266
dalton@2673
   267
    /* hack to fix the pixel format until I figure out why BGR doesn't work */
dalton@2673
   268
    for (i = 0; i < 256 * 192; ++i) {
dalton@2673
   269
        VRAM_A[i] = sdlds_rgb2bgr(VRAM_A[i]);
dalton@2673
   270
    }
dalton@2670
   271
    /* Update the flipping chain, if any */
dalton@2670
   272
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
dalton@2670
   273
        data->current_screen = (data->current_screen + 1) % 2;
dalton@2670
   274
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
dalton@2670
   275
        data->current_screen = (data->current_screen + 1) % 3;
dalton@2670
   276
    }
dalton@2670
   277
}
dalton@2670
   278
dalton@2670
   279
static void
dalton@2670
   280
SDL_NDS_DestroyRenderer(SDL_Renderer * renderer)
dalton@2670
   281
{
dalton@2670
   282
    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
dalton@2670
   283
    int i;
dalton@2670
   284
dalton@2670
   285
    printf("SDL_NDS_DestroyRenderer(renderer)\n");
dalton@2670
   286
    printf(" renderer: %s\n", renderer->info.name);
dalton@2670
   287
    if (data) {
dalton@2670
   288
        for (i = 0; i < SDL_arraysize(data->screens); ++i) {
dalton@2670
   289
            if (data->screens[i]) {
dalton@2670
   290
                SDL_FreeSurface(data->screens[i]);
dalton@2670
   291
            }
dalton@2670
   292
        }
dalton@2670
   293
        SDL_free(data);
dalton@2670
   294
    }
dalton@2670
   295
    SDL_free(renderer);
dalton@2670
   296
}
dalton@2670
   297
dalton@2670
   298
/* vi: set ts=4 sw=4 expandtab: */