galaxy.c (22707B)
1/* 2 * C version of the 'space' assembly demo. 3 * 4 * Little demo illustrating how to use the graphical possibilities 5 * of the GB (background, window and animated sprite) 6 * I have used fixed-point values for both the position and 7 * speed of objects to get smooth movements 8 * 9 * OBJ data : 0x8000 -> 0x8FFF (unsigned) 10 * Window data : 0x8800 -> 0x97FF (unsigned) 11 * Background data : 0x8800 -> 0x97FF (signed) 12 * 13 * Tiled 0xFC -> 0xFF are standard tiles (all black -> all white) 14 * 15 * Keys: 16 * Arrow keys : Change the speed (and direction) of the sprite 17 * Arrow keys + A : Change the speed (and direction) of the window 18 * Arrow keys + B : Change the speed (and direction) of the background 19 * START : Open/close the door 20 * SELECT : Basic fading effect 21 * 22 * Note that the window is kept in the lower right part of the screen 23 * since it can't be made transparent 24 */ 25 26#include <gb/gb.h> 27#include <stdint.h> 28 29const unsigned char std_data[] = { 30 31 /* Basic tiles (0xFC to 0xFF) */ 32 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 33 0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00, 34 0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF, 35 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 36}; 37 38const unsigned char earth_data[] = { 39 40 /* Tile 0x00 */ 41 0x07,0x07,0x18,0x1F,0x32,0x2D,0x71,0x4E,0x70,0x4F,0xF8,0x87,0xF8,0x87,0xF8,0x87, 42 0xFC,0x83,0xFE,0x81,0x7F,0x40,0x7F,0x40,0x3F,0x20,0x1F,0x18,0x07,0x07,0x00,0x00, 43 0xC0,0xC0,0xF0,0x30,0x78,0x88,0x3C,0xC4,0x5C,0xA4,0x9E,0x62,0x3E,0xC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00, 59 60 /* Tile 0x10 */ 61 0x07,0x07,0x18,0x1F,0x20,0x3F,0x40,0x7F,0x40,0x7F,0xEE,0x91,0xF1,0x8E,0xE0,0x9F, 62 0xE0,0x9F,0xF1,0x8E,0x71,0x4E,0x72,0x4D,0x3F,0x20,0x1F,0x18,0x07,0x07,0x00,0x00, 63 0xC0,0xC0,0xF0,0x30,0x08,0xF8,0x04,0xFC,0x04,0xFC,0x02,0xFE,0x02,0xFE,0x92,0x6E, 64 0xD6,0x2A,0xFE,0x02,0xEC,0x14,0xFC,0x04,0xF8,0x08,0xF0,0x30,0xC0,0xC0,0x00,0x00, 65 0x07,0x07,0x1D,0x1A,0x36,0x29,0x5C,0x63,0x6C,0x53,0xCE,0xB1,0x9F,0xE0,0x9E,0xE1, 66 0xAE,0xD1,0xBF,0xC0,0x47,0x78,0x47,0x78,0x2F,0x30,0x1F,0x18,0x07,0x07,0x00,0x00, 67 0xC0,0xC0,0x70,0xB0,0x08,0xF8,0x04,0xFC,0x04,0xFC,0xE2,0x1E,0x32,0xCE,0x0E,0xF2, 68 0x0E,0xF2,0x1E,0xE2,0x1C,0xE4,0x2C,0xD4,0xF8,0x08,0xF0,0x30,0xC0,0xC0,0x00,0x00, 69 70 /* Tile 0x18 */ 71 0x07,0x07,0x1E,0x19,0x33,0x2C,0x49,0x76,0x42,0x7D,0xC4,0xBB,0xC1,0xBE,0xC1,0xBE, 72 0xE2,0x9D,0xF3,0x8C,0x78,0x47,0x78,0x47,0x3C,0x23,0x1C,0x1B,0x07,0x07,0x00,0x00, 73 0xC0,0xC0,0x70,0xB0,0x68,0x98,0xC4,0x3C,0xC4,0x3C,0xEE,0x12,0xF2,0x0E,0xE2,0x1E, 74 0xE2,0x1E,0xF2,0x0E,0x7C,0x84,0x7C,0x84,0xF8,0x08,0xF0,0x30,0xC0,0xC0,0x00,0x00 75}; 76 77const unsigned char frame_data[] = { 78 79 /* Tile 0x00 */ 80 0xFF,0x00,0x80,0x7F,0x80,0x7F,0x80,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F1,0x0F,0xE1,0x1F,0xC1,0x3F,0x81,0x7F, 96 0x80,0x7F,0x80,0x7F,0x80,0x7F,0x80,0x7F,0x80,0x7F,0x80,0x7F,0x80,0x7F,0x80,0x7F, 97 0x01,0xFF,0x01,0xFF,0x03,0xFD,0x07,0xF9,0x0F,0xF1,0x1F,0xE1,0x3F,0xC1,0x7F,0x81, 98 99 /* Tile 0x10 */ 100 0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01, 101 0x01,0xFF,0x01,0xFE,0x03,0xFC,0x77,0xF8,0xFF,0x98,0xEF,0xB8,0xCF,0xF8,0x7F,0xF0, 102 0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x0E,0xFF,0x13,0xFD,0x17,0xF9,0x1F,0xFF,0x0E, 103 0x80,0x7F,0x81,0x7E,0x83,0x7C,0x87,0x78,0x8F,0x70,0x9F,0x60,0xBF,0x40,0xFF,0x7F, 104 0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,0xFF,0xFF, 105 106 /* Door1 */ 107 108 /* Tile 0x15 */ 109 0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF, 110 0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF, 111 0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF, 112 113 /* Door2 */ 114 115 /* Tile 0x18 */ 116 0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF, 117 0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF, 118 0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF, 119 0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 120 121 /* Door3 */ 122 123 /* Tile 0x1C */ 124 0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF, 125 0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF, 126 0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0x00,0xFF,0x00,0xFF, 127 0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 128 129 /* Door4 */ 130 131 /* Tile 0x20 */ 132 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF 133}; 134 135const unsigned char bkg_data[] = { 136 137 /* Tile 0x00 */ 138 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xEF,0xFF,0xFF,0xFF,0xFF, 139 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xDF,0xFF,0xFF,0xFF,0xFF, 140 0xFF,0xFF,0xFF,0xFB,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 141 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF7,0xF7,0xFF,0xFF, 142 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF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0xFF,0xFF,0xFF,0xFF,0xFF, 189 0xFF,0xFF,0xFF,0xFF,0xFF,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 190 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF, 191 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 192 0xFF,0xFF,0xFF,0xFF,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF 193}; 194 195/* 196 * Image size: 0x40 x 0x40 197 * Number of tiles (total - unique): 0x40 - 0x2D 198 */ 199 200const unsigned char bkg_tiles[] = { 201 0x00,0x01,0x02,0x03,0xFC,0xFC,0x04,0xFC, 202 0xFC,0x05,0x06,0xFC,0x07,0x08,0x09,0x0A, 203 0xFC,0xFC,0xFC,0x02,0x0B,0x0C,0x0D,0xFC, 204 0x0E,0x0F,0x10,0xFC,0x11,0x12,0x13,0x14, 205 0x15,0x16,0x17,0xFC,0x18,0x19,0x1A,0xFC, 206 0x1B,0x1C,0x1D,0xFC,0xFC,0x1E,0x1F,0x20, 207 0x21,0x22,0xFC,0x23,0x24,0x25,0xFC,0x26, 208 0x27,0x13,0x28,0x29,0x2A,0x2B,0x2C,0x11 209}; 210 211/* 212 * Image size: 0x10 x 0x70 213 * Number of tiles (total - unique): 0x1C - 0x1C 214 */ 215 216const unsigned char earth_tiles[] = { 217 0x00,0x02, 218 0x04,0x06, 219 0x08,0x0A, 220 0x0C,0x0E, 221 0x10,0x12, 222 0x14,0x16, 223 0x18,0x1A 224}; 225 226/* 227 * Image size: 0x80 x 0x50 228 * Number of tiles (total - unique): 0xA0 - 0x15 229 */ 230 231const unsigned char frame_tiles[] = { 232 0x80,0x81,0xFD,0x82,0x83,0x81,0xFD,0x82,0x83,0x81,0xFD,0x82,0x83,0x81,0xFD,0x84, 233 0x85,0x86,0x87,0x88,0x89,0x8A,0x87,0x88,0x89,0x8A,0x87,0x88,0x89,0x8A,0x8B,0x8C, 234 0xFD,0x8D,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x8E,0x8F, 235 0x82,0x8C,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x85,0x90, 236 0x8E,0x8F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,0x8D, 237 0x85,0x90,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x82,0x8C, 238 0xFD,0x8D,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x8E,0x8F, 239 0x82,0x8C,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x85,0x90, 240 0x8E,0x91,0xFD,0x82,0x83,0x81,0xFD,0x82,0x83,0x81,0xFD,0x82,0x83,0x81,0x92,0x8D, 241 0x93,0x8A,0x87,0x88,0x89,0x8A,0x87,0x88,0x89,0x8A,0x87,0x88,0x89,0x8A,0x87,0x94 242}; 243 244/* 245 * Image size: 0x60 x 0x30 246 * Number of tiles (total - unique): 0x48 - 0x03 247 */ 248 249const unsigned char door1_tiles[] = { 250 0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95, 251 0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96, 252 0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97, 253 0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95, 254 0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96, 255 0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97, 256 257 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 258 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 259 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 260 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 261 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 262 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC 263}; 264 265/* 266 * Image size: 0x60 x 0x30 267 * Number of tiles (total - unique): 0x48 - 0x04 268 */ 269 270const unsigned char door2_tiles[] = { 271 0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98, 272 0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99, 273 0x9A,0x9A,0x9A,0x9A,0x9A,0x9A,0x9A,0x9A,0x9A,0x9A,0x9A,0x9A, 274 0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98, 275 0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99, 276 0x9B,0x9B,0x9B,0x9B,0x9B,0x9B,0x9B,0x9B,0x9B,0x9B,0x9B,0x9B, 277 278 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 279 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 280 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 281 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 282 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 283}; 284 285/* 286 * Image size: 0x60 x 0x30 287 * Number of tiles (total - unique): 0x48 - 0x04 288 */ 289 290const unsigned char door3_tiles[] = { 291 0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C, 292 0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D, 293 0x9E,0x9E,0x9E,0x9E,0x9E,0x9E,0x9E,0x9E,0x9E,0x9E,0x9E,0x9E, 294 0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C,0x9C, 295 0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D,0x9D, 296 0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F, 297 298 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 299 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 300 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 301 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 302 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC 303}; 304 305/* 306 * Image size: 0x60 x 0x30 307 * Number of tiles (total - unique): 0x48 - 0x01 308 */ 309 310const unsigned char door4_tiles[] = { 311 0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95, 312 0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96, 313 0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97,0x97, 314 0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95,0x95, 315 0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96,0x96, 316 0xA0,0xA0,0xA0,0xA0,0xA0,0xA0,0xA0,0xA0,0xA0,0xA0,0xA0,0xA0, 317 318 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 319 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 320 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 321 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC, 322 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC 323}; 324 325/* Should really be const, but sdcc doesnt yet support it. */ 326const unsigned char * const film[] = { 327 &door1_tiles[0x0C*0], 328 &door2_tiles[0x0C*0], 329 &door3_tiles[0x0C*0], 330 &door4_tiles[0x0C*0], 331 &door1_tiles[0x0C*1], 332 &door2_tiles[0x0C*1], 333 &door3_tiles[0x0C*1], 334 &door4_tiles[0x0C*1], 335 &door1_tiles[0x0C*2], 336 &door2_tiles[0x0C*2], 337 &door3_tiles[0x0C*2], 338 &door4_tiles[0x0C*2], 339 &door1_tiles[0x0C*3], 340 &door2_tiles[0x0C*3], 341 &door3_tiles[0x0C*3], 342 &door4_tiles[0x0C*3], 343 &door1_tiles[0x0C*4], 344 &door2_tiles[0x0C*4], 345 &door3_tiles[0x0C*4], 346 &door4_tiles[0x0C*4], 347 &door1_tiles[0x0C*5], 348 &door2_tiles[0x0C*5], 349 &door3_tiles[0x0C*5], 350 &door4_tiles[0x0C*5], 351 &door1_tiles[0x0C*6] 352}; 353 354#define NBDFRAMES 0x18 /* Nb frames for the door */ 355#define NBSFRAMES 0x07 /* Nb frames for the sprite */ 356#define WINSZX 0x80 /* Size of the picture in the window */ 357#define WINSZY 0x50 358#define MINWINX (MAXWNDPOSX-WINSZX+1) /* Bounds of the window origin */ 359#define MINWINY (MAXWNDPOSY-WINSZY+1) 360#define MAXWINX MAXWNDPOSX 361#define MAXWINY MAXWNDPOSY 362#define FADESTEP 0x10 /* Nb steps for the fading effect */ 363#define STARTFADE (0x06*FADESTEP) /* Initial value for the fading effect */ 364 365#define CLOSED 0x00 366#define OPENING 0x01 367#define OPENED 0x02 368#define CLOSING 0x03 369 370static uint8_t time = 0; /* Global "time" value (counter) */ 371uint8_t doorstate = 0; /* State of the door (OPENED, CLOSED...) */ 372uint8_t doorpos = 0; /* Current position in the door animation */ 373static uint8_t color = 0; /* Current color for fading effect */ 374uint8_t sframe = 0; /* Current frame of the sprite */ 375fixed bposx, bposy; /* Background position (fixed point) */ 376fixed bspx, bspy; /* Background speed (fixed point) */ 377fixed wposx, wposy; /* Window position (fixed point) */ 378fixed wspx, wspy; /* Window speed (fixed point) */ 379fixed sposx, sposy; /* Sprite position (fixed point) */ 380fixed sspx, sspy; /* Sprite speed (fixed point) */ 381 382void fade(); 383void scroll(); 384void door(); 385void animate_sprite(); 386void tile_sprite(); 387void place_sprite(); 388 389/* Fade the screen (off and on) */ 390void fade() 391{ 392 if(color == 0) 393 return; 394 switch(color) 395 { 396 case STARTFADE: 397 case STARTFADE-4*FADESTEP: 398 BGP_REG = 0xF9U; 399 break; 400 case STARTFADE-FADESTEP: 401 case STARTFADE-3*FADESTEP: 402 BGP_REG = 0xFEU; 403 break; 404 case STARTFADE-2*FADESTEP: 405 BGP_REG = 0xFFU; 406 break; 407 case STARTFADE-5*FADESTEP: 408 BGP_REG = 0xE4U; 409 break; 410 } 411 color--; 412} 413 414/* Scroll the background, the window and the sprite */ 415void scroll() 416{ 417 /* Update background */ 418 bposx.w += bspx.w; 419 bposy.w += bspy.w; 420 SCX_REG = bposx.b.h; 421 SCY_REG = bposy.b.h; 422 423 /* Update window */ 424 wposx.w += wspx.w ; 425 wposy.w += wspy.w ; 426 /* X position */ 427 if(wposx.b.h >= MAXWINX) { 428 wposx.b.h = MAXWINX; 429 /* Invert speed */ 430 wspx.w = -(int16_t)wspx.w; 431 } else if(wposx.b.h <= MINWINX) { 432 wposx.b.h = MINWINX; 433 /* Invert speed */ 434 wspx.w = -(int16_t)wspx.w; 435 } 436 WX_REG = wposx.b.h; 437 /* Y position */ 438 if(wposy.b.h >= MAXWINY) { 439 wposy.b.h = MAXWINY; 440 /* Invert speed */ 441 wspy.w = -(int16_t)wspy.w; 442 } else if(wposy.b.h <= MINWINY) { 443 wposy.b.h = MINWINY; 444 /* Invert speed */ 445 wspy.w = -(int16_t)wspy.w; 446 } 447 WY_REG = wposy.b.h; 448 449 /* Update sprite */ 450 sposx.w += sspx.w; 451 sposy.w += sspy.w; 452 place_sprite(); 453} 454 455/* Open and close the door */ 456void door() 457{ 458 if(doorstate == OPENING) { 459 doorpos++; 460 /* Draw the door in the window */ 461 set_win_tiles(2, 2, 12, 6, (unsigned char*)film[doorpos]); 462 if(doorpos == NBDFRAMES) 463 doorstate = OPENED; 464 } else if(doorstate == CLOSING) { 465 doorpos--; 466 /* Draw the door in the window */ 467 set_win_tiles(2, 2, 12, 6, (unsigned char*)film[doorpos]); 468 if(doorpos == 0) 469 doorstate = CLOSED; 470 } 471} 472 473/* Animate sprite */ 474void animate_sprite() 475{ 476 if((time&0x07) == 0) { 477 sframe++; 478 if(sframe == NBSFRAMES) 479 sframe = 0; 480 tile_sprite(); 481 } 482} 483 484/* Set sprite tiles */ 485void tile_sprite() 486{ 487 uint8_t s; 488 489 s = sframe<<1; 490 set_sprite_tile(0, earth_tiles[s]); 491 set_sprite_tile(1, earth_tiles[s+1]); 492} 493 494/* Place sprite */ 495void place_sprite() 496{ 497 move_sprite(0, sposx.b.h, sposy.b.h); 498 move_sprite(1, sposx.b.h+8, sposy.b.h); 499} 500 501void main() 502{ 503 uint8_t i, j; 504 505 disable_interrupts(); 506 DISPLAY_OFF; 507 LCDC_REG = LCDCF_OFF | LCDCF_WIN9C00 | LCDCF_WINON | LCDCF_BG8800 | LCDCF_BG9800 | LCDCF_OBJ16 | LCDCF_OBJON | LCDCF_BGON; 508 /* 509 * LCD = Off 510 * WindowBank = 0x9C00 511 * Window = On 512 * BG Chr = 0x8800 513 * BG Bank = 0x9800 514 * OBJ = 8x16 515 * OBJ = On 516 * BG = On 517 */ 518 519 doorstate = CLOSED; 520 521 /* Set palettes */ 522 BGP_REG = OBP0_REG = OBP1_REG = 0xE4U; 523 524 /* Initialize the background */ 525 set_bkg_data(0xFC, 0x04, std_data); 526 set_bkg_data(0x00, 0x2D, bkg_data); 527 /* 528 * Draw the background 529 * 530 * Width = 0x100 = 0x20 * 8 531 * Height = 0x100 = 0x20 * 8 532 */ 533 for(i = 0; i < 32; i+=8) 534 for(j = 0; j < 32; j+=8) 535 set_bkg_tiles(i, j, 8, 8, bkg_tiles); 536 bposx.w = 0; 537 SCX_REG = 0; 538 bposy.w = 0; 539 SCY_REG = 0; 540 bspx.w = 0xFF00; 541 bspy.w = 0x0080; 542 543 /* Initialize the window */ 544 set_win_data(0x80, 0x21, frame_data); 545 /* 546 * Draw the frame in the window 547 * 548 * Width = 0x80 = 0x10 * 8 549 * Height = 0x50 = 0x0A * 8 550 */ 551 set_win_tiles(0, 0, 16, 10, frame_tiles); 552 /* 553 * Draw the door in the window 554 * 555 * Width = 0x60 = 0x20 * 12 556 * Height = 0x30 = 0x20 * 6 557 */ 558 set_win_tiles(2, 2, 12, 6, door1_tiles); 559 wposx.b.h = MAXWNDPOSX; 560 wposx.b.l = 0; 561 WX_REG = MAXWNDPOSX; 562 wposy.b.h = MAXWNDPOSY; 563 wposy.b.l = 0; 564 WY_REG = MAXWNDPOSY; 565 wspx.w = 0xFF80; 566 wspy.w = 0xFFC0; 567 568 /* Initialize the sprite */ 569 set_sprite_data(0x00, 0x1C, earth_data); 570 set_sprite_prop(0, 0x00); 571 set_sprite_prop(1, 0x00); 572 sframe = 0; 573 sposx.w = 0x1000; 574 sposy.w = 0x1000; 575 sspx.w = 0x0040; 576 sspy.w = 0x0040; 577 tile_sprite(); 578 place_sprite(); 579 580 DISPLAY_ON; 581 enable_interrupts(); 582 583 while(1) { 584 /* Skip four VBLs (slow down animation) */ 585 for(i = 0; i < 4; i++) 586 wait_vbl_done(); 587 time++; 588 fade(); 589 door(); 590 scroll(); 591 animate_sprite(); 592 i = joypad(); 593 if(i & J_B) { 594 if(i & J_UP) 595 bspy.w -= 0x0010; 596 if(i & J_DOWN) 597 bspy.w += 0x0010; 598 if(i & J_LEFT) 599 bspx.w -= 0x0010; 600 if(i & J_RIGHT) 601 bspx.w += 0x0010; 602 } else if(i & J_A) { 603 if(i & J_UP) 604 wspy.w -= 0x0010; 605 if(i & J_DOWN) 606 wspy.w += 0x0010; 607 if(i & J_LEFT) 608 wspx.w -= 0x0010; 609 if(i & J_RIGHT) 610 wspx.w += 0x0010; 611 } else { 612 if(i & J_SELECT) 613 color = STARTFADE; 614 if(i & J_START) 615 if(doorstate == CLOSED) { 616 doorstate = OPENING; 617 doorpos = 0; 618 } else if(doorstate == OPENED) { 619 doorstate = CLOSING; 620 doorpos = NBDFRAMES; 621 } 622 if(i & J_UP) 623 sspy.w -= 0x0010; 624 if(i & J_DOWN) 625 sspy.w += 0x0010; 626 if(i & J_LEFT) 627 sspx.w -= 0x0010; 628 if(i & J_RIGHT) 629 sspx.w += 0x0010; 630 } 631 } 632}