-#include <SDL/SDL_endian.h>\r
-#include <stdio.h>\r
-\r
-#include <stdlib.h>\r
-#include "chat.h"\r
-#include "../mydef.h"\r
-#include "../nicodef.h"\r
-\r
-int initChat(FILE* log,CHAT* chat,const char* file_path,CHAT_SLOT* slot,int video_length){\r
- int i;\r
- int max_no = INTEGER_MIN;\r
- int min_no = INTEGER_MAX;\r
- int max_item;\r
- chat->slot = slot;\r
- FILE* com_f = fopen(file_path,"rb");\r
- if(com_f == NULL){\r
- fputs("[chat/init]failed to open comment file.\n",log);\r
- return FALSE;\r
- }\r
- /*\97v\91f\90\94\82Ì\8eæ\93¾*/\r
- if(fread(&max_item,sizeof(max_item),1,com_f) <= 0){\r
- fputs("[chat/init]failed to read the number of comments.\n",log);\r
- return FALSE;\r
- }\r
- max_item = SDL_SwapLE32(max_item);\r
- fprintf(log,"[chat/init]%d comments.\n",max_item);\r
- chat->max_item = max_item;\r
- //\83A\83C\83e\83\80\94z\97ñ\82Ì\8am\95Û\r
- chat->item = malloc(sizeof(CHAT_ITEM) * max_item);\r
- if(chat->item == NULL){\r
- fputs("[chat/init]failed to malloc for comment.\n",log);\r
- return FALSE;\r
- }\r
- /*\8cÂ\95Ê\97v\91f\82Ì\8f\89\8aú\89»*/\r
- CHAT_ITEM* item;\r
- int no;\r
- int vpos;\r
- int location;\r
- int size;\r
- int color;\r
- int str_length;\r
- Uint16* str;\r
- for(i=0;i<max_item;i++){\r
- item = &chat->item[i];\r
- item->chat = chat;\r
- item->showed = FALSE;\r
- //\83R\83\81\83\93\83g\94Ô\8d\86\r
- if(fread(&no,sizeof(no),1,com_f) <= 0){\r
- fputs("[chat/init]failed to read comment number.\n",log);\r
- return FALSE;\r
- }\r
- no = SDL_SwapLE32(no);\r
- max_no = MAX(max_no,no);\r
- min_no = MIN(min_no,no);\r
- //vpos\r
- if(fread(&vpos,sizeof(vpos),1,com_f) <= 0){\r
- fputs("[chat/init]failed to read comment vpos.\n",log);\r
- return FALSE;\r
- }\r
- vpos = SDL_SwapLE32(vpos);\r
- //\95¶\8e\9a\82Ì\8fê\8f\8a\r
- if(fread(&location,sizeof(location),1,com_f) <= 0){\r
- fputs("[chat/init]failed to read comment location.\n",log);\r
- return FALSE;\r
- }\r
- location = SDL_SwapLE32(location);\r
- //\83T\83C\83Y\r
- if(fread(&size,sizeof(size),1,com_f) <= 0){\r
- fputs("[chat/init]failed to read comment size.\n",log);\r
- return FALSE;\r
- }\r
- size = SDL_SwapLE32(size);\r
- //\90F\r
- if(fread(&color,sizeof(color),1,com_f) <= 0){\r
- fputs("[chat/init]failed to read comment color.\n",log);\r
- return FALSE;\r
- }\r
- color = SDL_SwapLE32(color);\r
- //\95¶\8e\9a\90\94\r
- if(fread(&str_length,sizeof(str_length),1,com_f) <= 0){\r
- fputs("[chat/init]failed to read comment length.\n",log);\r
- return FALSE;\r
- }\r
- str_length = SDL_SwapLE32(str_length);\r
- //\95¶\8e\9a\97ñ\r
- str = malloc(str_length);\r
- if(str == NULL){\r
- fputs("[chat/init]failed to malloc for comment text.\n",log);\r
- return FALSE;\r
- }\r
- if(fread(str,str_length,1,com_f) <= 0){\r
- fputs("[chat/init]failed to read comment text.\n",log);\r
- return FALSE;\r
- }\r
- //\95Ï\90\94\83Z\83b\83g\r
- item->no = no;\r
- item->vpos = vpos;\r
- item->location = location;\r
- item->size = size;\r
- item->color = color;\r
- item->str = str;\r
- /*\93à\95\94\8f\88\97\9d\82æ\82è*/\r
- if(location != CMD_LOC_DEF){\r
- item->vstart = vpos;\r
- item->vend = vpos + TEXT_SHOW_SEC - TEXT_AHEAD_SEC;\r
- }else{\r
- item->vstart = vpos - TEXT_AHEAD_SEC;\r
- item->vend = item->vstart + TEXT_SHOW_SEC;\r
- }\r
- int fix = item->vend - video_length;\r
- if(fix > 0){\r
- item->vend -= fix;\r
- item->vpos -= fix;\r
- item->vstart -= fix;\r
- fprintf(log,"[chat/fix]comment %d time adjusted.\n",i);\r
- }\r
- /*\93à\95\94\8f\88\97\9d\82æ\82è\81@\82¨\82í\82è*/\r
- }\r
- fclose(com_f);\r
- chat->max_no = max_no;\r
- chat->min_no = min_no;\r
- return TRUE;\r
-}\r
-\r
-void closeChat(CHAT* chat){\r
- int i;\r
- int max_item = chat->max_item;\r
- for(i=0;i<max_item;i++){\r
- free((void*)chat->item[i].str);\r
- }\r
- free(chat->item);\r
-}\r
-\r
-/*\r
- * \83C\83e\83\8c\81[\83^\82ð\83\8a\83Z\83b\83g\82·\82é\81B\r
- */\r
-void resetChatIterator(CHAT* chat){\r
- chat->iterator_index = 0;\r
-}\r
-/*\r
- * \83C\83e\83\8c\81[\83^\82ð\93¾\82é\r
- */\r
-CHAT_ITEM* getChatShowed(CHAT* chat,int now_vpos){\r
- int *i = &chat->iterator_index;\r
- int max_item = chat->max_item;\r
- CHAT_ITEM* item;\r
- for(;*i<max_item;(*i)++){\r
- item = &chat->item[*i];\r
- if(now_vpos >= item->vstart && now_vpos <= item->vend && !item->showed){\r
- return item;\r
- }\r
- }\r
- return NULL;\r
-}\r
+#include <SDL/SDL_endian.h>
+#include <stdio.h>
+
+#include <stdlib.h>
+#include "chat.h"
+#include "../mydef.h"
+#include "../nicodef.h"
+
+int initChat(FILE* log,CHAT* chat,const char* file_path,CHAT_SLOT* slot,int video_length){
+ int i;
+ int max_no = INTEGER_MIN;
+ int min_no = INTEGER_MAX;
+ int max_item;
+ chat->slot = slot;
+ FILE* com_f = fopen(file_path,"rb");
+ if(com_f == NULL){
+ fputs("[chat/init]failed to open comment file.\n",log);
+ return FALSE;
+ }
+ /*要素数の取得*/
+ if(fread(&max_item,sizeof(max_item),1,com_f) <= 0){
+ fputs("[chat/init]failed to read the number of comments.\n",log);
+ return FALSE;
+ }
+ max_item = SDL_SwapLE32(max_item);
+ fprintf(log,"[chat/init]%d comments.\n",max_item);
+ chat->max_item = max_item;
+ //アイテム配列の確保
+ chat->item = malloc(sizeof(CHAT_ITEM) * max_item);
+ if(chat->item == NULL){
+ fputs("[chat/init]failed to malloc for comment.\n",log);
+ return FALSE;
+ }
+ /*個別要素の初期化*/
+ CHAT_ITEM* item;
+ int no;
+ int vpos;
+ int location;
+ int size;
+ int color;
+ int str_length;
+ Uint16* str;
+ for(i=0;i<max_item;i++){
+ item = &chat->item[i];
+ item->chat = chat;
+ item->showed = FALSE;
+ //コメント番号
+ if(fread(&no,sizeof(no),1,com_f) <= 0){
+ fputs("[chat/init]failed to read comment number.\n",log);
+ return FALSE;
+ }
+ no = SDL_SwapLE32(no);
+ max_no = MAX(max_no,no);
+ min_no = MIN(min_no,no);
+ //vpos
+ if(fread(&vpos,sizeof(vpos),1,com_f) <= 0){
+ fputs("[chat/init]failed to read comment vpos.\n",log);
+ return FALSE;
+ }
+ vpos = SDL_SwapLE32(vpos);
+ //文字の場所
+ if(fread(&location,sizeof(location),1,com_f) <= 0){
+ fputs("[chat/init]failed to read comment location.\n",log);
+ return FALSE;
+ }
+ location = SDL_SwapLE32(location);
+ //サイズ
+ if(fread(&size,sizeof(size),1,com_f) <= 0){
+ fputs("[chat/init]failed to read comment size.\n",log);
+ return FALSE;
+ }
+ size = SDL_SwapLE32(size);
+ //色
+ if(fread(&color,sizeof(color),1,com_f) <= 0){
+ fputs("[chat/init]failed to read comment color.\n",log);
+ return FALSE;
+ }
+ color = SDL_SwapLE32(color);
+ //文字数
+ if(fread(&str_length,sizeof(str_length),1,com_f) <= 0){
+ fputs("[chat/init]failed to read comment length.\n",log);
+ return FALSE;
+ }
+ str_length = SDL_SwapLE32(str_length);
+ //文字列
+ str = malloc(str_length);
+ if(str == NULL){
+ fputs("[chat/init]failed to malloc for comment text.\n",log);
+ return FALSE;
+ }
+ if(fread(str,str_length,1,com_f) <= 0){
+ fputs("[chat/init]failed to read comment text.\n",log);
+ return FALSE;
+ }
+ //変数セット
+ item->no = no;
+ item->vpos = vpos;
+ item->location = location;
+ item->size = size;
+ item->color = color;
+ item->str = str;
+ /*内部処理より*/
+ if(location != CMD_LOC_DEF){
+ item->vstart = vpos;
+ item->vend = vpos + TEXT_SHOW_SEC - TEXT_AHEAD_SEC;
+ }else{
+ item->vstart = vpos - TEXT_AHEAD_SEC;
+ item->vend = item->vstart + TEXT_SHOW_SEC;
+ }
+ int fix = item->vend - video_length;
+ if(fix > 0){
+ item->vend -= fix;
+ item->vpos -= fix;
+ item->vstart -= fix;
+ fprintf(log,"[chat/fix]comment %d time adjusted.\n",i);
+ }
+ /*内部処理より おわり*/
+ }
+ fclose(com_f);
+ chat->max_no = max_no;
+ chat->min_no = min_no;
+ return TRUE;
+}
+
+void closeChat(CHAT* chat){
+ int i;
+ int max_item = chat->max_item;
+ for(i=0;i<max_item;i++){
+ free((void*)chat->item[i].str);
+ }
+ free(chat->item);
+}
+
+/*
+ * イテレータをリセットする。
+ */
+void resetChatIterator(CHAT* chat){
+ chat->iterator_index = 0;
+}
+/*
+ * イテレータを得る
+ */
+CHAT_ITEM* getChatShowed(CHAT* chat,int now_vpos){
+ int *i = &chat->iterator_index;
+ int max_item = chat->max_item;
+ CHAT_ITEM* item;
+ for(;*i<max_item;(*i)++){
+ item = &chat->item[*i];
+ if(now_vpos >= item->vstart && now_vpos <= item->vend && !item->showed){
+ return item;
+ }
+ }
+ return NULL;
+}