#include #include #include #include #include "lighting.h" static volatile int game_timer = 0; static void inc_game_timer(void) { game_timer++; } END_OF_FUNCTION(inc_game_timer); static Point world_pts[] = { {10, 10}, {100, 10}, {100, 50}, {110, 50}, {110, 10}, {130, 10}, {130, 70}, {90, 70}, {90, 80}, {180, 80}, {180, 70}, {140, 70}, {140, 10}, {160, 10}, {160, 50}, {170, 50}, {170, 10}, {300, 10}, {300, 95}, {250, 95}, {250, 85}, {200, 85}, {200, 115}, {250, 115}, {250, 105}, {300, 105}, {300, 190}, {150, 220}, {10, 210}, {10, 130}, {60, 130}, {60, 190}, {70, 170}, {80, 160}, {100, 150}, {100, 110}, {50, 90}, {40, 100}, {90, 120}, {10, 120} }; #define WORLD_SIZE sizeof(world_pts) / sizeof(Point) static void my_circle(BITMAP* bmp, int x, int y, int moo) { circlefill(bmp, x, y, 2, moo); } BITMAP* buffer; static void generate_palette(PALETTE pal) { int s, v; int r, g, b; int c = 0; for (s = 0; s < 11; s++) for (v = 0; v < 11; v++) { hsv_to_rgb(0.0, s / 10.0, v / 10.0, &r, &g, &b); r /= 4; g /= 4; b /= 4; pal[c].r = r; pal[c].g = g; pal[c].b = b; c++; hsv_to_rgb(210.0, s / 10.0, v / 10.0, &r, &g, &b); r /= 4; g /= 4; b /= 4; pal[c].r = r; pal[c].g = g; pal[c].b = b; c++; } for (; c < 256; c++) pal[c].r = pal[c].g = pal[c].b = (c - 242) * 5; } int main(int argc, char** argv) { BITMAP* image; BITMAP* image2; BITMAP** light; BITMAP* vis; BITMAP* buffer; PALETTE pal; int x, y, i; int mx, my; VWedge* wedge; Heap world; Polygon* poly; /* Protect timer callback functions and data */ LOCK_VARIABLE(game_timer); LOCK_FUNCTION(inc_game_timer); /* Initialize Allegro */ srand(time(NULL)); allegro_init(); install_keyboard(); install_mouse(); install_timer(); /* Set the proper graphics mode */ set_color_depth(depth); if (depth == 8) { generate_palette(pal); rgb_map = (RGB_MAP*)malloc(sizeof(RGB_MAP) * 256); create_rgb_table(rgb_map, pal, NULL); } set_gfx_mode(GFX_AUTODETECT, width, height, 0, 0); if (depth == 8) set_palette(pal); /* Initialize world */ create_heap(&world, 5, 5, create_polygon, destroy_polygon); poly = new_from_heap(&world); poly->vtx = malloc(WORLD_SIZE * sizeof(Point)); poly->vtx_count = WORLD_SIZE; for (i = 0; i < WORLD_SIZE * 2; i++) ((int*)poly->vtx)[i] = ((int*)world_pts)[i];// * 2; /* Initialize various bitmaps */ light = load_lighting_maps(); vis = create_bitmap_ex(8, SCREEN_W, SCREEN_H); image = create_bitmap_ex(32, SCREEN_W, SCREEN_H); buffer = create_bitmap_ex(32, SCREEN_W, SCREEN_H); /* Render world */ clear_to_color(image, makecol32(0, 127, 127)); polygon(image, WORLD_SIZE, (int*)poly->vtx, makecol32(127, 0, 0)); for (i = 0; i < WORLD_SIZE - 1; i++) do_line(image, poly->vtx[i].x, poly->vtx[i].y, poly->vtx[i + 1].x, poly->vtx[i + 1].y, makecol32(127, 127, 127), my_circle); do_line(image, poly->vtx[WORLD_SIZE - 1].x, poly->vtx[WORLD_SIZE - 1].y, poly->vtx[0].x, poly->vtx[0].y, makecol32(127, 127, 127), my_circle); for (y = 0; y < SCREEN_H; y++) for (x = 0; x < SCREEN_W; x++) { int a = rand() % 192 - 96; int c = getpixel(image, x, y); int r = MID(0, getr32(c) + a, 255); int g = MID(0, getg32(c) + a, 255); int b = MID(0, getb32(c) + a, 255); putpixel(image, x, y, makecol32(r, g, b)); } image2 = create_bitmap_ex(32, SCREEN_W, SCREEN_H); for (y = 1; y < SCREEN_H - 1; y++) for (x = 1; x < SCREEN_W - 1; x++) { int xx, yy; int r = 0, g = 0, b = 0; for (yy = y - 1; yy < y + 2; yy++) for (xx = x - 1; xx < x + 2; xx++) { int c = getpixel(image, xx, yy); r += getr32(c); g += getg32(c); b += getb32(c); } putpixel(image2, x, y, makecol32(r / 9, g / 9, b / 9)); } blit(image2, image, 0, 0, 0, 0, SCREEN_W, SCREEN_H); destroy_bitmap(image2); /* Initialize various "game" variables */ wedge = create_vwedge(); wedge->x = SCREEN_W / 2; wedge->y = SCREEN_H / 2; wedge->a = 0.0; wedge->r = 400; wedge->fov = M_PI / 4; clear_bitmap(vis); set_trans_blender(0, 0, 0, 127); /* Main loop */ install_int_ex(inc_game_timer, BPS_TO_TIMER(30)); i = 0; while (1) { /* Keep in sync with timer */ while (game_timer == 0) rest(1000 / 15); game_timer = 0; /* Input handlng */ get_mouse_mickeys(&mx, &my); if (key[KEY_LEFT]) mx--; if (key[KEY_RIGHT]) mx++; if (key[KEY_UP]) my--; if (key[KEY_DOWN]) my++; if (key[KEY_ESC]) break; /* "Game" logic */ wedge->x = MID(0, wedge->x + mx, SCREEN_W - 1); wedge->y = MID(0, wedge->y + my, SCREEN_H - 1); // wedge->a = a; /* Render pipeline */ // clear_bitmap(buffer); find_vlines(&wedge->vis, &world); /* World --> vlines */ process_vwedge_vis(wedge); /* Refine vlines */ draw_vwedge(vis, wedge, 2); /* Show visibility */ combine_maps(light[i], vis, image, buffer); // combine_maps(NULL, vis, image, buffer); circlefill(buffer, wedge->x, wedge->y, 2, 0xffffff); /* Update the screen */ vsync(); blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); /* Age old visibility */ draw_vwedge(vis, wedge, 1); /* Increment frame */ i++; if (i == 29) i = 0; } remove_int(inc_game_timer); /* Cleanup */ destroy_heap(&world); destroy_vwedge(wedge); for (i = 0; i < 29; i++) destroy_bitmap(light[i]); free(light); free(rgb_map); remove_keyboard(); remove_mouse(); remove_timer(); allegro_exit(); return EXIT_SUCCESS; } END_OF_MAIN();