OSDN Git Service

Merge branch 'master' into dev20110528_setting_reduce
[coroid/inqubus.git] / vhook / chat / chat.c
1 #include <SDL/SDL_endian.h>
2 #include <stdio.h>
3
4 #include <stdlib.h>
5 #include "chat.h"
6 #include "../mydef.h"
7 #include "../nicodef.h"
8
9 int initChat(FILE* log,CHAT* chat,const char* file_path,CHAT_SLOT* slot,int video_length){
10         int i;
11         int max_no = INTEGER_MIN;
12         int min_no = INTEGER_MAX;
13         int max_item;
14         chat->slot = slot;
15         FILE* com_f = fopen(file_path,"rb");
16         if(com_f == NULL){
17                 fputs("[chat/init]failed to open comment file.\n",log);
18                 return FALSE;
19         }
20         /*要素数の取得*/
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);
23                 return FALSE;
24         }
25         max_item = SDL_SwapLE32(max_item);
26         fprintf(log,"[chat/init]%d comments.\n",max_item);
27         chat->max_item = max_item;
28         //アイテム配列の確保
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);
32                 return FALSE;
33         }
34         /*個別要素の初期化*/
35         CHAT_ITEM* item;
36         int no;
37         int vpos;
38         int location;
39         int size;
40         int color;
41         int str_length;
42         Uint16* str;
43         for(i=0;i<max_item;i++){
44                 item = &chat->item[i];
45                 item->chat = chat;
46                 item->showed = FALSE;
47                 //コメント番号
48                 if(fread(&no,sizeof(no),1,com_f) <= 0){
49                         fputs("[chat/init]failed to read comment number.\n",log);
50                         return FALSE;
51                 }
52                 no = SDL_SwapLE32(no);
53                 max_no = MAX(max_no,no);
54                 min_no = MIN(min_no,no);
55                 //vpos
56                 if(fread(&vpos,sizeof(vpos),1,com_f) <= 0){
57                         fputs("[chat/init]failed to read comment vpos.\n",log);
58                         return FALSE;
59                 }
60                 vpos = SDL_SwapLE32(vpos);
61                 //文字の場所
62                 if(fread(&location,sizeof(location),1,com_f) <= 0){
63                         fputs("[chat/init]failed to read comment location.\n",log);
64                         return FALSE;
65                 }
66                 location = SDL_SwapLE32(location);
67                 //サイズ
68                 if(fread(&size,sizeof(size),1,com_f) <= 0){
69                         fputs("[chat/init]failed to read comment size.\n",log);
70                         return FALSE;
71                 }
72                 size = SDL_SwapLE32(size);
73                 //色
74                 if(fread(&color,sizeof(color),1,com_f) <= 0){
75                         fputs("[chat/init]failed to read comment color.\n",log);
76                         return FALSE;
77                 }
78                 color = SDL_SwapLE32(color);
79                 //文字数
80                 if(fread(&str_length,sizeof(str_length),1,com_f) <= 0){
81                         fputs("[chat/init]failed to read comment length.\n",log);
82                         return FALSE;
83                 }
84                 str_length = SDL_SwapLE32(str_length);
85                 //文字列
86                 str = malloc(str_length);
87                 if(str == NULL){
88                         fputs("[chat/init]failed to malloc for comment text.\n",log);
89                         return FALSE;
90                 }
91                 if(fread(str,str_length,1,com_f) <= 0){
92                         fputs("[chat/init]failed to read comment text.\n",log);
93                         return FALSE;
94                 }
95                 //変数セット
96                 item->no = no;
97                 item->vpos = vpos;
98                 item->location = location;
99                 item->size = size;
100                 item->color = color;
101                 item->str = str;
102                 /*内部処理より*/
103                 if(location != CMD_LOC_DEF){
104                         item->vstart = vpos;
105                         item->vend = vpos + TEXT_SHOW_SEC - TEXT_AHEAD_SEC;
106                 }else{
107                         item->vstart = vpos - TEXT_AHEAD_SEC;
108                         item->vend = item->vstart + TEXT_SHOW_SEC;
109                 }
110                 int fix = item->vend - video_length;
111                 if(fix > 0){
112                         item->vend -= fix;
113                         item->vpos -= fix;
114                         item->vstart -= fix;
115                         fprintf(log,"[chat/fix]comment %d time adjusted.\n",i);
116                 }
117                 /*内部処理より おわり*/
118         }
119         fclose(com_f);
120         chat->max_no = max_no;
121         chat->min_no = min_no;
122         return TRUE;
123 }
124
125 void closeChat(CHAT* chat){
126         int i;
127         int max_item = chat->max_item;
128         for(i=0;i<max_item;i++){
129                 free((void*)chat->item[i].str);
130         }
131         free(chat->item);
132 }
133
134 /*
135  * イテレータをリセットする。
136  */
137 void resetChatIterator(CHAT* chat){
138         chat->iterator_index = 0;
139 }
140 /*
141  * イテレータを得る
142  */
143 CHAT_ITEM* getChatShowed(CHAT* chat,int now_vpos){
144         int *i = &chat->iterator_index;
145         int max_item = chat->max_item;
146         CHAT_ITEM* item;
147         for(;*i<max_item;(*i)++){
148                 item = &chat->item[*i];
149                 if(now_vpos >= item->vstart && now_vpos <= item->vend && !item->showed){
150                         return item;
151                 }
152         }
153         return NULL;
154 }