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