nds_test2
view 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 source
1 #include <stdint.h>
2 #include <math.h>
3 #include "dsregs.h"
4 #include "ds3.h"
6 static void xorpat(void *addr, int xsz, int ysz);
8 static void *vram = VRAM_LCDC_PTR;
9 static uint16_t *bgmem = VRAM_BGB_PTR;
11 #define SIN_TAB_SZ 256
12 static int32_t sintab[SIN_TAB_SZ];
13 static int32_t costab[SIN_TAB_SZ];
15 int main(void)
16 {
17 int i;
18 uint32_t frame;
19 int32_t m[16] = {
20 0x10000, 0, 0, 0,
21 0, 0x10000, 0, 0,
22 0, 0, 0x10000, 0,
23 0, 0, 0, 0x10000
24 };
26 REG_POWCNT1 = POWCNT1_LCD | POWCNT1_3DREND | POWCNT1_3DGEOM | POWCNT1_2DA | POWCNT1_2DB | POWCNT1_DSWAP;
28 REG_DISPCNT = DISPCNT_MODE(1) | DISPCNT_BG0 | DISPCNT_BG0_3D;
29 REG_B_DISPCNT = DISPCNT_MODE(1) | DISPCNT_BG2 | 5;
31 REG_B_BG2CNT = BGXCNT_BM_256X256 | BGXCNT_BM16 | BGXCNT_OVF_WRAP;
32 REG_B_BG2PA = 0x100;
33 REG_B_BG2PB = 0;
34 REG_B_BG2PC = 0;
35 REG_B_BG2PD = 0x100;
37 REG_VRAMCNT_A = VRAM_ENABLE;
38 REG_VRAMCNT_C = VRAM_ENABLE | 4;
40 xorpat(bgmem, 256, 256);
42 for(i=0; i<SIN_TAB_SZ; i++) {
43 float theta = (float)i * M_PI * 2.0 / (float)SIN_TAB_SZ;
44 float s = sin(theta);
45 float c = cos(theta);
46 sintab[i] = (int32_t)(s * 65536.0);
47 costab[i] = (int32_t)(c * 65536.0);
48 }
50 ds3_clear_color(RGB15(4, 4, 4), 31);
51 ds3_clear_depth(0x7fff);
52 ds3_viewport(0, 0, 256, 192);
54 ds3_enable(DS3_ANTIALIAS);
56 REG_POLYGON_ATTR = 0x001f00c0; /* alpha = 31, cull none */
58 ds3_matrix_mode(DS3_PROJECTION);
59 ds3_load_identity();
60 ds3_scale(49152, 65536, 65536);
62 for(;;) {
63 int idx = frame & 0xff;
64 int32_t scale = (sintab[(frame >> 1) & 0xff] >> 9) + 204;
65 int32_t sa = ((sintab[idx] >> 8) * scale) >> 8;
66 int32_t ca = ((costab[idx] >> 8) * scale) >> 8;
67 /*float scale = 0.5 * sin(t * 0.8) + 0.8;
68 int32_t sa = (int16_t)(sin(t) * 256 * scale);
69 int32_t ca = (int16_t)(cos(t) * 256 * scale);
70 */
72 int32_t x = ca * -128 + sa * -96 + (128 << 8);
73 int32_t y = -sa * -128 + ca * -96 + (96 << 8);
75 m[0] = ca << 8; m[1] = sa << 8;
76 m[4] = -sa << 8; m[5] = ca << 8;
78 ds3_matrix_mode(DS3_MODELVIEW);
79 ds3_load_matrix(m);
81 ds3_begin(DS3_QUADS);
82 ds3_color(RGB15(31, 0, 0));
83 ds3_vertex3(-0x8000, -0x8000, 0);
84 ds3_color(RGB15(0, 31, 0));
85 ds3_vertex3(0x8000, -0x8000, 0);
86 ds3_color(RGB15(0, 0, 31));
87 ds3_vertex3(0x8000, 0x8000, 0);
88 ds3_color(RGB15(31, 0, 31));
89 ds3_vertex3(-0x8000, 0x8000, 0);
90 ds3_end();
92 ds3_swap_buffers();
93 while(REG_VCOUNT < 192);
95 REG_B_BG2PA = ca;
96 REG_B_BG2PB = sa;
97 REG_B_BG2PC = -sa;
98 REG_B_BG2PD = ca;
99 REG_B_BG2X = x;
100 REG_B_BG2Y = y;
102 ++frame;
103 }
104 return 0;
105 }
107 static void xorpat(void *addr, int xsz, int ysz)
108 {
109 int i, j;
110 uint16_t *p = addr;
112 for(i=0; i<ysz; i++) {
113 for(j=0; j<xsz; j++) {
114 int xor = i^j;
115 uint16_t red = xor >> 2;
116 uint16_t green = xor >> 1;
117 uint16_t blue = xor;
118 *p++ = 0x8000 | red | ((green & 0x1f) << 5) | ((blue & 0x1f) << 10);
119 }
120 }
121 }