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