Example #1
0
File: cube.c Project: puyo/tankdemo
int main(void)
{
 allegro_init();
 install_timer();
 install_keyboard();

 rgb_map = malloc(sizeof(RGB_MAP));
 create_rgb_table(rgb_map, desktop_palette, NULL);

 set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0);

 buffer = create_bitmap(SCREEN_W, SCREEN_H);
 set_projection_viewport(0, 0, SCREEN_W, SCREEN_H);

 init_shape();

 while (!key[KEY_ESC]) {
   move_shape();
   translate_shape();
   vsync();
   draw();
 }

 destroy_bitmap(buffer);
 free(rgb_map);
 allegro_exit();
 return 0;
}
Example #2
0
  void regenerate(const Palette* palette) {
    m_palette = palette;
    m_modifications = palette->getModifications();

    PALETTE allegPal;
    palette->toAllegro(allegPal);
    create_rgb_table(m_allegMap, allegPal, NULL);

    for (int r=0; r<32; ++r)
      for (int g=0; g<32; ++g)
	for (int b=0; b<32; ++b) {
	  if (m_allegMap->data[r][g][b] >= palette->size())
	    m_allegMap->data[r][g][b] = palette->findBestfit(_rgb_scale_5[r],
							     _rgb_scale_5[g],
							     _rgb_scale_5[b]);
	}
  }
Example #3
0
/* _fixup_loaded_bitmap:
 *  Helper function for adjusting the color depth of a loaded image.
 *  Converts the bitmap BMP to the color depth BPP. If BMP is a 8-bit
 *  bitmap, PAL must be the palette attached to the bitmap. If BPP is
 *  equal to 8, the conversion is performed either by building a palette
 *  optimized for the bitmap if PAL is not NULL (in which case PAL gets
 *  filled in with this palette) or by using the current palette if PAL
 *  is NULL. In any other cases, PAL is unused.
 */
BITMAP *_fixup_loaded_bitmap(BITMAP *bmp, PALETTE pal, int bpp)
{
   BITMAP *b2;
   ASSERT(bmp);

   b2 = create_bitmap_ex(bpp, bmp->w, bmp->h);
   if (!b2) {
      destroy_bitmap(bmp);
      return NULL;
   }

   if (bpp == 8) {
      RGB_MAP *old_map = rgb_map;

      if (pal)
         generate_optimized_palette(bmp, pal, NULL);
      else
         pal = _current_palette;

      rgb_map = _AL_MALLOC(sizeof(RGB_MAP));
      if (rgb_map != NULL)
         create_rgb_table(rgb_map, pal, NULL);

      blit(bmp, b2, 0, 0, 0, 0, bmp->w, bmp->h);

      if (rgb_map != NULL)
         _AL_FREE(rgb_map);
      rgb_map = old_map;
   }
   else if (bitmap_color_depth(bmp) == 8) {
      select_palette(pal);
      blit(bmp, b2, 0, 0, 0, 0, bmp->w, bmp->h);
      unselect_palette();
   }
   else {
      blit(bmp, b2, 0, 0, 0, 0, bmp->w, bmp->h);
   }

   destroy_bitmap(bmp);

   return b2;
}
Example #4
0
int main(int argc, char *argv[])
{
    BITMAP *bmp;
    PACKFILE *f;

    if (argc != 3) {
        usage();
        return 1;
    }

    if (install_allegro(SYSTEM_NONE, &errno, atexit) != 0)
        return 1;
    set_color_conversion(COLORCONV_NONE);

    bmp = load_bitmap(argv[1], the_pal);
    if (!bmp) {
        printf("Error reading palette from '%s'\n", argv[1]);
        return 1;
    }

    destroy_bitmap(bmp);

    printf("Palette read from '%s'\n", argv[1]);
    printf("Creating RGB map\n");

    create_rgb_table(&the_map, the_pal, NULL);

    f = pack_fopen(argv[2], F_WRITE);
    if (!f) {
        printf("Error writing '%s'\n", argv[2]);
        return 1;
    }

    pack_fwrite(&the_map, sizeof(the_map), f);
    pack_fclose(f);

    printf("RGB mapping table written to '%s'\n", argv[2]);
    return 0;
}
Example #5
0
File: ex5.c Project: sdeming/alex
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
}
Example #7
0
int main(int argc, char *argv[])
{
   PALETTE pal;
   BITMAP *buffer;
   BITMAP *planet;
   char buf[256];

   if (allegro_init() != 0)
      return 1;
   install_keyboard();
   install_mouse();

   if (set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0) != 0) {
      if (set_gfx_mode(GFX_SAFE, 320, 240, 0, 0) != 0) {
	 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
	 allegro_message("Unable to set any graphic mode\n%s\n",
			 allegro_error);
	 return 1;
      }
   }

   replace_filename(buf, argv[0], "planet.pcx", sizeof(buf));

   planet = load_bitmap(buf, pal);
   if (!planet) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error reading %s\n", buf);
      return 1;
   }

   buffer = create_bitmap(SCREEN_W, SCREEN_H);
   clear_bitmap(buffer);

   set_palette(pal);

   create_rgb_table(&rgb_table, pal, NULL);
   rgb_map = &rgb_table;

   create_light_table(&light_table, pal, 0, 0, 0, NULL);
   color_map = &light_table;

   set_trans_blender(0, 0, 0, 128);

   do {
      poll_mouse();

      draw_gouraud_sprite(buffer, planet, SCREEN_W/2, SCREEN_H/2,
			  distance(SCREEN_W/2, SCREEN_H/2, mouse_x, mouse_y),
			  distance(SCREEN_W/2 + planet->w, SCREEN_H/2,
				   mouse_x, mouse_y),
			  distance(SCREEN_W/2 + planet->w,
				   SCREEN_H/2 + planet->h, mouse_x, mouse_y),
			  distance(SCREEN_W/2, SCREEN_H/2 + planet->h,
				   mouse_x, mouse_y));

      textout_ex(buffer, font, "Gouraud Shaded Sprite Demo", 0, 0,
		 palette_color[10], -1);

      show_mouse(buffer);
      blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
      show_mouse(NULL);

   } while (!keypressed());

   destroy_bitmap(planet);
   destroy_bitmap(buffer);

   return 0;
}