Player.cpp 3.8 KB
Newer Older
daFischer's avatar
first  
daFischer committed
1 2
/* 
 * File:   Player.cpp
3
 * Author: Johannes Fischer
daFischer's avatar
first  
daFischer committed
4 5 6 7 8
 * 
 * Created on April 30, 2014, 4:30 PM
 */

#include "Player.h"
daFischer's avatar
daFischer committed
9
#include "AudioJS.h"
10
#include "Controls.h"
daFischer's avatar
first  
daFischer committed
11 12

Player *player;
daFischer's avatar
daFischer committed
13
TTF_Font* Player::font;
daFischer's avatar
first  
daFischer committed
14

15
Player::Player(string filename) {
daFischer's avatar
first  
daFischer committed
16
    
17
    atexit(cleanUp);
daFischer's avatar
first  
daFischer committed
18
    paused=false;
daFischer's avatar
daFischer committed
19
    
daFischer's avatar
first  
daFischer committed
20 21 22
    player=this;
    
    //Initialize BOTH SDL video and SDL audio
23
    if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0)
daFischer's avatar
first  
daFischer committed
24 25 26 27 28
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return;
    }
    
29
    if(TTF_Init() < 0){
daFischer's avatar
daFischer committed
30 31 32
        printf("Unable to initialize TTF: %s\n", TTF_GetError());
        return;
    }
daFischer's avatar
first  
daFischer committed
33
    
daFischer's avatar
daFischer committed
34
#ifdef EMSCRIPTEN
35
    font = TTF_OpenFont("sans-serif",20);
daFischer's avatar
daFischer committed
36
#else
37
    font = TTF_OpenFont("Assets/arial.ttf",20);
daFischer's avatar
daFischer committed
38 39 40 41 42 43 44
#endif
    if(font==NULL)
    {
        printf("No font\n");
        return;
    }
    
daFischer's avatar
daFischer committed
45 46 47
#ifdef EMSCRIPTEN
    audio=new AudioJS();
#else
48
    audio=new Audio((filename).c_str());
daFischer's avatar
daFischer committed
49
#endif
daFischer's avatar
daFischer committed
50
    
daFischer's avatar
daFischer committed
51 52 53 54 55 56
    if(audio->hasFailed())
    {
        printf("Audio failed.\n");
        return;
    }
    
57
    video=new Video((filename+".ttt").c_str());
daFischer's avatar
daFischer committed
58 59 60 61 62 63 64
#ifdef EMSCRIPTEN
    emscripten_set_main_loop(loadAsync,0,0);
#else
    while(loadAsync()){}
#endif
}

65 66 67 68
/**
 * This function will be looped and loads Video from File step by step.
 * @return true if compiled without Emscripten and loading has not yet finished
 */
daFischer's avatar
daFischer committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#ifdef EMSCRIPTEN
void Player::loadAsync() {
    if(player->video->loadAsync())
        return;    //continue loading
    else        //finished loading
    {
        emscripten_cancel_main_loop();
        player->videoCallback();
    }
}
#else
bool Player::loadAsync() {
    if(player->video->loadAsync())
        return true;    //continue loading
    else        //finished loading
    {
        player->videoCallback();
        return false;
    }
}
#endif

91 92 93
/**
 * Called after the video object has been loaded completely
 */
daFischer's avatar
daFischer committed
94 95
void Player::videoCallback() {
    if(video->failed)
daFischer's avatar
daFischer committed
96
    {
daFischer's avatar
daFischer committed
97
        printf("Video failed.\n");
daFischer's avatar
daFischer committed
98 99 100
        return;
    }
    
101 102 103
    controls=new Controls(video,audio);
    
    audio->togglePlay();
daFischer's avatar
first  
daFischer committed
104 105 106
    
    printf("start looping\n");
#ifdef EMSCRIPTEN
107
    emscripten_set_main_loop(emLoop,0,0);
daFischer's avatar
first  
daFischer committed
108 109 110 111 112 113 114 115 116
#else
    quit = false;
    while(true)
    {
        emLoop();
        if(quit)
            break;
        SDL_Delay(30);
    }
117 118
    if(player!=NULL)
        delete(player);
daFischer's avatar
first  
daFischer committed
119 120 121
#endif
}

122 123 124 125 126
/**
 * This is the main loop
 * First events are being polled, then controls and video update
 * This loop isn't canceled unless the program is being stopped
 */
daFischer's avatar
first  
daFischer committed
127 128 129 130
void Player::loop()
{
    SDL_Event event;
    while (SDL_PollEvent(&event)) 
131
    {
daFischer's avatar
first  
daFischer committed
132 133 134 135 136
        if (event.type == SDL_KEYDOWN) {
            switch(event.key.keysym.sym) {
#ifndef EMSCRIPTEN
                case SDLK_ESCAPE:
                    quit=true;
137
                    return;
daFischer's avatar
first  
daFischer committed
138 139 140 141
#endif
                default:
                    break;
            }
142 143 144 145 146 147 148
        }
        else if(event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT)
            controls->registerClick(event.button.x,event.button.y);
        else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == SDL_BUTTON_LEFT)
            controls->registerMouseUp();
        else if(event.type == SDL_MOUSEMOTION)
            controls->registerMovement(event.motion.x,event.motion.y);
daFischer's avatar
first  
daFischer committed
149
    }
150
    controls->update();
151 152
    
    video->update(audio->getPosition(), controls);
daFischer's avatar
first  
daFischer committed
153 154 155 156
}

void emLoop()
{
157 158 159 160 161 162 163
    if(player!=NULL)
        player->loop();
}

void cleanUp(){
    if(player!=NULL)
        delete(player);
daFischer's avatar
first  
daFischer committed
164 165 166
}

Player::~Player() {
167 168 169 170 171 172
    delete(video);
    video=NULL;
    delete(audio);
    audio=NULL;
    delete(controls);
    controls=NULL;
daFischer's avatar
daFischer committed
173
    TTF_CloseFont(font);
174 175 176
#ifdef EMSCRIPTEN
    emscripten_cancel_main_loop();
#else
daFischer's avatar
daFischer committed
177
    //function doesn't exist in emscripten for some reason
178 179 180 181
    TTF_Quit();
#endif
    SDL_Quit();
    player=NULL;
182 183
    if(VERBOSE)
        printf("Freeing Memory completed\n");
daFischer's avatar
first  
daFischer committed
184 185
}