src/video/nds/SDL_ndsrender.c
author Darren Alton <dalton@stevens.edu>
Thu, 12 Jun 2008 02:38:49 +0000
branchgsoc2008_nds
changeset 2671 c3e7c0698cbb
parent 2670 6e4669f4db49
child 2672 74ac057679d5
permissions -rw-r--r--
some changes to the dummy driver for debug purposes that should be reverted.
most importantly, commenting out a check for an env. var.
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@2670
    64
SDL_Renderer *
dalton@2670
    65
SDL_NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
dalton@2670
    66
{
dalton@2670
    67
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2670
    68
    SDL_DisplayMode *displayMode = &display->current_mode;
dalton@2670
    69
    SDL_Renderer *renderer;
dalton@2670
    70
    SDL_NDS_RenderData *data;
dalton@2670
    71
    int i, n;
dalton@2670
    72
    int bpp = 16;
dalton@2670
    73
    Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
dalton@2670
    74
dalton@2670
    75
    printf("SDL_NDS_CreateRenderer(window, 0x%x)\n", flags);
dalton@2671
    76
    printf(" window: (%d,%d), %dx%d\n", window->x, window->y, window->w,
dalton@2671
    77
           window->h);
dalton@2670
    78
dalton@2670
    79
    /* hard coded this to ARGB1555 for now
dalton@2671
    80
       if (!SDL_PixelFormatEnumToMasks
dalton@2671
    81
       (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
dalton@2671
    82
       SDL_SetError("Unknown display format");
dalton@2671
    83
       return NULL;
dalton@2671
    84
       } */
dalton@2670
    85
dalton@2670
    86
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dalton@2670
    87
    if (!renderer) {
dalton@2670
    88
        SDL_OutOfMemory();
dalton@2670
    89
        return NULL;
dalton@2670
    90
    }
dalton@2670
    91
dalton@2670
    92
    data = (SDL_NDS_RenderData *) SDL_malloc(sizeof(*data));
dalton@2670
    93
    if (!data) {
dalton@2670
    94
        SDL_NDS_DestroyRenderer(renderer);
dalton@2670
    95
        SDL_OutOfMemory();
dalton@2670
    96
        return NULL;
dalton@2670
    97
    }
dalton@2670
    98
    SDL_zerop(data);
dalton@2670
    99
dalton@2670
   100
    renderer->RenderFill = SDL_NDS_RenderFill;
dalton@2670
   101
    renderer->RenderCopy = SDL_NDS_RenderCopy;
dalton@2670
   102
    renderer->RenderPresent = SDL_NDS_RenderPresent;
dalton@2670
   103
    renderer->DestroyRenderer = SDL_NDS_DestroyRenderer;
dalton@2670
   104
    renderer->info.name = SDL_NDS_RenderDriver.info.name;
dalton@2670
   105
    renderer->info.flags = 0;
dalton@2670
   106
    renderer->window = window->id;
dalton@2670
   107
    renderer->driverdata = data;
dalton@2670
   108
    Setup_SoftwareRenderer(renderer);
dalton@2670
   109
dalton@2670
   110
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
dalton@2670
   111
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
dalton@2670
   112
        n = 2;
dalton@2670
   113
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
dalton@2670
   114
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
dalton@2670
   115
        n = 3;
dalton@2670
   116
    } else {
dalton@2670
   117
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
dalton@2670
   118
        n = 1;
dalton@2670
   119
    }
dalton@2670
   120
    for (i = 0; i < n; ++i) {
dalton@2670
   121
        data->screens[i] =
dalton@2671
   122
            SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask,
dalton@2671
   123
                                 Amask);
dalton@2670
   124
        if (!data->screens[i]) {
dalton@2670
   125
            SDL_NDS_DestroyRenderer(renderer);
dalton@2670
   126
            return NULL;
dalton@2670
   127
        }
dalton@2670
   128
        SDL_SetSurfacePalette(data->screens[i], display->palette);
dalton@2670
   129
    }
dalton@2670
   130
dalton@2670
   131
    data->current_screen = 0;
dalton@2670
   132
    data->ultimate_answer = 42;
dalton@2670
   133
#if 0
dalton@2670
   134
#define blarg (data->screens[0])
dalton@2670
   135
    printf("hello?\n");
dalton@2671
   136
    if (!data || !(data->screens) || !blarg) {
dalton@2670
   137
        printf("they're null.\n");
dalton@2670
   138
    } else {
dalton@2670
   139
        printf("not null.\n");
dalton@2671
   140
        printf("%d\n%d\n%d\n%d\n%x\n%x\n%x\n%x\n", blarg->w, blarg->h, blarg->pitch, blarg->format->BitsPerPixel, blarg->format->Rmask, blarg->format->Gmask, blarg->format->Bmask, (u32) (blarg->pixels));     /* ARGH WHY DOESN'T THIS PRINT AT ALL? */
dalton@2670
   141
        printf("hurr\n");
dalton@2670
   142
    }
dalton@2670
   143
#undef blarg
dalton@2670
   144
#endif
dalton@2670
   145
    return renderer;
dalton@2670
   146
}
dalton@2670
   147
dalton@2670
   148
static int
dalton@2670
   149
SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
dalton@2671
   150
                   Uint8 a, const SDL_Rect * rect)
dalton@2670
   151
{
dalton@2670
   152
    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
dalton@2670
   153
    SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   154
    Uint32 color;
dalton@2670
   155
    SDL_Rect real_rect = *rect;
dalton@2670
   156
dalton@2670
   157
    color = SDL_MapRGBA(target->format, r, g, b, a);
dalton@2670
   158
dalton@2670
   159
    return SDL_FillRect(target, &real_rect, color);
dalton@2670
   160
}
dalton@2670
   161
dalton@2670
   162
/* this is mainly for testing stuff to put a surface where I can see it */
dalton@2671
   163
void
dalton@2671
   164
sdlds_surf2vram(SDL_Surface * s)
dalton@2671
   165
{
dalton@2670
   166
    int i;
dalton@2671
   167
    for (i = 0; i < 256 * 192; ++i) {
dalton@2671
   168
        ((u16 *) VRAM_A)[i] = ((u16 *) (s->pixels))[i];
dalton@2670
   169
    }
dalton@2670
   170
}
dalton@2670
   171
dalton@2670
   172
static int
dalton@2670
   173
SDL_NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dalton@2671
   174
                   const SDL_Rect * srcrect, const SDL_Rect * dstrect)
dalton@2670
   175
{
dalton@2671
   176
    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
dalton@2670
   177
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
dalton@2670
   178
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
dalton@2670
   179
    printf("SDL_NDS_RenderCopy(renderer, texture, srcrect, dstrect)\n");
dalton@2670
   180
    printf(" renderer: %s\n", renderer->info.name);
dalton@2670
   181
    printf(" texture: %dx%d\n", texture->w, texture->h);
dalton@2671
   182
    printf(" srcrect: (%d,%d), %dx%d\n", srcrect->x, srcrect->y, srcrect->w,
dalton@2671
   183
           srcrect->h);
dalton@2671
   184
    printf(" dstrect: (%d,%d), %dx%d\n", dstrect->x, dstrect->y, dstrect->w,
dalton@2671
   185
           dstrect->h);
dalton@2670
   186
dalton@2670
   187
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
dalton@2670
   188
        SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   189
        void *pixels =
dalton@2670
   190
            (Uint8 *) target->pixels + dstrect->y * target->pitch +
dalton@2670
   191
            dstrect->x * target->format->BytesPerPixel;
dalton@2670
   192
        return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
dalton@2670
   193
                                   srcrect, display->current_mode.format,
dalton@2670
   194
                                   dstrect->w, dstrect->h, pixels,
dalton@2670
   195
                                   target->pitch);
dalton@2670
   196
    } else {
dalton@2670
   197
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
dalton@2670
   198
        SDL_Surface *target = data->screens[data->current_screen];
dalton@2670
   199
        SDL_Rect real_srcrect = *srcrect;
dalton@2670
   200
        SDL_Rect real_dstrect = *dstrect;
dalton@2670
   201
        printf("Rmask %x Gmask %x Bmask %x Amask %x\n"
dalton@2670
   202
               "width %d, height %d, pitch %d\nbpp %d, pixels %x\n",
dalton@2671
   203
               surface->format->Rmask, surface->format->Gmask,
dalton@2671
   204
               surface->format->Bmask, surface->format->Amask,
dalton@2671
   205
               surface->w, surface->h, surface->pitch,
dalton@2671
   206
               surface->format->BitsPerPixel, (u32) (surface->pixels));
dalton@2670
   207
        sdlds_surf2vram(surface);
dalton@2670
   208
        return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
dalton@2670
   209
    }
dalton@2670
   210
#if 0
dalton@2670
   211
/* previous attempt to copy it directly to vram */
dalton@2671
   212
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
dalton@2671
   213
    int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h;
dalton@2671
   214
    int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h;
dalton@2671
   215
    int si, sj, di, dj;
dalton@2671
   216
    /*printf("DEBUG: still alive!\n"); */
dalton@2671
   217
    for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) {
dalton@2671
   218
        for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) {
dalton@2671
   219
            ((uint16 *) VRAM_A)[(dj + dy) * 256 + di + dx]
dalton@2671
   220
                = ((Uint16 *) surface->pixels)[(sj + sy) * (surface->w) + si +
dalton@2671
   221
                                               sx];
dalton@2670
   222
        }
dalton@2670
   223
    }
dalton@2671
   224
    /*printf("DEBUG: still alive!\n"); */
dalton@2671
   225
}
dalton@2671
   226
dalton@2671
   227
return 0;
dalton@2670
   228
#endif
dalton@2670
   229
}
dalton@2670
   230
dalton@2670
   231
static void
dalton@2670
   232
SDL_NDS_RenderPresent(SDL_Renderer * renderer)
dalton@2670
   233
{
dalton@2671
   234
    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
dalton@2670
   235
dalton@2670
   236
    printf("SDL_NDS_RenderPresent(renderer)\n");
dalton@2670
   237
    printf(" renderer: %s\n", renderer->info.name);
dalton@2670
   238
    /* Send the data to the display */
dalton@2670
   239
dalton@2670
   240
#if 0
dalton@2670
   241
/*testing to see if rectangles drawn get copied right*/
dalton@2670
   242
    {
dalton@2671
   243
        SDL_Rect ra;
dalton@2671
   244
        ra.x = 0;
dalton@2671
   245
        ra.y = 0;
dalton@2671
   246
        ra.w = 256;
dalton@2671
   247
        ra.h = 192;
dalton@2671
   248
        
dalton@2671
   249
SDL_FillRect(data->screens[data->current_screen], &ra, 0x250);
dalton@2671
   250
        ra.x = 32;
dalton@2671
   251
        ra.y = 32;
dalton@2671
   252
        ra.w = 192;
dalton@2671
   253
        ra.h = 128;
dalton@2671
   254
        
dalton@2671
   255
SDL_FillRect(data->screens[data->current_screen], &ra,
dalton@2671
   256
                      SDL_MapRGBA(data->screens[data->current_screen]->format,
dalton@2670
   257
                                  255, 255, 255, 255));
dalton@2670
   258
    }
dalton@2670
   259
/*okay so this works but why not when I do it in the main()?
dalton@2671
   260
  for some reason the screen I get from screen=SDL_SetVideoMode(...)
dalton@2671
   261
  doesn't get copied to renderer->driverdata? */
dalton@2670
   262
    for (i = 0; i < 30; ++i)
dalton@2670
   263
        swiWaitForVBlank();     /* delay for debug purpose */
dalton@2670
   264
#endif
dalton@2670
   265
    sdlds_surf2vram(data->screens[data->current_screen]);
dalton@2670
   266
dalton@2670
   267
    /* Update the flipping chain, if any */
dalton@2670
   268
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
dalton@2670
   269
        data->current_screen = (data->current_screen + 1) % 2;
dalton@2670
   270
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
dalton@2670
   271
        data->current_screen = (data->current_screen + 1) % 3;
dalton@2670
   272
    }
dalton@2670
   273
}
dalton@2670
   274
dalton@2670
   275
static void
dalton@2670
   276
SDL_NDS_DestroyRenderer(SDL_Renderer * renderer)
dalton@2670
   277
{
dalton@2670
   278
    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
dalton@2670
   279
    int i;
dalton@2670
   280
dalton@2670
   281
    printf("SDL_NDS_DestroyRenderer(renderer)\n");
dalton@2670
   282
    printf(" renderer: %s\n", renderer->info.name);
dalton@2670
   283
    if (data) {
dalton@2670
   284
        for (i = 0; i < SDL_arraysize(data->screens); ++i) {
dalton@2670
   285
            if (data->screens[i]) {
dalton@2670
   286
                SDL_FreeSurface(data->screens[i]);
dalton@2670
   287
            }
dalton@2670
   288
        }
dalton@2670
   289
        SDL_free(data);
dalton@2670
   290
    }
dalton@2670
   291
    SDL_free(renderer);
dalton@2670
   292
}
dalton@2671
   293
dalton@2671
   294
/* vi: set ts=4 sw=4 expandtab: */