OSDN Git Service

index on dev20110528_setting_reduce: 9817594 フィールド名修正
[coroid/inqubus.git] / vhook / chat / chat.c
1 #include <SDL/SDL_endian.h>\r
2 #include <stdio.h>\r
3 \r
4 #include <stdlib.h>\r
5 #include "chat.h"\r
6 #include "../mydef.h"\r
7 #include "../nicodef.h"\r
8 \r
9 int initChat(FILE* log,CHAT* chat,const char* file_path,CHAT_SLOT* slot,int video_length){\r
10         int i;\r
11         int max_no = INTEGER_MIN;\r
12         int min_no = INTEGER_MAX;\r
13         int max_item;\r
14         chat->slot = slot;\r
15         FILE* com_f = fopen(file_path,"rb");\r
16         if(com_f == NULL){\r
17                 fputs("[chat/init]failed to open comment file.\n",log);\r
18                 return FALSE;\r
19         }\r
20         /*\97v\91f\90\94\82Ì\8eæ\93¾*/\r
21         if(fread(&max_item,sizeof(max_item),1,com_f) <= 0){\r
22                 fputs("[chat/init]failed to read the number of comments.\n",log);\r
23                 return FALSE;\r
24         }\r
25         max_item = SDL_SwapLE32(max_item);\r
26         fprintf(log,"[chat/init]%d comments.\n",max_item);\r
27         chat->max_item = max_item;\r
28         //\83A\83C\83e\83\80\94z\97ñ\82Ì\8am\95Û\r
29         chat->item = malloc(sizeof(CHAT_ITEM) * max_item);\r
30         if(chat->item == NULL){\r
31                 fputs("[chat/init]failed to malloc for comment.\n",log);\r
32                 return FALSE;\r
33         }\r
34         /*\8cÂ\95Ê\97v\91f\82Ì\8f\89\8aú\89»*/\r
35         CHAT_ITEM* item;\r
36         int no;\r
37         int vpos;\r
38         int location;\r
39         int size;\r
40         int color;\r
41         int str_length;\r
42         Uint16* str;\r
43         for(i=0;i<max_item;i++){\r
44                 item = &chat->item[i];\r
45                 item->chat = chat;\r
46                 item->showed = FALSE;\r
47                 //\83R\83\81\83\93\83g\94Ô\8d\86\r
48                 if(fread(&no,sizeof(no),1,com_f) <= 0){\r
49                         fputs("[chat/init]failed to read comment number.\n",log);\r
50                         return FALSE;\r
51                 }\r
52                 no = SDL_SwapLE32(no);\r
53                 max_no = MAX(max_no,no);\r
54                 min_no = MIN(min_no,no);\r
55                 //vpos\r
56                 if(fread(&vpos,sizeof(vpos),1,com_f) <= 0){\r
57                         fputs("[chat/init]failed to read comment vpos.\n",log);\r
58                         return FALSE;\r
59                 }\r
60                 vpos = SDL_SwapLE32(vpos);\r
61                 //\95\8e\9a\82Ì\8fê\8f\8a\r
62                 if(fread(&location,sizeof(location),1,com_f) <= 0){\r
63                         fputs("[chat/init]failed to read comment location.\n",log);\r
64                         return FALSE;\r
65                 }\r
66                 location = SDL_SwapLE32(location);\r
67                 //\83T\83C\83Y\r
68                 if(fread(&size,sizeof(size),1,com_f) <= 0){\r
69                         fputs("[chat/init]failed to read comment size.\n",log);\r
70                         return FALSE;\r
71                 }\r
72                 size = SDL_SwapLE32(size);\r
73                 //\90F\r
74                 if(fread(&color,sizeof(color),1,com_f) <= 0){\r
75                         fputs("[chat/init]failed to read comment color.\n",log);\r
76                         return FALSE;\r
77                 }\r
78                 color = SDL_SwapLE32(color);\r
79                 //\95\8e\9a\90\94\r
80                 if(fread(&str_length,sizeof(str_length),1,com_f) <= 0){\r
81                         fputs("[chat/init]failed to read comment length.\n",log);\r
82                         return FALSE;\r
83                 }\r
84                 str_length = SDL_SwapLE32(str_length);\r
85                 //\95\8e\9a\97ñ\r
86                 str = malloc(str_length);\r
87                 if(str == NULL){\r
88                         fputs("[chat/init]failed to malloc for comment text.\n",log);\r
89                         return FALSE;\r
90                 }\r
91                 if(fread(str,str_length,1,com_f) <= 0){\r
92                         fputs("[chat/init]failed to read comment text.\n",log);\r
93                         return FALSE;\r
94                 }\r
95                 //\95Ï\90\94\83Z\83b\83g\r
96                 item->no = no;\r
97                 item->vpos = vpos;\r
98                 item->location = location;\r
99                 item->size = size;\r
100                 item->color = color;\r
101                 item->str = str;\r
102                 /*\93à\95\94\8f\88\97\9d\82æ\82è*/\r
103                 if(location != CMD_LOC_DEF){\r
104                         item->vstart = vpos;\r
105                         item->vend = vpos + TEXT_SHOW_SEC - TEXT_AHEAD_SEC;\r
106                 }else{\r
107                         item->vstart = vpos - TEXT_AHEAD_SEC;\r
108                         item->vend = item->vstart + TEXT_SHOW_SEC;\r
109                 }\r
110                 int fix = item->vend - video_length;\r
111                 if(fix > 0){\r
112                         item->vend -= fix;\r
113                         item->vpos -= fix;\r
114                         item->vstart -= fix;\r
115                         fprintf(log,"[chat/fix]comment %d time adjusted.\n",i);\r
116                 }\r
117                 /*\93à\95\94\8f\88\97\9d\82æ\82è\81@\82¨\82í\82è*/\r
118         }\r
119         fclose(com_f);\r
120         chat->max_no = max_no;\r
121         chat->min_no = min_no;\r
122         return TRUE;\r
123 }\r
124 \r
125 void closeChat(CHAT* chat){\r
126         int i;\r
127         int max_item = chat->max_item;\r
128         for(i=0;i<max_item;i++){\r
129                 free((void*)chat->item[i].str);\r
130         }\r
131         free(chat->item);\r
132 }\r
133 \r
134 /*\r
135  * \83C\83e\83\8c\81[\83^\82ð\83\8a\83Z\83b\83g\82·\82é\81B\r
136  */\r
137 void resetChatIterator(CHAT* chat){\r
138         chat->iterator_index = 0;\r
139 }\r
140 /*\r
141  * \83C\83e\83\8c\81[\83^\82ð\93¾\82é\r
142  */\r
143 CHAT_ITEM* getChatShowed(CHAT* chat,int now_vpos){\r
144         int *i = &chat->iterator_index;\r
145         int max_item = chat->max_item;\r
146         CHAT_ITEM* item;\r
147         for(;*i<max_item;(*i)++){\r
148                 item = &chat->item[*i];\r
149                 if(now_vpos >= item->vstart && now_vpos <= item->vend && !item->showed){\r
150                         return item;\r
151                 }\r
152         }\r
153         return NULL;\r
154 }\r