Statistics
| Branch: | Revision:

chunker-player / chunker_player / player_gui.c @ e11386c0

History | View | Annotate | Download (23.2 KB)

1
#include "player_gui.h"
2
// #include "player_commons.h"
3

    
4
#define SCREEN_BOTTOM_PADDING (BUTTONS_LAYER_OFFSET + BUTTONS_CONTAINER_HEIGHT + STATS_BOX_HEIGHT)
5

    
6
SDL_Cursor *InitSystemCursor(const char *image[]);
7
void AspectRatioResize(float aspect_ratio, int width, int height, int* out_width, int* out_height);
8
void UpdateOverlaySize(float aspect_ratio, int width, int height);
9
void RedrawButtons();
10
void RedrawChannelName();
11
void RedrawStats();
12
void SetupGUI();
13
void ToggleAudio();
14

    
15
static char AudioStatsText[255];
16
static char VideoStatsText[255];
17

    
18
SDL_Surface *ChannelTitleSurface = NULL;
19
//SDL_Surface *AudioStatisticsSurface = NULL, *VideoStatisticsSurface = NULL;
20
SDL_Rect ChannelTitleRect, AudioStatisticsRect, VideoStatisticsRect, tmpRect;
21
SDL_Color ChannelTitleColor = { 255, 0, 0 }, StatisticsColor = { 255, 255, 255 };
22
SDL_Color ChannelTitleBgColor = { 0, 0, 0 }, StatisticsBgColor = { 0, 0, 0 };
23
TTF_Font *MainFont = NULL;
24
TTF_Font *StatisticsFont = NULL;
25

    
26
/* XPM */
27
static const char *handXPM[] = {
28
/* columns rows colors chars-per-pixel */
29
"32 32 3 1",
30
"  c black",
31
". c gray100",
32
"X c None",
33
/* pixels */
34
"XXXXX  XXXXXXXXXXXXXXXXXXXXXXXXX",
35
"XXXX .. XXXXXXXXXXXXXXXXXXXXXXXX",
36
"XXXX .. XXXXXXXXXXXXXXXXXXXXXXXX",
37
"XXXX .. XXXXXXXXXXXXXXXXXXXXXXXX",
38
"XXXX .. XXXXXXXXXXXXXXXXXXXXXXXX",
39
"XXXX ..   XXXXXXXXXXXXXXXXXXXXXX",
40
"XXXX .. ..   XXXXXXXXXXXXXXXXXXX",
41
"XXXX .. .. ..  XXXXXXXXXXXXXXXXX",
42
"XXXX .. .. .. . XXXXXXXXXXXXXXXX",
43
"   X .. .. .. .. XXXXXXXXXXXXXXX",
44
" ..  ........ .. XXXXXXXXXXXXXXX",
45
" ... ........... XXXXXXXXXXXXXXX",
46
"X .. ........... XXXXXXXXXXXXXXX",
47
"XX . ........... XXXXXXXXXXXXXXX",
48
"XX ............. XXXXXXXXXXXXXXX",
49
"XXX ............ XXXXXXXXXXXXXXX",
50
"XXX ........... XXXXXXXXXXXXXXXX",
51
"XXXX .......... XXXXXXXXXXXXXXXX",
52
"XXXX .......... XXXXXXXXXXXXXXXX",
53
"XXXXX ........ XXXXXXXXXXXXXXXXX",
54
"XXXXX ........ XXXXXXXXXXXXXXXXX",
55
"XXXXX          XXXXXXXXXXXXXXXXX",
56
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
57
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
58
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
59
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
60
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
61
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
62
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
63
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
64
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
65
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
66
"0,0"
67
};
68

    
69
int ChunkerPlayerGUI_Init()
70
{
71
        // some initializations
72
        ratio = DEFAULT_RATIO;
73
        FullscreenWidth = 0;
74
        FullscreenHeight = 0;
75
        Audio_ON = 1;
76
        
77
        UpdateOverlaySize(ratio, DEFAULT_WIDTH, DEFAULT_HEIGHT);
78
        
79
        if(!SilentMode)
80
                SetupGUI();
81
        
82
        return 0;
83
}
84

    
85
int SetVideoMode(int width, int height, int fullscreen)
86
{
87
        if(SilentMode)
88
                return 1;
89
                
90
        // printf("SetVideoMode(%d, %d, %d)\n", width, height, fullscreen);
91
        SDL_LockMutex(OverlayMutex);
92

    
93
        if(fullscreen)
94
        {
95
#ifndef __DARWIN__
96
                MainScreen = SDL_SetVideoMode(width, height, 0, SDL_SWSURFACE | SDL_NOFRAME | SDL_FULLSCREEN);
97
#else
98
                MainScreen = SDL_SetVideoMode(width, height, 24, SDL_SWSURFACE | SDL_NOFRAME | SDL_FULLSCREEN);
99
#endif
100
        }
101
        else
102
        {
103
#ifndef __DARWIN__
104
                MainScreen = SDL_SetVideoMode(width, height, 0, SDL_SWSURFACE | SDL_RESIZABLE);
105
#else
106
                MainScreen = SDL_SetVideoMode(width, height, 24, SDL_SWSURFACE | SDL_RESIZABLE);
107
#endif
108
        }
109

    
110
        if(!MainScreen) {
111
                fprintf(stderr, "ERROR: could not change video mode\n");
112
                return 1;
113
        }
114
        SDL_UnlockMutex(OverlayMutex);
115
        
116
        return 0;
117
}
118

    
119
void ChunkerPlayerGUI_HandleResize(int resize_w, int resize_h)
120
{
121
        if(SilentMode)
122
                return;
123
                
124
        SDL_LockMutex(OverlayMutex);
125
        
126
        int res = SetVideoMode(resize_w, resize_h, FullscreenMode?1:0);
127

    
128
        if(res && FullscreenMode)
129
        {
130
                // an error has occurred while switching to fullscreen mode
131
                
132
                // trying resize without fullscreen mode
133
                FullscreenMode = 0;
134
                res = SetVideoMode(resize_w, resize_h, 0);
135
        }
136
        if(res)
137
        {
138
                // nothing to do
139
                fprintf(stderr, "CRITICAL ERROR: could not change video mode\n");
140
                exit(1);
141
        }
142
        
143
        window_width = resize_w;
144
        window_height = resize_h;
145
        
146
        // update the overlay surface size, mantaining the aspect ratio
147
        UpdateOverlaySize(ratio, resize_w, resize_h);
148
        
149
        // update each button coordinates
150
        int i;
151
        for(i=0; i<NBUTTONS; i++)
152
        {
153
                if(Buttons[i].XOffset > 0)
154
                        Buttons[i].ButtonIconBox.x = Buttons[i].XOffset;
155
                else
156
                        Buttons[i].ButtonIconBox.x = (resize_w + Buttons[i].XOffset);
157
                        
158
                Buttons[i].ButtonIconBox.y = resize_h - Buttons[i].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
159
        }
160
        
161
        RedrawButtons();
162
        RedrawChannelName();
163
        RedrawStats();
164
        
165
        SDL_UnlockMutex(OverlayMutex);
166
}
167

    
168
void ChunkerPlayerGUI_HandleGetFocus()
169
{
170
        if(SilentMode)
171
                return;
172

    
173
        RedrawButtons();
174
        RedrawChannelName();
175
        RedrawStats();
176
}
177

    
178
void ChunkerPlayerGUI_HandleMouseMotion(int x, int y)
179
{
180
        if(SilentMode)
181
                return;
182
                
183
        int i;
184
        for(i=0; i<NBUTTONS; i++)
185
        {
186
                //If the mouse is over the button
187
                if(
188
                        ( x > Buttons[i].ButtonIconBox.x ) && ( x < Buttons[i].ButtonIconBox.x + Buttons[i].ButtonIcon->w )
189
                        && ( y > Buttons[i].ButtonIconBox.y ) && ( y < Buttons[i].ButtonIconBox.y + Buttons[i].ButtonIcon->h )
190
                )
191
                {
192
                        Buttons[i].Hover = 1;
193
                        SDL_SetCursor(handCursor);
194
                        break;
195
                }
196
                
197
                else
198
                {
199
                        Buttons[i].Hover = 0;
200
                        SDL_SetCursor(defaultCursor);
201
                }
202
        }
203
}
204

    
205
void ChunkerPlayerGUI_HandleLButton(int x, int y)
206
{
207
        if(SilentMode)
208
                return;
209
                
210
        int i;
211
        for(i=0; i<NBUTTONS; i++)
212
        {
213
                //If the mouse is over the button
214
                if(
215
                        ( x > Buttons[i].ButtonIconBox.x ) && ( x < Buttons[i].ButtonIconBox.x + Buttons[i].ButtonIcon->w )
216
                        && ( y > Buttons[i].ButtonIconBox.y ) && ( y < Buttons[i].ButtonIconBox.y + Buttons[i].ButtonIcon->h )
217
                )
218
                {
219
                        Buttons[i].LButtonUpCallback();
220
                        break;
221
                }
222
        }
223
}
224

    
225
void ChunkerPlayerGUI_HandleKey()
226
{
227
        static Uint32 LastTime=0;
228
        static int LastKey=-1;
229

    
230
        Uint32 Now=SDL_GetTicks();
231
        Uint8* keystate=SDL_GetKeyState(NULL);
232
        if(keystate[SDLK_ESCAPE] &&
233
          (LastKey!=SDLK_ESCAPE || (LastKey==SDLK_ESCAPE && (Now-LastTime>1000))))
234
        {
235
                LastKey=SDLK_ESCAPE;
236
                LastTime=Now;
237
                quit=1;
238
        }
239
}
240

    
241
void ChunkerPlayerGUI_Close()
242
{
243
        if(ChannelTitleSurface != NULL)
244
                SDL_FreeSurface( ChannelTitleSurface );
245
        
246
        TTF_CloseFont( MainFont );
247
        TTF_CloseFont( StatisticsFont );
248
        TTF_Quit();
249
        IMG_Quit();
250
}
251

    
252
void RedrawButtons()
253
{
254
        if(SilentMode)
255
                return;
256
                
257
        int i;
258
        for(i=0; i<NBUTTONS; i++)
259
        {
260
                if(!Buttons[i].Visible)
261
                {
262
                        SDL_LockMutex(OverlayMutex);
263
                        SDL_FillRect( MainScreen, &Buttons[i].ButtonIconBox, SDL_MapRGB(MainScreen->format, 0, 0, 0) );
264
                        SDL_UpdateRects(MainScreen, 1, &Buttons[i].ButtonIconBox);
265
                        SDL_UnlockMutex(OverlayMutex);
266
                }
267
        }
268
        for(i=0; i<NBUTTONS; i++)
269
        {
270
                if(Buttons[i].Visible)
271
                {
272
                        if(!Buttons[i].Hover)
273
                        {
274
                                SDL_LockMutex(OverlayMutex);
275
                                SDL_BlitSurface(Buttons[i].ButtonIcon, NULL, MainScreen, &Buttons[i].ButtonIconBox);
276
                                SDL_UpdateRects(MainScreen, 1, &Buttons[i].ButtonIconBox);
277
                                SDL_UnlockMutex(OverlayMutex);
278
                        }
279
                        else
280
                        {
281
                                SDL_LockMutex(OverlayMutex);
282
                                SDL_BlitSurface(Buttons[i].ButtonHoverIcon, NULL, MainScreen, &(Buttons[i].ButtonIconBox));
283
                                SDL_UpdateRects(MainScreen, 1, &(Buttons[i].ButtonIconBox));
284
                                SDL_UnlockMutex(OverlayMutex);
285
                        }
286
                }
287
        }
288
}
289

    
290
void RedrawChannelName()
291
{
292
        if(SilentMode)
293
                return;
294
                
295
        if(ChannelTitleSurface != NULL)
296
        {
297
                SDL_LockMutex(OverlayMutex);
298
                SDL_FillRect( MainScreen, &ChannelTitleRect, SDL_MapRGB(MainScreen->format, 0, 0, 0) );
299
                SDL_UpdateRect(MainScreen, ChannelTitleRect.x, ChannelTitleRect.y, ChannelTitleRect.w, ChannelTitleRect.h);
300
                
301
                ChannelTitleRect.w = ChannelTitleSurface->w;
302
                ChannelTitleRect.h = ChannelTitleSurface->h;
303
                ChannelTitleRect.x = ((FullscreenMode?FullscreenWidth:window_width) - ChannelTitleRect.w)/2;
304
                ChannelTitleRect.y = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y+5;
305
                SDL_BlitSurface(ChannelTitleSurface, NULL, MainScreen, &ChannelTitleRect);
306
                SDL_UpdateRects(MainScreen, 1, &ChannelTitleRect);
307
                SDL_UnlockMutex(OverlayMutex);
308
        }
309
}
310

    
311
void ChunkerPlayerGUI_ToggleFullscreen()
312
{
313
        if(SilentMode)
314
                return;
315

    
316
        SDL_LockMutex(OverlayMutex);
317
                
318
        int i, done = 0;
319
        //If the screen is windowed
320
        if( !FullscreenMode )
321
        {
322
                int res = SetVideoMode(FullscreenWidth, FullscreenHeight, 1);
323
                //Set the window state flag
324
                FullscreenMode = 1;
325

    
326
                if(res)
327
                {
328
                        fprintf(stderr, "ERROR: an error has occurred while switching to fullscreen mode\n");
329
                }
330
                else
331
                {
332
                        // update the overlay surface size, mantaining the aspect ratio
333
                        UpdateOverlaySize(ratio, FullscreenWidth, FullscreenHeight);
334
                        
335
                        // update each button coordinates
336
                        for(i=0; i<NBUTTONS; i++)
337
                        {
338
                                if(Buttons[i].XOffset > 0)
339
                                        Buttons[i].ButtonIconBox.x = Buttons[i].XOffset;
340
                                else
341
                                        Buttons[i].ButtonIconBox.x = (FullscreenWidth + Buttons[i].XOffset);
342
                                        
343
                                Buttons[i].ButtonIconBox.y = FullscreenHeight - Buttons[i].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
344
                        }
345
                        
346
                        Buttons[FULLSCREEN_BUTTON_INDEX].Visible = 0;
347
                        Buttons[NO_FULLSCREEN_BUTTON_INDEX].Visible = 1;
348
                        
349
                        done = 1;
350
                }
351
        }
352
        
353
        //If the screen is fullscreen
354
        if(FullscreenMode && !done)
355
        {
356
                int res = SetVideoMode(window_width, window_height, 0);
357
                if(res)
358
                {
359
                        // nothing to do
360
                        fprintf(stderr, "CRITICAL ERROR: could not change video mode\n");
361
                        exit(1);
362
                }
363
                
364
                // update the overlay surface size, mantaining the aspect ratio
365
                UpdateOverlaySize(ratio, window_width, window_height);
366
                
367
                // update each button coordinates
368
                for(i=0; i<NBUTTONS; i++)
369
                {
370
                        if(Buttons[i].XOffset > 0)
371
                                Buttons[i].ButtonIconBox.x = Buttons[i].XOffset;
372
                        else
373
                                Buttons[i].ButtonIconBox.x = (window_width + Buttons[i].XOffset);
374
                                
375
                        Buttons[i].ButtonIconBox.y = window_height - Buttons[i].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
376
                }
377
                
378
                //Set the window state flag
379
                FullscreenMode = 0;
380
                
381
                Buttons[FULLSCREEN_BUTTON_INDEX].Visible = 1;
382
                Buttons[NO_FULLSCREEN_BUTTON_INDEX].Visible = 0;
383
        }
384

    
385
        RedrawButtons();
386
        RedrawChannelName();
387
        RedrawStats();
388
        
389
        SDL_UnlockMutex(OverlayMutex);
390
}
391

    
392
void ChunkerPlayerGUI_AspectRatioResize(float aspect_ratio, int width, int height, int* out_width, int* out_height)
393
{
394
        AspectRatioResize(aspect_ratio, width, height, out_width, out_height);
395
}
396

    
397
void AspectRatioResize(float aspect_ratio, int width, int height, int* out_width, int* out_height)
398
{
399
        int h,w;
400
        h = (int)((float)width/aspect_ratio);
401
        if(h<=height)
402
        {
403
                w = width;
404
        }
405
        else
406
        {
407
                w = (int)((float)height*aspect_ratio);
408
                h = height;
409
        }
410
        *out_width = w;
411
        *out_height = h;
412
}
413

    
414
/**
415
 * Updates the overlay surface size, mantaining the aspect ratio
416
 */
417
void UpdateOverlaySize(float aspect_ratio, int width, int height)
418
{
419
        // height -= (BUTTONS_LAYER_OFFSET + BUTTONS_CONTAINER_HEIGHT);
420
        height -= SCREEN_BOTTOM_PADDING;
421
        int h = 0, w = 0, x, y;
422
        AspectRatioResize(aspect_ratio, width, height, &w, &h);
423
        x = (width - w) / 2;
424
        y = (height - h) / 2;
425
        SDL_LockMutex(OverlayMutex);
426
        OverlayRect.x = x;
427
        OverlayRect.y = y;
428
        OverlayRect.w = w;
429
        OverlayRect.h = h;
430
        // SDL_FillRect( SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 0,0,0) );
431
        SDL_UpdateRect(MainScreen, 0, 0, 0, 0);
432
        SDL_UnlockMutex(OverlayMutex);
433
}
434

    
435
void GetScreenSizeFromOverlay(int overlayWidth, int overlayHeight, int* screenWidth, int* screenHeight)
436
{
437
        *screenHeight = overlayHeight + SCREEN_BOTTOM_PADDING;
438
        *screenWidth = overlayWidth;
439
}
440

    
441
/* From the SDL documentation. */
442
SDL_Cursor *InitSystemCursor(const char *image[])
443
{
444
        int i, row, col;
445
        Uint8 data[4*32];
446
        Uint8 mask[4*32];
447
        int hot_x, hot_y;
448

    
449
        i = -1;
450
        for ( row=0; row<32; ++row ) {
451
                for ( col=0; col<32; ++col ) {
452
                        if ( col % 8 ) {
453
                                data[i] <<= 1;
454
                                mask[i] <<= 1;
455
                        } else {
456
                                ++i;
457
                                data[i] = mask[i] = 0;
458
                        }
459
                        
460
                        switch (image[4+row][col]) {
461
                                case ' ':
462
                                        data[i] |= 0x01;
463
                                        mask[i] |= 0x01;
464
                                        break;
465
                                case '.':
466
                                        mask[i] |= 0x01;
467
                                        break;
468
                                case 'X':
469
                                        break;
470
                        }
471
                }
472
        }
473
        
474
        sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
475
        return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
476
}
477

    
478
void SetupGUI()
479
{
480
        //Initialize SDL_ttf 
481
        if( TTF_Init() == -1 )
482
        {
483
                printf("TTF_Init: Failed to init SDL_ttf library!\n");
484
                printf("TTF_Init: %s\n", TTF_GetError());
485
                exit(1);
486
        }
487
        
488
        //Open the font
489
        MainFont = TTF_OpenFont(MAIN_FONT_FILE , MAIN_FONT_SIZE );
490
        StatisticsFont = TTF_OpenFont(STATS_FONT_FILE, STATS_FONT_SIZE );
491
        
492
        //If there was an error in loading the font
493
        if( MainFont == NULL)
494
        {
495
                printf("Cannot initialize GUI, %s file not found\n", MAIN_FONT_FILE);
496
                exit(1);
497
        }
498
        if( StatisticsFont == NULL )
499
        {
500
                printf("Cannot initialize GUI, %s file not found\n", STATS_FONT_FILE);
501
                exit(1);
502
        }
503
        
504
        // init SDL_image
505
        int flags=IMG_INIT_JPG|IMG_INIT_PNG;
506
        int initted=IMG_Init(flags);
507
        if(initted&flags != flags)
508
        {
509
                printf("IMG_Init: Failed to init required jpg and png support!\n");
510
                printf("IMG_Init: %s\n", IMG_GetError());
511
                exit(1);
512
        }
513
        
514
        SDL_VideoInfo* InitialVideoInfo = SDL_GetVideoInfo();
515
        FullscreenWidth = InitialVideoInfo->current_w;
516
        FullscreenHeight = InitialVideoInfo->current_h;
517

    
518
        SDL_Surface *temp;
519
        int screen_w = 0, screen_h = 0;
520

    
521
        if(OverlayRect.w > BUTTONS_CONTAINER_WIDTH)
522
                screen_w = OverlayRect.w;
523
        else
524
                screen_w = BUTTONS_CONTAINER_WIDTH;
525
        screen_h = OverlayRect.h + SCREEN_BOTTOM_PADDING;
526

    
527
        SDL_WM_SetCaption("Filling buffer...", NULL);
528
        
529
        // Make a screen to put our video
530
        SetVideoMode(screen_w, screen_h, 0);
531
        
532
        window_width = screen_w;
533
        window_height = screen_h;
534
        
535
        /** Setting up cursors */
536
        defaultCursor = SDL_GetCursor();
537
        handCursor = InitSystemCursor(handXPM);
538
        
539
        /** Init Buttons */
540
        int i;
541
        for(i=0; i<NBUTTONS; i++)
542
        {
543
                SButton* tmp = &(Buttons[i]);
544
                tmp->Hover = 0;
545
                tmp->ToggledButton = NULL;
546
                tmp->Visible = 1;
547
                tmp->HoverCallback = NULL;
548
                tmp->LButtonUpCallback = NULL;
549
        }
550
        
551
        /** Loading icons */
552
        
553
        // fullscreen
554
        temp = IMG_Load(FULLSCREEN_ICON_FILE);
555
        if (temp == NULL) {
556
                fprintf(stderr, "Error loading %s: %s\n", FULLSCREEN_ICON_FILE, SDL_GetError());
557
                exit(1);
558
        }
559
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
560
        if(Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIcon == NULL)
561
        {
562
                printf("ERROR in SDL_DisplayFormatAlpha, cannot load fullscreen button, error message: '%s'\n", SDL_GetError());
563
                exit(1);
564
        }
565
        SDL_FreeSurface(temp);
566
        
567
        // fullscreen hover
568
        temp = IMG_Load(FULLSCREEN_HOVER_ICON_FILE);
569
        if (temp == NULL) {
570
                fprintf(stderr, "Error loading %s: %s\n", FULLSCREEN_HOVER_ICON_FILE, SDL_GetError());
571
                exit(1);
572
        }
573
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
574
        SDL_FreeSurface(temp);
575

    
576
        // no fullscreen
577
        temp = IMG_Load(NOFULLSCREEN_ICON_FILE);
578
        if (temp == NULL) {
579
                fprintf(stderr, "Error loading %s: %s\n", NOFULLSCREEN_ICON_FILE, SDL_GetError());
580
                exit(1);
581
        }
582
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
583
        SDL_FreeSurface(temp);
584

    
585
        // no fullscreen hover
586
        temp = IMG_Load(NOFULLSCREEN_HOVER_ICON_FILE);
587
        if (temp == NULL) {
588
                fprintf(stderr, "Error loading %s: %s\n", NOFULLSCREEN_HOVER_ICON_FILE, SDL_GetError());
589
                exit(1);
590
        }
591
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
592
        SDL_FreeSurface(temp);
593
        
594
        // channel up
595
        temp = IMG_Load(CHANNEL_UP_ICON_FILE);
596
        if (temp == NULL) {
597
                fprintf(stderr, "Error loading %s: %s\n", CHANNEL_UP_ICON_FILE, SDL_GetError());
598
                exit(1);
599
        }
600
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
601
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
602
        SDL_FreeSurface(temp);
603
        
604
        // channel down
605
        temp = IMG_Load(CHANNEL_DOWN_ICON_FILE);
606
        if (temp == NULL) {
607
                fprintf(stderr, "Error loading %s: %s\n", CHANNEL_DOWN_ICON_FILE, SDL_GetError());
608
                exit(1);
609
        }
610
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
611
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
612
        SDL_FreeSurface(temp);
613
        
614
        // audio OFF
615
        temp = IMG_Load(AUDIO_OFF_ICON_FILE);
616
        if (temp == NULL) {
617
                fprintf(stderr, "Error loading %s: %s\n", AUDIO_OFF_ICON_FILE, SDL_GetError());
618
                exit(1);
619
        }
620
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
621
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
622
        SDL_FreeSurface(temp);
623
        
624
        // audio ON
625
        temp = IMG_Load(AUDIO_ON_ICON_FILE);
626
        if (temp == NULL) {
627
                fprintf(stderr, "Error loading %s: %s\n", AUDIO_ON_ICON_FILE, SDL_GetError());
628
                exit(1);
629
        }
630
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
631
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
632
        SDL_FreeSurface(temp);
633

    
634
        /** Setting up icon boxes */
635
        Buttons[FULLSCREEN_BUTTON_INDEX].XOffset = Buttons[NO_FULLSCREEN_BUTTON_INDEX].XOffset = 20;
636
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.x = 20;
637
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.w = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIcon->w;
638
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.h = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIcon->h;
639
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
640
        
641
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.x = 20;
642
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.w = Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIcon->w;
643
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.h = Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIcon->h;
644
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
645
        
646
        Buttons[CHANNEL_UP_BUTTON_INDEX].XOffset = -61;
647
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.w = Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIcon->w;
648
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.h = Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIcon->h;
649
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.x = (screen_w + Buttons[CHANNEL_UP_BUTTON_INDEX].XOffset);
650
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
651
        
652
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].XOffset = -36;
653
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.w = Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIcon->w;
654
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.h = Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIcon->h;
655
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.x = (screen_w + Buttons[CHANNEL_DOWN_BUTTON_INDEX].XOffset);
656
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
657
        
658
        Buttons[AUDIO_OFF_BUTTON_INDEX].XOffset = Buttons[AUDIO_ON_BUTTON_INDEX].XOffset = 70;
659
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.x = 20;
660
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.w = Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIcon->w;
661
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.h = Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIcon->h;
662
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
663
        Buttons[AUDIO_OFF_BUTTON_INDEX].Visible = 1;
664

    
665
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.x = 20;
666
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.w = Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIcon->w;
667
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.h = Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIcon->h;
668
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
669
        Buttons[AUDIO_ON_BUTTON_INDEX].Visible = 0;
670
        
671
        // Setting up buttons events
672
        Buttons[FULLSCREEN_BUTTON_INDEX].ToggledButton = &(Buttons[NO_FULLSCREEN_BUTTON_INDEX]);
673
        Buttons[FULLSCREEN_BUTTON_INDEX].LButtonUpCallback = &ChunkerPlayerGUI_ToggleFullscreen;
674
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].LButtonUpCallback = &ChunkerPlayerGUI_ToggleFullscreen;
675
        Buttons[CHANNEL_UP_BUTTON_INDEX].LButtonUpCallback = &ZapUp;
676
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].LButtonUpCallback = &ZapDown;
677
        Buttons[AUDIO_OFF_BUTTON_INDEX].LButtonUpCallback = &ToggleAudio;
678
        Buttons[AUDIO_ON_BUTTON_INDEX].LButtonUpCallback = &ToggleAudio;
679
}
680

    
681
void ChunkerPlayerGUI_SetChannelTitle(char* title)
682
{
683
        if(SilentMode)
684
                return;
685
                
686
        SDL_LockMutex(OverlayMutex);
687
        
688
        SDL_FreeSurface( ChannelTitleSurface );
689
        // ChannelTitleSurface = TTF_RenderText_Solid( MainFont, channel->Title, ChannelTitleColor );
690
        ChannelTitleSurface = TTF_RenderText_Shaded( MainFont, title, ChannelTitleColor, ChannelTitleBgColor );
691
        if(ChannelTitleSurface == NULL)
692
        {
693
                printf("WARNING: CANNOT RENDER CHANNEL TITLE\n");
694
        }
695
        
696
        SDL_UnlockMutex(OverlayMutex);
697
        
698
        RedrawChannelName();
699
}
700

    
701
void ChunkerPlayerGUI_SetStatsText(char* audio_text, char* video_text)
702
{
703
        if(SilentMode)
704
                return;
705
                
706
        if(audio_text == NULL)
707
                audio_text = AudioStatsText;
708
        
709
        if(video_text == NULL)
710
                video_text = VideoStatsText;
711

    
712
        if((strlen(audio_text) > 255) || (strlen(video_text) > 255))
713
        {
714
                printf("WARNING IN player_gui.c: stats text too long, could not refresh text\n");
715
                return;
716
        }
717
        
718
        strcpy(AudioStatsText, audio_text);
719
        strcpy(VideoStatsText, video_text);
720
        
721
        RedrawStats();
722
}
723

    
724
void RedrawStats()
725
{
726
        if(SilentMode)
727
                return;
728
                
729
        SDL_Surface *text_surface;
730

    
731
        SDL_LockMutex(OverlayMutex);
732
        
733
        // clear stats text
734
        SDL_FillRect( MainScreen, &VideoStatisticsRect, SDL_MapRGB(MainScreen->format, 0, 0, 0) );
735
        SDL_FillRect( MainScreen, &AudioStatisticsRect, SDL_MapRGB(MainScreen->format, 0, 0, 0) );
736
        SDL_UpdateRect(MainScreen, VideoStatisticsRect.x, VideoStatisticsRect.y, VideoStatisticsRect.w, VideoStatisticsRect.h);
737
        SDL_UpdateRect(MainScreen, AudioStatisticsRect.x, AudioStatisticsRect.y, AudioStatisticsRect.w, AudioStatisticsRect.h);
738
        
739
        
740
        text_surface = TTF_RenderText_Shaded( StatisticsFont, AudioStatsText, StatisticsColor, StatisticsBgColor );
741
    if (text_surface != NULL)
742
    {
743
                AudioStatisticsRect.w = text_surface->w;
744
                AudioStatisticsRect.h = text_surface->h;
745
                AudioStatisticsRect.x = ((FullscreenMode?FullscreenWidth:window_width) - AudioStatisticsRect.w)>>1;
746
                AudioStatisticsRect.y = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y+(STATS_FONT_SIZE<<1)+STATS_BOX_HEIGHT;
747

    
748
        SDL_BlitSurface(text_surface, NULL, MainScreen, &AudioStatisticsRect);
749
        SDL_FreeSurface(text_surface);
750
    }
751
    else
752
    {
753
        // report error
754
    }
755
    
756
        text_surface = TTF_RenderText_Shaded( StatisticsFont, VideoStatsText, StatisticsColor, StatisticsBgColor );
757
    if (text_surface != NULL)
758
    {
759
                VideoStatisticsRect.w = text_surface->w;
760
                VideoStatisticsRect.h = text_surface->h;
761
                VideoStatisticsRect.x = ((FullscreenMode?FullscreenWidth:window_width) - VideoStatisticsRect.w)>>1;
762
                VideoStatisticsRect.y = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y+(STATS_FONT_SIZE)+STATS_BOX_HEIGHT;
763

    
764
        SDL_BlitSurface(text_surface, NULL, MainScreen, &VideoStatisticsRect);
765
        SDL_FreeSurface(text_surface);
766
    }
767
    else
768
    {
769
        // report error
770
    }
771
    
772
    SDL_UpdateRect(MainScreen, VideoStatisticsRect.x, VideoStatisticsRect.y, VideoStatisticsRect.w, VideoStatisticsRect.h);
773
        SDL_UpdateRect(MainScreen, AudioStatisticsRect.x, AudioStatisticsRect.y, AudioStatisticsRect.w, AudioStatisticsRect.h);
774
        
775
        SDL_UnlockMutex(OverlayMutex);
776
}
777

    
778
void ChunkerPlayerGUI_SetupOverlayRect(SChannel* channel)
779
{
780
        ratio = channel->Ratio;
781
        int w, h;
782
        GetScreenSizeFromOverlay(channel->Width, channel->Height, &w, &h);
783
        UpdateOverlaySize(ratio, w, h);
784
        // UpdateOverlaySize(ratio, channel->Width, channel->Height);
785
}
786

    
787
void ChunkerPlayerGUI_ForceResize(int width, int height)
788
{
789
        FullscreenMode = 0;
790
        int w, h;
791
        GetScreenSizeFromOverlay(width, height, &w, &h);
792
        ChunkerPlayerGUI_HandleResize(w, h);
793
}
794

    
795
void ToggleAudio()
796
{
797
        if(Audio_ON)
798
        {
799
                SDL_PauseAudio(1);
800
                Buttons[AUDIO_OFF_BUTTON_INDEX].Visible = 0;
801
                Buttons[AUDIO_ON_BUTTON_INDEX].Visible = 1;
802
                Audio_ON = 0;
803
        }
804
        else
805
        {
806
                SDL_PauseAudio(0);
807
                Buttons[AUDIO_OFF_BUTTON_INDEX].Visible = 1;
808
                Buttons[AUDIO_ON_BUTTON_INDEX].Visible = 0;
809
                Audio_ON = 1;
810
        }
811
        
812
        RedrawButtons();
813
}
814

    
815
void ChunkerPlayerGUI_ChannelSwitched()
816
{
817
        if(!Audio_ON)
818
        {
819
                SDL_PauseAudio(1);
820
                Buttons[AUDIO_OFF_BUTTON_INDEX].Visible = 0;
821
                Buttons[AUDIO_ON_BUTTON_INDEX].Visible = 1;
822
        }
823
        
824
        RedrawButtons();
825
}