/*! \brief The opposite of shutdown, maybe? * * Inits everything needed for user input, graphics, etc. */ int startup (void) { static COLOR_MAP colormap; /* Buffer for all strings */ strbuf = (char *) malloc (256); set_color_depth (32); shared_startup (); gmap.map_no = -1; gmap.tileset = 0; // bufferize (); create_trans_table (&colormap, pal, 128, 128, 128, NULL); color_map = &colormap; return 1; } /* startup () */
int main(int argc, char *argv[]) { DATAFILE *datf; BITMAP *scrFlip; BITMAP *scrMap; PALETTE pal; WINDOW *winInfo; long frames = 0L; float start_time, end_time; MAP *map, *overlay; int x = 0, y = 0; int old_x = -1, old_y = -1; int dx = 1, dy = 1; int view_w, view_h; int do_idle = 1; char s[128]; // Default screen resolution to 320x200. screen_width = 320; screen_height = 200; // Read command line args. Set screen size accordingly. if (argc > 2) { screen_width = atoi(argv[1]); screen_height = atoi(argv[2]); } right_size = 0; bottom_size = 0; printf("screen: %dX%d\n", screen_width, screen_height); // Initialize Allegro. Great library! allegro_init(); install_timer(); install_keyboard(); // Set graphics mode. if (set_gfx_mode(GFX_AUTODETECT, screen_width, screen_height, 0, 0) == -1) { printf("Cannot set video mode to %d, %d.\n", screen_width, screen_height); return 1; } // load comic font from ex4. datf = load_datafile_object("ex.dat", "comic12"); if (datf == NULL) { allegro_exit(); printf("Error loading ex.dat\n"); return 1; } text_mode(-1); // load map //printf("loading map\n"); map = load_map("tile1.wmp", "tile1.spr", pal); if (map == NULL) { allegro_exit(); printf("Error loading map file.\n"); return 1; } // load overlay //printf("loading overlay\n"); overlay = load_map("tile1ovr.wmp", NULL, NULL); if (overlay == NULL) { allegro_exit(); printf("Error loading overlay file.\n"); return 1; } map_settileset(overlay, map->tileset); // Allocate buffers for our rendering. //printf("allocating buffers\n"); scrFlip = create_bitmap(SCREEN_W, SCREEN_H); clear(scrFlip); scrMap = create_bitmap(SCREEN_W + map->t_width*2, SCREEN_H + map->t_height*2); // set palette //printf("setting palette\n"); gui_setpalette(pal); // Build rgb_table. text_mode(makecol8(0, 0, 0)); textout_centre(screen, font, "Building RGB table.", SCREEN_W>>1, 10, 100); create_rgb_table(&rgb_table, pal, rgb_table_callback); rgb_map = &rgb_table; // Build lighting table. text_mode(makecol8(0, 0, 0)); textout_centre(screen, font, "Building lighting table.", SCREEN_W>>1, 10, 100); create_light_table(&light_table, pal, 64, 64, 64, rgb_table_callback); // Build translucency table. text_mode(makecol8(0, 0, 0)); textout_centre(screen, font, "Building translucency table.", SCREEN_W>>1, 10, 100); create_trans_table(&trans_table, pal, 128, 128, 128, rgb_table_callback); color_map = &trans_table; // initialize gui gui_initialize(&light_table, &trans_table); // set map and overlay color tables map_settables(map, &trans_table, &light_table); map_settables(overlay, &trans_table, &light_table); // create info window winInfo = gui_create_window(128, 200, WB_THICK, WD_TRANSLUCENT); gui_move_window(winInfo, SCREEN_W-winInfo->window.w, 0); // set up vars. view_w = scrMap->w/map->t_width; view_h = scrMap->h/map->t_height; // setup time_to_blit interrupt. LOCK_VARIABLE(time_to_blit); LOCK_FUNCTION(time_to_blit_timer); install_int_ex(time_to_blit_timer, BPS_TO_TIMER(30)); missed_frames = 0; // main loop. start_time = clock(); while (1) { idle_time = 0; if (do_idle) while (!time_to_blit) idle_proc(); // lock it in at around 30 fps. time_to_blit = 0; if (idle_time < lowest_idle) lowest_idle = idle_time; if (idle_time > highest_idle) highest_idle = idle_time; // render map if (map_getx_tile(map, x) != old_x || map_gety_tile(map, y) != old_y) { render_map(map, scrMap, 0, 0, x, y, view_w, view_h); render_map(overlay, scrMap, 0, 0, x, y, view_w, view_h); old_x = map_getx_tile(map, x); old_y = map_gety_tile(map, y); } blit(scrMap, scrFlip, map_getx_offset(map, x), map_gety_offset(map, y), 0, 0, SCREEN_W-right_size, SCREEN_H-bottom_size); gui_clear_window(winInfo, makecol8(0, 0, 0)); gui_textout_centre_window(winInfo, "Map Demo", 2, makecol8(255, 0, 0), -1); sprintf(s, "X: %d, Y: %d", x, y); gui_textout_centre_window(winInfo, s, 14, makecol8(0, 0, 255), -1); sprintf(s, "DX: %d, DY: %d", dx, dy); gui_textout_centre_window(winInfo, s, 24, makecol8(0, 0, 255), -1); sprintf(s, "X Ofs: %d", map_getx_offset(map, x)); gui_textout_centre_window(winInfo, s, 34, makecol8(0, 0, 255), -1); sprintf(s, "Y Ofs: %d", map_gety_offset(map, y)); gui_textout_centre_window(winInfo, s, 44, makecol8(0, 0, 255), -1); sprintf(s, "Idle: %d", idle_time); gui_textout_centre_window(winInfo, s, 64, makecol8(0, 255, 255), -1); sprintf(s, "Missed: %d", missed_frames); gui_textout_centre_window(winInfo, s, 74, makecol8(0, 255, 255), -1); gui_draw_window(winInfo, scrFlip); // blit it //vsync(); blit(scrFlip, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // check keys if (key[KEY_SPACE]) { break; } if (key[KEY_LSHIFT]) do_idle = 0; else do_idle = 1; if (key[KEY_CONTROL]) gui_set_window_styles(winInfo, -1, WD_BLIT); else gui_set_window_styles(winInfo, -1, WD_TRANSLUCENT); if (key[KEY_PGDN]) { dx--; if (dx < 1) dx = 1; dy--; if (dy < 1) dy = 1; } if (key[KEY_PGUP]) { dx++; dy++; } if (key[KEY_RIGHT]) { x+=dx; if (x > (map->w_width-1) - SCREEN_W) { x-=dx; } } if (key[KEY_DOWN]) { y+=dy; if (y > (map->w_height-1) - SCREEN_H) { y-=dy; } } if (key[KEY_LEFT]) { x-=dx; if (x < 0) { x = 0; } } if (key[KEY_UP]) { y-=dy; if (y < 0) { y = 0; } } // Increment frame counter. frames++; } end_time = clock(); // Clean up. unload_datafile_object(datf); destroy_map(map); overlay->tileset=NULL; destroy_map(overlay); destroy_bitmap(scrFlip); destroy_bitmap(scrMap); // Close down allegro. allegro_exit(); // Report. printf("Tile Map, Copyright 1997 by Scott Deming.\n"); printf("\nHow'd we do?\n"); printf("===============================================================================\n"); printf(" Time: %3.2f\n", (float) ((end_time-start_time) / 100)); printf("Frames: %lu\n", frames); printf(" FPS: %3.2f\n", (float) (float) frames / (float) ((end_time-start_time) / 100)); printf("Missed: %d\n", missed_frames); printf(" Idle: %d (lowest was %d, highest %d)\n", idle_time, lowest_idle, highest_idle); printf("Screen: %dX%d\n", screen_width, screen_height); printf("-------------------------------------------------------------------------------\n"); printf("Note: If \"Idle\" starts to get close to zero then things should be done to\n"); printf(" speed things up. When idle proc hits zero, we'll start missing frames\n"); printf(" (indicated by \"Missed\") and things can start to get a bit jerky.\n"); printf(" If \"Missed\" is greater than 0, start optimizing.\n"); printf(" If you turned off the 30 fps timer halt then your lowest idle will be 0.\n"); return 0; }
void TextureUtil::initGrx() { Config * config = Config::getInstance(); #if EM_USE_SDL cerr << "Initing SDL" << endl << endl; if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { cerr << "Couldn't initialize SDL video" << SDL_GetError() << endl; exit(1); } if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) { cerr << "Couldn't initialize SDL joystick: " << SDL_GetError() << endl << endl; } else { int njoystick = SDL_NumJoysticks(); cerr << njoystick << " joysticks were found." << endl; if (njoystick != 0) { cerr << "The names of the joysticks are:" << endl; for(int a=0; a<njoystick; a++ ) { cerr << " " << SDL_JoystickName(a) << endl; } cerr << "Using " << SDL_JoystickName(0) << endl << endl; SDL_JoystickOpen(0); SDL_JoystickEventState(SDL_ENABLE); } } // See if we should detect the display depth if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) { SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 2 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 3 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 3 ); } else if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 16 ) { SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); } else { SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 ); } SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); /* Initialize the display */ SDL_Surface* screen = SDL_SetVideoMode(config->getWidth(), config->getHeight(), config->getBpp(), SDL_OPENGL | (config->useFullScreen() ? SDL_FULLSCREEN : 0)); // if (config->useFullScreen()) { SDL_ShowCursor(SDL_DISABLE); // } SDL_WM_SetCaption("Emilia Pinball", NULL); if (screen == NULL) { cerr << "Couldn't set video mode: " << SDL_GetError() << endl; exit(1); } cerr << "Vendor : " << glGetString( GL_VENDOR ) << endl; cerr << "Renderer : " << glGetString( GL_RENDERER ) << endl; cerr << "Version : " << glGetString( GL_VERSION ) << endl; cerr << "Extensions : " << glGetString( GL_EXTENSIONS ) << endl << endl; //TODO: that would be usefull to report CPU/RAM specs also //!rzr int value; SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value ); cerr << "SDL_GL_RED_SIZE: " << value << endl; SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value ); cerr << "SDL_GL_GREEN_SIZE: " << value << endl; SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value ); cerr << "SDL_GL_BLUE_SIZE: " << value << endl; SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value ); cerr << "SDL_GL_DEPTH_SIZE: " << value << endl; SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value ); cerr << "SDL_GL_DOUBLEBUFFER: " << value << endl << endl; this->resizeView(config->getWidth(), config->getHeight()); #endif // EM_USE_SDL #if EM_USE_ALLEGRO //config->setSize(320, 240); allegro_init(); install_keyboard(); install_timer(); install_mouse(); COLOR_MAP colorMap; RGB_MAP rgbMap; COLOR_MAP transMap; RGB* paPalette = (RGB*) calloc(256, sizeof(RGB)); generate_332_palette(paPalette); // create rgb table create_rgb_table(&rgbMap, paPalette, NULL); rgb_map = &rgbMap; // create light table and setup the truecolor blending functions. create_light_table(&colorMap, paPalette, 0, 0, 0, NULL); color_map = &colorMap; // texture and flat polygons are 50% transparent create_trans_table(&transMap, paPalette, 128, 128, 128, NULL); set_trans_blender(0, 0, 0, 128); // set the graphics mode int tc = GFX_AUTODETECT_WINDOWED, tw = config->getWidth(); int th = config->getHeight(), tbpp = 16; /* set_gfx_mode(GFX_SAFE, 320, 200, 0, 0); set_palette(desktop_palette); if (!gfx_mode_select_ex(&tc, &tw, &th, &tbpp)) { allegro_exit(); cerr << "Error setting safe graphics mode" << endl; } */ set_color_depth(tbpp); if (set_gfx_mode(tc, tw, th, 0, 0) != 0) { allegro_exit(); cerr << "Error setting graphics mode " << endl << allegro_error << endl; } set_palette(paPalette); config->setSize(tw, th); set_projection_viewport(0, 0, tw, th); // Create back buffer. backbuffer = create_bitmap(tw, th); clear(backbuffer); zbuffer = create_zbuffer(backbuffer); set_zbuffer(zbuffer); clear_zbuffer(zbuffer, 0); #endif // EM_USE_ALLEGRO }
// ========================================================================== // make (or read) all colormaps for all palettes void misc_make_cmaps(void) { CMAP_E cm; int a, cmap_ok, i, c, start; char tmp[100]; FILE * out, * in; printf("\n"); for (cm=0; cm < CM_MAX; cm++) { for (a=0; a < ACT_MAX; a++) { fprintf(stderr, "."); sprintf(tmp, "%s/cmap%i_%i.bin", glb_ds1edit_data_dir, a, cm); cmap_ok = FALSE; if (file_exists(tmp, -1, NULL) && (glb_ds1edit.pal_loaded[a] == TRUE)) { // load the colormap from disk, instead of making it in = fopen(tmp, "rb"); if (in == NULL){ printf("can't read %s\n", tmp); }else{ printf("loading %s\n", tmp); fread(& glb_ds1edit.cmap[cm][a], sizeof(COLOR_MAP), 1, in); fclose(in); cmap_ok = TRUE; } } if (cmap_ok == FALSE) { // not found or can't be open, so create it if (cm == CM_SELECT) { // color table create_color_table(& glb_ds1edit.cmap[cm][a], glb_ds1edit.vga_pal[a], misc_make_cmaps_helper, NULL); } else if (cm == CM_TRANS) { create_trans_table(& glb_ds1edit.cmap[cm][a], glb_ds1edit.vga_pal[a], 128, 128, 128, NULL); } else if (cm == CM_SHADOW) { for (c=0; c < 256; c++) { start = 1024 + (256 * (c/8)); for (i=0; i<256; i++) { glb_ds1edit.cmap[cm][a].data[c][i] = glb_ds1edit.d2_pal[a][start + i]; } } } out = fopen(tmp, "wb"); if (out == NULL) printf("can't write %s\n", tmp); else { printf("saving %s\n", tmp); fwrite(& glb_ds1edit.cmap[cm][a], sizeof(COLOR_MAP), 1, out); fclose(out); } } } } }
int main(void) { BITMAP *scrbuffer; DATAFILE *main_data; int vmode_error; RGB black = { 0, 0, 0 }; // initialise allegro allegro_init(); install_keyboard(); install_timer(); //install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL); // load main data file into memory main_data = load_datafile("dogfight.dat"); // use the palette stored in the datafile and // set the "clear" colour from a working hot pink to an invisible black memcpy(&pal, main_data[MAINPAL].dat, sizeof(pal)); // enter graphics mode -- don't debug from here -- vmode_error = set_gfx_mode(GFX_AUTODETECT, SET_W, SET_H, 0, 0); if (vmode_error) { printf("Error setting graphics mode...\n%s\n\n", allegro_error); allegro_exit(); exit(1); } set_palette(pal); set_color(0, &black); // build a colour lookup table for translucent drawing // NB: 128 translucency = 50% textout_centre_ex(screen, main_data[FONTSMALL].dat, "Dogfight by Gregory McIntyre", SCREEN_W/2, SCREEN_H/2-58, GREY+15, -1); textout_centre_ex(screen, font, "Loading. Please wait...", SCREEN_W/2, SCREEN_H/2-20, GREY+15, -1); callback_count = 0; create_trans_table(&trans_table, main_data[MAINPAL].dat, CLOUD_TRANSPARENCY, CLOUD_TRANSPARENCY, CLOUD_TRANSPARENCY, callback_func); // allocate memory for screen buffer scrbuffer = create_bitmap(SCREEN_W, SCREEN_H); clear(scrbuffer); // when everything is ready fade out and go to the title screen fade_out(10); title_page(scrbuffer, main_data); // free allocated memory and exit allegro destroy_bitmap(scrbuffer); unload_datafile(main_data); allegro_exit(); // print a message that will be displayed at the DOS prompt on leaving printf("Thankyou for playing Dogfight.\n"); printf("Dogfight was created using the Allegro game library.\n"); printf("It's free so enjoy and distribute at your leasure.\n\n"); printf("-------------------------------\n"); printf("The author can be contacted at:\n\n"); printf("[email protected]*\n\n"); printf("-------------------------------\n\n"); printf("Have a nice day!\n"); printf("\n"); printf("* Email no longer valid.\n"); return 0; }