OSDN Git Service

ソースの文字コードと改行コードを修正
[coroid/inqubus.git] / vhook / chat / chat_slot.c
index 6e3dd7b..1c9f4f0 100644 (file)
-#include "chat.h"\r
-#include "chat_slot.h"\r
-#include "process_chat.h"\r
-#include "../mydef.h"\r
-#include "../comment/com_surface.h"\r
-#include "../nicodef.h"\r
-#include "../util.h"\r
-#include <SDL/SDL.h>\r
-#include <stdio.h>\r
-#include <string.h>\r
-\r
-int initChatSlot(FILE* log,CHAT_SLOT* slot,int max_slot,CHAT* chat){\r
-       slot->max_item=max_slot;\r
-       slot->chat = chat;\r
-       slot->item = malloc(sizeof(CHAT_SLOT_ITEM) * max_slot);\r
-       if(slot->item == NULL){\r
-               fputs("failed to malloc for comment slot.\n",log);\r
-               return FALSE;\r
-       }\r
-       int i;\r
-       CHAT_SLOT_ITEM* item;\r
-       for(i=0;i<max_slot;i++){\r
-               item = &slot->item[i];\r
-               item->used = FALSE;\r
-               item->slot = slot;\r
-               item->surf=NULL;\r
-       }\r
-       return TRUE;\r
-}\r
-void closeChatSlot(CHAT_SLOT* slot){\r
-       int i;\r
-       CHAT_SLOT_ITEM* item;\r
-       for(i=0;i<slot->max_item;i++){\r
-               item = &slot->item[i];\r
-               SDL_FreeSurface(item->surf);\r
-       }\r
-       //\83A\83C\83e\83\80\82ð\8fÁ\8b\8e\81B\r
-       free(slot->item);\r
-}\r
-\r
-void deleteChatSlot(CHAT_SLOT* slot,CHAT_SLOT_ITEM* item){\r
-       item->chat_item=NULL;\r
-       SDL_FreeSurface(item->surf);\r
-       item->surf = NULL;\r
-       item->used = FALSE;\r
-}\r
-\r
-void deleteChatSlotFromIndex(CHAT_SLOT* slot,int index){\r
-       CHAT_SLOT_ITEM* item = &slot->item[index];\r
-       deleteChatSlot(slot,item);\r
-}\r
-\r
-/*\r
- * \83X\83\8d\83b\83g\82É\92Ç\89Á\82·\82é\81B\r
- */\r
-void addChatSlot(COMMDATA* data,CHAT_SLOT* slot,CHAT_ITEM* item,int video_width,int video_height){\r
-       //\82à\82¤\8c©\82¹\82ç\82ê\82½\81B\r
-       item->showed = TRUE;\r
-       if(slot->max_item <= 0){\r
-               return;\r
-       }\r
-       SDL_Surface* surf = makeCommentSurface(data,item,video_width,video_height);\r
-       /*\8aJ\82«\83X\83\8d\83b\83g\83\8b\8c\9f\8dõ*/\r
-       int i;\r
-       int cnt = -1;\r
-       int slot_max = slot->max_item;\r
-       for(i=0;i<slot_max;i++){\r
-               if(!slot->item[i].used){\r
-                       cnt = i;\r
-                       break;\r
-               }\r
-               if(cnt < 0 || slot->item[cnt].chat_item->vend > slot->item[i].chat_item->vend){\r
-                       cnt = i;\r
-               }\r
-       }\r
-       CHAT_SLOT_ITEM* slot_item = &slot->item[cnt];\r
-       /*\8bó\82«\82ª\96³\82¯\82ê\82Î\8b­\90§\93I\82É\8dì\82é\81B*/\r
-       if(slot_item->used){\r
-               deleteChatSlotFromIndex(slot,cnt);\r
-       }\r
-       //\82±\82Ì\8e\9e\93_\82Å\92Ç\89Á\r
-       slot_item->chat_item = item;\r
-       slot_item->surf = surf;\r
-       /*\83\8d\83P\81[\83V\83\87\83\93\82Å\95ª\8aò*/\r
-       int y;\r
-       if(item->location == CMD_LOC_BOTTOM){\r
-               y = video_height - surf->h;\r
-       }else{\r
-               y = 0;\r
-       }\r
-       int running;\r
-       do{\r
-               running = FALSE;\r
-               for(i=0;i<slot_max;i++){\r
-                       CHAT_SLOT_ITEM* other_slot = &slot->item[i];\r
-                       if(!other_slot->used){\r
-                               continue;\r
-                       }\r
-                       const CHAT_ITEM* other_item = other_slot->chat_item;\r
-                       int other_y = other_slot->y;\r
-                       /*\96³\8e\8b\82·\82é\8fð\8c\8f*/\r
-                       if(other_y + other_slot->surf->h <= y){\r
-                               continue;\r
-                       }\r
-                       if(y + surf->h <= other_y){\r
-                               continue;\r
-                       }\r
-                       if(other_item->location != item->location){\r
-                               continue;\r
-                       }\r
-                       int start = MAX(other_item->vstart,item->vstart);\r
-                       int end = MIN(other_item->vend,item->vend);\r
-                       int obj_x_t1 = getX(start,slot_item,video_width);\r
-                       int obj_x_t2 = getX(end,slot_item,video_width);\r
-                       int o_x_t1 = getX(start,other_slot,video_width);\r
-                       int o_x_t2 = getX(end,other_slot,video_width);\r
-                       //\93\96\82½\82è\94»\92è\r
-                       if ((obj_x_t1 <= o_x_t1 + other_slot->surf->w && o_x_t1 <= obj_x_t1 + surf->w)\r
-                || (obj_x_t2 <= o_x_t2 + other_slot->surf->w && o_x_t2 <= obj_x_t2 + surf->w)){\r
-                               if(item->location == CMD_LOC_BOTTOM){\r
-                                       y = other_y - surf->h - 1;\r
-                               }else{\r
-                                       y = other_y + other_slot->surf->h + 1;\r
-                               }\r
-                               running = TRUE;\r
-                               break;\r
-                       }\r
-               }\r
-       }while(running);\r
-       /*\82»\82à\82»\82à\89æ\96Ê\93à\82É\96³\82¯\82ê\82Î\96³\88Ó\96¡\81B*/\r
-       if(y < 0 || y+surf->h > video_height){//\94Í\88Í\82ð\92´\82¦\82Ä\82é\82Ì\82Å\81A\83\89\83\93\83_\83\80\82É\94z\92u\81B\r
-               y = ((rnd() & 0xffff) * (video_height - surf->h)) / 0xffff;\r
-       }\r
-       //\92Ç\89Á\r
-       slot_item->used = TRUE;\r
-       slot_item->y = y;\r
-}\r
-/*\r
- * \83C\83e\83\8c\81[\83^\82ð\83\8a\83Z\83b\83g\82·\82é\81B\r
- */\r
-void resetChatSlotIterator(CHAT_SLOT* slot){\r
-       slot->iterator_index = 0;\r
-}\r
-/*\r
- * \83C\83e\83\8c\81[\83^\82ð\93¾\82é\r
- */\r
-CHAT_SLOT_ITEM* getChatSlotErased(CHAT_SLOT* slot,int now_vpos){\r
-       int *i = &slot->iterator_index;\r
-       int max_item = slot->max_item;\r
-       CHAT_ITEM* item;\r
-       CHAT_SLOT_ITEM* slot_item;\r
-       for(;*i<max_item;(*i)++){\r
-               slot_item = &slot->item[*i];\r
-               if(!slot_item->used){\r
-                       continue;\r
-               }\r
-               item = slot_item->chat_item;\r
-               if(item==NULL)continue;\r
-               if(now_vpos < item->vstart || now_vpos > item->vend){\r
-                       return slot_item;\r
-               }\r
-       }\r
-       return NULL;\r
-}\r
+#include "chat.h"
+#include "chat_slot.h"
+#include "process_chat.h"
+#include "../mydef.h"
+#include "../comment/com_surface.h"
+#include "../nicodef.h"
+#include "../util.h"
+#include <SDL/SDL.h>
+#include <stdio.h>
+#include <string.h>
+
+int initChatSlot(FILE* log,CHAT_SLOT* slot,int max_slot,CHAT* chat){
+       slot->max_item=max_slot;
+       slot->chat = chat;
+       slot->item = malloc(sizeof(CHAT_SLOT_ITEM) * max_slot);
+       if(slot->item == NULL){
+               fputs("failed to malloc for comment slot.\n",log);
+               return FALSE;
+       }
+       int i;
+       CHAT_SLOT_ITEM* item;
+       for(i=0;i<max_slot;i++){
+               item = &slot->item[i];
+               item->used = FALSE;
+               item->slot = slot;
+               item->surf=NULL;
+       }
+       return TRUE;
+}
+void closeChatSlot(CHAT_SLOT* slot){
+       int i;
+       CHAT_SLOT_ITEM* item;
+       for(i=0;i<slot->max_item;i++){
+               item = &slot->item[i];
+               SDL_FreeSurface(item->surf);
+       }
+       //アイテムを消去。
+       free(slot->item);
+}
+
+void deleteChatSlot(CHAT_SLOT* slot,CHAT_SLOT_ITEM* item){
+       item->chat_item=NULL;
+       SDL_FreeSurface(item->surf);
+       item->surf = NULL;
+       item->used = FALSE;
+}
+
+void deleteChatSlotFromIndex(CHAT_SLOT* slot,int index){
+       CHAT_SLOT_ITEM* item = &slot->item[index];
+       deleteChatSlot(slot,item);
+}
+
+/*
+ * スロットに追加する。
+ */
+void addChatSlot(COMMDATA* data,CHAT_SLOT* slot,CHAT_ITEM* item,int video_width,int video_height){
+       //もう見せられた。
+       item->showed = TRUE;
+       if(slot->max_item <= 0){
+               return;
+       }
+       SDL_Surface* surf = makeCommentSurface(data,item,video_width,video_height);
+       /*開きスロットル検索*/
+       int i;
+       int cnt = -1;
+       int slot_max = slot->max_item;
+       for(i=0;i<slot_max;i++){
+               if(!slot->item[i].used){
+                       cnt = i;
+                       break;
+               }
+               if(cnt < 0 || slot->item[cnt].chat_item->vend > slot->item[i].chat_item->vend){
+                       cnt = i;
+               }
+       }
+       CHAT_SLOT_ITEM* slot_item = &slot->item[cnt];
+       /*空きが無ければ強制的に作る。*/
+       if(slot_item->used){
+               deleteChatSlotFromIndex(slot,cnt);
+       }
+       //この時点で追加
+       slot_item->chat_item = item;
+       slot_item->surf = surf;
+       /*ロケーションで分岐*/
+       int y;
+       if(item->location == CMD_LOC_BOTTOM){
+               y = video_height - surf->h;
+       }else{
+               y = 0;
+       }
+       int running;
+       do{
+               running = FALSE;
+               for(i=0;i<slot_max;i++){
+                       CHAT_SLOT_ITEM* other_slot = &slot->item[i];
+                       if(!other_slot->used){
+                               continue;
+                       }
+                       const CHAT_ITEM* other_item = other_slot->chat_item;
+                       int other_y = other_slot->y;
+                       /*無視する条件*/
+                       if(other_y + other_slot->surf->h <= y){
+                               continue;
+                       }
+                       if(y + surf->h <= other_y){
+                               continue;
+                       }
+                       if(other_item->location != item->location){
+                               continue;
+                       }
+                       int start = MAX(other_item->vstart,item->vstart);
+                       int end = MIN(other_item->vend,item->vend);
+                       int obj_x_t1 = getX(start,slot_item,video_width);
+                       int obj_x_t2 = getX(end,slot_item,video_width);
+                       int o_x_t1 = getX(start,other_slot,video_width);
+                       int o_x_t2 = getX(end,other_slot,video_width);
+                       //当たり判定
+                       if ((obj_x_t1 <= o_x_t1 + other_slot->surf->w && o_x_t1 <= obj_x_t1 + surf->w)
+                || (obj_x_t2 <= o_x_t2 + other_slot->surf->w && o_x_t2 <= obj_x_t2 + surf->w)){
+                               if(item->location == CMD_LOC_BOTTOM){
+                                       y = other_y - surf->h - 1;
+                               }else{
+                                       y = other_y + other_slot->surf->h + 1;
+                               }
+                               running = TRUE;
+                               break;
+                       }
+               }
+       }while(running);
+       /*そもそも画面内に無ければ無意味。*/
+       if(y < 0 || y+surf->h > video_height){//範囲を超えてるので、ランダムに配置。
+               y = ((rnd() & 0xffff) * (video_height - surf->h)) / 0xffff;
+       }
+       //追加
+       slot_item->used = TRUE;
+       slot_item->y = y;
+}
+/*
+ * イテレータをリセットする。
+ */
+void resetChatSlotIterator(CHAT_SLOT* slot){
+       slot->iterator_index = 0;
+}
+/*
+ * イテレータを得る
+ */
+CHAT_SLOT_ITEM* getChatSlotErased(CHAT_SLOT* slot,int now_vpos){
+       int *i = &slot->iterator_index;
+       int max_item = slot->max_item;
+       CHAT_ITEM* item;
+       CHAT_SLOT_ITEM* slot_item;
+       for(;*i<max_item;(*i)++){
+               slot_item = &slot->item[*i];
+               if(!slot_item->used){
+                       continue;
+               }
+               item = slot_item->chat_item;
+               if(item==NULL)continue;
+               if(now_vpos < item->vstart || now_vpos > item->vend){
+                       return slot_item;
+               }
+       }
+       return NULL;
+}