#include #include "utils.h" #include "icon.c" #define cos1 254 #define sin1 32 #define stpsize 51 asm(".string \"PRG\"\n"); int main(void) { //set CPU to 150 Mhz *(volatile unsigned*) 0x900B0000 = 0x00000002; *(volatile unsigned*) 0x900B000C = 4; //allocate a buffer char* scrbuf = (char*) malloc(SCREEN_BYTES_SIZE); //map array and a blank tile int map[64][64]; char blank[64][64]; //various variables, initialized int px = 3072, py = 3072; int dx = -256, dy = 0; int plx = 0, ply = 167; int w = 320, h = 240; int h256 = h * 256; int halfh = h / 2 + 1; //a couple of uninitialized variables int x, y; int olddx; //build map for (x = 0; x < 64; x++) { for (y = 0; y < 64; y++) { if ((x + y) % 2 == 0) { map[x][y] = 1; } else { map[x][y] = 0; } } } //build blank tile for (x = 0; x < 64; x++) { for (y = 0; y < 64; y++) { blank[x][y] = 15; } } //order the textures char* tex0 = &icon[0][0]; char* tex1 = &blank[0][0]; char* tex[2]; tex[0] = tex0; tex[1] = tex1; //build a lookup table int ctab[h]; for (y = 0; y < h; y++) { int y1 = h256 - (y << 8); int y2 = (y << 8) - h256 / 2; ctab[y] = 256 + (y1 << 8) / y2; } int redraw; while (!isKeyPressed(KEY_NSPIRE_ESC)) { redraw = 0; if (isKeyPressed(KEY_NSPIRE_LEFT)) { olddx = dx; dx = (dx * cos1 - dy * sin1) >> 8; dy = (olddx * sin1 + dy * cos1) >> 8; int oldplx = plx; plx = (plx * cos1 - ply * sin1) >> 8; ply = (oldplx * sin1 + ply * cos1) >> 8; redraw = 1; } if (isKeyPressed(KEY_NSPIRE_RIGHT)) { olddx = dx; dx = (dx * cos1 + dy * sin1) >> 8; dy = (-olddx * sin1 + dy * cos1) >> 8; int oldplx = plx; plx = (plx * cos1 + ply * sin1) >> 8; ply = (-oldplx * sin1 + ply * cos1) >> 8; redraw = 1; } if (isKeyPressed(KEY_NSPIRE_UP)) { px += (stpsize * dx) >> 8; py += (stpsize * dy) >> 8; redraw = true; } if (isKeyPressed(KEY_NSPIRE_DOWN)) { px -= (stpsize * dx) >> 8; py -= (stpsize * dy) >> 8; redraw = true; } if (redraw) { memset(scrbuf, 0xFF, SCREEN_BYTES_SIZE); for(x = 0; x < w - 1; x += 2) { int cameraX = 2 * (x << 8) / w - 256; int raydx = dx + ((plx * cameraX) >> 8); int raydy = dy + ((ply * cameraX) >> 8); for (y = halfh; y < h; y++) { int c = ctab[y]; int floorX = ((raydx * c) >> 8) + px; int floorY = ((raydy * c) >> 8) + py; int sX = floorX >> 2, sY = floorY >> 2; int mX = sX >> 6, mY = sY >> 6; if (mX > 0 && mX < 64 && mY > 0 && mY < 64) { int tX = sX & 63; int tY = sY & 63; int color = (tex[map[mX][mY]])[tX + (tY << 6)]; setPixelBuf(scrbuf, x, y, color); setPixelBuf(scrbuf, x + 1, y, color); } } } refresh(scrbuf); } } *(volatile unsigned*) 0x900B0000 = 0x00141002; *(volatile unsigned*) 0x900B000C = 4; free(scrbuf); return 0; }