Code Search for Developers
 
 
  

exgui.c from Allegro game programming library at Krugle


Show exgui.c syntax highlighted

/*
 *    Example program for the Allegro library, by Shawn Hargreaves.
 *
 *    This program demonstrates how to use the GUI routines.
 */


#include <stdio.h>

#include "allegro.h"
#include "example.h"



/* we need to load example.dat to access the big al_font_8x8 */
AL_DATAFILE *datafile;


/* for the al_dialog_edit_proc() object */
char the_string[32] = "Change Me!";


/* since we change the al_font_8x8, we need to store a copy of the original one */
AL_FONT *original_font;



/* A custom dialog procedure for the 'change al_font_8x8' button. This uses a
 * simple form of inheritance: it calls al_dialog_button_proc() to do most of
 * the work, so it behaves exactly like any other button, but when the
 * button is clicked and al_dialog_button_proc() returns D_CLOSE, it intercepts
 * the message and changes the al_font_8x8 instead.
 */
int change_font_proc(int msg, AL_DIALOG *d, int c)
{
   int ret;

   /* call the parent object */
   ret = al_dialog_button_proc(msg, d, c);

   /* trap the close return value and change the al_font_8x8 */
   if (ret == D_CLOSE) {
      if (al_font_8x8 == original_font)
	 al_font_8x8 = datafile[BIG_FONT].dat;
      else
	 al_font_8x8 = original_font;

      return D_REDRAW; 
   }

   /* otherwise just return */
   return ret;
}



/* callback function to specify the contents of the listbox */
char *listbox_getter(int index, int *list_size)
{
   static char *strings[] =
   {
      "Zero",  "One",   "Two",   "Three", "Four",  "Five", 
      "Six",   "Seven", "Eight", "Nine",  "Ten"
   };

   if (index < 0) {
      *list_size = 11;
      return NULL;
   }
   else
      return strings[index]; 
}



AL_DIALOG the_dialog[] =
{
   /* (dialog proc)     (x)   (y)   (w)   (h)   (fg)  (bg)  (al_key) (flags)  (d1)                    (d2)  (dp)              (dp2) (dp3) */
   { al_dialog_clear_proc,      0,    0,    0,    0,    255,  0,    0,    0,       0,                      0,    NULL,             NULL, NULL  },
   { al_dialog_edit_proc,       80,   32,   512,  48,   255,  0,    0,    0,       sizeof(the_string)-1,   0,    the_string,       NULL, NULL  },
   { al_dialog_button_proc,     80,   132,  161,  49,   255,  0,    't',  0,       0,                      0,    "&Toggle Me",     NULL, NULL  },
   { al_dialog_list_proc,       360,  100,  207,  207,  255,  0,    0,    0,       0,                      0,    listbox_getter,   NULL, NULL  },
   { change_font_proc,  80,   232,  161,  49,   255,  0,    'f',  D_EXIT,  0,                      0,    "Change &Font",   NULL, NULL  },
   { al_dialog_button_proc,     80,   400,  161,  49,   255,  0,    0,    D_EXIT,  0,                      0,    "OK",             NULL, NULL  },
   { al_dialog_button_proc,     360,  400,  161,  49,   255,  0,    0,    D_EXIT,  0,                      0,    "Cancel",         NULL, NULL  },
   { NULL,              0,    0,    0,    0,    0,    0,    0,    0,       0,                      0,    NULL,             NULL, NULL  }
};



/* index of the listbox object in the dialog array */
#define LISTBOX_OBJECT     3



int main(int argc, char *argv[])
{
   char buf1[256], buf2[80], buf3[80];
   int ret;

   /* initialise everything */
   allegro_init();
   al_install_keyboard(); 
   al_install_mouse();
   al_install_timer();
   if (al_set_gfx_mode(AL_GFX_SAFE, 640, 480, 0, 0) != 0) {
      al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
      al_show_message("Unable to set any graphic mode\n%s\n", al_error);
      return 1;
   }
   al_set_palette(al_desktop_palette);

   /* We set up colors to match al_screen color depth (in case it changed) */
   for (ret = 0; the_dialog[ret].proc; ret++) {
      the_dialog[ret].fg = al_make_color(0, 0, 0);
      the_dialog[ret].bg = al_make_color(255, 255, 255);
   }

   /* load the datafile */
   al_replace_filename(buf1, argv[0], "example.dat", sizeof(buf1));
   datafile = al_load_datafile(buf1);
   if (!datafile) {
      al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
      al_show_message("Error loading %s!\n", buf1);
      return 1;
   }

   /* store a copy of the default al_font_8x8 */
   original_font = al_font_8x8;

   /* do the dialog */
   ret = al_do_dialog(the_dialog, -1);

   /* and report the results */
   sprintf(buf1, "al_do_dialog() returned %d", ret);
   sprintf(buf2, "string is '%s'", the_string);
   sprintf(buf3, "listbox selection is %d", the_dialog[LISTBOX_OBJECT].d1);
   al_show_alert(buf1, buf2, buf3, "OK", NULL, 0, 0);

   al_destroy_datafile(datafile);
   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