Frank Zago to SDL
authorSam Lantinga <slouken@libsdl.org>
Sun, 13 Feb 2011 14:01:02 -0800
changeset 52891916a9e9714d
parent 5288 d4381f3b0d1e
child 5290 25af68c1901d
Frank Zago to SDL

On 02/12/2011 01:44 PM, Sam Lantinga wrote:
> BTW, you probably want to nuke the NDS renderer and just implement these three
> functions instead:
> int (*CreateWindowFramebuffer) (_THIS, SDL_Window * window, Uint32 *
> format, void ** pixels, int *pitch);
> int (*UpdateWindowFramebuffer) (_THIS, SDL_Window * window, int numrects,
> SDL_Rect * rects);
> void (*DestroyWindowFramebuffer) (_THIS, SDL_Window * window);

Patch attached. The renderer for the DS is not used anymore, but I left the
file in place if someone wants to finish it.

I've also added a README.ds and fixed the spinlocks.
Makefile.ds
README.ds
src/atomic/SDL_spinlock.c
src/render/SDL_render.c
src/video/nds/SDL_ndsvideo.c
     1.1 --- a/Makefile.ds	Sun Feb 13 13:46:10 2011 -0800
     1.2 +++ b/Makefile.ds	Sun Feb 13 14:01:02 2011 -0800
     1.3 @@ -1,13 +1,13 @@
     1.4  
     1.5 -#LibSDL 1.3 porting and enhancements by Darren Alton (lifning)
     1.6 -#LibSDL 1.2.9 DS porting by Troy Davis(GPF)
     1.7 -
     1.8 -ifeq ($(strip $(DEVKITPRO)),)
     1.9 -$(error "Please set DEVKITPRO in your environment (available from http://www.devkitpro.org). export DEVKITPRO=<path to>devkitPro")
    1.10 -endif
    1.11 -ifeq ($(strip $(DEVKITARM)),)
    1.12 +#LibSDL 1.3 porting and enhancements by Darren Alton (lifning)
    1.13 +#LibSDL 1.2.9 DS porting by Troy Davis(GPF)
    1.14 +
    1.15 +ifeq ($(strip $(DEVKITPRO)),)
    1.16 +$(error "Please set DEVKITPRO in your environment (available from http://www.devkitpro.org). export DEVKITPRO=<path to>devkitPro")
    1.17 +endif
    1.18 +ifeq ($(strip $(DEVKITARM)),)
    1.19  DEVKITARM = $(DEVKITPRO)/devkitARM
    1.20 -endif
    1.21 +endif
    1.22  PATH := $(PATH):$(DEVKITARM)/bin
    1.23  
    1.24  CC = arm-eabi-gcc
    1.25 @@ -40,6 +40,7 @@
    1.26  src/SDL_hints.c \
    1.27  src/SDL_log.c \
    1.28  src/atomic/SDL_atomic.c \
    1.29 +src/atomic/SDL_spinlock.c \
    1.30  src/audio/SDL_audio.c \
    1.31  src/audio/SDL_audiocvt.c \
    1.32  src/audio/SDL_audiodev.c \
    1.33 @@ -118,6 +119,10 @@
    1.34  
    1.35  all: $(TARGET) install nds_test
    1.36  
    1.37 +# That file must be compiled in arm mode, not thumb mode.
    1.38 +src/atomic/SDL_spinlock.o: src/atomic/SDL_spinlock.c
    1.39 +	$(CC) $(CFLAGS) -mno-thumb -o $@ -c $^
    1.40 +
    1.41  $(TARGET): copy_config \
    1.42  	$(OBJS)
    1.43  	$(AR) rc $(TARGET) $(OBJS)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/README.ds	Sun Feb 13 14:01:02 2011 -0800
     2.3 @@ -0,0 +1,19 @@
     2.4 +================================================================================
     2.5 +Simple DirectMedia Layer for Nintendo DS
     2.6 +================================================================================
     2.7 +
     2.8 +-Requirements-
     2.9 +The devkitpro SDK available at http://devkitpro.org.
    2.10 +Read the information at http://devkitpro.org/wiki/Getting_Started/devkitARM
    2.11 +The necessary packages are devkitARM, libnds and default arm7.
    2.12 +
    2.13 +-Building SDL-
    2.14 +After setting the devkitpro environment, type:
    2.15 +  make -f Makefile.ds
    2.16 +
    2.17 +This will compile and install the library and headers into the proper libnds directories.
    2.18 +Additionnaly it will compile the general test, that you can run either on the DS or with desmume:
    2.19 +  desmume test/nds-test-progs/general/general.nds
    2.20 +
    2.21 +
    2.22 +Note that the port is very basic and incomplete.
     3.1 --- a/src/atomic/SDL_spinlock.c	Sun Feb 13 13:46:10 2011 -0800
     3.2 +++ b/src/atomic/SDL_spinlock.c	Sun Feb 13 14:01:02 2011 -0800
     3.3 @@ -62,7 +62,7 @@
     3.4  #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
     3.5      return (__sync_lock_test_and_set(lock, 1) == 0);
     3.6  
     3.7 -#elif defined(__GNUC__) && defined(__arm__) && defined(__ARM_ARCH_5__)
     3.8 +#elif defined(__GNUC__) && defined(__arm__) && (defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__))
     3.9      int result;
    3.10      __asm__ __volatile__ (
    3.11          "swp %0, %1, [%2]\n"
     4.1 --- a/src/render/SDL_render.c	Sun Feb 13 13:46:10 2011 -0800
     4.2 +++ b/src/render/SDL_render.c	Sun Feb 13 14:01:02 2011 -0800
     4.3 @@ -60,9 +60,6 @@
     4.4  #if SDL_VIDEO_RENDER_DIRECTFB
     4.5      &DirectFB_RenderDriver,
     4.6  #endif
     4.7 -#if SDL_VIDEO_RENDER_NDS
     4.8 -	&NDS_RenderDriver,
     4.9 -#endif
    4.10      &SW_RenderDriver
    4.11  #endif /* !SDL_RENDER_DISABLED */
    4.12  };
     5.1 --- a/src/video/nds/SDL_ndsvideo.c	Sun Feb 13 13:46:10 2011 -0800
     5.2 +++ b/src/video/nds/SDL_ndsvideo.c	Sun Feb 13 14:01:02 2011 -0800
     5.3 @@ -35,18 +35,37 @@
     5.4  
     5.5  #include "SDL_video.h"
     5.6  #include "SDL_mouse.h"
     5.7 -#include "../SDL_sysvideo.h"
     5.8 -#include "../SDL_pixels_c.h"
     5.9 -#include "../../events/SDL_events_c.h"
    5.10 +#include "SDL_sysvideo.h"
    5.11 +#include "SDL_pixels_c.h"
    5.12 +#include "SDL_events_c.h"
    5.13  #include "SDL_render.h"
    5.14 -#include "../../render/SDL_sysrender.h"
    5.15 -
    5.16  #include "SDL_ndsvideo.h"
    5.17  #include "SDL_ndsevents_c.h"
    5.18 -#include "SDL_ndsrender_c.h"
    5.19  
    5.20  #define NDSVID_DRIVER_NAME "nds"
    5.21  
    5.22 +/* Per Window information. */
    5.23 +struct NDS_WindowData {
    5.24 +    int hw_index;               /* index of sprite in OAM or bg from libnds */
    5.25 +	int bg;						/* which bg is that attached to (2 or 3) */
    5.26 +    int pitch, bpp;             /* useful information about the texture */
    5.27 +    struct {
    5.28 +        int x, y;
    5.29 +    } scale;                    /* x/y stretch (24.8 fixed point) */
    5.30 +    struct {
    5.31 +        int x, y;
    5.32 +    } scroll;                   /* x/y offset */
    5.33 +    int rotate;                 /* -32768 to 32767, texture rotation */
    5.34 +    u16 *vram_pixels;           /* where the pixel data is stored (a pointer into VRAM) */
    5.35 +};
    5.36 +
    5.37 +/* Per device information. */
    5.38 +struct NDS_DeviceData {
    5.39 +	int has_bg2;				/* backgroud 2 has been attached */
    5.40 +	int has_bg3;				/* backgroud 3 has been attached */
    5.41 +    int sub;
    5.42 +};
    5.43 +
    5.44  /* Initialization/Query functions */
    5.45  static int NDS_VideoInit(_THIS);
    5.46  static int NDS_SetDisplayMode(_THIS, SDL_VideoDisplay *display,
    5.47 @@ -61,6 +80,110 @@
    5.48      return (1);                 /* always here */
    5.49  }
    5.50  
    5.51 +static int NDS_CreateWindowFramebuffer(_THIS, SDL_Window * window,
    5.52 +									   Uint32 * format, void ** pixels,
    5.53 +									   int *pitch)
    5.54 +{
    5.55 +	struct NDS_DeviceData *data = _this->driverdata;
    5.56 +	struct NDS_WindowData *wdata;
    5.57 +    int bpp;
    5.58 +    Uint32 Rmask, Gmask, Bmask, Amask;
    5.59 +	int whichbg = -1;
    5.60 +
    5.61 +	*format = SDL_PIXELFORMAT_BGR555;
    5.62 +
    5.63 +    if (!SDL_PixelFormatEnumToMasks
    5.64 +        (*format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
    5.65 +        SDL_SetError("Unknown texture format");
    5.66 +        return -1;
    5.67 +    }
    5.68 +
    5.69 +	if (!data->has_bg2)
    5.70 +		whichbg = 2;
    5.71 +	else if (!data->has_bg3)
    5.72 +		whichbg = 3;
    5.73 +	else {
    5.74 +		SDL_SetError("Out of NDS backgrounds.");
    5.75 +		return -1;
    5.76 +	}
    5.77 +
    5.78 +	wdata = SDL_calloc(1, sizeof(struct NDS_WindowData));
    5.79 +	if (!wdata) {
    5.80 +		SDL_OutOfMemory();
    5.81 +		return -1;
    5.82 +	}
    5.83 +
    5.84 +	if (!data->sub) {
    5.85 +		if (bpp == 8) {
    5.86 +			wdata->hw_index =
    5.87 +				bgInit(whichbg, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
    5.88 +		} else {
    5.89 +			wdata->hw_index =
    5.90 +				bgInit(whichbg, BgType_Bmp16, BgSize_B16_256x256, 0,
    5.91 +					   0);
    5.92 +		}
    5.93 +	} else {
    5.94 +		if (bpp == 8) {
    5.95 +			wdata->hw_index =
    5.96 +				bgInitSub(whichbg, BgType_Bmp8, BgSize_B8_256x256, 0,
    5.97 +						  0);
    5.98 +		} else {
    5.99 +			wdata->hw_index =
   5.100 +				bgInitSub(whichbg, BgType_Bmp16, BgSize_B16_256x256,
   5.101 +						  0, 0);
   5.102 +		}
   5.103 +	}
   5.104 +
   5.105 +	wdata->bg = whichbg;
   5.106 +	wdata->pitch = (window->w) * ((bpp+1) / 8);
   5.107 +	wdata->bpp = bpp;
   5.108 +	wdata->rotate = 0;
   5.109 +	wdata->scale.x = 0x100;
   5.110 +	wdata->scale.y = 0x100;
   5.111 +	wdata->scroll.x = 0;
   5.112 +	wdata->scroll.y = 0;
   5.113 +	wdata->vram_pixels = (u16 *) bgGetGfxPtr(wdata->hw_index);
   5.114 +
   5.115 +	bgSetCenter(wdata->hw_index, 0, 0);
   5.116 +	bgSetRotateScale(wdata->hw_index, wdata->rotate, wdata->scale.x,
   5.117 +					 wdata->scale.y);
   5.118 +	bgSetScroll(wdata->hw_index, wdata->scroll.x, wdata->scroll.y);
   5.119 +	bgUpdate();
   5.120 +
   5.121 +	*pixels = wdata->vram_pixels;
   5.122 +	*pitch = wdata->pitch;
   5.123 +
   5.124 +	if (!data->has_bg2)
   5.125 +		data->has_bg2 = 1;
   5.126 +	else 
   5.127 +		data->has_bg3 = 1;
   5.128 +
   5.129 +	window->driverdata = wdata;
   5.130 +
   5.131 +	return 0;
   5.132 +}
   5.133 +
   5.134 +static int NDS_UpdateWindowFramebuffer(_THIS, SDL_Window * window, int numrects,
   5.135 +									   SDL_Rect * rects)
   5.136 +{
   5.137 +	/* Nothing to do because writes are done directly into the
   5.138 +	 * framebuffer. */
   5.139 +    return 0;
   5.140 +}
   5.141 +
   5.142 +static void NDS_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
   5.143 +{
   5.144 +	struct NDS_DeviceData *data = _this->driverdata;
   5.145 +    struct NDS_WindowData *wdata = window->driverdata;
   5.146 +
   5.147 +	if (wdata->bg == 2)
   5.148 +		data->has_bg2 = 0;
   5.149 +	else
   5.150 +		data->has_bg3 = 0;
   5.151 +
   5.152 +    SDL_free(wdata);
   5.153 +}
   5.154 +
   5.155  static void
   5.156  NDS_DeleteDevice(SDL_VideoDevice * device)
   5.157  {
   5.158 @@ -73,17 +196,27 @@
   5.159      SDL_VideoDevice *device;
   5.160  
   5.161      /* Initialize all variables that we clean on shutdown */
   5.162 -    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   5.163 +    device = SDL_calloc(1, sizeof(SDL_VideoDevice));
   5.164      if (!device) {
   5.165          SDL_OutOfMemory();
   5.166          return NULL;
   5.167      }
   5.168  
   5.169 +	device->driverdata = SDL_calloc(1, sizeof(SDL_VideoDevice));
   5.170 +    if (!device) {
   5.171 +		SDL_free(device);
   5.172 +        SDL_OutOfMemory();
   5.173 +        return NULL;
   5.174 +    }
   5.175 +
   5.176      /* Set the function pointers */
   5.177      device->VideoInit = NDS_VideoInit;
   5.178      device->VideoQuit = NDS_VideoQuit;
   5.179      device->SetDisplayMode = NDS_SetDisplayMode;
   5.180      device->PumpEvents = NDS_PumpEvents;
   5.181 +	device->CreateWindowFramebuffer = NDS_CreateWindowFramebuffer;
   5.182 +	device->UpdateWindowFramebuffer = NDS_UpdateWindowFramebuffer;
   5.183 +	device->DestroyWindowFramebuffer = NDS_DestroyWindowFramebuffer;
   5.184  
   5.185      device->num_displays = 2;   /* DS = dual screens */
   5.186