Permalink
Browse files

Added examples sprite256 and palettes

  • Loading branch information...
1 parent f8ae169 commit 2448fe6b27a79f6c2b19f42e39242c577c1253d5 @madprog committed Apr 29, 2012
Showing with 1,846 additions and 7 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 include/SPI.h
  3. +108 −3 src/GD.cpp
  4. +15 −4 test/CMakeLists.txt
  5. +59 −0 test/palettes.cpp
  6. +922 −0 test/pickups2.h
  7. +79 −0 test/sprites256.cpp
  8. +658 −0 test/sprites256.h
View
@@ -1,6 +1,8 @@
/CMakeCache.txt
+/test/palettes
/test/scroll
/test/snake
+/test/sprites256
CMakeFiles/
Makefile
cmake_install.cmake
View
@@ -285,6 +285,9 @@ void loop();
#define pgm_read_byte_near(x) (*(x))
+#define lowByte(x) ((x) & 0x00ff)
+#define highByte(x) (((x) & 0xff00) >> 8)
+
int random(int x, int y);
inline int random(int y) { return random(0, y); }
View
@@ -14,6 +14,14 @@ static byte RAM[RAM_SIZE];
void GDClass::begin() {
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(WINDOW_WIDTH * WINDOW_ZOOM, WINDOW_HEIGHT * WINDOW_ZOOM, 15, SDL_HWSURFACE|SDL_DOUBLEBUF);
+
+ // Hide all sprites
+ for(int spr = 0; spr < 512; ++ spr) {
+ *(RAM + RAM_SPR + (spr << 2) + 0) = lowByte(400);
+ *(RAM + RAM_SPR + (spr << 2) + 1) = highByte(400);
+ *(RAM + RAM_SPR + (spr << 2) + 2) = lowByte(400);
+ *(RAM + RAM_SPR + (spr << 2) + 3) = highByte(400);
+ }
}
void GDClass::end() {
@@ -86,7 +94,11 @@ void GDClass::setpal(int pal, unsigned int rgb) {
}
void GDClass::sprite(int spr, int x, int y, byte image, byte palette, byte rot, byte jk) {
- // TODO
+ byte *sprite_ptr = RAM + RAM_SPR + 4 * spr;
+ sprite_ptr[0] = x & 0x00ff;
+ sprite_ptr[1] = ((palette & 0x0f) << 4) | ((rot & 0x07) << 1) | ((x & 0x0100) >> 8);
+ sprite_ptr[2] = y & 0x00ff;
+ sprite_ptr[3] = ((jk & 0x01) << 7) | ((image & 0x3f) << 1) | ((y & 0x0100) >> 8);
}
void redraw_background(SDL_Surface *surface) {
@@ -113,13 +125,105 @@ void redraw_background(SDL_Surface *surface) {
&& first_pixel_y + py >= 0 && first_pixel_y + py < WINDOW_HEIGHT) {
for(int i = 0; i < WINDOW_ZOOM; ++ i) {
for(int j = 0; j < WINDOW_ZOOM; ++ j) {
- first_pixel[WINDOW_WIDTH * WINDOW_ZOOM * (WINDOW_ZOOM * py + j) + WINDOW_ZOOM * px + i] = color_data[color_index];
+ if(!(color_data[color_index] & 0x8000)) {
+ first_pixel[WINDOW_WIDTH * WINDOW_ZOOM * (WINDOW_ZOOM * py + j) + WINDOW_ZOOM * px + i] = color_data[color_index];
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+void redraw_sprites(SDL_Surface *surface) {
+ uint16_t *pixels = (uint16_t *)surface->pixels;
+
+ for(int spr_ = 0; spr_ < 256; ++ spr_) {
+ byte *sprite_ptr = RAM + RAM_SPR + 4 * spr_;
+ int x, y;
+ byte rot, palette, image, jk;
+ uint16_t palette_nb_colors = 0;
+ uint16_t *palette_ptr;
+ byte palette_mask, palette_shift;
+ byte *sprite_image;
+
+ x = sprite_ptr[0] | ((sprite_ptr[1] & 0x01) << 8);
+ y = sprite_ptr[2] | ((sprite_ptr[3] & 0x01) << 8);
+ rot = (sprite_ptr[1] & 0x0e) >> 1;
+ palette = (sprite_ptr[1] & 0xf0) >> 4;
+ image = (sprite_ptr[3] & 0x7e) >> 1;
+ jk = (sprite_ptr[3] & 0x80) >> 7;
+
+ if(palette & B00001000) {
+ // 2-bit mode
+ palette_nb_colors = 4;
+ if(palette & 0x01) {
+ palette_ptr = (uint16_t *)(RAM + PALETTE4B);
+ } else {
+ palette_ptr = (uint16_t *)(RAM + PALETTE4A);
+ }
+ switch((palette & 0x06) >> 1) {
+ case 0:
+ palette_mask = 0x03;
+ palette_shift = 0;
+ break;
+ case 1:
+ palette_mask = 0x0c;
+ palette_shift = 2;
+ break;
+ case 2:
+ palette_mask = 0x30;
+ palette_shift = 4;
+ break;
+ case 3:
+ palette_mask = 0xc0;
+ palette_shift = 6;
+ break;
+ }
+ } else if(palette & B00000100) {
+ // 4-bit mode
+ palette_nb_colors = 16;
+ if(palette & 0x01) {
+ palette_ptr = (uint16_t *)(RAM + PALETTE16B);
+ } else {
+ palette_ptr = (uint16_t *)(RAM + PALETTE16A);
+ }
+ if(palette & 0x02) {
+ palette_mask = 0xf0;
+ palette_shift = 4;
+ } else {
+ palette_mask = 0x0f;
+ palette_shift = 0;
+ }
+ } else {
+ // 8-bit mode
+ palette_nb_colors = 256;
+ palette_ptr = (uint16_t *)(RAM + RAM_SPRPAL + ((palette & 0x03) << 8));
+ palette_mask = 0xff;
+ palette_shift = 0;
+ }
+
+ sprite_image = RAM + RAM_SPRIMG + ((image & 0x3f) << 8);
+
+ for(int spr_y = 0; spr_y < 16; ++ spr_y) {
+ for(int spr_x = 0; spr_x < 16; ++ spr_x) {
+ if(((x + spr_x) & 0x1ff) >= 0
+ && ((x + spr_x) & 0x1ff) < WINDOW_WIDTH
+ && ((y + spr_y) & 0x1ff) >= 0
+ && ((y + spr_y) & 0x1ff) < WINDOW_HEIGHT) {
+ for(int j = 0; j < WINDOW_ZOOM; ++ j) {
+ for(int i = 0; i < WINDOW_ZOOM; ++ i) {
+ byte color_index = (sprite_image[(spr_y << 4) | spr_x] & palette_mask) >> palette_shift;
+ uint16_t color = palette_ptr[color_index];
+ if(!(color & 0x8000)) {
+ pixels[(((y + spr_y) & 0x1ff) * WINDOW_ZOOM + j) * WINDOW_ZOOM * WINDOW_WIDTH + ((x + spr_x) & 0x1ff) * WINDOW_ZOOM + i] = color;
}
}
}
}
}
- //exit(0);
}
}
}
@@ -146,6 +250,7 @@ int main() {
SDL_LockSurface(surface);
redraw_background(surface);
+ redraw_sprites(surface);
SDL_UnlockSurface(surface);
SDL_Flip(surface);
View
@@ -1,12 +1,23 @@
+include_directories(../include)
+
add_executable(snake
main.cpp
snake.cpp
walls.cpp)
-add_executable(scroll
- scroll.cpp)
-include_directories(gdemu
- ../include)
target_link_libraries(snake
gdemu)
+
+add_executable(scroll
+ scroll.cpp)
target_link_libraries(scroll
gdemu)
+
+add_executable(palettes
+ palettes.cpp)
+target_link_libraries(palettes
+ gdemu)
+
+add_executable(sprites256
+ sprites256.cpp)
+target_link_libraries(sprites256
+ gdemu)
View
@@ -0,0 +1,59 @@
+#include <SPI.h>
+#include <GD.h>
+
+void spr(int x, int y, byte pal)
+{
+ static int sprnum = 0;
+ GD.sprite(sprnum++, x, y, 0, pal, 0);
+}
+
+static unsigned int random_color()
+{
+ return RGB(64 + random(192), 64 + random(192), 64 + random(192));
+}
+
+void setup()
+{
+ int i;
+
+ GD.begin();
+ GD.ascii();
+ GD.putstr(0, 0,"Sprite palettes");
+
+ for (i = 0; i < 256; i++)
+ GD.wr(RAM_SPRIMG + i, i);
+
+ // Fill all the palettes with random colors
+ for (i = 0; i < (4 * 256); i++)
+ GD.wr16(RAM_SPRPAL + (i << 1), random_color());
+ for (i = 0; i < 16; i++) {
+ GD.wr16(PALETTE16A + (i << 1), random_color());
+ GD.wr16(PALETTE16B + (i << 1), random_color());
+ }
+ for (i = 0; i < 4; i++) {
+ GD.wr16(PALETTE4A + (i << 1), random_color());
+ GD.wr16(PALETTE4B + (i << 1), random_color());
+ }
+
+ GD.putstr(0, 8, "Four 256-color palettes");
+ for (i = 0; i < 4; i++)
+ spr(200 + 20 * i, (8 * 8), i);
+
+ GD.putstr(0, 12, "Two 16-color palettes");
+ for (i = 0; i < 2; i++) {
+ spr(200 + 20 * i, (8 * 12), 0x4 | i);
+ spr(200 + 20 * i, (8 * 12) + 20, 0x6 | i);
+ }
+
+ GD.putstr(0, 18, "Two 4-color palettes");
+ for (i = 0; i < 2; i++) {
+ spr(200 + 20 * i, (8 * 18), 0x8 | i);
+ spr(200 + 20 * i, (8 * 18) + 20, 0xa | i);
+ spr(200 + 20 * i, (8 * 18) + 40, 0xc | i);
+ spr(200 + 20 * i, (8 * 18) + 60, 0xe | i);
+ }
+}
+
+void loop()
+{
+}
Oops, something went wrong.

0 comments on commit 2448fe6

Please sign in to comment.