diff --git a/README.md b/README.md index baf114d..6523bec 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,36 @@ fimuni-c-cpp-examples ===================== -Examples related to talks at FI MUNI \ No newline at end of file +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 diff --git a/allegro/Makefile b/allegro/Makefile new file mode 100644 index 0000000..64f3c59 --- /dev/null +++ b/allegro/Makefile @@ -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 + diff --git a/allegro/example-01.c b/allegro/example-01.c new file mode 100644 index 0000000..259b5b3 --- /dev/null +++ b/allegro/example-01.c @@ -0,0 +1,61 @@ +/** + * Example 01 - Allegro 5 + * Display image + * + * Juraj Michalek - http://georgik.sinusgear.com + */ + +#include +#include +#include + + +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; +} + diff --git a/allegro/example-02.c b/allegro/example-02.c new file mode 100644 index 0000000..149e259 --- /dev/null +++ b/allegro/example-02.c @@ -0,0 +1,82 @@ +/** + * Example 02 - Allegro 5 + * Display image based on keyboard events. + * + * Juraj Michalek - http://georgik.sinusgear.com + */ + +#include +#include +#include + + +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; +} + + diff --git a/allegro/example-03.c b/allegro/example-03.c new file mode 100644 index 0000000..46572f2 --- /dev/null +++ b/allegro/example-03.c @@ -0,0 +1,77 @@ +/** + * Example 02 - Allegro 5 + * Display image based on keyboard events. + * + * Juraj Michalek - http://georgik.sinusgear.com + */ + +#include +#include +#include + + +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; +} + + diff --git a/allegro/smajlik.png b/allegro/smajlik.png new file mode 100644 index 0000000..6d8b23a Binary files /dev/null and b/allegro/smajlik.png differ