1 #include <SDL/SDL_endian.h>
7 #include "../nicodef.h"
9 int initChat(FILE* log,CHAT* chat,const char* file_path,CHAT_SLOT* slot,int video_length){
11 int max_no = INTEGER_MIN;
12 int min_no = INTEGER_MAX;
15 FILE* com_f = fopen(file_path,"rb");
17 fputs("[chat/init]failed to open comment file.\n",log);
21 if(fread(&max_item,sizeof(max_item),1,com_f) <= 0){
22 fputs("[chat/init]failed to read the number of comments.\n",log);
25 max_item = SDL_SwapLE32(max_item);
26 fprintf(log,"[chat/init]%d comments.\n",max_item);
27 chat->max_item = max_item;
29 chat->item = malloc(sizeof(CHAT_ITEM) * max_item);
30 if(chat->item == NULL){
31 fputs("[chat/init]failed to malloc for comment.\n",log);
43 for(i=0;i<max_item;i++){
44 item = &chat->item[i];
48 if(fread(&no,sizeof(no),1,com_f) <= 0){
49 fputs("[chat/init]failed to read comment number.\n",log);
52 no = SDL_SwapLE32(no);
53 max_no = MAX(max_no,no);
54 min_no = MIN(min_no,no);
56 if(fread(&vpos,sizeof(vpos),1,com_f) <= 0){
57 fputs("[chat/init]failed to read comment vpos.\n",log);
60 vpos = SDL_SwapLE32(vpos);
62 if(fread(&location,sizeof(location),1,com_f) <= 0){
63 fputs("[chat/init]failed to read comment location.\n",log);
66 location = SDL_SwapLE32(location);
68 if(fread(&size,sizeof(size),1,com_f) <= 0){
69 fputs("[chat/init]failed to read comment size.\n",log);
72 size = SDL_SwapLE32(size);
74 if(fread(&color,sizeof(color),1,com_f) <= 0){
75 fputs("[chat/init]failed to read comment color.\n",log);
78 color = SDL_SwapLE32(color);
80 if(fread(&str_length,sizeof(str_length),1,com_f) <= 0){
81 fputs("[chat/init]failed to read comment length.\n",log);
84 str_length = SDL_SwapLE32(str_length);
86 str = malloc(str_length);
88 fputs("[chat/init]failed to malloc for comment text.\n",log);
91 if(fread(str,str_length,1,com_f) <= 0){
92 fputs("[chat/init]failed to read comment text.\n",log);
98 item->location = location;
103 if(location != CMD_LOC_DEF){
105 item->vend = vpos + TEXT_SHOW_SEC - TEXT_AHEAD_SEC;
107 item->vstart = vpos - TEXT_AHEAD_SEC;
108 item->vend = item->vstart + TEXT_SHOW_SEC;
110 int fix = item->vend - video_length;
115 fprintf(log,"[chat/fix]comment %d time adjusted.\n",i);
120 chat->max_no = max_no;
121 chat->min_no = min_no;
125 void closeChat(CHAT* chat){
127 int max_item = chat->max_item;
128 for(i=0;i<max_item;i++){
129 free((void*)chat->item[i].str);
137 void resetChatIterator(CHAT* chat){
138 chat->iterator_index = 0;
143 CHAT_ITEM* getChatShowed(CHAT* chat,int now_vpos){
144 int *i = &chat->iterator_index;
145 int max_item = chat->max_item;
147 for(;*i<max_item;(*i)++){
148 item = &chat->item[*i];
149 if(now_vpos >= item->vstart && now_vpos <= item->vend && !item->showed){