Commit 58da01b1 authored by daFischer's avatar daFischer
Browse files

Audio controls and fullscreen work, except for seeking

parent 7803c731
emBuild/TTT/
\ No newline at end of file
emBuild/TTT/
emBuild/fileplayer.data
......@@ -64,14 +64,19 @@ Audio::Audio(const char* path) {
failed=false;
}
void Audio::play(){
//Play the audio file
printf("play1\n");
alSourcePlay(source);
printf("play2\n");
printf("Start playing for %d ms\n",getDuration());
//The audio is playing!
void Audio::togglePlay(){
ALint state;
alGetSourcei(source, AL_SOURCE_STATE, &state);
if (state != AL_PLAYING)
{
//Play the audio file
alSourcePlay(source);
printf("play2\n");
}
else
{
alSourcePause(source);
}
}
int Audio::getPosition()
......@@ -87,15 +92,18 @@ int Audio::getPosition()
}
/*bool Audio::setPosition(int time)
void Audio::setPosition(int pos)
{
ALfloat pos=(ALfloat)pos/1000.0;
alSourcef(source, AL_SEC_OFFSET, pos);
ALfloat time=(ALfloat)pos/1000.0;
alSourcef(source, AL_SEC_OFFSET, time);
if(alGetError()== AL_NO_ERROR)
return true;
else
return false;
}*/
printf("Audio.cpp: set Position error");
}
void Audio::changeVolume(float volume){
ALfloat v=volume;
alSourcef(source,AL_GAIN,v);
}
int Audio::getDuration()
{
......
......@@ -33,11 +33,12 @@ using namespace std;
class Audio: public AudioInterface{
public:
Audio(const char*);
void play();
void togglePlay();
int getPosition();
//bool setPosition(int time);
void setPosition(int pos);
int getDuration();
bool hasFailed();
void changeVolume(float volume);
private:
bool failed;
......
......@@ -11,7 +11,7 @@ AudioInterface::AudioInterface(){
}
void AudioInterface::play(){
void AudioInterface::togglePlay(){
}
int AudioInterface::getDuration(){
......@@ -22,6 +22,12 @@ int AudioInterface::getPosition(){
return 0;
}
void AudioInterface::setPosition(int pos){
}
void AudioInterface::changeVolume(float volume){
}
bool AudioInterface::hasFailed(){
return true;
}
......@@ -16,10 +16,12 @@ class AudioInterface {
public:
AudioInterface();
virtual void play();
virtual void togglePlay();
virtual int getPosition();
virtual void setPosition(int pos);
virtual int getDuration();
virtual bool hasFailed();
virtual void changeVolume(float volume);
};
......
......@@ -16,9 +16,9 @@ AudioJS::AudioJS() {
printf("Duration: %d min\n",duration/60000);
}
void AudioJS::play(){
void AudioJS::togglePlay(){
EM_ASM(
x_play();
x_togglePlay();
);
}
......@@ -26,6 +26,13 @@ int AudioJS::getDuration(){
return duration;
}
void AudioJS::setPosition(int pos){
EM_ASM_INT({
x_setDuration($0);
return 0;
},pos);
}
int AudioJS::getPosition(){
return EM_ASM_INT({
return x_getPosition();
......@@ -36,4 +43,10 @@ bool AudioJS::hasFailed(){
return false;
}
void AudioJS::changeVolume(float volume){
EM_ASM_INT({
x_changeVolume($0);
},volume);
}
#endif
......@@ -22,10 +22,12 @@ using namespace std;
class AudioJS: public AudioInterface{
public:
AudioJS();
void play();
void togglePlay();
int getPosition();
void setPosition(int pos);
int getDuration();
bool hasFailed();
void changeVolume(float volume);
private:
int duration;
......
/*
* File: Controls.cpp
* Author: user
*
* Created on July 13, 2014, 6:40 PM
*/
#include "Controls.h"
bool mouseOnFullScreenButton;
Controls::Controls(Video* video, AudioInterface* audio) {
this->video=video;
this->audio=audio;
ProtocolPreferences prefs;
//TODO: might make these relative to video's height
timeLineHeight=16;
height=32;
y=prefs.framebufferHeight - height;
width=prefs.framebufferWidth;
timeLineClicked=false;
timeLineChange=-1;
volumeClicked=false;
volume=1;
mouseOnFullScreenButton=false;
#ifdef EMSCRIPTEN
EM_ASM_INT({
x_setupFullScreen($0, $1);
}, 48, height);
#endif
}
void Controls::registerClick(Uint16 mx, Uint16 my){
if(my<y-timeLineHeight)
return;
if(my<y)
{
timeLineClicked=true;
}
else
{
if(mx<48)
togglePlay();
else if(mx>=width-48)
toggleFullscreen();
else if(mx<=128 && mx>=64)
volumeClicked=true;
}
}
void Controls::registerMouseUp(){
timeLineClicked=false;
volumeClicked=false;
}
void Controls::registerMovement(Uint16 mx, Uint16 my) {
if(timeLineClicked)
timeLineChange=mx;
if(volumeClicked)
changeVolume(max(min((float)(mx-64),(float)64),(float)0)/64);
//#ifdef EMSCRIPTEN
mouseOnFullScreenButton=(mx>=width-48 && my>=y);
//#endif
}
//#ifdef EMSCRIPTEN
extern "C" bool getOnFullScreenButton(){
return mouseOnFullScreenButton;
}
//#endif
void Controls::update(){
if(!timeLineClicked && timeLineChange>=0)
{
skipTo(audio->getDuration()*timeLineChange/width);
timeLineChange=-1;
}
draw();
}
void Controls::togglePlay(){
audio->togglePlay();
}
void Controls::toggleFullscreen(){
#ifndef EMSCRIPTEN
video->toggleFullscreen();
#endif
}
void Controls::draw(){
SDL_Surface *screen=video->getScreen();
//main body
SDL_Rect rect = {0, y, width, height};
SDL_FillRect(screen, &rect, 0xff000000);
//play/pause
redefineRect(&rect, 0, y, 48, height);
SDL_FillRect(screen, &rect, 0xff338844);
//volume
redefineRect(&rect, 64, y, 64, height);
SDL_FillRect(screen, &rect, 0xff338844);
redefineRect(&rect, 64, y, (int)(volume*64.0), height);
SDL_FillRect(screen, &rect, 0xff0000aa);
//fullscreen button
redefineRect(&rect, width-48, y, 48, height);
SDL_FillRect(screen, &rect, 0xff338844);
//timeLine background
redefineRect(&rect, 0, y-timeLineHeight+2, width, timeLineHeight-2);
SDL_FillRect(screen, &rect, 0xff333333);
//timeLine foreground
redefineRect(&rect, 0, y-timeLineHeight, width*audio->getPosition()/audio->getDuration(), timeLineHeight);
SDL_FillRect(screen, &rect, 0xffaa0000);
SDL_UpdateRect(screen, 0,y-timeLineHeight,width,height+timeLineHeight);
}
void Controls::redefineRect(SDL_Rect* rect, int x, int y, int w, int h){
rect->x=x;
rect->y=y;
rect->w=w;
rect->h=h;
}
void Controls::skipTo(int position){
}
void Controls::changeVolume(float volume){
this->volume=volume;
audio->changeVolume(volume);
}
/*
* File: Controls.h
* Author: user
*
* Created on July 13, 2014, 6:40 PM
*/
#ifndef CONTROLS_H
#define CONTROLS_H
#include "AudioInterface.h"
#include "Video.h"
#include "ProtocolPreferences.h"
#include <stdio.h>
#include <SDL/SDL.h>
#ifdef EMSCRIPTEN
#include <emscripten/emscripten.h>
#endif
using namespace std;
class Controls {
public:
Controls(Video* video, AudioInterface* audio);
void update();
void registerClick(Uint16 mx, Uint16 my);
void registerMouseUp();
void registerMovement(Uint16 mx, Uint16 my);
private:
Video* video;
AudioInterface* audio;
int width, height;
int timeLineHeight;
bool timeLineClicked;
int timeLineChange; //For when the user seeks through the video
bool volumeClicked;
float volume;
int y;
void togglePlay();
void toggleFullscreen();
void skipTo(int position);
void draw();
void redefineRect(SDL_Rect* rect, int x, int y, int w, int h);
void changeVolume(float volume);
};
//#ifdef EMSCRIPTEN
extern "C" bool getOnFullScreenButton();
//#endif
#endif /* CONTROLS_H */
......@@ -7,6 +7,7 @@
#include "Player.h"
#include "AudioJS.h"
#include "Controls.h"
Player *player;
......@@ -39,11 +40,13 @@ Player::Player(const char* cpath, const char* cfilename) {
return;
}
audio->play();
controls=new Controls(video,audio);
audio->togglePlay();
printf("start looping\n");
#ifdef EMSCRIPTEN
emscripten_set_main_loop(emLoop,30,1);
emscripten_set_main_loop(emLoop,0,0);
#else
quit = false;
while(true)
......@@ -63,6 +66,7 @@ void Player::loop()
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN) {
switch(event.key.keysym.sym) {
#ifndef EMSCRIPTEN
......@@ -74,7 +78,16 @@ void Player::loop()
default:
break;
}
}
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);
}
controls->update();
}
void emLoop()
......
......@@ -27,6 +27,7 @@
#include "Audio.h"
#endif
#include "Video.h"
#include "Controls.h"
using namespace std;
......@@ -39,6 +40,7 @@ public:
private:
AudioInterface *audio;
Video *video;
Controls* controls;
bool paused;
const char* filename;
#ifndef EMSCRIPTEN
......
......@@ -6,7 +6,6 @@
*/
#include "Video.h"
#include "Message.h"
Video::Video(const char* path) {
......@@ -64,6 +63,18 @@ void Video::update(int zeit)
}
}
SDL_Surface* Video::getScreen(){
return screen;
}
void Video::toggleFullscreen(){
return; //doesn't work yet, but not really important
{
printf("toggle fullscreen %d,%d\n",screen->flags,screen->flags & SDL_FULLSCREEN);
screen=SDL_SetVideoMode(screen->w, screen->h, screen->format->BitsPerPixel, screen->flags ^ SDL_FULLSCREEN);
}
}
bool readServerInit(Inflater* in)
{
ProtocolPreferences prefs;
......
......@@ -32,6 +32,8 @@ public:
//Video(const Video& orig);
virtual ~Video();
void update(int zeit);
SDL_Surface* getScreen();
void toggleFullscreen();
bool failed;
......
build/Debug/GNU-Linux-x86/Controls.o: Controls.cpp Controls.h \
AudioInterface.h Video.h Inflater.h SizedArray.h ProtocolPreferences.h \
Message.h Constants.h
Controls.h:
AudioInterface.h:
Video.h:
Inflater.h:
SizedArray.h:
ProtocolPreferences.h:
Message.h:
Constants.h:
build/Debug/GNU-Linux-x86/Player.o: Player.cpp Player.h AudioInterface.h \
Audio.h Video.h Inflater.h SizedArray.h ProtocolPreferences.h Message.h \
Constants.h AudioJS.h
Constants.h Controls.h AudioJS.h
Player.h:
......@@ -20,4 +20,6 @@ Message.h:
Constants.h:
Controls.h:
AudioJS.h:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment