How do I draw chat histories of all clients?

first, see attachment(1.mp4)

i did the following coding.

1) type chat message on client
2) send chat message to server
3) receive chat message from client &
send chat message to all clients
4) receive chat message from server &
draw them(soon)

i want to save 5 chat message and draw them.
every time a new chat message is received, the chat list is scrolled and the oldest message is deleted when five are exceeded.
(exam/ 2.png)

what is the best way to implemented it?

Edgar Reynaldo

Use a std::deque. Push front until 5 are received then push front and pop back for every message after that. You can use al_draw_text to draw the text.


Sorry, I use c lang. because it is my final exam of 'c language programming'class in my college.
So I can not use std:: and stl

Do you know other solution?

Frank Drebin

I would use a char array and strcpy():


char messages[MAX_MESSAGE_LENGTH][5];
for (int i=0;i<5;i++) messages[0][i]='\n';

if (newmessagereceived())
   for (int i=0;i<4;i++) strcpy(messages[0][i],messages[0][i+1]);


Could you tell me that's mean?

that code??

Oh, i understand that. Ty!

Edgar Reynaldo

Don't initialize the first character to newline, initialize it to zero...

You don't need a deque, as Frank Drebin showed clearly. A deque is just a concept. It doesn't matter if its C++ or C, it's a container, and you can implement containers in any language you wish. ;)

Frank implemented an array of 5 string buffers. When you get a new message, you move them all 'up' one and overwrite the first. Pretty simple, but effective.

Also, the array dimensions are reversed. It should be an array of 5 MAX_LENGTH buffers, not MAX_LENGTH buffers of size 5...

Frank Drebin

Yeah Edgar is right... This should work better:


char messages[5][MAX_MESSAGE_LENGTH];
for (int i=0;i<5;i++) messages[i][0]=0;

if (newmessagereceived())
   for (int i=0;i<4;i++) strcpy(messages[i][0],messages[i+1][0]);


thank you everyone :D

attachment file(1234.mp4) is the test screen

Thread #617876. Printed from