#include "input.h"
#include "sprite.h"
#include "debug.h"
+#include "level.h"
+#include "xgl.h"
+#include "polyfill.h"
+static void update(void);
static void draw(void);
static void vblank(void);
static int nframes, num_vbl, backbuf;
static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
-static uint16_t bnstate;
+static const char *testlvl =
+ "########\n"
+ "### s#\n"
+ "### ####\n"
+ "### #\n"
+ "## #\n"
+ "## #\n"
+ "## #\n"
+ "## ### #\n"
+ "## ### #\n"
+ "## #\n"
+ "#### ###\n"
+ "########\n";
+
+static struct xvertex cube[] __attribute__((section(".rodata"))) = {
+ /* front */
+ {-0x10000, -0x10000, -0x10000, 0, 0, -0x10000, 255},
+ {0x10000, -0x10000, -0x10000, 0, 0, -0x10000, 255},
+ {0x10000, 0x10000, -0x10000, 0, 0, -0x10000, 255},
+ {-0x10000, 0x10000, -0x10000, 0, 0, -0x10000, 255},
+ /* right */
+ {0x10000, -0x10000, -0x10000, 0x10000, 0, 0, 128},
+ {0x10000, -0x10000, 0x10000, 0x10000, 0, 0, 128},
+ {0x10000, 0x10000, 0x10000, 0x10000, 0, 0, 128},
+ {0x10000, 0x10000, -0x10000, 0x10000, 0, 0, 128},
+ /* back */
+ {0x10000, -0x10000, 0x10000, 0, 0, 0x10000, 200},
+ {-0x10000, -0x10000, 0x10000, 0, 0, 0x10000, 200},
+ {-0x10000, 0x10000, 0x10000, 0, 0, 0x10000, 200},
+ {0x10000, 0x10000, 0x10000, 0, 0, 0x10000, 200},
+ /* left */
+ {-0x10000, -0x10000, 0x10000, -0x10000, 0, 0, 192},
+ {-0x10000, -0x10000, -0x10000, -0x10000, 0, 0, 192},
+ {-0x10000, 0x10000, -0x10000, -0x10000, 0, 0, 192},
+ {-0x10000, 0x10000, 0x10000, -0x10000, 0, 0, 192},
+ /* top */
+ {-0x10000, 0x10000, -0x10000, 0, 0x10000, 0, 150},
+ {0x10000, 0x10000, -0x10000, 0, 0x10000, 0, 150},
+ {0x10000, 0x10000, 0x10000, 0, 0x10000, 0, 150},
+ {-0x10000, 0x10000, 0x10000, 0, 0x10000, 0, 150},
+ /* bottom */
+ {0x10000, -0x10000, -0x10000, 0, -0x10000, 0, 210},
+ {-0x10000, -0x10000, -0x10000, 0, -0x10000, 0, 210},
+ {-0x10000, -0x10000, 0x10000, 0, -0x10000, 0, 210},
+ {0x10000, -0x10000, 0x10000, 0, -0x10000, 0, 210}
+};
+
+
+static struct level *lvl;
+
+static int32_t cam_theta, cam_phi;
void gamescr(void)
{
- int i;
+ unsigned char *fb;
REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1;
- vblperf_setcolor(0xff);//192);
+ vblperf_setcolor(0xff);
+
+ lvl = init_level(testlvl);
- fillblock_16byte(vram[0], 0xffffffff, 240 * 160 / 16);
- fillblock_16byte(vram[1], 0xffffffff, 240 * 160 / 16);
+ xgl_init();
+
+ select_input(BN_DPAD);
mask(INTR_VBLANK);
screen_vblank = vblank;
nframes = 0;
for(;;) {
backbuf = ++nframes & 1;
+ fb = (unsigned char*)vram[backbuf];
- bnstate = ~REG_KEYINPUT;
+ polyfill_framebuffer(fb, 240, 160);
+ fillblock_16byte(fb, 0, 240 * 160 / 16);
+ update();
draw();
vblperf_end();
}
}
-static void draw(void)
+static void update(void)
{
- int i, j;
- uint16_t pix;
- uint16_t *fb = vram[backbuf];
-
- for(i=0; i<160; i++) {
- for(j=0; j<240/2; j++) {
- pix = ((i^j) << 1) & 0xff;
- pix |= (i^j) << 9;
- *fb++ = pix;
- }
+ uint16_t bnstate;
+
+ bnstate = get_input();
+
+ if(bnstate & KEY_UP) {
+ cam_phi += 0x2000;
+ if(cam_phi > X_HPI) cam_phi = X_HPI;
}
+ if(bnstate & KEY_DOWN) {
+ cam_phi -= 0x2000;
+ if(cam_phi < -X_HPI) cam_phi = -X_HPI;
+ }
+ if(bnstate & KEY_LEFT) {
+ cam_theta += 0x2000;
+ if(cam_theta > X_2PI) cam_theta -= X_2PI;
+ }
+ if(bnstate & KEY_RIGHT) {
+ cam_theta -= 0x2000;
+ if(cam_theta < X_2PI) cam_theta += X_2PI;
+ }
+}
+
+static void draw(void)
+{
+ xgl_load_identity();
+ //xgl_translate(0, -0x50000, 0);
+ xgl_translate(0, 0, 0x80000);
+ xgl_rotate_x(cam_phi);
+ xgl_rotate_y(cam_theta);
+
+ xgl_draw(XGL_QUADS, cube, sizeof cube / sizeof *cube);
}
__attribute__((noinline, target("arm"), section(".iwram")))
+#include <stdlib.h>
+#include <string.h>
#include "util.h"
#include "debug.h"
iwram_brk(top + delta);
return prev;
}
+
+int ispow2(unsigned int x)
+{
+ int cnt = 0;
+ while(x) {
+ if(x & 1) cnt++;
+ x >>= 1;
+ }
+ return cnt == 1;
+}
+
+
+
+void *malloc_nf_impl(size_t sz, const char *file, int line)
+{
+ void *p;
+ if(!(p = malloc(sz))) {
+ panic(get_pc(), "%s:%d malloc %lu\n", file, line, (unsigned long)sz);
+ }
+ return p;
+}
+
+void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line)
+{
+ void *p;
+ if(!(p = calloc(num, sz))) {
+ panic(get_pc(), "%s:%d calloc %lu\n", file, line, (unsigned long)(num * sz));
+ }
+ return p;
+}
+
+void *realloc_nf_impl(void *p, size_t sz, const char *file, int line)
+{
+ if(!(p = realloc(p, sz))) {
+ panic(get_pc(), "%s:%d realloc %lu\n", file, line, (unsigned long)sz);
+ }
+ return p;
+}
+
+char *strdup_nf_impl(const char *s, const char *file, int line)
+{
+ int len;
+ char *res;
+
+ len = strlen(s);
+ if(!(res = malloc(len + 1))) {
+ panic(get_pc(), "%s:%d strdup\n", file, line);
+ }
+ memcpy(res, s, len + 1);
+ return res;
+}