OSDN Git Service

更新履歴
[coroid/inqubus.git] / vhook / main.c
1
2 #include <SDL/SDL.h>
3 #include <SDL/SDL_ttf.h>
4 #include <stdio.h>
5 #include "main.h"
6 #include "mydef.h"
7 #include "nicodef.h"
8 #include "process.h"
9 /**
10  * ライブラリ初期化
11  */
12 int init(FILE* log){
13         fputs("[main/init]initializing libs...\n",log);
14         //SDL
15         if(SDL_Init(SDL_INIT_VIDEO)>=0){
16                 fputs("[main/init]initialized SDL.\n",log);
17         }else{
18                 fputs("[main/init]failed to initialize SDL.\n",log);
19             return FALSE;
20         }
21         //SDL_ttf
22         if(TTF_Init() >= 0){
23                 fputs("[main/init]initialized SDL_ttf.\n",log);
24         }else{
25                 fputs("[main/init]failed to initialize SDL_ttf.\n",log);
26             return FALSE;
27         }
28         fputs("[main/init]initialized libs.\n",log);
29         return TRUE;
30 }
31 /*
32  * データの初期化
33  */
34 int initData(DATA* data,FILE* log,const SETTING* setting){
35     for(int i = 0; i < N_COMMENT_TYPE; i++) {
36         COMMDATA* const commdata = &data->comment[i];
37         const SETTING_COMMENT* const commset = &setting->comment[i];
38
39         commdata->common = data;
40         commdata->enable = commset->enable;
41         commdata->opaque_comment = setting->opaque_comment;
42     }
43     // オーナコメントは常に不透明
44     data->comment[COMMENT_OWNER].opaque_comment = OPAQUE_TRUE;
45     // オプショナルユーザコメントは半透明時常にアルファ値0.5
46     if(data->comment[COMMENT_USER_OPT].opaque_comment == OPAQUE_FALSE) {
47         data->comment[COMMENT_USER_OPT].opaque_comment = OPAQUE_HALF;
48     }
49
50         data->log = log;
51         data->fontsize_fix = setting->fontsize_fix;
52         data->show_video = setting->show_video;
53         data->shadow_kind = setting->shadow_kind;
54         data->process_first_called=FALSE;
55         data->video_length = setting->video_length;
56     data->aspect_mode = setting->aspect_mode;
57         fputs("[main/init]initializing context...\n",log);
58         //フォント
59         TTF_Font** font = data->font;
60         const char* font_path = setting->font_path;
61         const int font_index = setting->font_index;
62         for(int i=0;i<CMD_FONT_MAX;i++){
63                 int fontsize = COMMENT_FONT_SIZE[i];
64                 if(setting->fontsize_fix){
65                         fontsize <<= 1;
66                 }
67                 font[i] = TTF_OpenFontIndex(font_path,fontsize,font_index);
68                 if(font[i] == NULL){
69                     fprintf(log,"[main/init]failed to load font:%s index:[%d].\n",font_path,font_index);
70                     //0でも試してみる。
71                     fputs("[main/init]retrying to open font at index:0...",log);
72                     font[i] = TTF_OpenFontIndex(font_path,fontsize,0);
73                     if(font[i] == NULL){
74                             fputs("failed.\n",log);
75                             return FALSE;
76                     }else{
77                             fputs("success.\n",log);
78                     }
79                 }
80                 TTF_SetFontStyle(font[i],TTF_STYLE_BOLD);
81         }
82         fputs("[main/init]initialized font.\n",log);
83
84     for (int i = 0; i < N_COMMENT_TYPE; i++) {
85         COMMDATA* const commdata = &data->comment[i];
86         if (commdata->enable) {
87             fprintf(log, "[main/init]Comment[%d] is enabled.\n", i);
88             const SETTING_COMMENT* const commset = &setting->comment[i];
89             //コメントデータ
90             if (initChat(log, &commdata->chat, commset->path, &commdata->slot, data->video_length)) {
91                 fputs("[main/init]initialized comment.\n", log);
92             } else {
93                 fputs("[main/init]failed to initialize comment.", log);
94                 return FALSE;
95             }
96             //コメントスロット
97             if (initChatSlot(log, &commdata->slot, setting->user_slot_max, &commdata->chat)) {
98                 fputs("[main/init]initialized comment slot.\n", log);
99             } else {
100                 fputs("[main/init]failed to initialize comment slot.", log);
101                 return FALSE;
102             }
103         }
104     }
105
106         //終わり。
107         fputs("[main/init]initialized context.\n",log);
108         return TRUE;
109 }
110 /*
111  * 映像の変換
112  */
113 int main_process(DATA* data,SDL_Surface* surf,const int now_vpos){
114         FILE* log = data->log;
115         if(!data->process_first_called){
116                 fprintf(log,"[main/process]screen size is %dx%d.\n",surf->w,surf->h);
117                 fflush(log);
118         }
119         /*フィルタをかける*/
120         if(process(data,surf,now_vpos)){
121         }
122         fflush(log);
123         /*変換した画像を見せる。*/
124         if(data->show_video){
125                 if(!data->process_first_called){
126                         data->screen = SDL_SetVideoMode(surf->w, surf->h, 24, SDL_HWSURFACE | SDL_DOUBLEBUF);
127                         if(data->screen == NULL){
128                                 fputs("[main/process]failed to initialize screen.\n",log);
129                                 fflush(log);
130                                 return FALSE;
131                         }
132                 }
133                 SDL_BlitSurface(surf,NULL,data->screen,NULL);
134                 SDL_Flip(data->screen);
135                 SDL_Event event;
136                 while(SDL_PollEvent(&event)){}
137         }
138         //一回目以降はTRUEになる。
139         data->process_first_called=TRUE;
140         fflush(log);
141         return TRUE;
142 }
143 /*
144  * データのクローズ
145  */
146 int closeData(DATA* data){
147         // コメントが有効なら開放
148     for (int i = 0; i < N_COMMENT_TYPE; i++) {
149         COMMDATA* const comment = &data->comment[i];
150         if (comment->enable) {
151             closeChat(&comment->chat);
152             closeChatSlot(&comment->slot);
153         }
154     }
155
156     //フォント開放
157         for(int i=0;i<CMD_FONT_MAX;i++){
158                 TTF_CloseFont(data->font[i]);
159         }
160         return TRUE;
161 }
162
163 /*
164  * ライブラリシャットダウン
165  */
166 int close(){
167     //SDLをシャットダウン
168     SDL_Quit();
169         //同じくTTFをシャットダウン
170     TTF_Quit();
171         return TRUE;
172 }