Code Search for Developers
 
 
  

exscroll.c from Allegro game programming library at Krugle


Show exscroll.c syntax highlighted

/*
 *    Example program for the Allegro library, by Shawn Hargreaves.
 *
 *    This program demonstrates how to use hardware scrolling and split
 *    screens in mode-X. The split al_screen part only works on DOS and Linux
 *    console platforms, but the scrolling should work on anything that
 *    supports large virtual screens.
 */


#include <stdio.h>

#include "allegro.h"



int main()
{
   AL_BITMAP *scroller, *status_bar;
   int counter = 0;
   char tmp[80];
   AL_RGB black = { 0, 0, 0, 0 };
   int x = 0;
   int next_x;
   int h = 100;
   int split_h = 0;

   allegro_init();
   al_install_keyboard();

   #ifdef AL_GFX_MODEX

      /* create a nice wide virtual al_screen, and split it at al_draw_line 200 */
      if (al_set_gfx_mode(AL_GFX_MODEX, 320, 240, 640, 240) != 0) {
	 if (al_set_gfx_mode(AL_GFX_AUTODETECT, 320, 240, 640, 240) != 0) {
	    al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
	    al_show_message("Unable to set a 320x240 mode with 640x240 virtual dimensions\n");
	    return 1;
	 }
      }

      if (gfx_driver->id == AL_GFX_MODEX) {
	 al_split_modex_screen(200);
	 al_scroll_screen(0, 40);
	 split_h = 40;
      }

   #else

      /* no mode-X on this platform, so just make do the best we can */
      if (al_set_gfx_mode(AL_GFX_AUTODETECT, 320, 240, 640, 240) != 0) {
	 al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
	 al_show_message("Unable to set a 320x240 mode with 640x240 virtual dimensions\n");
	 return 1;
      }

   #endif

   /* the scrolling area is sized 640x200 and starts at al_draw_line 40 */
   scroller = al_create_sub_bitmap(al_screen, 0, split_h, AL_SCREEN_W*2, AL_SCREEN_H-split_h);

   /* the status bar is sized 320x40 and starts at al_draw_line 0 */
   if (split_h > 0)
      status_bar = al_create_sub_bitmap(al_screen, 0, 0, AL_SCREEN_W, split_h);
   else
      status_bar = NULL;

   al_set_palette(al_desktop_palette);
   al_set_palette_color(0, &black);

   if (status_bar)
      al_put_text(status_bar, al_font_8x8, "This area isn't scrolling", 8, 8, 1);

   al_draw_rect_fill(scroller, 0, 0, AL_SCREEN_W, 100, 6);
   al_draw_rect_fill(scroller, 0, 100, AL_SCREEN_W, AL_SCREEN_H-split_h, 2);

   do {
      /* update the status bar */
      if (status_bar) {
	 sprintf(tmp, "Counter = %d", counter++);
	 al_acquire_bitmap(status_bar);
	 al_put_text(status_bar, al_font_8x8, tmp, 8, 20, 1);
	 al_release_bitmap(status_bar);
      }

      /* advance the scroller, wrapping every 320 pixels */
      next_x = x + 1;
      if (next_x >= 320)
	 next_x = 0;

      /* draw another column of the landscape */
      al_acquire_bitmap(scroller);
      al_draw_vline(scroller, next_x+AL_SCREEN_W-1, 0, h, 6);
      al_draw_vline(scroller, next_x+AL_SCREEN_W-1, h+1, AL_SCREEN_H-split_h, 2);
      al_release_bitmap(scroller);

      /* scroll the al_screen */
      al_scroll_screen(next_x, split_h);

      /* duplicate the landscape column so we can wrap the scroller */
      if (next_x > 0) {
	 al_acquire_bitmap(scroller);
	 al_draw_vline(scroller, x, 0, h, 6);
	 al_draw_vline(scroller, x, h+1, AL_SCREEN_H-split_h, 2);
	 al_release_bitmap(scroller);
      }

      /* randomly alter the landscape position */
      if (rand()&1) {
	 if (h > 5)
	    h--;
      }
      else {
	 if (h < 195)
	    h++;
      }

      x = next_x;

   } while (!al_key_pressed());

   al_destroy_bitmap(scroller);

   if (status_bar)
      al_destroy_bitmap(status_bar);

   al_clear_keybuf();

   return 0;
}

AL_END_OF_MAIN();




See more files for this project here

Allegro game programming library

Allegro is a cross-platform library intended for use in computer games and other types of multimedia programming.

Project homepage: http://sourceforge.net/projects/alleg
Programming language(s): Assembly,C,Shell Script
License: other

  allegro.pcx
  ex12bit.c
  ex3buf.c
  ex3d.c
  exaccel.c
  exalpha.c
  example.dat
  example.h
  examples.txt
  exbitmap.c
  exblend.c
  excamera.c
  excolmap.c
  excustom.c
  exdata.c
  exdbuf.c
  exdodgy.c
  exexedat.c
  exfixed.c
  exflame.c
  exflip.c
  exgui.c
  exhello.c
  exjoy.c
  exkeys.c
  exlights.c
  exmem.c
  exmidi.c
  exmouse.c
  expal.c
  expat.c
  exquat.c
  exrgbhsv.c
  exsample.c
  exscale.c
  exscn3d.c
  exscroll.c
  exshade.c
  exspline.c
  exsprite.c
  exstars.c
  exstream.c
  exswitch.c
  extimer.c
  extrans.c
  extruec.c
  exunicod.c
  exupdate.c
  exxfade.c
  exzbuf.c
  mysha.pcx
  planet.pcx
  running.dat
  running.h
  unifont.dat