From 8d987797c9e5a3d17428b38337c57f0e1639a233 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 12 Feb 2012 21:04:01 -0500 Subject: [PATCH] Updated Nintendo DS support Frank Zago This patch updates the DS port: - do not use the now removed compat layer. - integrate parts of libgl2D since I got permission from the author, and thus removed an external dependancy, - a few bugs fixes. Now, the textures should be completely supported, except reading from them which doesn't makes sense to have on the DS. Sound is still not supported. If someone else wants to work on the missing pieces, feel free. --- Makefile.ds | 6 +- README.ds | 21 +- src/render/nds/SDL_libgl2D.c | 310 ++++++++++++++++++ src/render/nds/SDL_libgl2D.h | 150 +++++++++ src/render/nds/SDL_ndsrender.c | 87 ++++- src/video/nds/SDL_ndsvideo.c | 2 +- test/nds-test-progs/general/Makefile | 43 ++- test/nds-test-progs/general/source/main.c | 87 +++-- test/nds-test-progs/testscale/Makefile | 2 +- test/nds-test-progs/testsprite2/Makefile | 2 +- .../nds-test-progs/testspriteminimal/Makefile | 2 +- 11 files changed, 613 insertions(+), 99 deletions(-) create mode 100644 src/render/nds/SDL_libgl2D.c create mode 100644 src/render/nds/SDL_libgl2D.h diff --git a/Makefile.ds b/Makefile.ds index 7b31356c0..9a79f6f1b 100644 --- a/Makefile.ds +++ b/Makefile.ds @@ -28,7 +28,7 @@ ARCH := -mthumb -mthumb-interwork \ -D__NDS__ -DENABLE_NDS -DNO_SIGNAL_H -DDISABLE_THREADS -DPACKAGE=\"SDL\" \ -DVERSION=\"2.0\" -DHAVE_ALLOCA_H=1 -DHAVE_ALLOCA=1 -CFLAGS := -g -Wall -O2\ +CFLAGS := -g -Wall -Os\ -march=armv5te -mtune=arm946e-s \ -fomit-frame-pointer -ffast-math \ $(ARCH) @@ -39,7 +39,7 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions ASFLAGS := -g $(ARCH) -march=armv5te -mtune=arm946e-s LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) -# Set to 0 to use a framer buffer, or 1 to use the hardware +# Set to 0 to use a framer buffer, or 1 to use the GL like hardware # renderer. Alas, both cannot be used at the same time for lack of # display/texture memory. USE_HW_RENDERER := 1 @@ -72,7 +72,6 @@ export DEPSDIR := $(CURDIR)/$(BUILD) CFILES := \ SDL.c \ SDL_assert.c \ - SDL_compat.c \ SDL_error.c \ SDL_fatal.c \ SDL_hints.c \ @@ -104,6 +103,7 @@ CFILES := \ render/SDL_render.c \ render/SDL_yuv_sw.c \ render/nds/SDL_ndsrender.c \ + render/nds/SDL_libgl2D.c \ render/software/SDL_blendfillrect.c \ render/software/SDL_blendline.c \ render/software/SDL_blendpoint.c \ diff --git a/README.ds b/README.ds index 4e89168f4..40bf8823e 100644 --- a/README.ds +++ b/README.ds @@ -6,11 +6,8 @@ Simple DirectMedia Layer for Nintendo DS * The devkitpro SDK available at http://devkitpro.org. Read the information at http://devkitpro.org/wiki/Getting_Started/devkitARM The necessary packages are devkitARM, libnds, libfat and default arm7. -* The hardware renderer is using the libgl2d abstraction library that can be found at: - http://rel.phatcode.net/junk.php?id=117 - Build it, and install the library and the header where SDL can find them (ie. in - the libnds/lib and libnds/include directories). - +* Optionally, use a DS emulator, such as desmume (http://desmume.org/) + to program and debug. -Building SDL- @@ -18,16 +15,24 @@ After setting the devkitpro environment, cd into your SDL directory and type: make -f Makefile.ds This will compile and install the library and headers into the -devkitpro's portlibs directory. Additionnaly it will compile several -tests that you can run either on the DS or with desmume. For instance: +devkitpro's portlibs directory (../portlibs/arm/lib/ and +../portlibs/arm/include/). Additionally it will compile several tests +that you can run either on the DS or with desmume. For instance: + desmume --cflash-path=test/ test/nds-test-progs/testsprite2/testsprite2.nds + desmume --cflash-path=test/ test/nds-test-progs/testspriteminimal/testspriteminimal.nds + desmume --cflash-path=test/ test/nds-test-progs/testscale/testscale.nds desmume test/nds-test-progs/general/general.nds -Notes- * The renderer code is based on the gl like engine. It's not using the sprite engine. +* The hardware renderer is using the parts of the libgl2d abstraction library that can be found at: + http://rel.phatcode.net/junk.php?id=117 + Used with the author's permission. * The port is very basic and incomplete: - - SDL currently has to be compiled for either framebuffer mode or render mode. + - SDL currently has to be compiled for either framebuffer mode or renderer mode. See USE_HW_RENDERER in Makefile.ds. - some optional renderer functions are not implemented. + - no sound -Limitations- * in hardware renderer mode, don't load too many textures. The internal format is diff --git a/src/render/nds/SDL_libgl2D.c b/src/render/nds/SDL_libgl2D.c new file mode 100644 index 000000000..aaca4ce3b --- /dev/null +++ b/src/render/nds/SDL_libgl2D.c @@ -0,0 +1,310 @@ +/* + * Note: The Nintendo DS port to SDL uses excerpts from the libGL2D, + * with permission of the original author. The following is mostly his + * code/comments. + * + * + * Easy GL2D + * + * Relminator 2010 + * Richard Eric M. Lope BSN RN + * + * http://rel.betterwebber.com + * + * A very small and simple DS rendering lib using the 3d core to render 2D stuff + */ + +#include "SDL_libgl2D.h" + +/* + * Our static global variable used for Depth values since we cannot + * disable depth testing in the DS hardware This value is incremented + * for every draw call. */ +v16 g_depth; +int gCurrentTexture; + +/* + * !!! PRIVATE !!! Set orthographic projection at 1:1 correspondence + * to screen coords glOrtho expects f32 values but if we use the + * standard f32 values, we need to rescale either every vert or the + * modelview matrix by the same amount to make it work. That's gonna + * give us lots of overflows and headaches. So we "scale down" and + * use an all integer value. + */ +void SetOrtho(void) +{ + glMatrixMode(GL_PROJECTION); // set matrixmode to projection + glLoadIdentity(); // reset + glOrthof32(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1 << 12, 1 << 12); // downscale projection matrix +} + +/* + * Initializes GL in 2D mode Also initializes GL in 3d mode so that we + * could combine 2D and 3D later Almost a direct copy from the DS + * example files + */ +void glScreen2D(void) +{ + // initialize gl + glInit(); + + // enable textures + glEnable(GL_TEXTURE_2D); + + // enable antialiasing + glEnable(GL_ANTIALIAS); + + // setup the rear plane + glClearColor(0, 0, 0, 31); // BG must be opaque for AA to work + glClearPolyID(63); // BG must have a unique polygon ID for AA to work + + glClearDepth(GL_MAX_DEPTH); + + // this should work the same as the normal gl call + glViewport(0,0,255,191); + + // any floating point gl call is being converted to fixed prior to being implemented + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(70, 256.0 / 192.0, 1, 200); + + gluLookAt( 0.0, 0.0, 1.0, //camera possition + 0.0, 0.0, 0.0, //look at + 0.0, 1.0, 0.0); //up + + glMaterialf(GL_AMBIENT, RGB15(31,31,31)); + glMaterialf(GL_DIFFUSE, RGB15(31,31,31)); + glMaterialf(GL_SPECULAR, BIT(15) | RGB15(31,31,31)); + glMaterialf(GL_EMISSION, RGB15(31,31,31)); + + // ds uses a table for shinyness..this generates a half-ass one + glMaterialShinyness(); + + // not a real gl function and will likely change + glPolyFmt(POLY_ALPHA(31) | POLY_CULL_BACK); +} + +/* + * Sets up OpenGL for 2d rendering Call this before drawing any of + * GL2D's drawing or sprite functions. + */ +void glBegin2D(void) +{ + // save 3d perpective projection matrix + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + + // save 3d modelview matrix for safety + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + + // what?!! No glDisable(GL_DEPTH_TEST)?!!!!!! + glEnable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + glDisable(GL_ANTIALIAS); // disable AA + glDisable(GL_OUTLINE); // disable edge-marking + + glColor(0x7FFF); // max color + + glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE); // no culling + + SetOrtho(); + + glMatrixMode(GL_TEXTURE); // reset texture matrix just in case we did some funky stuff with it + glLoadIdentity(); + + glMatrixMode(GL_MODELVIEW); // reset modelview matrix. No need to scale up by << 12 + glLoadIdentity(); + + gCurrentTexture = 0; // set current texture to 0 + g_depth = 0; // set depth to 0. We need this var since we cannot disable depth testing +} + +/* + * Issue this after drawing 2d so that we don't mess the matrix stack. + * The complement of glBegin2D. + */ +void glEnd2D(void) +{ + // restore 3d matrices and set current matrix to modelview + glMatrixMode(GL_PROJECTION); + glPopMatrix(1); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(1); +} + +/* + * Draws a pixel + * Parameters: + * x,y -> First coordinate of the line + * color -> RGB15/ARGB16 color + */ +void glPutPixel(int x, int y, int color) +{ + glBindTexture(0, 0); + glColor(color); + glBegin(GL_TRIANGLES); + gxVertex3i(x, y, g_depth); + gxVertex2i(x, y); + gxVertex2i(x, y); + glEnd(); + glColor(0x7FFF); + g_depth++; + gCurrentTexture = 0; +} + +/* + * Draws a line + * Parameters: + * x1,y1 -> First coordinate of the line + * x2,y2 -> Second coordinate of the line + * color -> RGB15/ARGB16 color + */ +void glLine(int x1, int y1, int x2, int y2, int color) +{ + x2++; + y2++; + + glBindTexture(0, 0); + glColor(color); + glBegin(GL_TRIANGLES); + gxVertex3i(x1, y1, g_depth); + gxVertex2i(x2, y2); + gxVertex2i(x2, y2); + glEnd(); + glColor(0x7FFF); + g_depth++; + gCurrentTexture = 0; +} + +/* + * Draws a Filled Box + * Parameters: + * x1,y1 -> Top-left corner of the box + * x2,y2 -> Bottom-Right corner of the box + * color -> RGB15/ARGB16 color +*/ +void glBoxFilled(int x1, int y1, int x2, int y2, int color) +{ + x2++; + y2++; + + glBindTexture(0, 0); + glColor(color); + glBegin(GL_QUADS); + gxVertex3i(x1, y1, g_depth); // use 3i for first vertex so that we increment HW depth + gxVertex2i(x1, y2); // no need for 3 vertices as 2i would share last depth call + gxVertex2i(x2, y2); + gxVertex2i(x2, y1); + glEnd(); + glColor(0x7FFF); + g_depth++; + gCurrentTexture = 0; +} + +/* + * + * Create a tile. + * Very rigid and prone to human error. + * + * Parameters: + * *sprite -> pointer to a glImage + * texture_width -> width/height of the texture; + * texture_height -> valid sizes are enumerated in GL_TEXTURE_TYPE_ENUM (see glTexImage2d) + * sprite_width + * sprite_height -> width/height of the picture in the texture. + * type -> The format of the texture (see glTexImage2d) + * param -> parameters for the texture (see glTexImage2d) + */ +int glLoadTile(glImage *sprite, + int texture_width, + int texture_height, + int sprite_width, + int sprite_height, + GL_TEXTURE_TYPE_ENUM type, + int param, + int pallette_width, + const u16 *palette, + const uint8 *texture) +{ + int textureID; + + glGenTextures(1, &textureID); + glBindTexture(0, textureID); + glTexImage2D(0, 0, type, texture_width, texture_height, 0, param, texture); + glColorTableEXT(0, 0, pallette_width, 0, 0, palette); + + sprite->width = sprite_width; + sprite->height = sprite_height; + sprite->textureID = textureID; + + return textureID; +} + +/* + * I made this since the scale wrappers are either the vectorized mode + * or does not permit you to scale only the axis you want to + * scale. Needed for sprite scaling. + */ +static inline void gxScalef32(s32 x, s32 y, s32 z) +{ + MATRIX_SCALE = x; + MATRIX_SCALE = y; + MATRIX_SCALE = z; +} + +/* + * I this made for future naming conflicts. + */ +static inline void gxTranslate3f32(int32 x, int32 y, int32 z) +{ + MATRIX_TRANSLATE = x; + MATRIX_TRANSLATE = y; + MATRIX_TRANSLATE = z; +} + +/* + * Draws an axis exclusive scaled sprite + * Parameters: + * x -> x position of the sprite + * y -> y position of the sprite + * scaleX -> 20.12 FP X axis scale value (1 << 12 is normal) + * scaleY -> 20.12 FP Y axis scale value (1 << 12 is normal) + * flipmode -> mode for flipping (see GL_FLIP_MODE enum) + * *spr -> pointer to a glImage + */ +void glSpriteScaleXY(int x, int y, s32 scaleX, s32 scaleY, int flipmode, const glImage *spr) +{ + const int x1 = 0; + const int y1 = 0; + const int x2 = spr->width; + const int y2 = spr->height; + const int u1 = ((flipmode & GL_FLIP_H) ? spr->width-1 : 0); + const int u2 = ((flipmode & GL_FLIP_H) ? 0 : spr->width-1); + const int v1 = ((flipmode & GL_FLIP_V) ? spr->height-1 : 0); + const int v2 = ((flipmode & GL_FLIP_V) ? 0 : spr->height-1); + + if (spr->textureID != gCurrentTexture) + { + glBindTexture(GL_TEXTURE_2D, spr->textureID); + gCurrentTexture = spr->textureID; + } + + glPushMatrix(); + + gxTranslate3f32(x, y, 0); + gxScalef32(scaleX, scaleY, 1 << 12); + + glBegin(GL_QUADS); + + gxTexcoord2i(u1, v1); gxVertex3i(x1, y1, g_depth); + gxTexcoord2i(u1, v2); gxVertex2i(x1, y2); + gxTexcoord2i(u2, v2); gxVertex2i(x2, y2); + gxTexcoord2i(u2, v1); gxVertex2i(x2, y1); + + glEnd(); + + glPopMatrix(1); + g_depth++; +} diff --git a/src/render/nds/SDL_libgl2D.h b/src/render/nds/SDL_libgl2D.h new file mode 100644 index 000000000..97bbebb0e --- /dev/null +++ b/src/render/nds/SDL_libgl2D.h @@ -0,0 +1,150 @@ +/* + * Note: The Nintendo DS port to SDL uses excerpts from the libGL2D, + * with permission of the original author. The following is mostly his + * code/comments. + * + * + * Easy GL2D + * + * Relminator 2010 + * Richard Eric M. Lope BSN RN + * + * http://rel.betterwebber.com + * + * A very small and simple DS rendering lib using the 3d core to render 2D stuff + */ + +#include + +/* LibGL extension(s) */ +static inline void gxTexcoord2i(t16 u, t16 v) +{ + GFX_TEX_COORD = (v << 20) | ((u << 4) & 0xFFFF); +} + +static inline void gxVertex3i(v16 x, v16 y, v16 z) +{ + GFX_VERTEX16 = (y << 16) | (x & 0xFFFF); + GFX_VERTEX16 = ((uint32)(uint16)z); +} + +static inline void gxVertex2i(v16 x, v16 y) +{ + GFX_VERTEX_XY = (y << 16) | (x & 0xFFFF); +} + +/* + * Enums selecting flipping mode. + * + * These enums are bits for flipping the sprites. + * You can "|" (or) GL_FLIP_V and GL_FLIP_H to flip + * both ways. + */ +typedef enum +{ + GL_FLIP_NONE = (1 << 0), /* No flipping */ + GL_FLIP_V = (1 << 1), /* Sprite is rendered vertically flipped */ + GL_FLIP_H = (1 << 2), /* Sprite is rendered horizontally flipped */ +} GL_FLIP_MODE; + +/* Struct for out GL-Based Images. */ +typedef struct +{ + int width; /* Width of the Sprite */ + int height; /* Height of the Sprite */ + int textureID; /* Texture handle (used in glDeleteTextures()) + The texture handle in VRAM (returned by glGenTextures()) + ie. This references the actual texture stored in VRAM. */ +} glImage; + +extern v16 g_depth; +extern int gCurrentTexture; + +/* + * Draws an Axis Exclusive Scaled Sprite + * Parameters: + * x X position of the sprite. + * y Y position of the sprite. + * scaleX 20.12 fixed-point X-Axis scale value (1 << 12 is normal). + * scaleY 20.12 fixed-point Y-Axis scale value (1 << 12 is normal). + * flipmode mode for flipping (see GL_FLIP_MODE enum). + * *spr pointer to a glImage. +*/ +void glSpriteScaleXY(int x, int y, s32 scaleX, s32 scaleY, int flipmode, const glImage *spr); + +/* Initializes our Tileset (like glInitSpriteset()) but without the use of Texture Packer auto-generated files. + * Can only be used when tiles in a tilset are of the same dimensions. + * Parameters: + * *sprite Pointer to an array of glImage. + * tile_wid Width of each tile in the texture. + * tile_hei Height of each tile in the texture. + * bmp_wid Width of of the texture or tileset. + * bmp_hei height of of the texture or tileset. + * type The format of the texture (see glTexImage2d()). + * sizeX The horizontal size of the texture; valid sizes are enumerated in GL_TEXTURE_TYPE_ENUM (see glTexImage2d()). + * sizeY The vertical size of the texture; valid sizes are enumerated in GL_TEXTURE_TYPE_ENUM (see glTexImage2d()). + * param parameters for the texture (see glTexImage2d()). + * pallette_width Length of the palette. Valid values are 4, 16, 32, 256 (if 0, then palette is removed from currently bound texture). + * *palette Pointer to the palette data to load (if NULL, then palette is removed from currently bound texture). + * *texture Pointer to the texture data to load. +*/ +int glLoadTile(glImage *sprite, + int texture_width, + int texture_height, + int sprite_width, + int sprite_height, + GL_TEXTURE_TYPE_ENUM type, + int param, + int pallette_width, + const u16 *palette, + const uint8 *texture); + +/* Initializes GL in 2D mode */ +void glScreen2D(void); + +/* + * Sets up OpenGL for 2d rendering. + * + * Call this before drawing any of GL2D's drawing or sprite functions. + */ +void glBegin2D(void); + +/* + * Issue this after drawing 2d so that we don't mess the matrix stack. + * + * The complement of glBegin2D(). + */ +void glEnd2D(void); + +/* + * Draws a Pixel + * x X position of the pixel. + * y Y position of the pixel. + * color RGB15/ARGB16 color. + */ +void glPutPixel(int x, int y, int color); + +/* + * Draws a Line + * x1,y1 Top-Left coordinate of the line. + * x2,y2 Bottom-Right coordinate of the line. + * color RGB15/ARGB16 color. + */ +void glLine(int x1, int y1, int x2, int y2, int color); + +/* + * Draws a Box + * x1,y1 Top-Left coordinate of the box. + * x2,y2 Bottom-Right coordinate of the box. + * color RGB15/ARGB16 color. +*/ +void glBox(int x1, int y1, int x2, int y2, int color); + +/* + * Draws a Filled Box + * x1,y1 Top-Left coordinate of the box. + * x2,y2 Bottom-Right coordinate of the box. + * color RGB15/ARGB16 color. + */ +void glBoxFilled(int x1, int y1, int x2, int y2, int color); + diff --git a/src/render/nds/SDL_ndsrender.c b/src/render/nds/SDL_ndsrender.c index 337004d5b..4666bb031 100755 --- a/src/render/nds/SDL_ndsrender.c +++ b/src/render/nds/SDL_ndsrender.c @@ -26,7 +26,7 @@ #include #include -#include +#include "SDL_libgl2D.h" #include "SDL_video.h" #include "../../video/SDL_sysvideo.h" @@ -34,6 +34,36 @@ #include "../SDL_sysrender.h" #include "SDL_log.h" +/* Draws a partial sprite. Based on glSprite. */ +static void glSpritePartial(const SDL_Rect * srcrect, int x, int y, int flipmode, const glImage *spr) +{ + int x1 = x; + int y1 = y; + int x2 = x + srcrect->w; + int y2 = y + srcrect->h; + + int u1 = srcrect->x + ((flipmode & GL_FLIP_H) ? spr->width-1 : 0); + int u2 = srcrect->x + ((flipmode & GL_FLIP_H) ? 0 : srcrect->h); + int v1 = srcrect->y + ((flipmode & GL_FLIP_V) ? spr->height-1 : 0); + int v2 = srcrect->y + ((flipmode & GL_FLIP_V) ? 0 : srcrect->h); + + if (spr->textureID != gCurrentTexture) { + glBindTexture(GL_TEXTURE_2D, spr->textureID); + gCurrentTexture = spr->textureID; + } + + glBegin(GL_QUADS); + + gxTexcoord2i(u1, v1); gxVertex3i(x1, y1, g_depth); + gxTexcoord2i(u1, v2); gxVertex2i(x1, y2); + gxTexcoord2i(u2, v2); gxVertex2i(x2, y2); + gxTexcoord2i(u2, v1); gxVertex2i(x2, y1); + + glEnd(); + + g_depth++; +} + /* SDL NDS renderer implementation */ extern SDL_RenderDriver NDS_RenderDriver; @@ -49,7 +79,6 @@ typedef struct glImage image[1]; } NDS_TextureData; - static int NDS_UpdateViewport(SDL_Renderer *renderer) { /* Nothing to do. */ @@ -70,9 +99,9 @@ NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, dest_y = dstrect->y-SCREEN_HEIGHT; } - if (texture->w == dstrect->w && texture->h == dstrect->h) { + if (srcrect->w == dstrect->w && srcrect->h == dstrect->h) { /* No scaling */ - glSprite(dstrect->x, dest_y, GL_FLIP_NONE, txdat->image); + glSpritePartial(srcrect, dstrect->x, dest_y, GL_FLIP_NONE, txdat->image); } else { /* Convert the scaling proportion into a 20.12 value. */ s32 scale_w = divf32(dstrect->w << 12, texture->w << 12); @@ -153,15 +182,42 @@ static int NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * rect, const void *pixels, int pitch) { NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata; + char *new_pixels = NULL; + const int gl_w = get_gltexture_size(rect->w); + const int gl_h = get_gltexture_size(rect->h); + const int w = 1 << (3+gl_w); /* Texture sizes must be a power of 2. */ + const int h = 1 << (3+gl_h); /* Texture sizes must be a power of 2. */ + + if (w != rect->w || h != rect->h) { + /* Allocate a temporary surface and copy pixels into it while + * enlarging the pitch. */ + const char *src; + char *dst; + int new_pitch = 2 * w; + int i; + + new_pixels = malloc(2 * w * h); + if (!new_pixels) + return SDL_ENOMEM; + + src = pixels; + dst = new_pixels; + for (i=0; ih; i++) { + memcpy(dst, src, pitch); + src += pitch; + dst += new_pitch; + } + } - glLoadTileSet(txdat->image, - rect->w, rect->h, + glLoadTile(txdat->image, + gl_w, gl_h, rect->w, rect->h, texture->format == SDL_PIXELFORMAT_ABGR1555 ? GL_RGBA : GL_RGB, - get_gltexture_size(rect->w), - get_gltexture_size(rect->h), TEXGEN_OFF, 0, NULL, - pixels); + new_pixels? new_pixels : pixels); + + if (new_pixels) + free(new_pixels); return 0; } @@ -193,7 +249,6 @@ static int NDS_RenderClear(SDL_Renderer *renderer) static void NDS_RenderPresent(SDL_Renderer * renderer) { NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata; - static int frame =0; glEnd2D(); @@ -324,18 +379,20 @@ NDS_CreateRenderer(SDL_Window * window, Uint32 flags) renderer->info = NDS_RenderDriver.info; renderer->info.flags = SDL_RENDERER_ACCELERATED; - renderer->UpdateViewport = NDS_UpdateViewport; - renderer->CreateTexture = NDS_CreateTexture; - renderer->DestroyTexture = NDS_DestroyTexture; - renderer->RenderCopy = NDS_RenderCopy; + renderer->CreateTexture = NDS_CreateTexture; renderer->UpdateTexture = NDS_UpdateTexture; renderer->LockTexture = NDS_LockTexture; renderer->UnlockTexture = NDS_UnlockTexture; + renderer->UpdateViewport = NDS_UpdateViewport; renderer->RenderClear = NDS_RenderClear; - renderer->RenderPresent = NDS_RenderPresent; renderer->RenderDrawPoints = NDS_RenderDrawPoints; renderer->RenderDrawLines = NDS_RenderDrawLines; renderer->RenderFillRects = NDS_RenderFillRects; + renderer->RenderCopy = NDS_RenderCopy; + /* renderer->RenderReadPixels = NDS_RenderReadPixels; - todo ? */ + renderer->RenderPresent = NDS_RenderPresent; + renderer->DestroyTexture = NDS_DestroyTexture; + /* renderer->DestroyRenderer = NDS_DestroyRenderer; - todo ? */ renderer->driverdata = data; diff --git a/src/video/nds/SDL_ndsvideo.c b/src/video/nds/SDL_ndsvideo.c index 42ed5ccab..53ffbe413 100755 --- a/src/video/nds/SDL_ndsvideo.c +++ b/src/video/nds/SDL_ndsvideo.c @@ -28,12 +28,12 @@ #include #include #include -#include #include "SDL_video.h" #include "SDL_ndsvideo.h" #include "SDL_ndsevents_c.h" #include "../../render/SDL_sysrender.h" +#include "../../render/nds/SDL_libgl2D.h" #include "SDL_log.h" #define NDSVID_DRIVER_NAME "nds" diff --git a/test/nds-test-progs/general/Makefile b/test/nds-test-progs/general/Makefile index 0f59a0e76..0dcaac884 100755 --- a/test/nds-test-progs/general/Makefile +++ b/test/nds-test-progs/general/Makefile @@ -28,7 +28,7 @@ ARCH := -mthumb -mthumb-interwork # note: arm9tdmi isn't the correct CPU arch, but anything newer and LD # *insists* it has a FPU or VFP, and it won't take no for an answer! CFLAGS := -save-temps -g -Wall -O0\ - -mcpu=arm9tdmi -mtune=arm9tdmi \ + -mcpu=arm9tdmi -mtune=arm9tdmi \ $(ARCH) CFLAGS += $(INCLUDE) -DARM9 -D__NDS__ @@ -41,23 +41,23 @@ LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -mno-fpu -Wl,-Map,$(notdir $*.map) # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- LIBS := -lSDL -lfat -lnds9 - - + + #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing # include and lib #--------------------------------------------------------------------------------- -LIBDIRS := $(LIBNDS) - +LIBDIRS := $(LIBNDS) $(PORTLIBS) + #--------------------------------------------------------------------------------- # no real need to edit anything past this point unless you need to add additional # rules for different file extensions #--------------------------------------------------------------------------------- ifneq ($(BUILD),$(notdir $(CURDIR))) #--------------------------------------------------------------------------------- - + export OUTPUT := $(CURDIR)/$(TARGET) - + export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ $(foreach dir,$(DATA),$(CURDIR)/$(dir)) @@ -67,7 +67,7 @@ CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) - + #--------------------------------------------------------------------------------- # use CXX for linking C++ projects, CC for standard C #--------------------------------------------------------------------------------- @@ -84,32 +84,31 @@ endif export OFILES := $(addsuffix .o,$(BINFILES)) \ $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) - + export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ - $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ -I$(CURDIR)/$(BUILD) - + export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) - + .PHONY: $(BUILD) clean - + #--------------------------------------------------------------------------------- $(BUILD): @[ -d $@ ] || mkdir -p $@ @make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile - + #--------------------------------------------------------------------------------- clean: @echo clean ... @rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds $(TARGET).arm9 $(TARGET).ds.gba - - + + #--------------------------------------------------------------------------------- else - + DEPENDS := $(OFILES:.o=.d) - + #--------------------------------------------------------------------------------- # main targets #--------------------------------------------------------------------------------- @@ -117,16 +116,16 @@ $(OUTPUT).ds.gba : $(OUTPUT).nds $(OUTPUT).nds : $(OUTPUT).arm9 $(OUTPUT).arm9 : $(OUTPUT).elf $(OUTPUT).elf : $(OFILES) - + #--------------------------------------------------------------------------------- %.pcx.o : %.pcx #--------------------------------------------------------------------------------- @echo $(notdir $<) @$(bin2o) - - + + -include $(DEPENDS) - + #--------------------------------------------------------------------------------------- endif #--------------------------------------------------------------------------------------- diff --git a/test/nds-test-progs/general/source/main.c b/test/nds-test-progs/general/source/main.c index b9c74c0ec..087dc4da1 100755 --- a/test/nds-test-progs/general/source/main.c +++ b/test/nds-test-progs/general/source/main.c @@ -1,39 +1,34 @@ - +/* + * Really basic sample for the NDS. + * + * Fills a rectangle increasingly smaller of random color every time a + * button (a, b, x, y) is pressed. + * + * The behaviour whether SDL is compiled with HW support or not (see + * USE_HW_RENDERER in Makefile.ds). + * + * In framebuffer mode, the old rectangles stay because the screen has + * not been cleared. + * + * In accelerated mode, old the last rectangle is visible. + * + * No text is displayed. + */ + #include #if defined(NDS) || defined(__NDS__) || defined (__NDS) #include #include #else -#define swiWaitForVBlank() #define consoleDemoInit() #define fatInitDefault() #define RGB15(r,g,b) SDL_MapRGB(screen->format,((r)<<3),((g)<<3),((b)<<3)) #endif - void -splash(SDL_Surface * screen, int s) -{ - SDL_Surface *logo; - SDL_Rect area = { 0, 0, 256, 192 }; - logo = SDL_LoadBMP("sdl.bmp"); - if (!logo) { - printf("Couldn't splash.\n"); - return; - } - /*logo->flags &= ~SDL_PREALLOC; */ - SDL_BlitSurface(logo, NULL, screen, &area); - SDL_Flip(screen); - while (s-- > 0) { - int i = 60; - while (--i) - swiWaitForVBlank(); - } -} - - int -main(void) +int main(void) { - SDL_Surface *screen; + SDL_Window *window; + SDL_Renderer *renderer; SDL_Joystick *stick; SDL_Event event; SDL_Rect rect = { 0, 0, 256, 192 }; @@ -41,48 +36,48 @@ main(void) consoleDemoInit(); puts("Hello world! Initializing FAT..."); - fatInitDefault(); - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { + + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { puts("# error initializing SDL"); puts(SDL_GetError()); return 1; } puts("* initialized SDL"); - screen = SDL_SetVideoMode(256, 192, 15, SDL_SWSURFACE); - if (!screen) { - puts("# error setting video mode"); - puts(SDL_GetError()); - return 2; + + if (SDL_CreateWindowAndRenderer(256, 192, SDL_RENDERER_ACCELERATED, &window, &renderer) < 0 && + SDL_CreateWindowAndRenderer(256, 192, SDL_RENDERER_SOFTWARE, &window, &renderer) < 0) { + exit(1); } - screen->flags &= ~SDL_PREALLOC; - puts("* set video mode"); + stick = SDL_JoystickOpen(0); if (stick == NULL) { puts("# error opening joystick"); puts(SDL_GetError()); -// return 3; } puts("* opened joystick"); - /*splash(screen, 3); */ - - SDL_FillRect(screen, &rect, RGB15(0, 0, 31) | 0x8000); - SDL_Flip(screen); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(renderer, &rect); + SDL_RenderPresent(renderer); while (1) while (SDL_PollEvent(&event)) switch (event.type) { case SDL_JOYBUTTONDOWN: - SDL_FillRect(screen, &rect, (u16) rand() | 0x8000); - SDL_Flip(screen); + + SDL_SetRenderDrawColor(renderer, rand(), rand(), rand(), SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(renderer, &rect); + SDL_RenderPresent(renderer); if (rect.w > 8) { rect.x += 4; rect.y += 3; rect.w -= 8; rect.h -= 6; } - printf("button %d pressed at %d ticks\n", - event.jbutton.button, SDL_GetTicks()); + /* + printf("button %d pressed at %d ticks\n", + event.jbutton.button, SDL_GetTicks()); + */ break; case SDL_QUIT: SDL_Quit(); @@ -90,7 +85,5 @@ main(void) default: break; } - return 0; - } - - + return 0; +} diff --git a/test/nds-test-progs/testscale/Makefile b/test/nds-test-progs/testscale/Makefile index d26167b75..31a123220 100644 --- a/test/nds-test-progs/testscale/Makefile +++ b/test/nds-test-progs/testscale/Makefile @@ -42,7 +42,7 @@ LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project (order is important) #--------------------------------------------------------------------------------- -LIBS := -lSDL -lgl2d -lfat -lnds9 +LIBS := -lSDL -lfat -lnds9 #--------------------------------------------------------------------------------- diff --git a/test/nds-test-progs/testsprite2/Makefile b/test/nds-test-progs/testsprite2/Makefile index c43663b5a..b765e6c84 100644 --- a/test/nds-test-progs/testsprite2/Makefile +++ b/test/nds-test-progs/testsprite2/Makefile @@ -42,7 +42,7 @@ LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project (order is important) #--------------------------------------------------------------------------------- -LIBS := -lSDL -lgl2d -lfat -lnds9 +LIBS := -lSDL -lfat -lnds9 #--------------------------------------------------------------------------------- diff --git a/test/nds-test-progs/testspriteminimal/Makefile b/test/nds-test-progs/testspriteminimal/Makefile index 68b276223..e54101f9e 100644 --- a/test/nds-test-progs/testspriteminimal/Makefile +++ b/test/nds-test-progs/testspriteminimal/Makefile @@ -42,7 +42,7 @@ LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project (order is important) #--------------------------------------------------------------------------------- -LIBS := -lSDL -lgl2d -lfat -lnds9 +LIBS := -lSDL -lfat -lnds9 #---------------------------------------------------------------------------------