Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How do I draw chat histories of all clients?

This thread is locked; no one can reply to it. rss feed Print
How do I draw chat histories of all clients?
foretion
Member #17,091
June 2019

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
Major Reynaldo
May 2007
avatar

foretion
Member #17,091
June 2019

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
Member #2,987
December 2002
avatar

I would use a char array and strcpy():

#define MAX_MESSAGE_LENGTH 128

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]);
   strcpy(messages[0][4],newmessagetext());
}

foretion
Member #17,091
June 2019

Could you tell me that's mean?
messages[0][i]='\n';

that code??

----
Oh, i understand that. Ty!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

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
Member #2,987
December 2002
avatar

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

#define MAX_MESSAGE_LENGTH 128

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]);
   strcpy(messages[4][0],newmessagetext());
}

foretion
Member #17,091
June 2019

thank you everyone :D

attachment file(1234.mp4) is the test screen

Go to: