nds_test2
diff src/main.c @ 0:abcaf667f2bd
initial commit (3d + 2d)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 28 Jan 2018 20:05:26 +0200 |
parents | |
children | d625ba001a62 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main.c Sun Jan 28 20:05:26 2018 +0200 1.3 @@ -0,0 +1,121 @@ 1.4 +#include <stdint.h> 1.5 +#include <math.h> 1.6 +#include "dsregs.h" 1.7 +#include "ds3.h" 1.8 + 1.9 +static void xorpat(void *addr, int xsz, int ysz); 1.10 + 1.11 +static void *vram = VRAM_LCDC_PTR; 1.12 +static uint16_t *bgmem = VRAM_BGB_PTR; 1.13 + 1.14 +#define SIN_TAB_SZ 256 1.15 +static int32_t sintab[SIN_TAB_SZ]; 1.16 +static int32_t costab[SIN_TAB_SZ]; 1.17 + 1.18 +int main(void) 1.19 +{ 1.20 + int i; 1.21 + uint32_t frame; 1.22 + int32_t m[16] = { 1.23 + 0x10000, 0, 0, 0, 1.24 + 0, 0x10000, 0, 0, 1.25 + 0, 0, 0x10000, 0, 1.26 + 0, 0, 0, 0x10000 1.27 + }; 1.28 + 1.29 + REG_POWCNT1 = POWCNT1_LCD | POWCNT1_3DREND | POWCNT1_3DGEOM | POWCNT1_2DA | POWCNT1_2DB | POWCNT1_DSWAP; 1.30 + 1.31 + REG_DISPCNT = DISPCNT_MODE(1) | DISPCNT_BG0 | DISPCNT_BG0_3D; 1.32 + REG_B_DISPCNT = DISPCNT_MODE(1) | DISPCNT_BG2 | 5; 1.33 + 1.34 + REG_B_BG2CNT = BGXCNT_BM_256X256 | BGXCNT_BM16 | BGXCNT_OVF_WRAP; 1.35 + REG_B_BG2PA = 0x100; 1.36 + REG_B_BG2PB = 0; 1.37 + REG_B_BG2PC = 0; 1.38 + REG_B_BG2PD = 0x100; 1.39 + 1.40 + REG_VRAMCNT_A = VRAM_ENABLE; 1.41 + REG_VRAMCNT_C = VRAM_ENABLE | 4; 1.42 + 1.43 + xorpat(bgmem, 256, 256); 1.44 + 1.45 + for(i=0; i<SIN_TAB_SZ; i++) { 1.46 + float theta = (float)i * M_PI * 2.0 / (float)SIN_TAB_SZ; 1.47 + float s = sin(theta); 1.48 + float c = cos(theta); 1.49 + sintab[i] = (int32_t)(s * 65536.0); 1.50 + costab[i] = (int32_t)(c * 65536.0); 1.51 + } 1.52 + 1.53 + ds3_clear_color(RGB15(4, 4, 4), 31); 1.54 + ds3_clear_depth(0x7fff); 1.55 + ds3_viewport(0, 0, 256, 192); 1.56 + 1.57 + ds3_enable(DS3_ANTIALIAS); 1.58 + 1.59 + REG_POLYGON_ATTR = 0x001f00c0; /* alpha = 31, cull none */ 1.60 + 1.61 + ds3_matrix_mode(DS3_PROJECTION); 1.62 + ds3_load_identity(); 1.63 + ds3_scale(49152, 65536, 65536); 1.64 + 1.65 + for(;;) { 1.66 + int idx = frame & 0xff; 1.67 + int32_t scale = (sintab[(frame >> 1) & 0xff] >> 9) + 204; 1.68 + int32_t sa = ((sintab[idx] >> 8) * scale) >> 8; 1.69 + int32_t ca = ((costab[idx] >> 8) * scale) >> 8; 1.70 + /*float scale = 0.5 * sin(t * 0.8) + 0.8; 1.71 + int32_t sa = (int16_t)(sin(t) * 256 * scale); 1.72 + int32_t ca = (int16_t)(cos(t) * 256 * scale); 1.73 + */ 1.74 + 1.75 + int32_t x = ca * -128 + sa * -96 + (128 << 8); 1.76 + int32_t y = -sa * -128 + ca * -96 + (96 << 8); 1.77 + 1.78 + m[0] = ca << 8; m[1] = sa << 8; 1.79 + m[4] = -sa << 8; m[5] = ca << 8; 1.80 + 1.81 + ds3_matrix_mode(DS3_MODELVIEW); 1.82 + ds3_load_matrix(m); 1.83 + 1.84 + ds3_begin(DS3_QUADS); 1.85 + ds3_color(RGB15(31, 0, 0)); 1.86 + ds3_vertex3(-0x8000, -0x8000, 0); 1.87 + ds3_color(RGB15(0, 31, 0)); 1.88 + ds3_vertex3(0x8000, -0x8000, 0); 1.89 + ds3_color(RGB15(0, 0, 31)); 1.90 + ds3_vertex3(0x8000, 0x8000, 0); 1.91 + ds3_color(RGB15(31, 0, 31)); 1.92 + ds3_vertex3(-0x8000, 0x8000, 0); 1.93 + ds3_end(); 1.94 + 1.95 + ds3_swap_buffers(); 1.96 + while(REG_VCOUNT < 192); 1.97 + 1.98 + REG_B_BG2PA = ca; 1.99 + REG_B_BG2PB = sa; 1.100 + REG_B_BG2PC = -sa; 1.101 + REG_B_BG2PD = ca; 1.102 + REG_B_BG2X = x; 1.103 + REG_B_BG2Y = y; 1.104 + 1.105 + ++frame; 1.106 + } 1.107 + return 0; 1.108 +} 1.109 + 1.110 +static void xorpat(void *addr, int xsz, int ysz) 1.111 +{ 1.112 + int i, j; 1.113 + uint16_t *p = addr; 1.114 + 1.115 + for(i=0; i<ysz; i++) { 1.116 + for(j=0; j<xsz; j++) { 1.117 + int xor = i^j; 1.118 + uint16_t red = xor >> 2; 1.119 + uint16_t green = xor >> 1; 1.120 + uint16_t blue = xor; 1.121 + *p++ = 0x8000 | red | ((green & 0x1f) << 5) | ((blue & 0x1f) << 10); 1.122 + } 1.123 + } 1.124 +}