Show exswitch.c syntax highlighted
/*
* Example program for the Allegro library, by Shawn Hargreaves.
*
* This program shows how to control the console switching mode, and
* let your program run in the background. These functions don't apply
* to every platform, for example you can't control the switching mode
* from a DOS program.
*
* Yes, I know the fractal drawing is very slow: that's the point!
* This is so you can easily check whether it goes on working in the
* background after you switch away from the app.
*/
#include <math.h>
#include "allegro.h"
/* there is no particular reason to use sub-bitmaps here: I just do it as a
* stress-test, to make sure the switching code will handle them correctly.
*/
AL_BITMAP *text_area;
AL_BITMAP *graphics_area;
int in_callback = 0;
int out_callback = 0;
/* timer callbacks should go on running when we are in background mode */
volatile int counter = 0;
void increment_counter(void)
{
counter++;
}
AL_END_OF_FUNCTION(increment_counter);
/* displays a text message in the scrolling part of the al_screen */
void show_msg(char *msg)
{
al_acquire_bitmap(text_area);
al_blit(text_area, text_area, 0, 8, 0, 0, text_area->w, text_area->h-8);
al_draw_rect_fill(text_area, 0, text_area->h-8, text_area->w, text_area->h, al_palette_color[0]);
if (msg)
al_put_text_centre(text_area, al_font_8x8, msg, text_area->w/2, text_area->h-8, al_palette_color[255]);
al_release_bitmap(text_area);
}
/* displays the current switch mode setting */
void show_switch_mode(void)
{
switch (al_get_display_switch_mode()) {
case AL_SWITCH_NONE:
show_msg("Current mode is AL_SWITCH_NONE");
break;
case AL_SWITCH_PAUSE:
show_msg("Current mode is AL_SWITCH_PAUSE");
break;
case AL_SWITCH_AMNESIA:
show_msg("Current mode is AL_SWITCH_AMNESIA");
break;
case AL_SWITCH_BACKGROUND:
show_msg("Current mode is AL_SWITCH_BACKGROUND");
break;
case AL_SWITCH_BACKAMNESIA:
show_msg("Current mode is AL_SWITCH_BACKAMNESIA");
break;
default:
show_msg("Eeek! Unknown switch mode...");
break;
}
}
/* callback for switching back to our program */
void switch_in_callback(void)
{
in_callback++;
}
/* callback for switching away from our program */
void switch_out_callback(void)
{
out_callback++;
}
/* changes the display switch mode */
void set_sw_mode(int mode)
{
if (al_set_display_switch_mode(mode) != 0) {
show_msg("Error changing switch mode");
show_msg(NULL);
return;
}
show_switch_mode();
if (al_set_display_switch_callback(AL_SWITCH_IN, switch_in_callback) == 0)
show_msg("AL_SWITCH_IN callback activated");
else
show_msg("AL_SWITCH_IN callback not available");
if (al_set_display_switch_callback(AL_SWITCH_OUT, switch_out_callback) == 0)
show_msg("AL_SWITCH_OUT callback activated");
else
show_msg("AL_SWITCH_OUT callback not available");
show_msg(NULL);
}
/* draws some graphics, for no particular reason at all */
void draw_pointless_graphics(void)
{
static int x=0, y=0;
float zr, zi, cr, ci, tr, ti;
int c;
if ((!x) && (!y))
al_clear_to_color(graphics_area, al_palette_color[255]);
cr = ((float)x / (float)graphics_area->w - 0.75) * 2.0;
ci = ((float)y / (float)graphics_area->h - 0.5) * 2.0;
zr = 0;
zi = 0;
for (c=0; c<100; c++) {
tr = zr*zr - zi*zi;
ti = zr*zi*2;
zr = tr + cr;
zi = ti + ci;
}
if ((zi != zi) || (zr != zr))
c = 0;
else
c = sqrt(zi*zi + zr*zr) * 256;
if (c > 255)
c = 255;
al_put_pixel(graphics_area, x, y, al_make_color(c, c, c));
x++;
if (x >= graphics_area->w) {
x = 0;
y++;
if (y >= graphics_area->h)
y = 0;
}
}
int main(void)
{
AL_PALETTE pal;
int finished = FALSE;
int last_counter = 0;
int c = AL_GFX_AUTODETECT;
int w, h, bpp, i;
allegro_init();
al_install_keyboard();
al_install_mouse();
al_install_timer();
if (al_set_gfx_mode(AL_GFX_SAFE, 320, 200, 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);
w = AL_SCREEN_W;
h = AL_SCREEN_H;
bpp = al_bitmap_color_depth(al_screen);
if (!al_select_gfx_mode(&c, &w, &h, &bpp)) {
al_remove_system();
return 1;
}
al_set_color_depth(bpp);
if (al_set_gfx_mode(c, w, h, 0, 0) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error setting graphics mode\n%s\n", al_error);
return 1;
}
for (i=0; i<256; i++)
pal[i].r = pal[i].g = pal[i].b = i/4;
al_set_palette(pal);
al_text_mode(al_palette_color[0]);
text_area = al_create_sub_bitmap(al_screen, 0, 0, AL_SCREEN_W, AL_SCREEN_H/2);
graphics_area = al_create_sub_bitmap(al_screen, 0, AL_SCREEN_H/2, AL_SCREEN_W/2, AL_SCREEN_H/2);
LOCK_VARIABLE(counter);
LOCK_FUNCTION(increment_counter);
al_create_timer(increment_counter, MSEC_TO_TIMER(10));
show_msg("Console switching test");
show_msg("Press 1-5 to change mode");
show_msg(NULL);
show_switch_mode();
show_msg(NULL);
while (!finished) {
if (counter != last_counter) {
last_counter = counter;
al_acquire_screen();
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W*3/4, AL_SCREEN_H*3/4, al_palette_color[255], "Time: %d", last_counter);
al_release_screen();
al_acquire_bitmap(graphics_area);
draw_pointless_graphics();
al_release_bitmap(graphics_area);
}
if (al_key_pressed()) {
switch (al_read_key() & 255) {
case '1':
set_sw_mode(AL_SWITCH_NONE);
break;
case '2':
set_sw_mode(AL_SWITCH_PAUSE);
break;
case '3':
set_sw_mode(AL_SWITCH_AMNESIA);
break;
case '4':
set_sw_mode(AL_SWITCH_BACKGROUND);
break;
case '5':
set_sw_mode(AL_SWITCH_BACKAMNESIA);
break;
case 27:
finished = TRUE;
break;
}
}
while (in_callback > 0) {
in_callback--;
show_msg("AL_SWITCH_IN callback");
show_msg(NULL);
}
while (out_callback > 0) {
out_callback--;
show_msg("AL_SWITCH_OUT callback");
show_msg(NULL);
}
}
al_destroy_bitmap(text_area);
al_destroy_bitmap(graphics_area);
return 0;
}
AL_END_OF_MAIN();
See more files for this project here