Add Allegro examples

This commit is contained in:
Juraj Michalek
2012-11-25 18:26:48 +01:00
parent 2f8a23729c
commit c242d92abf
6 changed files with 278 additions and 1 deletions

View File

@@ -1,4 +1,36 @@
fimuni-c-cpp-examples
=====================
Examples related to talks at FI MUNI
Examples related to talks at FI MUNI.
Allegro
-------
Three examples for Allegro 5.0 library.
- example-01 - display smiley
- example-02 - display smiley when pressed space
- example-03 - smiley follows mouse
Requirements: Allegro 5.0 (works also with 5.1)
Package for Debian: liballegro5-dev
How to run:
cd allegro
make
./example-01
./example-02
./example-03
GTK+
----
Helloworld example from http://en.wikipedia.org/wiki/Gtk.
Requires: GTK+-3
Package for Debian: libgtk-3-dev
How to run:
cd gtk
make
./helloworld

25
allegro/Makefile Normal file
View File

@@ -0,0 +1,25 @@
EXAMPLE_OBJECTS_01 = example-01.o
EXAMPLE_OBJECTS_02 = example-02.o
EXAMPLE_OBJECTS_03 = example-03.o
CFLAGS = `pkg-config --cflags allegro-5.0 allegro_image-5.0`
LDADD = `pkg-config --libs allegro-5.0 allegro_image-5.0`
all: example-01 example-02 example-03
example-01: $(EXAMPLE_OBJECTS_01)
${CC} -o example-01 ${EXAMPLE_OBJECTS_01} ${LDADD}
example-02: $(EXAMPLE_OBJECTS_02)
${CC} -o example-02 ${EXAMPLE_OBJECTS_02} ${LDADD}
example-03: $(EXAMPLE_OBJECTS_03)
${CC} -o example-03 ${EXAMPLE_OBJECTS_03} ${LDADD}
.c.o:
${CC} ${CFLAGS} -c $<
clean:
rm -f example-01 example-02 example-03 *.o

61
allegro/example-01.c Normal file
View File

@@ -0,0 +1,61 @@
/**
* Example 01 - Allegro 5
* Display image
*
* Juraj Michalek - http://georgik.sinusgear.com
*/
#include <allegro5/allegro5.h>
#include <allegro5/allegro_image.h>
#include <stdio.h>
int main()
{
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_DISPLAY *display;
ALLEGRO_EVENT event;
ALLEGRO_BITMAP *image;
al_init();
al_install_keyboard();
al_init_image_addon();
display = al_create_display(640, 480);
if (!display)
{
return 2;
}
// Create Event queue
event_queue = al_create_event_queue();
// Register Keyboard to queue
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
// Clear screen
al_clear_to_color(al_map_rgb_f(1.0, 1.0, 1.0));
// Load and display image
image = al_load_bitmap("smajlik.png");
if (image != NULL)
{
al_draw_bitmap(image, 10, 10, 0);
} else {
printf("No bitmap.\n");
return 1;
}
al_flip_display();
// Wait until some key is pressed
al_wait_for_event(event_queue, &event);
// Release
al_destroy_bitmap(image);
al_uninstall_keyboard();
al_uninstall_system();
return 0;
}

82
allegro/example-02.c Normal file
View File

@@ -0,0 +1,82 @@
/**
* Example 02 - Allegro 5
* Display image based on keyboard events.
*
* Juraj Michalek - http://georgik.sinusgear.com
*/
#include <allegro5/allegro5.h>
#include <allegro5/allegro_image.h>
#include <stdio.h>
int main()
{
bool isStopSignal = false;
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_DISPLAY *display;
ALLEGRO_EVENT event;
ALLEGRO_BITMAP *image;
ALLEGRO_COLOR white = al_map_rgb_f(1.0, 1.0, 1.0);
al_init();
al_install_keyboard();
al_init_image_addon();
display = al_create_display(640, 480);
if (!display)
{
return 2;
}
// Create Event queue
event_queue = al_create_event_queue();
// Register Keyboard to queue
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
// Clear screen
al_clear_to_color(white);
// Load and display image
image = al_load_bitmap("smajlik.png");
if (image == NULL)
{
printf("No bitmap.\n");
return 1;
}
do {
al_get_next_event(event_queue, &event);
// Handle SPACE key - display smiley in case of pressed space
// Clear screen in case of other key
if (event.keyboard.keycode == ALLEGRO_KEY_SPACE)
{
al_draw_bitmap(image, 10, 10, 0);
} else {
// Clear screen
al_clear_to_color(white);
}
al_flip_display();
// Handle ESC key
if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
{
isStopSignal = true;
}
} while (!isStopSignal);
// Release
al_destroy_bitmap(image);
al_uninstall_keyboard();
al_uninstall_system();
return 0;
}

77
allegro/example-03.c Normal file
View File

@@ -0,0 +1,77 @@
/**
* Example 02 - Allegro 5
* Display image based on keyboard events.
*
* Juraj Michalek - http://georgik.sinusgear.com
*/
#include <allegro5/allegro5.h>
#include <allegro5/allegro_image.h>
#include <stdio.h>
int main()
{
bool isStopSignal = false;
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_DISPLAY *display;
ALLEGRO_EVENT event;
ALLEGRO_BITMAP *image;
ALLEGRO_MOUSE_STATE msestate;
ALLEGRO_KEYBOARD_STATE kbdstate;
ALLEGRO_COLOR white = al_map_rgb_f(1.0, 1.0, 1.0);
al_init();
al_install_keyboard();
al_init_image_addon();
display = al_create_display(640, 480);
if (!display)
{
return 2;
}
// Create Event queue
event_queue = al_create_event_queue();
// Register Keyboard to queue
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
// Register Mouse
al_install_mouse();
// Clear screen
al_clear_to_color(white);
// Load and display image
image = al_load_bitmap("smajlik.png");
if (image == NULL)
{
printf("No bitmap.\n");
return 1;
}
do {
al_get_mouse_state(&msestate);
al_get_keyboard_state(&kbdstate);
al_clear_to_color(white);
al_draw_bitmap(image, msestate.x, msestate.y, 0);
al_flip_display();
} while (!al_key_down(&kbdstate, ALLEGRO_KEY_ESCAPE));
// Release
al_destroy_bitmap(image);
al_uninstall_mouse();
al_uninstall_keyboard();
al_uninstall_system();
return 0;
}

BIN
allegro/smajlik.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB