src/video/nds/SDL_ndsrender.c
branchgsoc2008_nds
changeset 2677 6386764eb222
parent 2673 24a6b3588eac
child 2678 3895761db26a
     1.1 --- a/src/video/nds/SDL_ndsrender.c	Tue Jul 01 13:02:50 2008 +0000
     1.2 +++ b/src/video/nds/SDL_ndsrender.c	Wed Jul 02 13:59:30 2008 +0000
     1.3 @@ -34,20 +34,51 @@
     1.4  
     1.5  /* SDL surface based renderer implementation */
     1.6  
     1.7 -static SDL_Renderer *SDL_NDS_CreateRenderer(SDL_Window * window,
     1.8 -                                            Uint32 flags);
     1.9 -static int SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g,
    1.10 +static SDL_Renderer *NDS_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.11 +static int NDS_ActivateRenderer(SDL_Renderer * renderer);
    1.12 +static int NDS_DisplayModeChanged(SDL_Renderer * renderer);
    1.13 +static int NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.14 +static int NDS_QueryTexturePixels(SDL_Renderer * renderer,
    1.15 +                                 SDL_Texture * texture, void **pixels,
    1.16 +                                 int *pitch);
    1.17 +static int NDS_SetTexturePalette(SDL_Renderer * renderer,
    1.18 +                                SDL_Texture * texture,
    1.19 +                                const SDL_Color * colors, int firstcolor,
    1.20 +                                int ncolors);
    1.21 +static int NDS_GetTexturePalette(SDL_Renderer * renderer,
    1.22 +                                SDL_Texture * texture, SDL_Color * colors,
    1.23 +                                int firstcolor, int ncolors);
    1.24 +static int NDS_SetTextureColorMod(SDL_Renderer * renderer,
    1.25 +                                 SDL_Texture * texture);
    1.26 +static int NDS_SetTextureAlphaMod(SDL_Renderer * renderer,
    1.27 +                                 SDL_Texture * texture);
    1.28 +static int NDS_SetTextureBlendMode(SDL_Renderer * renderer,
    1.29 +                                  SDL_Texture * texture);
    1.30 +static int NDS_SetTextureScaleMode(SDL_Renderer * renderer,
    1.31 +                                  SDL_Texture * texture);
    1.32 +static int NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.33 +                            const SDL_Rect * rect, const void *pixels,
    1.34 +                            int pitch);
    1.35 +static int NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.36 +                          const SDL_Rect * rect, int markDirty, void **pixels,
    1.37 +                          int *pitch);
    1.38 +static void NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.39 +static void NDS_DirtyTexture(SDL_Renderer * renderer,
    1.40 +                            SDL_Texture * texture, int numrects,
    1.41 +                            const SDL_Rect * rects);
    1.42 +static int 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 +static int NDS_RenderCopy(SDL_Renderer * renderer,
    1.46                                SDL_Texture * texture,
    1.47                                const SDL_Rect * srcrect,
    1.48                                const SDL_Rect * dstrect);
    1.49 -static void SDL_NDS_RenderPresent(SDL_Renderer * renderer);
    1.50 -static void SDL_NDS_DestroyRenderer(SDL_Renderer * renderer);
    1.51 +static void NDS_RenderPresent(SDL_Renderer * renderer);
    1.52 +static void NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.53 +static void NDS_DestroyRenderer(SDL_Renderer * renderer);
    1.54  
    1.55  
    1.56 -SDL_RenderDriver SDL_NDS_RenderDriver = {
    1.57 -    SDL_NDS_CreateRenderer,
    1.58 +SDL_RenderDriver NDS_RenderDriver = {
    1.59 +    NDS_CreateRenderer,
    1.60      {"nds", SDL_RENDERER_PRESENTCOPY}
    1.61  /*   (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
    1.62        SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
    1.63 @@ -57,9 +88,8 @@
    1.64  typedef struct
    1.65  {
    1.66      int current_screen;
    1.67 -    SDL_Surface *screens[3];
    1.68 -    int ultimate_answer;
    1.69 -} SDL_NDS_RenderData;
    1.70 +    u16* fb;
    1.71 +} NDS_RenderData;
    1.72  
    1.73  
    1.74  
    1.75 @@ -81,8 +111,16 @@
    1.76  sdlds_surf2vram(SDL_Surface * s)
    1.77  {
    1.78      if (s->w == 256) {
    1.79 +        u16 tmpbuf[0x20000];
    1.80 +        int i;
    1.81 +
    1.82          dmaCopy((u8 *) (s->pixels) + 156 * sizeof(u16),
    1.83 -                VRAM_A, 256 * 192 * sizeof(u16));
    1.84 +                tmpbuf, 256 * 192 * sizeof(u16));
    1.85 +    /* hack to fix the pixel format until I figure out why BGR doesn't work */
    1.86 +        for (i = 0; i < 256 * 192; ++i) {
    1.87 +            tmpbuf[i] = sdlds_rgb2bgr(tmpbuf[i]);
    1.88 +        }
    1.89 +        dmaCopy(tmpbuf, VRAM_A, 256 * 192 * sizeof(u16));
    1.90      }
    1.91  }
    1.92  
    1.93 @@ -108,12 +146,12 @@
    1.94  
    1.95  
    1.96  SDL_Renderer *
    1.97 -SDL_NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
    1.98 +NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
    1.99  {
   1.100      SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.101      SDL_DisplayMode *displayMode = &display->current_mode;
   1.102      SDL_Renderer *renderer;
   1.103 -    SDL_NDS_RenderData *data;
   1.104 +    NDS_RenderData *data;
   1.105      int i, n;
   1.106      int bpp = 15;
   1.107      Uint32 Rmask, Gmask, Bmask, Amask;
   1.108 @@ -121,12 +159,6 @@
   1.109      Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000;
   1.110  */
   1.111  
   1.112 -#if 0
   1.113 -    printf("SDL_NDS_CreateRenderer(window, 0x%x)\n", flags);
   1.114 -    printf(" window: (%d,%d), %dx%d\n",
   1.115 -           window->x, window->y, window->w, window->h);
   1.116 -#endif
   1.117 -
   1.118      /* hard coded this to BGR555 for now */
   1.119      if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp,
   1.120                                      &Rmask, &Gmask, &Bmask, &Amask)) {
   1.121 @@ -140,23 +172,23 @@
   1.122          return NULL;
   1.123      }
   1.124  
   1.125 -    data = (SDL_NDS_RenderData *) SDL_malloc(sizeof(*data));
   1.126 +    data = (NDS_RenderData *) SDL_malloc(sizeof(*data));
   1.127      if (!data) {
   1.128 -        SDL_NDS_DestroyRenderer(renderer);
   1.129 +        NDS_DestroyRenderer(renderer);
   1.130          SDL_OutOfMemory();
   1.131          return NULL;
   1.132      }
   1.133      SDL_zerop(data);
   1.134  
   1.135 -    renderer->RenderFill = SDL_NDS_RenderFill;
   1.136 -    renderer->RenderCopy = SDL_NDS_RenderCopy;
   1.137 -    renderer->RenderPresent = SDL_NDS_RenderPresent;
   1.138 -    renderer->DestroyRenderer = SDL_NDS_DestroyRenderer;
   1.139 -    renderer->info.name = SDL_NDS_RenderDriver.info.name;
   1.140 +    renderer->RenderFill = NDS_RenderFill;
   1.141 +    renderer->RenderCopy = NDS_RenderCopy;
   1.142 +    renderer->RenderPresent = NDS_RenderPresent;
   1.143 +    renderer->DestroyRenderer = NDS_DestroyRenderer;
   1.144 +    renderer->info.name = NDS_RenderDriver.info.name;
   1.145      renderer->info.flags = 0;
   1.146      renderer->window = window->id;
   1.147      renderer->driverdata = data;
   1.148 -    Setup_SoftwareRenderer(renderer);
   1.149 +    Setup_SoftwareRenderer(renderer); /* TODO: well, "TODON'T" is more like it */
   1.150  
   1.151      if (flags & SDL_RENDERER_PRESENTFLIP2) {
   1.152          renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
   1.153 @@ -167,55 +199,51 @@
   1.154      } else {
   1.155          renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
   1.156          n = 1;
   1.157 -    }
   1.158 +    }/*
   1.159      for (i = 0; i < n; ++i) {
   1.160          data->screens[i] =
   1.161              SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask,
   1.162                                   Amask);
   1.163          if (!data->screens[i]) {
   1.164 -            SDL_NDS_DestroyRenderer(renderer);
   1.165 +            NDS_DestroyRenderer(renderer);
   1.166              return NULL;
   1.167          }
   1.168          SDL_SetSurfacePalette(data->screens[i], display->palette);
   1.169          sdlds_print_surface_info(data->screens[i]);
   1.170 -    }
   1.171 +    }*/
   1.172  
   1.173 -    data->current_screen = 0;
   1.174 -    data->ultimate_answer = 42;
   1.175 +    data->fb = (u16*)0x06020000;
   1.176  
   1.177      return renderer;
   1.178  }
   1.179  
   1.180  static int
   1.181 -SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
   1.182 +NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
   1.183                     Uint8 a, const SDL_Rect * rect)
   1.184  {
   1.185 -    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
   1.186 -    SDL_Surface *target = data->screens[data->current_screen];
   1.187 -    Uint32 color;
   1.188 +    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   1.189      SDL_Rect real_rect = *rect;
   1.190 +    u16 color;
   1.191 +    int i, j;
   1.192  
   1.193 -    color = SDL_MapRGBA(target->format, r, g, b, a);
   1.194 -
   1.195 -    return SDL_FillRect(target, &real_rect, color);
   1.196 +    color = RGB15(r>>3,g>>3,b>>3);
   1.197 +    for (i = real_rect.x; i < real_rect.x+real_rect.w; ++i) {
   1.198 +        for (j = real_rect.y; j < real_rect.y+real_rect.h; ++j) {
   1.199 +            data->fb[(j + real_rect.y) * 256 + i + real_rect.x] = 
   1.200 +                0x8000 | color;
   1.201 +        }
   1.202 +    }
   1.203 +    return 0;
   1.204  }
   1.205 -
   1.206  static int
   1.207 -SDL_NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.208 +NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.209                     const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   1.210  {
   1.211 -    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
   1.212 +    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   1.213      SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.214      SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.215 +
   1.216  #if 0
   1.217 -    printf("SDL_NDS_RenderCopy(renderer, texture, srcrect, dstrect)\n");
   1.218 -    printf(" renderer: %s\n", renderer->info.name);
   1.219 -    printf(" texture: %dx%d\n", texture->w, texture->h);
   1.220 -    printf(" srcrect: (%d,%d), %dx%d\n", srcrect->x, srcrect->y, srcrect->w,
   1.221 -           srcrect->h);
   1.222 -    printf(" dstrect: (%d,%d), %dx%d\n", dstrect->x, dstrect->y, dstrect->w,
   1.223 -           dstrect->h);
   1.224 -#endif
   1.225      if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.226          SDL_Surface *target = data->screens[data->current_screen];
   1.227          void *pixels =
   1.228 @@ -235,39 +263,31 @@
   1.229          sdlds_surf2vram(surface);
   1.230          return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
   1.231      }
   1.232 -#if 0
   1.233 +#endif
   1.234      /* copy it directly to vram */
   1.235      SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.236 +    sdlds_surf2vram(surface);
   1.237 +    /*
   1.238      int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h;
   1.239      int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h;
   1.240      int si, sj, di, dj;
   1.241      for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) {
   1.242          for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) {
   1.243 -            VRAM_A[(dj + dy) * 256 + di + dx] =
   1.244 -                ((Uint16 *) surface->pixels)[(sj + sy) * (surface->w) + si +
   1.245 +            data->fb[(dj + dy) * 256 + di + dx] = 0x8000 |
   1.246 +                ((u16 *) surface->pixels)[(sj + sy) * (surface->w) + si +
   1.247                                               sx];
   1.248          }
   1.249 -    }
   1.250 +    }*/
   1.251      return 0;
   1.252 -#endif
   1.253  }
   1.254  
   1.255  
   1.256  static void
   1.257 -SDL_NDS_RenderPresent(SDL_Renderer * renderer)
   1.258 +NDS_RenderPresent(SDL_Renderer * renderer)
   1.259  {
   1.260 -    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
   1.261 -    int i;
   1.262 -#if 0
   1.263 -    printf("SDL_NDS_RenderPresent(renderer)\n");
   1.264 -    printf(" renderer: %s\n", renderer->info.name);
   1.265 -#endif
   1.266 -    /* Send the data to the display */
   1.267 +    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   1.268 +    /* Send the data to the display TODO */
   1.269  
   1.270 -    /* hack to fix the pixel format until I figure out why BGR doesn't work */
   1.271 -    for (i = 0; i < 256 * 192; ++i) {
   1.272 -        VRAM_A[i] = sdlds_rgb2bgr(VRAM_A[i]);
   1.273 -    }
   1.274      /* Update the flipping chain, if any */
   1.275      if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
   1.276          data->current_screen = (data->current_screen + 1) % 2;
   1.277 @@ -277,19 +297,19 @@
   1.278  }
   1.279  
   1.280  static void
   1.281 -SDL_NDS_DestroyRenderer(SDL_Renderer * renderer)
   1.282 +NDS_DestroyRenderer(SDL_Renderer * renderer)
   1.283  {
   1.284 -    SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
   1.285 +    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   1.286      int i;
   1.287  
   1.288 -    printf("SDL_NDS_DestroyRenderer(renderer)\n");
   1.289 +    printf("NDS_DestroyRenderer(renderer)\n");
   1.290      printf(" renderer: %s\n", renderer->info.name);
   1.291      if (data) {
   1.292 -        for (i = 0; i < SDL_arraysize(data->screens); ++i) {
   1.293 +        /*for (i = 0; i < SDL_arraysize(data->screens); ++i) {
   1.294              if (data->screens[i]) {
   1.295                  SDL_FreeSurface(data->screens[i]);
   1.296              }
   1.297 -        }
   1.298 +        }*/
   1.299          SDL_free(data);
   1.300      }
   1.301      SDL_free(renderer);