src/video/nds/SDL_ndsvideo.c
author Darren Alton <dalton@stevens.edu>
Sun, 13 Jul 2008 04:28:54 +0000
branchgsoc2008_nds
changeset 2679 bc3e3e889f6d
parent 2677 6386764eb222
child 2680 4135aa9c5645
permissions -rw-r--r--
More NDS video driver work.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* Dummy SDL video driver implementation; this is just enough to make an
    25  *  SDL-based application THINK it's got a working video driver, for
    26  *  applications that call SDL_Init(SDL_INIT_VIDEO) when they don't need it,
    27  *  and also for use as a collection of stubs when porting SDL to a new
    28  *  platform for which you haven't yet written a valid video driver.
    29  *
    30  * This is also a great way to determine bottlenecks: if you think that SDL
    31  *  is a performance problem for a given platform, enable this driver, and
    32  *  then see if your application runs faster without video overhead.
    33  *
    34  * Initial work by Ryan C. Gordon (icculus@icculus.org). A good portion
    35  *  of this was cut-and-pasted from Stephane Peter's work in the AAlib
    36  *  SDL video driver.  Renamed to "DUMMY" by Sam Lantinga.
    37  */
    38 
    39 #include <stdio.h>
    40 #include <stdlib.h>
    41 #include <nds.h>
    42 
    43 #include "SDL_video.h"
    44 #include "SDL_mouse.h"
    45 #include "../SDL_sysvideo.h"
    46 #include "../SDL_pixels_c.h"
    47 #include "../../events/SDL_events_c.h"
    48 
    49 #include "SDL_ndsvideo.h"
    50 #include "SDL_ndsevents_c.h"
    51 #include "SDL_ndsrender_c.h"
    52 
    53 #define NDSVID_DRIVER_NAME "nds"
    54 
    55 /* Initialization/Query functions */
    56 static int NDS_VideoInit(_THIS);
    57 static int NDS_SetDisplayMode(_THIS, SDL_DisplayMode * mode);
    58 static void NDS_VideoQuit(_THIS);
    59 
    60 /* DUMMY driver bootstrap functions */
    61 
    62 static int
    63 NDS_Available(void)
    64 {
    65     const char *envr = SDL_getenv("SDL_VIDEODRIVER");
    66     /*printf("NDS_Available()\n"); */
    67     return (1);
    68 }
    69 
    70 static void
    71 NDS_DeleteDevice(SDL_VideoDevice * device)
    72 {
    73     SDL_free(device);
    74 }
    75 
    76 static SDL_VideoDevice *
    77 NDS_CreateDevice(int devindex)
    78 {
    79     SDL_VideoDevice *device;
    80     /*printf("NDS_CreateDevice(%d)\n", devindex); */
    81 
    82     /* Initialize all variables that we clean on shutdown */
    83     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    84     if (!device) {
    85         SDL_OutOfMemory();
    86         if (device) {
    87             SDL_free(device);
    88         }
    89         return (0);
    90     }
    91 
    92     /* Set the function pointers */
    93     device->VideoInit = NDS_VideoInit;
    94     device->VideoQuit = NDS_VideoQuit;
    95     device->SetDisplayMode = NDS_SetDisplayMode;
    96     device->PumpEvents = NDS_PumpEvents;
    97 
    98     device->num_displays = 2;   /* DS = dual screens */
    99 
   100     device->free = NDS_DeleteDevice;
   101 
   102     return device;
   103 }
   104 
   105 VideoBootStrap NDS_bootstrap = {
   106     NDSVID_DRIVER_NAME, "SDL NDS video driver",
   107     NDS_Available, NDS_CreateDevice
   108 };
   109 
   110 int
   111 NDS_VideoInit(_THIS)
   112 {
   113     SDL_DisplayMode mode;
   114     int i;
   115 
   116     /* simple 256x192x15x60 for now */
   117     mode.w = 256;
   118     mode.h = 192;
   119     mode.format = SDL_PIXELFORMAT_ABGR1555;
   120     mode.refresh_rate = 60;
   121     mode.driverdata = NULL;
   122 
   123     SDL_AddBasicVideoDisplay(&mode);
   124     SDL_AddRenderDriver(0, &NDS_RenderDriver);
   125     /*SDL_AddBasicVideoDisplay(&mode); two screens, same mode. uncomment later
   126     SDL_AddRenderDriver(1, &NDS_RenderDriver);*/
   127 
   128     SDL_zero(mode);
   129     SDL_AddDisplayMode(0, &mode);
   130 
   131     /* hackish stuff to get things up and running for now, and for a console */
   132     powerON(POWER_ALL);
   133     irqInit();
   134     irqEnable(IRQ_VBLANK);
   135     NDS_SetDisplayMode(_this, &mode);
   136     return 0;
   137 }
   138 
   139 static int
   140 NDS_SetDisplayMode(_THIS, SDL_DisplayMode * mode)
   141 {
   142     /* right now this function is just hard-coded for 256x192 ABGR1555 */
   143     videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE);       /* display on main core */
   144     videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE);    /* debug text on sub */
   145     vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000, VRAM_B_LCD,
   146                      VRAM_C_SUB_BG, VRAM_D_LCD);
   147 
   148     /* set up console for debug text 'n stuff */
   149     SUB_BG0_CR = BG_MAP_BASE(31);
   150     BG_PALETTE_SUB[255] = RGB15(31, 31, 31);
   151     consoleInitDefault((u16 *) SCREEN_BASE_BLOCK_SUB(31),
   152                        (u16 *) CHAR_BASE_BLOCK_SUB(0), 16);
   153 
   154 #if 0
   155 /* we should be using this as a texture for rendering, not as a framebuffer */
   156     /* maps well to the 256x192 screen anyway.  note: need VRAM_B for bigger */
   157     BACKGROUND.control[3] = BG_BMP16_256x256;
   158     /* affine transformation matrix.  nothing too fancy here */
   159     BG3_XDX = 0x100;
   160     BG3_XDY = 0;
   161     BG3_YDX = 0;
   162     BG3_YDY = 0x100;
   163     /* x/y position */
   164     BG3_CX = 0;
   165     BG3_CY = 0;
   166 #endif
   167     return 0;
   168 }
   169 
   170 void
   171 NDS_VideoQuit(_THIS)
   172 {
   173     videoSetMode(DISPLAY_SCREEN_OFF);
   174     videoSetModeSub(DISPLAY_SCREEN_OFF);
   175     vramSetMainBanks(VRAM_A_LCD, VRAM_B_LCD, VRAM_C_LCD, VRAM_D_LCD);
   176 }
   177 
   178 /* vi: set ts=4 sw=4 expandtab: */