Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro ENet problem

This thread is locked; no one can reply to it. rss feed Print
Allegro ENet problem
Bingocat
Member #15,424
December 2013

I've just started learning how to do stuff with enet. I decided to try to use it to make some kind of multiplayer game with allegro. So I started writing it but have gotten stuck because of a weird problem. When enet is receiving files or whatever the allegro window is just white. Like it just isn't drawing anything for some reason. I've figured out that if i change 1000 to 10 in this line allegro is able to draw, but enet can't connect to the server while. (enet_host_service (client, &enetEvent, 1000) > 0) I don't know if it has something to do with the allegro timer or what. This is the code for the client that I got so far.

#SelectExpand
1#include <enet\enet.h> 2#include <allegro5\allegro.h> 3#include <allegro5\allegro_primitives.h> 4#include <iostream> 5 6const int screenWidth = 640, screenHeight = 480; 7 8int main() { 9 bool done = false; 10 bool redraw = true; 11 float FPS = 60; 12 13 al_init(); 14 al_init_primitives_addon(); 15 al_install_keyboard(); 16 enet_initialize(); 17 18 ALLEGRO_DISPLAY *display = NULL; 19 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 20 ALLEGRO_TIMER *timer = NULL; 21 22 display = al_create_display(screenWidth, screenHeight); 23 event_queue = al_create_event_queue(); 24 timer = al_create_timer(1.0 / FPS); 25 26 al_register_event_source(event_queue, al_get_display_event_source(display)); 27 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 28 al_register_event_source(event_queue, al_get_keyboard_event_source()); 29 30 ENetAddress address; 31 ENetPeer *peer; 32 ENetHost * client; 33 ENetEvent enetEvent; 34 35 client = enet_host_create (NULL, 1, 1, 0, 0); 36 if (client == NULL){ 37 fprintf (stderr, "An error occurred while trying to create an ENet client host.\n"); 38 exit (EXIT_FAILURE); 39 } 40 else 41 std::cout << "Client created" << std::endl; 42 43 enet_address_set_host(&address, "my IP here"); 44 address.port = 12345; 45 46 peer = enet_host_connect(client, &address, 1, 0); 47 48 if (peer == NULL) { 49 fprintf(stderr, "No available peers for initializing an ENet connection"); 50 exit(EXIT_FAILURE); 51 } 52 53 al_start_timer(timer); 54 55 while (!done) { 56 ALLEGRO_EVENT ev; 57 al_wait_for_event(event_queue, &ev); 58 59 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 60 done = true; 61 else if (ev.type == ALLEGRO_EVENT_TIMER) { 62 redraw = true; 63 } 64 65 while (enet_host_service (client, &enetEvent, 1000) > 0) { 66 switch(enetEvent.type) { 67 case ENET_EVENT_TYPE_CONNECT: 68 printf("(Client) We got a new connection from %x\n", enetEvent.peer->address.host); 69 break; 70 71 case ENET_EVENT_TYPE_RECEIVE: 72 printf("(Client) Message from server : %s\n", enetEvent.packet->data); 73 enet_packet_destroy(enetEvent.packet); 74 break; 75 76 case ENET_EVENT_TYPE_DISCONNECT: 77 printf("(Client) %s disconnected.\n", enetEvent.peer->data); 78 enetEvent.peer->data = NULL; 79 break; 80 } 81 } 82 83 84 if (redraw && al_is_event_queue_empty(event_queue)) { 85 redraw = false; 86 87 al_draw_filled_circle(screenWidth / 2, screenHeight / 2, 10, al_map_rgb(255, 0, 255)); 88 al_flip_display(); 89 al_clear_to_color(al_map_rgb(0,0,0)); 90 } 91 92 93 } 94 al_destroy_display(display); 95 al_destroy_event_queue(event_queue); 96 al_destroy_timer(timer); 97 98 enet_host_destroy(client); 99}

jmasterx
Member #11,410
October 2009

I think the problem is this:

ENet waits 1000 ms for an event, during which, your Allegro code cannot be executed. If you set it to 10 ms, then your loop will run, but enet polling will fail...

You would almost have to have ENet poll for events on a separate thread.

Bingocat
Member #15,424
December 2013

How would I do that? Is there any other way to fix it that would be more simple? Like just changing some numbers?

jmasterx
Member #11,410
October 2009

There is a library called lacewing which supports udp and tcp and it will do all the polling on a separate thread for you, among other things.

It has 3 ways you can integrate it, including just shoving it in your main loop.

http://lacewing-project.org/

Kris Asick
Member #1,424
July 2001

Allegro itself also has multi-threading functions you could look into using.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Elias
Member #358
May 2000

Can't you call enet_host_service inside the timer event, with 0 as a parameter? That way enet only polls 60 times a second (and you add some artificial lag) - but it would be very simple and may be good enough.

--
"Either help out or stop whining" - Evert

AMCerasoli
Member #11,955
May 2010
avatar

Lacewing doesn't have any problems with endianness? In the case I have a Windows Client and a Linux Server? I was using Enet and Protocol Buffers for my abandoned project but I don't remember why... I'm a bit rusty.

jmasterx
Member #11,410
October 2009

Lacewing is a little endian library as said here http://community.clickteam.com/threads/60311-Lacewing-Build-17/page3

I use it for my Spades game and it works great and is easy to use.

AMCerasoli
Member #11,955
May 2010
avatar

Man, I didn't know you had finish the game! it looks pretty good! I don't know how to play cards, though. You have a lobby chat and all that huh? It says "Join us every Friday at 7 PM" It's possible to join as predator spectator? :P

jmasterx
Member #11,410
October 2009

You can spectate games, yeah. The main issue is a lack of people. I get several people that come in each day, but at different times. Therefore there are never enough people in it at a given time to initiate a game :(

I managed to play a few games with people though and they loved it :)

AMCerasoli
Member #11,955
May 2010
avatar

You need to publish it on other game platforms like Desura, and many more, with time the game will be all over the world! Free games spread really fast ;).

Go to: