Frameless window
wracker

Morning everyone,

I've found this topic regarding borderless windows posted by Mark Oates on this forum and was wondering if anyone tried something similar.

The fact is that Mark's example isn't quite working and I would be more than glad if
someone could give me some pointers as where the snag is.

I'm trying to figure out why the the window keeps flipping out whenever I'm dragginf the window?

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3#include <cstdlib> 4#include <iostream> 5#include <stdio.h> 6 7 8class Mouse 9{ 10public: 11 bool dragging; 12 int drag_start_x; 13 int drag_start_y; 14 int drag_offset_x; 15 int drag_offset_y; 16 17 int x; 18 int y; 19 20 Mouse():dragging(false), drag_start_x(0), drag_start_y(0) {} 21 22 void start_drag(int x, int y) 23 { 24 drag_start_x = x; 25 drag_start_y = y; 26 this->x = x; 27 this->y = y; 28 dragging = true; 29 std::cout << "starting drag \n"; 30 } 31 32 void end_drag() 33 { 34 dragging = false; 35 drag_start_x = 0; 36 drag_start_y = 0; 37 std::cout << "ending drag \n"; 38 } 39}; 40 41 42int main(int argc, char *argv[]) 43{ 44 ALLEGRO_DISPLAY *display = NULL; 45 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 46 ALLEGRO_TIMER *timer = NULL; 47 48 bool done = false; 49 50 if(!al_init()) { 51 printf("failed to initialize allegro!\n"); 52 return -1; 53 } 54 55 al_set_new_display_flags(ALLEGRO_NOFRAME); 56 57 display = al_create_display(320, 240); 58 if(!display) { 59 printf("failed to create display!\n"); 60 al_destroy_timer(timer); 61 return -1; 62 } 63 64 timer = al_create_timer(1.0 / 60); 65 if(!timer) { 66 printf("failed to create timer!\n"); 67 return -1; 68 } 69 70 al_set_target_bitmap(al_get_backbuffer(display)); 71 72 if(!al_install_mouse()) { 73 fprintf(stderr, "failed to initialize the mouse!\n"); 74 return -1; 75 } 76 77 event_queue = al_create_event_queue(); 78 if(!event_queue) { 79 fprintf(stderr, "failed to create event_queue!\n"); 80 al_destroy_display(display); 81 al_destroy_timer(timer); 82 return -1; 83 } 84 85 al_register_event_source(event_queue, al_get_display_event_source(display)); 86 al_register_event_source(event_queue, al_get_mouse_event_source()); 87 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 88 al_clear_to_color(al_map_rgb(0,0,0)); 89 al_flip_display(); 90 al_start_timer(timer); 91 92 Mouse mouse; 93 94 while(!done) 95 { 96 ALLEGRO_EVENT al_event; 97 al_wait_for_event(event_queue, &al_event); 98 99 switch(al_event.type) 100 { 101 case ALLEGRO_EVENT_MOUSE_AXES: 102 if (mouse.dragging) 103 { 104 int x, y; 105 al_get_window_position(display, &x, &y); 106 al_set_window_position(display, x+al_event.mouse.dx, y+al_event.mouse.dy); 107 al_set_mouse_xy(display, al_event.mouse.x - al_event.mouse.dx, al_event.mouse.y - al_event.mouse.dy); 108 mouse.x = al_event.mouse.x - al_event.mouse.dx; 109 mouse.y = al_event.mouse.y - al_event.mouse.dy; 110 } 111 break; 112 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: 113 mouse.start_drag(al_event.mouse.x, al_event.mouse.y); 114 break; 115 case ALLEGRO_EVENT_MOUSE_BUTTON_UP: 116 mouse.end_drag(); 117 break; 118 case ALLEGRO_EVENT_KEY_DOWN: 119 done = true; 120 break; 121 case ALLEGRO_EVENT_TIMER: 122 al_clear_to_color(al_map_rgb(0,0,0)); 123 al_flip_display(); 124 break; 125 default: 126 break; 127 } 128 } 129 130 131 return 0; 132}

Thanks in advance,
Alex

Trent Gamblin

Have you looked at ex_noframe.c from the Allegro source? Here it is:

#SelectExpand
1#include "allegro5/allegro.h" 2#include "allegro5/allegro_image.h" 3 4#include <stdio.h> 5 6#include "common.c" 7 8int main(void) 9{ 10 ALLEGRO_DISPLAY *display; 11 ALLEGRO_BITMAP *bitmap; 12 ALLEGRO_EVENT_QUEUE *events; 13 ALLEGRO_EVENT event; 14 bool down = false; 15 int down_x = 0, down_y = 0; 16 ALLEGRO_TIMER *timer; 17 18 if (!al_init()) { 19 abort_example("Could not init Allegro.\n"); 20 return 1; 21 } 22 23 al_install_mouse(); 24 al_install_keyboard(); 25 al_init_image_addon(); 26 27 al_set_new_display_flags(ALLEGRO_FRAMELESS); 28 display = al_create_display(300, 200); 29 if (!display) { 30 abort_example("Error creating display\n"); 31 return 1; 32 } 33 34 bitmap = al_load_bitmap("data/fakeamp.bmp"); 35 if (!bitmap) { 36 abort_example("Error loading fakeamp.bmp\n"); 37 return 1; 38 } 39 40 timer = al_create_timer(1.0f/30.0f); 41 42 events = al_create_event_queue(); 43 al_register_event_source(events, al_get_mouse_event_source()); 44 al_register_event_source(events, al_get_keyboard_event_source()); 45 al_register_event_source(events, al_get_display_event_source(display)); 46 al_register_event_source(events, al_get_timer_event_source(timer)); 47 48 al_start_timer(timer); 49 50 for (;;) { 51 al_wait_for_event(events, &event); 52 if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { 53 if (event.mouse.button == 1 && event.mouse.x) { 54 down = true; 55 down_x = event.mouse.x; 56 down_y = event.mouse.y; 57 } 58 if (event.mouse.button == 2) { 59 al_set_display_flag(display, ALLEGRO_FRAMELESS, 60 !(al_get_display_flags(display) & ALLEGRO_FRAMELESS)); 61 } 62 } 63 else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 64 break; 65 } 66 else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 67 if (event.mouse.button == 1) { 68 down = false; 69 } 70 } 71 else if (event.type == ALLEGRO_EVENT_MOUSE_AXES) { 72 if (down) { 73 int cx, cy; 74 if (al_get_mouse_cursor_position(&cx, &cy)) { 75 al_set_window_position(display, cx - down_x, cy - down_y); 76 } 77 } 78 } 79 else if (event.type == ALLEGRO_EVENT_KEY_DOWN && 80 event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 81 break; 82 } 83 else if (event.type == ALLEGRO_EVENT_TIMER) { 84 al_draw_bitmap(bitmap, 0, 0, 0); 85 al_flip_display(); 86 } 87 } 88 89 al_destroy_timer(timer); 90 al_destroy_event_queue(events); 91 al_destroy_display(display); 92 93 return 0; 94}

wracker

ohh that works perfectly!! :) ..Thanks mate!

Thread #612905. Printed from Allegro.cc