File: osd.cpp

package info (click to toggle)
mupen64plus-core 2.5-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,776 kB
  • ctags: 7,778
  • sloc: ansic: 59,320; asm: 1,994; cpp: 1,823; python: 619; makefile: 600; sh: 311
file content (606 lines) | stat: -rw-r--r-- 17,912 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *   Mupen64plus - osd.cpp                                                 *
 *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
 *   Copyright (C) 2008 Nmn Ebenblues                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <SDL.h>
// On-screen Display
#include <SDL_opengl.h>
#include <SDL_thread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "OGLFT.h"
#include "api/m64p_types.h"
#include "osd.h"

extern "C" {
    #define M64P_CORE_PROTOTYPES 1
    #include "api/callbacks.h"
    #include "api/config.h"
    #include "api/m64p_config.h"
    #include "api/m64p_vidext.h"
    #include "api/vidext.h"
    #include "main/list.h"
    #include "main/main.h"
    #include "osal/files.h"
    #include "osal/preproc.h"
    #include "plugin/plugin.h"
}

#define FONT_FILENAME "/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf"

typedef void (APIENTRYP PTRGLACTIVETEXTURE)(GLenum texture);
static PTRGLACTIVETEXTURE pglActiveTexture = NULL;

// static variables for OSD
static int l_OsdInitialized = 0;

static LIST_HEAD(l_messageQueue);
static OGLFT::Monochrome *l_font;
static float l_fLineHeight = -1.0;

static void animation_none(osd_message_t *);
static void animation_fade(osd_message_t *);
static void osd_remove_message(osd_message_t *msg);
static osd_message_t * osd_message_valid(osd_message_t *testmsg);

static float fCornerScroll[OSD_NUM_CORNERS];

static SDL_mutex *osd_list_lock;

// animation handlers
static void (*l_animations[OSD_NUM_ANIM_TYPES])(osd_message_t *) = {
    animation_none, // animation handler for OSD_NONE
    animation_fade  // animation handler for OSD_FADE
};

// private functions
// draw message on screen
static void draw_message(osd_message_t *msg, int width, int height)
{
    float x = 0.,
          y = 0.;

    if(!l_font || !l_font->isValid())
        return;

    // set color. alpha is hard coded to 1. animation can change this
    l_font->setForegroundColor(msg->color[R], msg->color[G], msg->color[B], 1.0);
    l_font->setBackgroundColor(0.0, 0.0, 0.0, 0.0);

    // set justification based on corner
    switch(msg->corner)
    {
        case OSD_TOP_LEFT:
            l_font->setVerticalJustification(OGLFT::Face::TOP);
            l_font->setHorizontalJustification(OGLFT::Face::LEFT);
            x = 0.;
            y = (float)height;
            break;
        case OSD_TOP_CENTER:
            l_font->setVerticalJustification(OGLFT::Face::TOP);
            l_font->setHorizontalJustification(OGLFT::Face::CENTER);
            x = ((float)width)/2.0f;
            y = (float)height;
            break;
        case OSD_TOP_RIGHT:
            l_font->setVerticalJustification(OGLFT::Face::TOP);
            l_font->setHorizontalJustification(OGLFT::Face::RIGHT);
            x = (float)width;
            y = (float)height;
            break;
        case OSD_MIDDLE_LEFT:
            l_font->setVerticalJustification(OGLFT::Face::MIDDLE);
            l_font->setHorizontalJustification(OGLFT::Face::LEFT);
            x = 0.;
            y = ((float)height)/2.0f;
            break;
        case OSD_MIDDLE_CENTER:
            l_font->setVerticalJustification(OGLFT::Face::MIDDLE);
            l_font->setHorizontalJustification(OGLFT::Face::CENTER);
            x = ((float)width)/2.0f;
            y = ((float)height)/2.0f;
            break;
        case OSD_MIDDLE_RIGHT:
            l_font->setVerticalJustification(OGLFT::Face::MIDDLE);
            l_font->setHorizontalJustification(OGLFT::Face::RIGHT);
            x = (float)width;
            y = ((float)height)/2.0f;
            break;
        case OSD_BOTTOM_LEFT:
            l_font->setVerticalJustification(OGLFT::Face::BOTTOM);
            l_font->setHorizontalJustification(OGLFT::Face::LEFT);
            x = 0.;
            y = 0.;
            break;
        case OSD_BOTTOM_CENTER:
            l_font->setVerticalJustification(OGLFT::Face::BOTTOM);
            l_font->setHorizontalJustification(OGLFT::Face::CENTER);
            x = ((float)width)/2.0f;
            y = 0.;
            break;
        case OSD_BOTTOM_RIGHT:
            l_font->setVerticalJustification(OGLFT::Face::BOTTOM);
            l_font->setHorizontalJustification(OGLFT::Face::RIGHT);
            x = (float)width;
            y = 0.;
            break;
        default:
            l_font->setVerticalJustification(OGLFT::Face::BOTTOM);
            l_font->setHorizontalJustification(OGLFT::Face::LEFT);
            x = 0.;
            y = 0.;
            break;
    }

    // apply animation for current message state
    (*l_animations[msg->animation[msg->state]])(msg);

    // xoffset moves message left
    x -= msg->xoffset;
    // yoffset moves message up
    y += msg->yoffset;

    // get the bounding box if invalid
    if (msg->sizebox[0] == 0 && msg->sizebox[2] == 0)  // xmin and xmax
    {
        OGLFT::BBox bbox = l_font->measure_nominal(msg->text);
        msg->sizebox[0] = bbox.x_min_;
        msg->sizebox[1] = bbox.y_min_;
        msg->sizebox[2] = bbox.x_max_;
        msg->sizebox[3] = bbox.y_max_;
    }

    // draw the text line
    l_font->draw(x, y, msg->text, msg->sizebox);
}

// null animation handler
static void animation_none(osd_message_t *msg) { }

// fade in/out animation handler
static void animation_fade(osd_message_t *msg)
{
    float alpha = 1.;
    float elapsed_frames;
    float total_frames = (float)msg->timeout[msg->state];

    switch(msg->state)
    {
        case OSD_DISAPPEAR:
            elapsed_frames = (float)(total_frames - msg->frames);
            break;
        case OSD_APPEAR:
        default:
            elapsed_frames = (float)msg->frames;
            break;
    }

    if(total_frames != 0.)
        alpha = elapsed_frames / total_frames;

    l_font->setForegroundColor(msg->color[R], msg->color[G], msg->color[B], alpha);
}

// sets message Y offset depending on where they are in the message queue
static float get_message_offset(osd_message_t *msg, float fLinePos)
{
    float offset = (float) (l_font->height() * fLinePos);

    switch(msg->corner)
    {
        case OSD_TOP_LEFT:
        case OSD_TOP_CENTER:
        case OSD_TOP_RIGHT:
            return -offset;
            break;
        default:
            return offset;
            break;
    }
}

// public functions
extern "C"
void osd_init(int width, int height)
{
    const char *fontpath;

    osd_list_lock = SDL_CreateMutex();
    if (!osd_list_lock) {
        DebugMessage(M64MSG_ERROR, "Could not create osd list lock");
        return;
    }

    if (!OGLFT::Init_FT())
    {
        DebugMessage(M64MSG_ERROR, "Could not initialize freetype library.");
        return;
    }

    fontpath = FONT_FILENAME;

    l_font = new OGLFT::Monochrome(fontpath, (float) height / 35.0f);  // make font size proportional to screen height

    if(!l_font || !l_font->isValid())
    {
        DebugMessage(M64MSG_ERROR, "Could not construct face from %s", fontpath);
        return;
    }

    // clear statics
    for (int i = 0; i < OSD_NUM_CORNERS; i++)
        fCornerScroll[i] = 0.0;

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
#if defined(GL_RASTER_POSITION_UNCLIPPED_IBM)
    glEnable(GL_RASTER_POSITION_UNCLIPPED_IBM);
#endif

    pglActiveTexture = (PTRGLACTIVETEXTURE) VidExt_GL_GetProcAddress("glActiveTexture");
    if (pglActiveTexture == NULL)
    {
        DebugMessage(M64MSG_WARNING, "OpenGL function glActiveTexture() not supported.  OSD deactivated.");
        return;
    }

    // set initialized flag
    l_OsdInitialized = 1;
}

extern "C"
void osd_exit(void)
{
    osd_message_t *msg, *safe;

    // delete font renderer
    if (l_font)
    {
        delete l_font;
        l_font = NULL;
    }

    // delete message queue
    SDL_LockMutex(osd_list_lock);
    list_for_each_entry_safe_t(msg, safe, &l_messageQueue, osd_message_t, list) {
        osd_remove_message(msg);
        if (!msg->user_managed)
            free(msg);
    }
    SDL_UnlockMutex(osd_list_lock);

    // shut down the Freetype library
    OGLFT::Uninit_FT();

    SDL_DestroyMutex(osd_list_lock);

    // reset initialized flag
    l_OsdInitialized = 0;
}

// renders the current osd message queue to the screen
extern "C"
void osd_render()
{
    osd_message_t *msg, *safe;
    int i;

    // if we're not initialized or list is empty, then just skip it all
    if (!l_OsdInitialized || list_empty(&l_messageQueue))
        return;

    // get the viewport dimensions
    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);

    // save all the attributes
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    bool bFragmentProg = glIsEnabled(GL_FRAGMENT_PROGRAM_ARB) != 0;
    bool bColorArray = glIsEnabled(GL_COLOR_ARRAY) != 0;
    bool bTexCoordArray = glIsEnabled(GL_TEXTURE_COORD_ARRAY) != 0;
    bool bSecColorArray = glIsEnabled(GL_SECONDARY_COLOR_ARRAY) != 0;

    // deactivate all the texturing units
    GLint  iActiveTex;
    bool bTexture2D[8];
    glGetIntegerv(GL_ACTIVE_TEXTURE_ARB, &iActiveTex);
    for (i = 0; i < 8; i++)
    {
        pglActiveTexture(GL_TEXTURE0_ARB + i);
        bTexture2D[i] = glIsEnabled(GL_TEXTURE_2D) != 0;
        glDisable(GL_TEXTURE_2D);
    }

    // save the matrices and set up new ones
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(viewport[0],viewport[2],viewport[1],viewport[3]);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    // setup for drawing text
    glDisable(GL_FOG);
    glDisable(GL_LIGHTING);
    glDisable(GL_ALPHA_TEST);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glDisable(GL_SCISSOR_TEST);
    glDisable(GL_STENCIL_TEST);
    glDisable(GL_FRAGMENT_PROGRAM_ARB);
    glDisable(GL_COLOR_MATERIAL);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_SECONDARY_COLOR_ARRAY);
    glShadeModel(GL_FLAT);

    // get line height if invalid
    if (l_fLineHeight < 0.0)
    {
        OGLFT::BBox bbox = l_font->measure("01abjZpqRGB");
        l_fLineHeight = (bbox.y_max_ - bbox.y_min_) / 30.0f;
    }

    // keeps track of next message position for each corner
    float fCornerPos[OSD_NUM_CORNERS];
    for (i = 0; i < OSD_NUM_CORNERS; i++)
        fCornerPos[i] = 0.5f * l_fLineHeight;

    SDL_LockMutex(osd_list_lock);
    list_for_each_entry_safe_t(msg, safe, &l_messageQueue, osd_message_t, list) {
        // update message state
        if(msg->timeout[msg->state] != OSD_INFINITE_TIMEOUT &&
           ++msg->frames >= msg->timeout[msg->state])
        {
            // if message is in last state, mark it for deletion and continue to the next message
            if(msg->state >= OSD_NUM_STATES - 1)
            {
                if (msg->user_managed) {
                    osd_remove_message(msg);
                } else {
                    osd_remove_message(msg);
                    free(msg);
                }

                continue;
            }

            // go to next state and reset frame count
            msg->state++;
            msg->frames = 0;
        }

        // offset y depending on how many other messages are in the same corner
        float fStartOffset;
        if (msg->corner >= OSD_MIDDLE_LEFT && msg->corner <= OSD_MIDDLE_RIGHT)  // don't scroll the middle messages
            fStartOffset = fCornerPos[msg->corner];
        else
            fStartOffset = fCornerPos[msg->corner] + (fCornerScroll[msg->corner] * l_fLineHeight);
        msg->yoffset += get_message_offset(msg, fStartOffset);

        draw_message(msg, viewport[2], viewport[3]);

        msg->yoffset -= get_message_offset(msg, fStartOffset);
        fCornerPos[msg->corner] += l_fLineHeight;
    }
    SDL_UnlockMutex(osd_list_lock);

    // do the scrolling
    for (int i = 0; i < OSD_NUM_CORNERS; i++)
    {
        fCornerScroll[i] += 0.1f;
        if (fCornerScroll[i] >= 0.0)
            fCornerScroll[i] = 0.0;
    }

    // restore the matrices
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    // restore the attributes
    for (int i = 0; i < 8; i++)
    {
        pglActiveTexture(GL_TEXTURE0_ARB + i);
        if (bTexture2D[i])
            glEnable(GL_TEXTURE_2D);
        else
            glDisable(GL_TEXTURE_2D);
    }
    pglActiveTexture(iActiveTex);
    glPopAttrib();
    if (bFragmentProg)
        glEnable(GL_FRAGMENT_PROGRAM_ARB);
    if (bColorArray)
        glEnableClientState(GL_COLOR_ARRAY);
    if (bTexCoordArray)
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    if (bSecColorArray)
        glEnableClientState(GL_SECONDARY_COLOR_ARRAY);

    glFinish();
}

// creates a new osd_message_t, adds it to the message queue and returns it in case
// the user wants to modify its parameters. Note, if the message can't be created,
// NULL is returned.
extern "C"
osd_message_t * osd_new_message(enum osd_corner eCorner, const char *fmt, ...)
{
    va_list ap;
    char buf[1024];

    if (!l_OsdInitialized) return NULL;

    osd_message_t *msg = (osd_message_t *)malloc(sizeof(*msg));

    if (!msg) return NULL;

    va_start(ap, fmt);
    vsnprintf(buf, 1024, fmt, ap);
    buf[1023] = 0;
    va_end(ap);

    // set default values
    memset(msg, 0, sizeof(osd_message_t));
    msg->text = strdup(buf);
    msg->user_managed = 0;
    // default to white
    msg->color[R] = 1.;
    msg->color[G] = 1.;
    msg->color[B] = 1.;

    msg->sizebox[0] = 0.0;  // set a null bounding box
    msg->sizebox[1] = 0.0;
    msg->sizebox[2] = 0.0;
    msg->sizebox[3] = 0.0;

    msg->corner = eCorner;
    msg->state = OSD_APPEAR;
    fCornerScroll[eCorner] -= 1.0;  // start this one before the beginning of the list and scroll it in

    msg->animation[OSD_APPEAR] = OSD_FADE;
    msg->animation[OSD_DISPLAY] = OSD_NONE;
    msg->animation[OSD_DISAPPEAR] = OSD_FADE;

    if (eCorner >= OSD_MIDDLE_LEFT && eCorner <= OSD_MIDDLE_RIGHT)
    {
        msg->timeout[OSD_APPEAR] = 20;
        msg->timeout[OSD_DISPLAY] = 60;
        msg->timeout[OSD_DISAPPEAR] = 20;
    }
    else
    {
        msg->timeout[OSD_APPEAR] = 20;
        msg->timeout[OSD_DISPLAY] = 180;
        msg->timeout[OSD_DISAPPEAR] = 40;
    }

    // add to message queue
    SDL_LockMutex(osd_list_lock);
    list_add(&msg->list, &l_messageQueue);
    SDL_UnlockMutex(osd_list_lock);

    return msg;
}

// update message string
extern "C"
void osd_update_message(osd_message_t *msg, const char *fmt, ...)
{
    va_list ap;
    char buf[1024];

    if (!l_OsdInitialized || !msg) return;

    va_start(ap, fmt);
    vsnprintf(buf, 1024, fmt, ap);
    buf[1023] = 0;
    va_end(ap);

    free(msg->text);
    msg->text = strdup(buf);

    // reset bounding box
    msg->sizebox[0] = 0.0;
    msg->sizebox[1] = 0.0;
    msg->sizebox[2] = 0.0;
    msg->sizebox[3] = 0.0;

    // reset display time counter
    if (msg->state >= OSD_DISPLAY)
    {
        msg->state = OSD_DISPLAY;
        msg->frames = 0;
    }

    SDL_LockMutex(osd_list_lock);
    if (!osd_message_valid(msg))
        list_add(&msg->list, &l_messageQueue);
    SDL_UnlockMutex(osd_list_lock);

}

// remove message from message queue
static void osd_remove_message(osd_message_t *msg)
{
    if (!l_OsdInitialized || !msg) return;

    free(msg->text);
    msg->text = NULL;
    list_del(&msg->list);
}

// remove message from message queue and free it
extern "C"
void osd_delete_message(osd_message_t *msg)
{
    if (!l_OsdInitialized || !msg) return;

    SDL_LockMutex(osd_list_lock);
    osd_remove_message(msg);
    free(msg);
    SDL_UnlockMutex(osd_list_lock);
}

// set message so it doesn't automatically expire in a certain number of frames.
extern "C"
void osd_message_set_static(osd_message_t *msg)
{
    if (!l_OsdInitialized || !msg) return;

    msg->timeout[OSD_DISPLAY] = OSD_INFINITE_TIMEOUT;
    msg->state = OSD_DISPLAY;
    msg->frames = 0;
}

// set message so it doesn't automatically get freed when finished transition.
extern "C"
void osd_message_set_user_managed(osd_message_t *msg)
{
    if (!l_OsdInitialized || !msg) return;

    msg->user_managed = 1;
}

// return message pointer if valid (in the OSD list), otherwise return NULL
static osd_message_t * osd_message_valid(osd_message_t *testmsg)
{
    osd_message_t *msg;

    if (!l_OsdInitialized || !testmsg) return NULL;

    list_for_each_entry_t(msg, &l_messageQueue, osd_message_t, list) {
        if (msg == testmsg)
            return testmsg;
    }

    return NULL;
}