src/video/nds/SDL_ndsrender.c
branchgsoc2008_nds
changeset 2670 6e4669f4db49
child 2671 c3e7c0698cbb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/nds/SDL_ndsrender.c	Tue Jun 10 06:57:57 2008 +0000
     1.3 @@ -0,0 +1,285 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +#include <stdio.h>
    1.27 +#include <stdlib.h>
    1.28 +#include <nds.h>
    1.29 +
    1.30 +#include "SDL_config.h"
    1.31 +
    1.32 +#include "SDL_video.h"
    1.33 +#include "../SDL_sysvideo.h"
    1.34 +#include "../SDL_yuv_sw_c.h"
    1.35 +#include "../SDL_renderer_sw.h"
    1.36 +
    1.37 +
    1.38 +/* SDL surface based renderer implementation */
    1.39 +
    1.40 +static SDL_Renderer *SDL_NDS_CreateRenderer(SDL_Window * window,
    1.41 +                                              Uint32 flags);
    1.42 +static int SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g,
    1.43 +                                Uint8 b, Uint8 a, const SDL_Rect * rect);
    1.44 +static int SDL_NDS_RenderCopy(SDL_Renderer * renderer,
    1.45 +                                SDL_Texture * texture,
    1.46 +                                const SDL_Rect * srcrect,
    1.47 +                                const SDL_Rect * dstrect);
    1.48 +static void SDL_NDS_RenderPresent(SDL_Renderer * renderer);
    1.49 +static void SDL_NDS_DestroyRenderer(SDL_Renderer * renderer);
    1.50 +
    1.51 +
    1.52 +SDL_RenderDriver SDL_NDS_RenderDriver = {
    1.53 +    SDL_NDS_CreateRenderer,
    1.54 +    { "nds",  SDL_RENDERER_PRESENTCOPY }
    1.55 +/*   (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
    1.56 +      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
    1.57 +      SDL_RENDERER_PRESENTDISCARD),*/
    1.58 +};
    1.59 +
    1.60 +typedef struct
    1.61 +{
    1.62 +    int current_screen;
    1.63 +    SDL_Surface *screens[3];
    1.64 +    int ultimate_answer;
    1.65 +} SDL_NDS_RenderData;
    1.66 +
    1.67 +SDL_Renderer *
    1.68 +SDL_NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
    1.69 +{
    1.70 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
    1.71 +    SDL_DisplayMode *displayMode = &display->current_mode;
    1.72 +    SDL_Renderer *renderer;
    1.73 +    SDL_NDS_RenderData *data;
    1.74 +    int i, n;
    1.75 +    int bpp = 16;
    1.76 +    Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
    1.77 +
    1.78 +    printf("SDL_NDS_CreateRenderer(window, 0x%x)\n", flags);
    1.79 +    printf(" window: (%d,%d), %dx%d\n", window->x, window->y, window->w, window->h);
    1.80 +
    1.81 +    /* hard coded this to ARGB1555 for now
    1.82 +    if (!SDL_PixelFormatEnumToMasks
    1.83 +        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
    1.84 +        SDL_SetError("Unknown display format");
    1.85 +        return NULL;
    1.86 +    }*/
    1.87 +
    1.88 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
    1.89 +    if (!renderer) {
    1.90 +        SDL_OutOfMemory();
    1.91 +        return NULL;
    1.92 +    }
    1.93 +
    1.94 +    data = (SDL_NDS_RenderData *) SDL_malloc(sizeof(*data));
    1.95 +    if (!data) {
    1.96 +        SDL_NDS_DestroyRenderer(renderer);
    1.97 +        SDL_OutOfMemory();
    1.98 +        return NULL;
    1.99 +    }
   1.100 +    SDL_zerop(data);
   1.101 +
   1.102 +    renderer->RenderFill = SDL_NDS_RenderFill;
   1.103 +    renderer->RenderCopy = SDL_NDS_RenderCopy;
   1.104 +    renderer->RenderPresent = SDL_NDS_RenderPresent;
   1.105 +    renderer->DestroyRenderer = SDL_NDS_DestroyRenderer;
   1.106 +    renderer->info.name = SDL_NDS_RenderDriver.info.name;
   1.107 +    renderer->info.flags = 0;
   1.108 +    renderer->window = window->id;
   1.109 +    renderer->driverdata = data;
   1.110 +    Setup_SoftwareRenderer(renderer);
   1.111 +
   1.112 +    if (flags & SDL_RENDERER_PRESENTFLIP2) {
   1.113 +        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
   1.114 +        n = 2;
   1.115 +    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
   1.116 +        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
   1.117 +        n = 3;
   1.118 +    } else {
   1.119 +        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
   1.120 +        n = 1;
   1.121 +    }
   1.122 +    for (i = 0; i < n; ++i) {
   1.123 +        data->screens[i] =
   1.124 +            SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask, Amask);
   1.125 +        if (!data->screens[i]) {
   1.126 +            SDL_NDS_DestroyRenderer(renderer);
   1.127 +            return NULL;
   1.128 +        }
   1.129 +        SDL_SetSurfacePalette(data->screens[i], display->palette);
   1.130 +    }
   1.131 +
   1.132 +    data->current_screen = 0;
   1.133 +    data->ultimate_answer = 42;
   1.134 +#if 0
   1.135 +#define blarg (data->screens[0])
   1.136 +    printf("hello?\n");
   1.137 +    if(!data || !(data->screens) || !blarg) {
   1.138 +        printf("they're null.\n");
   1.139 +    } else {
   1.140 +        printf("not null.\n");
   1.141 +        printf("%d\n%d\n%d\n%d\n%x\n%x\n%x\n%x\n",
   1.142 +        blarg->w, blarg->h, blarg->pitch,
   1.143 +        blarg->format->BitsPerPixel,
   1.144 +        blarg->format->Rmask,
   1.145 +        blarg->format->Gmask,
   1.146 +        blarg->format->Bmask,
   1.147 +        (u32)(blarg->pixels)); /* ARGH WHY DOESN'T THIS PRINT AT ALL? */
   1.148 +        printf("hurr\n");
   1.149 +    }
   1.150 +#undef blarg
   1.151 +#endif
   1.152 +    return renderer;
   1.153 +}
   1.154 +
   1.155 +static int
   1.156 +SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
   1.157 +                     Uint8 a, const SDL_Rect * rect)
   1.158 +{
   1.159 +    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
   1.160 +    SDL_Surface *target = data->screens[data->current_screen];
   1.161 +    Uint32 color;
   1.162 +    SDL_Rect real_rect = *rect;
   1.163 +
   1.164 +    color = SDL_MapRGBA(target->format, r, g, b, a);
   1.165 +
   1.166 +    return SDL_FillRect(target, &real_rect, color);
   1.167 +}
   1.168 +
   1.169 +/* this is mainly for testing stuff to put a surface where I can see it */
   1.170 +void sdlds_surf2vram(SDL_Surface *s) {
   1.171 +    int i;
   1.172 +    for(i = 0; i < 256*192; ++i) {
   1.173 +        ((u16*)VRAM_A)[i] = ((u16*)(s->pixels))[i];
   1.174 +    }
   1.175 +}
   1.176 +
   1.177 +static int
   1.178 +SDL_NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.179 +                     const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   1.180 +{
   1.181 +    SDL_NDS_RenderData *data =
   1.182 +        (SDL_NDS_RenderData *) renderer->driverdata;
   1.183 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.184 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.185 +    printf("SDL_NDS_RenderCopy(renderer, texture, srcrect, dstrect)\n");
   1.186 +    printf(" renderer: %s\n", renderer->info.name);
   1.187 +    printf(" texture: %dx%d\n", texture->w, texture->h);
   1.188 +    printf(" srcrect: (%d,%d), %dx%d\n", srcrect->x, srcrect->y, srcrect->w, srcrect->h);
   1.189 +    printf(" dstrect: (%d,%d), %dx%d\n", dstrect->x, dstrect->y, dstrect->w, dstrect->h);
   1.190 +
   1.191 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.192 +        SDL_Surface *target = data->screens[data->current_screen];
   1.193 +        void *pixels =
   1.194 +            (Uint8 *) target->pixels + dstrect->y * target->pitch +
   1.195 +            dstrect->x * target->format->BytesPerPixel;
   1.196 +        return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
   1.197 +                                   srcrect, display->current_mode.format,
   1.198 +                                   dstrect->w, dstrect->h, pixels,
   1.199 +                                   target->pitch);
   1.200 +    } else {
   1.201 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.202 +        SDL_Surface *target = data->screens[data->current_screen];
   1.203 +        SDL_Rect real_srcrect = *srcrect;
   1.204 +        SDL_Rect real_dstrect = *dstrect;
   1.205 +        printf("Rmask %x Gmask %x Bmask %x Amask %x\n"
   1.206 +               "width %d, height %d, pitch %d\nbpp %d, pixels %x\n",
   1.207 +            surface->format->Rmask, surface->format->Gmask,
   1.208 +            surface->format->Bmask, surface->format->Amask,
   1.209 +            surface->w, surface->h, surface->pitch,
   1.210 +            surface->format->BitsPerPixel, (u32)(surface->pixels));
   1.211 +        sdlds_surf2vram(surface);
   1.212 +        return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
   1.213 +    }
   1.214 +#if 0
   1.215 +/* previous attempt to copy it directly to vram */
   1.216 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.217 +        int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h;
   1.218 +        int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h;
   1.219 +        int si,sj, di,dj;
   1.220 +        /*printf("DEBUG: still alive!\n");*/
   1.221 +        for(sj=0, dj=0; sj<sh && dj<dh; ++sj, ++dj) {
   1.222 +            for(si=0, di=0; si<sw && di<dw; ++si, ++di) {
   1.223 +                ((uint16*)VRAM_A)[(dj+dy)*256 + di+dx]
   1.224 +                 = ((Uint16*)surface->pixels)[(sj+sy)*(surface->w) + si+sx];
   1.225 +            }
   1.226 +        }
   1.227 +        /*printf("DEBUG: still alive!\n");*/
   1.228 +    }
   1.229 +    return 0;
   1.230 +#endif
   1.231 +}
   1.232 +
   1.233 +static void
   1.234 +SDL_NDS_RenderPresent(SDL_Renderer * renderer)
   1.235 +{
   1.236 +    SDL_NDS_RenderData *data =
   1.237 +        (SDL_NDS_RenderData *) renderer->driverdata;
   1.238 +
   1.239 +    printf("SDL_NDS_RenderPresent(renderer)\n");
   1.240 +    printf(" renderer: %s\n", renderer->info.name);
   1.241 +    /* Send the data to the display */
   1.242 +
   1.243 +#if 0
   1.244 +/*testing to see if rectangles drawn get copied right*/
   1.245 +    {
   1.246 +    SDL_Rect ra;
   1.247 +  	ra.x=0; ra.y=0; ra.w=256; ra.h=192;
   1.248 +	SDL_FillRect(data->screens[data->current_screen], &ra, 0x250);
   1.249 +	ra.x=32; ra.y=32; ra.w=192; ra.h=128;
   1.250 +	SDL_FillRect(data->screens[data->current_screen], &ra,
   1.251 +	    SDL_MapRGBA(data->screens[data->current_screen]->format,
   1.252 +	    255,255,255,255));
   1.253 +	}
   1.254 +/*okay so this works but why not when I do it in the main()?
   1.255 +  for some reason the screen I get from screen=SDL_SetVideoMode(...)
   1.256 +  doesn't get copied to renderer->driverdata? */
   1.257 +    for(i = 0; i < 30; ++i) swiWaitForVBlank(); /* delay for debug purpose */
   1.258 +#endif
   1.259 +    sdlds_surf2vram(data->screens[data->current_screen]);
   1.260 +
   1.261 +    /* Update the flipping chain, if any */
   1.262 +    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
   1.263 +        data->current_screen = (data->current_screen + 1) % 2;
   1.264 +    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
   1.265 +        data->current_screen = (data->current_screen + 1) % 3;
   1.266 +    }
   1.267 +}
   1.268 +
   1.269 +static void
   1.270 +SDL_NDS_DestroyRenderer(SDL_Renderer * renderer)
   1.271 +{
   1.272 +    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
   1.273 +    int i;
   1.274 +
   1.275 +    printf("SDL_NDS_DestroyRenderer(renderer)\n");
   1.276 +    printf(" renderer: %s\n", renderer->info.name);
   1.277 +    if (data) {
   1.278 +        for (i = 0; i < SDL_arraysize(data->screens); ++i) {
   1.279 +            if (data->screens[i]) {
   1.280 +                SDL_FreeSurface(data->screens[i]);
   1.281 +            }
   1.282 +        }
   1.283 +        SDL_free(data);
   1.284 +    }
   1.285 +    SDL_free(renderer);
   1.286 +}
   1.287 +
   1.288 +/* vi: set ts=4 sw=4 expandtab: */