OSDN Git Service

r224をNetBeans用に設定変更.
[coroid/inqubus.git] / vhook / chat / chat_slot.c
1 #include "chat.h"\r
2 #include "chat_slot.h"\r
3 #include "process_chat.h"\r
4 #include "../mydef.h"\r
5 #include "../comment/com_surface.h"\r
6 #include "../nicodef.h"\r
7 #include "../util.h"\r
8 #include <SDL/SDL.h>\r
9 #include <stdio.h>\r
10 #include <string.h>\r
11 \r
12 int initChatSlot(FILE* log,CHAT_SLOT* slot,int max_slot,CHAT* chat){\r
13         slot->max_item=max_slot;\r
14         slot->chat = chat;\r
15         slot->item = malloc(sizeof(CHAT_SLOT_ITEM) * max_slot);\r
16         if(slot->item == NULL){\r
17                 fputs("failed to malloc for comment slot.\n",log);\r
18                 return FALSE;\r
19         }\r
20         int i;\r
21         CHAT_SLOT_ITEM* item;\r
22         for(i=0;i<max_slot;i++){\r
23                 item = &slot->item[i];\r
24                 item->used = FALSE;\r
25                 item->slot = slot;\r
26                 item->surf=NULL;\r
27         }\r
28         return TRUE;\r
29 }\r
30 void closeChatSlot(CHAT_SLOT* slot){\r
31         int i;\r
32         CHAT_SLOT_ITEM* item;\r
33         for(i=0;i<slot->max_item;i++){\r
34                 item = &slot->item[i];\r
35                 SDL_FreeSurface(item->surf);\r
36         }\r
37         //\83A\83C\83e\83\80\82ð\8fÁ\8b\8e\81B\r
38         free(slot->item);\r
39 }\r
40 \r
41 void deleteChatSlot(CHAT_SLOT* slot,CHAT_SLOT_ITEM* item){\r
42         item->chat_item=NULL;\r
43         SDL_FreeSurface(item->surf);\r
44         item->surf = NULL;\r
45         item->used = FALSE;\r
46 }\r
47 \r
48 void deleteChatSlotFromIndex(CHAT_SLOT* slot,int index){\r
49         CHAT_SLOT_ITEM* item = &slot->item[index];\r
50         deleteChatSlot(slot,item);\r
51 }\r
52 \r
53 /*\r
54  * \83X\83\8d\83b\83g\82É\92Ç\89Á\82·\82é\81B\r
55  */\r
56 void addChatSlot(DATA* data,CHAT_SLOT* slot,CHAT_ITEM* item,int video_width,int video_height){\r
57         //\82à\82¤\8c©\82¹\82ç\82ê\82½\81B\r
58         item->showed = TRUE;\r
59         if(slot->max_item <= 0){\r
60                 return;\r
61         }\r
62         SDL_Surface* surf = makeCommentSurface(data,item,video_width,video_height);\r
63         /*\8aJ\82«\83X\83\8d\83b\83g\83\8b\8c\9f\8dõ*/\r
64         int i;\r
65         int cnt = -1;\r
66         int slot_max = slot->max_item;\r
67         for(i=0;i<slot_max;i++){\r
68                 if(!slot->item[i].used){\r
69                         cnt = i;\r
70                         break;\r
71                 }\r
72                 if(cnt < 0 || slot->item[cnt].chat_item->vend > slot->item[i].chat_item->vend){\r
73                         cnt = i;\r
74                 }\r
75         }\r
76         CHAT_SLOT_ITEM* slot_item = &slot->item[cnt];\r
77         /*\8bó\82«\82ª\96³\82¯\82ê\82Î\8b­\90§\93I\82É\8dì\82é\81B*/\r
78         if(slot_item->used){\r
79                 deleteChatSlotFromIndex(slot,cnt);\r
80         }\r
81         //\82±\82Ì\8e\9e\93_\82Å\92Ç\89Á\r
82         slot_item->chat_item = item;\r
83         slot_item->surf = surf;\r
84         /*\83\8d\83P\81[\83V\83\87\83\93\82Å\95ª\8aò*/\r
85         int y;\r
86         if(item->location == CMD_LOC_BOTTOM){\r
87                 y = video_height - surf->h;\r
88         }else{\r
89                 y = 0;\r
90         }\r
91         int running;\r
92         do{\r
93                 running = FALSE;\r
94                 for(i=0;i<slot_max;i++){\r
95                         CHAT_SLOT_ITEM* other_slot = &slot->item[i];\r
96                         if(!other_slot->used){\r
97                                 continue;\r
98                         }\r
99                         const CHAT_ITEM* other_item = other_slot->chat_item;\r
100                         int other_y = other_slot->y;\r
101                         /*\96³\8e\8b\82·\82é\8fð\8c\8f*/\r
102                         if(other_y + other_slot->surf->h <= y){\r
103                                 continue;\r
104                         }\r
105                         if(y + surf->h <= other_y){\r
106                                 continue;\r
107                         }\r
108                         if(other_item->location != item->location){\r
109                                 continue;\r
110                         }\r
111                         int start = MAX(other_item->vstart,item->vstart);\r
112                         int end = MIN(other_item->vend,item->vend);\r
113                         int obj_x_t1 = getX(start,slot_item,video_width);\r
114                         int obj_x_t2 = getX(end,slot_item,video_width);\r
115                         int o_x_t1 = getX(start,other_slot,video_width);\r
116                         int o_x_t2 = getX(end,other_slot,video_width);\r
117                         //\93\96\82½\82è\94»\92è\r
118                         if ((obj_x_t1 <= o_x_t1 + other_slot->surf->w && o_x_t1 <= obj_x_t1 + surf->w)\r
119                 || (obj_x_t2 <= o_x_t2 + other_slot->surf->w && o_x_t2 <= obj_x_t2 + surf->w)){\r
120                                 if(item->location == CMD_LOC_BOTTOM){\r
121                                         y = other_y - surf->h - 1;\r
122                                 }else{\r
123                                         y = other_y + other_slot->surf->h + 1;\r
124                                 }\r
125                                 running = TRUE;\r
126                                 break;\r
127                         }\r
128                 }\r
129         }while(running);\r
130         /*\82»\82à\82»\82à\89æ\96Ê\93à\82É\96³\82¯\82ê\82Î\96³\88Ó\96¡\81B*/\r
131         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
132                 y = ((rnd() & 0xffff) * (video_height - surf->h)) / 0xffff;\r
133         }\r
134         //\92Ç\89Á\r
135         slot_item->used = TRUE;\r
136         slot_item->y = y;\r
137 }\r
138 /*\r
139  * \83C\83e\83\8c\81[\83^\82ð\83\8a\83Z\83b\83g\82·\82é\81B\r
140  */\r
141 void resetChatSlotIterator(CHAT_SLOT* slot){\r
142         slot->iterator_index = 0;\r
143 }\r
144 /*\r
145  * \83C\83e\83\8c\81[\83^\82ð\93¾\82é\r
146  */\r
147 CHAT_SLOT_ITEM* getChatSlotErased(CHAT_SLOT* slot,int now_vpos){\r
148         int *i = &slot->iterator_index;\r
149         int max_item = slot->max_item;\r
150         CHAT_ITEM* item;\r
151         CHAT_SLOT_ITEM* slot_item;\r
152         for(;*i<max_item;(*i)++){\r
153                 slot_item = &slot->item[*i];\r
154                 if(!slot_item->used){\r
155                         continue;\r
156                 }\r
157                 item = slot_item->chat_item;\r
158                 if(item==NULL)continue;\r
159                 if(now_vpos < item->vstart || now_vpos > item->vend){\r
160                         return slot_item;\r
161                 }\r
162         }\r
163         return NULL;\r
164 }\r