Commit 949072d9 authored by daFischer's avatar daFischer

Added a few minor fixes and more comments

parent e32350e0
/*
* File: Audio.cpp
* Author: user
* http://wiki.delphigl.com/index.php/OpenAL-Funktions%C3%BCbersicht
* http://www.codeproject.com/Articles/656543/The-LAME-wrapper-An-audio-converter
* https://github.com/sopel39/audioconverter.js
* Author: Johannes Fischer
* Created on April 30, 2014, 3:39 PM
*/
......@@ -94,13 +91,13 @@ int Audio::getPosition()
if((error = alGetError())!= AL_NO_ERROR)
printf("getTime error: %d\n", error);
//printf("%f\n",(float) time);
return (int)time;
return (int)(time*1000);
}
void Audio::setPosition(int pos)
{
ALfloat time=(ALfloat)pos;
ALfloat time=(ALfloat)(pos/1000);
alSourcef(source, AL_SEC_OFFSET, time);
uint error;
if((error=alGetError()) != AL_NO_ERROR)
......@@ -112,6 +109,11 @@ void Audio::changeVolume(float volume){
alSourcef(source,AL_GAIN,v);
}
void Audio::changeSpeed(float speed){
ALfloat s=speed;
alSourcef(source,AL_PITCH,s);
}
int Audio::getDuration()
{
ALint numBytes;
......@@ -132,7 +134,7 @@ int Audio::getDuration()
if(alGetError()!= AL_NO_ERROR)
printf("getDuration error\n");
return (int)seconds;
return (int)(seconds*1000);
}
bool Audio::hasFailed()
......
/*
* File: Audio.h
* Author: user
* Author: Johannes Fischer
*
* Created on April 30, 2014, 3:39 PM
*/
......@@ -42,6 +42,7 @@ public:
int getDuration();
bool hasFailed();
void changeVolume(float volume);
void changeSpeed(float volume);
bool isPlaying();
private:
......
/*
* File: AudioInterface.cpp
* Author: user
* Author: Johannes Fischer
*
* Created on July 13, 2014, 2:15 PM
*/
......@@ -28,6 +28,9 @@ void AudioInterface::setPosition(int pos){
void AudioInterface::changeVolume(float volume){
}
void AudioInterface::changeSpeed(float volume){
}
bool AudioInterface::hasFailed(){
return true;
}
......
/*
* File: AudioInterface.h
* Author: user
* Author: Johannes Fischer
*
* Created on July 13, 2014, 2:15 PM
*/
......@@ -8,7 +8,7 @@
#ifndef AUDIOINTERFACE_H
#define AUDIOINTERFACE_H
using namespace std;
//using namespace std;
class AudioInterface {
public:
......@@ -20,6 +20,7 @@ public:
virtual int getDuration();
virtual bool hasFailed();
virtual void changeVolume(float volume);
virtual void changeSpeed(float volume);
virtual bool isPlaying();
};
......
/*
* File: AudioJS.cpp
* Author: user
* Author: Johannes Fischer
*
* Created on July 13, 2014, 2:51 PM
*/
......@@ -49,6 +49,12 @@ void AudioJS::changeVolume(float volume){
},volume);
}
void AudioJS::changeSpeed(float speed){
EM_ASM_INT({
x_changeSpeed($0);
},speed);
}
bool AudioJS::isPlaying(){
return EM_ASM_INT({
return x_audio.paused;
......
/*
* File: AudioJS.h
* Author: user
* Author: Johannes Fischer
*
* Created on July 13, 2014, 2:51 PM
*/
......@@ -30,6 +30,7 @@ public:
int getDuration();
bool hasFailed();
void changeVolume(float volume);
void changeSpeed(float volume);
bool isPlaying();
private:
int duration;
......
/*
* File: ColorConverter.cpp
* Author: user
* Author: Johannes Fischer
*
* Created on June 4, 2014, 3:55 PM
*/
......
/*
* File: ColorConverter.h
* Author: user
* Author: Johannes Fischer
*
* Created on June 4, 2014, 3:55 PM
*/
......
/*
* File: Constants.cpp
* Author: user
* Author: Johannes Fischer
*
* Created on May 5, 2014, 5:39 PM
*/
......
/*
* File: Constants.h
* Author: user
* Author: Johannes Fischer
*
* Original by Fabian Petter on 10/11/10.
* https://www2.in.tum.de/repos/ttt/branches/iOS/viewer/1.0/Classes/Constants.h
......@@ -22,7 +22,7 @@
//message kinds
#define ANNOTATION 'a'
#define EMPTY '0'
#define RAW 'r'
#define FRAMEBUFFER 'r'
#define CURSOR 'c'
//message types
......
/*
* File: Controls.cpp
* Author: user
* Author: Johannes Fischer
*
* Created on July 13, 2014, 6:40 PM
*/
......@@ -10,6 +10,7 @@
#include "Player.h"
bool mouseOnFullScreenButton;
bool mouseOnPlayButton;
Controls::Controls(Video* video, AudioInterface* audio) {
this->video=video;
......@@ -19,36 +20,41 @@ Controls::Controls(Video* video, AudioInterface* audio) {
ProtocolPreferences prefs;
progress=0;
//TODO: might make these relative to video's height
//TODO: maybe make these relative to the screen's height
timeLineHeight=16;
height=timeLineHeight+32;
width=prefs.framebufferWidth;
screenHeight=prefs.framebufferHeight;
y=screenHeight;
visible=false;
y=screenHeight-height;
visible=true;
//In the beginning, the whole screen should be redrawn
redefineRect(&videoUpdate,0,0,width,screenHeight);
timeLineClicked=false;
volumeClicked=false;
volume=1;
speedClicked=false;
speed=1;
mouseOnFullScreenButton=false;
mouseOnPlayButton=false;
#ifdef EMSCRIPTEN
//Make Fullscreen and starting the <audio> element possible
EM_ASM(
x_setupFullScreen();
x_setupListener();
);
#endif
surfPlay=SDL_LoadBMP("Assets/PlayPause.bmp");
surfVolume=SDL_LoadBMP("Assets/volume.bmp");
surfVolume2=SDL_LoadBMP("Assets/volume2.bmp");
surfFullscreen=SDL_LoadBMP("Assets/fullscreen.bmp");
#else
surfPlay=SDL_LoadBMP("/home/user/NetBeansProjects/TTTPlayer/emBuild/Assets/PlayPause.bmp");
surfVolume=SDL_LoadBMP("/home/user/NetBeansProjects/TTTPlayer/emBuild/Assets/volume.bmp");
surfVolume2=SDL_LoadBMP("/home/user/NetBeansProjects/TTTPlayer/emBuild/Assets/volume2.bmp");
surfFullscreen=SDL_LoadBMP("/home/user/NetBeansProjects/TTTPlayer/emBuild/Assets/fullscreen.bmp");
#endif
if(surfPlay==NULL||surfVolume==NULL||surfVolume2==NULL||surfFullscreen==NULL)
surfSpeed=SDL_LoadBMP("Assets/speed.bmp");
surfSpeed2=SDL_LoadBMP("Assets/speed2.bmp");
if(surfPlay==NULL||surfVolume==NULL||surfVolume2==NULL||surfFullscreen==NULL||surfSpeed==NULL||surfSpeed2==NULL)
printf("BMPs ARE NULL\n");
}
......@@ -56,11 +62,16 @@ Controls::~Controls() {
SDL_FreeSurface(surfPlay);
SDL_FreeSurface(surfVolume);
SDL_FreeSurface(surfVolume2);
SDL_FreeSurface(surfSpeed);
SDL_FreeSurface(surfSpeed2);
SDL_FreeSurface(surfFullscreen);
audio=NULL;
video=NULL;
}
/**
* Will be called after the user presses the left mouse button
*/
void Controls::registerClick(Uint16 mx, Uint16 my){
if(my<y)
return;
......@@ -70,45 +81,66 @@ void Controls::registerClick(Uint16 mx, Uint16 my){
}
else
{
#ifndef EMSCRIPTEN
if(mx<48)
togglePlay();
else if(mx>=width-48)
toggleFullscreen();
else if(mx<=128 && mx>=64)
else
#endif
if(mx<=128 && mx>=64)
volumeClicked=true;
else if(mx<=196 && mx>=160)
speedClicked=true;
}
registerMovement(mx,my);
}
/**
* Will be called after the user releases the left mouse button
*/
void Controls::registerMouseUp(){
if(timeLineClicked)
{
redefineRect(&videoUpdate,0,0,width,screenHeight-height);
skipTo(duration*mouseX/width);
skipTo(duration/width*mouseX);
}
timeLineClicked=false;
volumeClicked=false;
speedClicked=false;
}
/**
* Will be called whenever the mouse has moved
*/
void Controls::registerMovement(Uint16 mx, Uint16 my) {
visible=(my>=screenHeight-height)||timeLineClicked||volumeClicked;
visible=(my>=screenHeight-height)||timeLineClicked||volumeClicked||speedClicked;
mouseX=mx;
mouseY=my;
if(volumeClicked)
changeVolume(max(min((float)(mx-64),(float)64),(float)0)/64);
if(speedClicked)
changeSpeed(max(min((float)(mx-160),(float)36),(float)0)/72+1);
//#ifdef EMSCRIPTEN
mouseOnFullScreenButton=(mx>=width-48 && my>=y+timeLineHeight);
mouseOnPlayButton=(mx<48 && my>=y+timeLineHeight);
//#endif
}
//#ifdef EMSCRIPTEN
#ifdef EMSCRIPTEN
extern "C" bool getOnFullScreenButton(){
return mouseOnFullScreenButton;
}
//#endif
extern "C" bool getOnPlayButton(){
return mouseOnPlayButton;
}
#endif
/**
* When the mouse is not hovering over Controls' position, Controls moves out of the screen
*/
void Controls::update(){
if(visible)
{
......@@ -155,7 +187,6 @@ void Controls::draw(SDL_Surface *screen, bool hasDrawn){
//play/pause
redefineRect(&rect, 0, y+timeLineHeight, 48, height-timeLineHeight);
SDL_FillRect(screen, &rect, emColor(0x338844));
if(surfPlay!=NULL)
{
if(audio->isPlaying())
......@@ -166,24 +197,31 @@ void Controls::draw(SDL_Surface *screen, bool hasDrawn){
}
//volume
redefineRect(&rect, 64, y+timeLineHeight, 64, height-timeLineHeight);
SDL_FillRect(screen, &rect, emColor(0x333333));
redefineRect(&rect, 64, y+timeLineHeight, (int)(volume*64.0), height-timeLineHeight);
SDL_FillRect(screen, &rect, emColor(0x00ff00));
if(surfVolume!=NULL && surfVolume2!=NULL)
{
redefineRect(&rect, 64, y+timeLineHeight, 64, 32);
redefineRect(&srcRect,0,0,64, 32);
SDL_BlitSurface(surfVolume2,&srcRect,screen,&rect);
redefineRect(&srcRect,0,0,(int)(volume*64.0),32);
redefineRect(&rect,64,y+timeLineHeight,(int)(volume*64.0),32);
if((int)(volume*64.0)>=1)
redefineRect(&rect,64,y+timeLineHeight,srcRect.w,32);
if(srcRect.w>=1)
SDL_BlitSurface(surfVolume,&srcRect,screen,&rect);
}
//speed
if(surfSpeed!=NULL && surfSpeed2!=NULL)
{
redefineRect(&rect, 160, y+timeLineHeight, 36, 32);
redefineRect(&srcRect,0,0,36, 32);
SDL_BlitSurface(surfSpeed2,&srcRect,screen,&rect);
redefineRect(&srcRect,0,0,(int)(speed*72.0)-72,32);
redefineRect(&rect,160,y+timeLineHeight,srcRect.w,32);
if(srcRect.w>=1)
SDL_BlitSurface(surfSpeed,&srcRect,screen,&rect);
}
//fullscreen button
redefineRect(&rect, width-48, y+timeLineHeight, 48, height-timeLineHeight);
SDL_FillRect(screen, &rect, emColor(0x338844));
if(surfFullscreen!=NULL)
{
redefineRect(&srcRect,0,0,48, 32);
......@@ -195,14 +233,14 @@ void Controls::draw(SDL_Surface *screen, bool hasDrawn){
SDL_FillRect(screen, &rect, emColor(0x333333));
if(progress>=0)
{
redefineRect(&rect, progress/1000*width/duration, y, width-progress/1000*width/duration, timeLineHeight);
redefineRect(&rect, (progress/1000)*width/(duration/1000), y, width-(progress/1000)*width/(duration/1000), timeLineHeight);
SDL_FillRect(screen, &rect, emColor(0x000000));
}
int currentPosition=audio->getPosition();
//timeLine foreground
if(!timeLineClicked)
redefineRect(&rect, 0, y, currentPosition*width/duration, timeLineHeight);
redefineRect(&rect, 0, y, (currentPosition/1000)*width/(duration/1000), timeLineHeight);
else
redefineRect(&rect, 0, y, mouseX, timeLineHeight);
SDL_FillRect(screen, &rect, emColor(0xaa0000));
......@@ -210,7 +248,7 @@ void Controls::draw(SDL_Surface *screen, bool hasDrawn){
//draw "time / duration"
SDL_Color white = {0xff,0xff,0xff,0xff};
ostringstream oss;
oss << currentPosition/60 << ":" << ((currentPosition%60<10) ? "0" : "") << currentPosition%60 << "/" << duration/60 << ":" << ((duration%60<10) ? "0" : "") << duration%60;
oss << (currentPosition/1000)/60 << ":" << (((currentPosition/1000)%60<10) ? "0" : "") << (currentPosition/1000)%60 << "/" << (duration/1000)/60 << ":" << (((duration/1000)%60<10) ? "0" : "") << (duration/1000)%60;
SDL_Surface* times=TTF_RenderText_Solid(Player::font,oss.str().c_str(),white);
#ifdef EMSCRIPTEN
......@@ -223,10 +261,10 @@ void Controls::draw(SDL_Surface *screen, bool hasDrawn){
if(mouseY>=y&&mouseY<y+timeLineHeight)
{
video->drawThumbnail(duration*mouseX/width*1000,mouseX,y);
video->drawThumbnail(duration/width*mouseX,mouseX,y);
//draw time at cursor position
ostringstream oss2;
oss2 << duration*mouseX/width/60 << ":" << (((duration*mouseX/width)%60<10) ? "0" : "") << (duration*mouseX/width)%60;
oss2 << (duration/1000)*mouseX/width/60 << ":" << ((((duration/1000)*mouseX/width)%60<10) ? "0" : "") << ((duration/1000)*mouseX/width)%60;
SDL_Color black={0,0,0,0xff};
/*TTF_SetFontOutline(Player::font,2);
times=TTF_RenderText_Solid(Player::font,oss2.str().c_str(),black);
......@@ -260,6 +298,13 @@ Uint32 Controls::emColor(unsigned int color){
#endif
}
/**
* Needed to draw the text indicating the times scaled
* @param source source text image
* @param x
* @param y
* @return Color in the source images color format
*/
Uint32 Controls::readPixel(SDL_Surface* source, int x, int y){
switch(source->format->BytesPerPixel)
{
......@@ -272,6 +317,14 @@ Uint32 Controls::readPixel(SDL_Surface* source, int x, int y){
}
}
/**
* Font size doesn't work right with Emscripten. Therefore we have to scale it up manually
* @param screen the target surface
* @param text the original (small) text image
* @param x
* @param y
* @param factor how strong the image should be upscaled
*/
void Controls::drawScaledText(SDL_Surface* screen, SDL_Surface* text, short x, short y, char factor) {
SDL_Rect rect={0,0,factor,factor};
SDL_LockSurface(text);
......@@ -301,3 +354,8 @@ void Controls::changeVolume(float volume){
this->volume=volume;
audio->changeVolume(volume);
}
void Controls::changeSpeed(float speed){
this->speed=speed;
audio->changeSpeed(speed);
}
/*
* File: Controls.h
* Author: user
* Author: Johannes Fischer
*
* Created on July 13, 2014, 6:40 PM
*/
......@@ -47,6 +47,7 @@ private:
Uint32 emColor(Uint32);
void redefineRect(SDL_Rect* rect, int x, int y, int w, int h);
void changeVolume(float volume);
void changeSpeed(float volume);
void drawScaledText(SDL_Surface* screen, SDL_Surface* text, short x, short y, char factor);
Uint32 readPixel(SDL_Surface* source, int x, int y);
......@@ -60,16 +61,21 @@ private:
int mouseX, mouseY;
bool volumeClicked;
float volume;
bool speedClicked;
float speed;
int duration;
SDL_Surface* surfPlay;
SDL_Surface* surfVolume;
SDL_Surface* surfVolume2;
SDL_Surface* surfFullscreen;
SDL_Surface* surfSpeed;
SDL_Surface* surfSpeed2;
};
//#ifdef EMSCRIPTEN
extern "C" bool getOnFullScreenButton();
extern "C" bool getOnPlayButton();
//#endif
......
/*
* File: Index.cpp
* Author: user
* Author: Johannes Fischer
*
* Created on July 23, 2014, 10:10 PM
*/
......@@ -40,7 +40,7 @@ Index::Index(Message** messages, int numMessages) {
Message* message = messages[i];
// sum up area(s)
if (message->type == RAW) {
if (message->type == FRAMEBUFFER) {
area += message->getArea();
//printf("%d,",area);
} else if (area == 0)
......@@ -196,7 +196,7 @@ bool Index::fillSurface(SDL_Surface* screen, Message** messages, int numMessages
while (currentMessage < numMessages) {
if (messages[currentMessage]->timestamp > (*it)->timestamp)
break;
if (messages[currentMessage]->type == RAW)
if (messages[currentMessage]->type == FRAMEBUFFER)
messages[currentMessage]->paint(waypoint, prefs);
currentMessage++;
}
......
/*
* File: Index.h
* Author: user
* Author: Johannes Fischer
*
* Created on July 23, 2014, 10:10 PM
*/
......
/*
* File: IndexEntry.cpp
* Author: user
* Author: Johannes Fischer
*
* Created on July 24, 2014, 12:54 AM
*/
......@@ -89,6 +89,7 @@ void writePixel(SDL_Surface* target, int x, int y, Uint32 color){
SDL_Surface* IndexEntry::scaleDownSurface(SDL_Surface* source, char factor) {
SDL_Surface* result=SDL_CreateRGBSurface(0,source->w/factor,source->h/factor,source->format->BitsPerPixel,source->format->Rmask,source->format->Gmask,source->format->Bmask,source->format->Amask);
SDL_LockSurface(result);
Uint32 alpha,red,green,blue;
Uint32 color, colorPart;
for(int i=0;i<result->w;i++)
......@@ -102,5 +103,6 @@ SDL_Surface* IndexEntry::scaleDownSurface(SDL_Surface* source, char factor) {
readPixel(source,i+k,j+l);
}*/
}
SDL_UnlockSurface(result);
return result;
}
/*
* File: IndexEntry.h
* Author: user
* Author: Johannes Fischer
*
* Created on July 24, 2014, 12:54 AM
*/
......
/*
* File: Inflater.cpp
* Author: user
* using http://zlib.net/zlib_how.html
* Author: Johannes Fischer
*
* Created on May 8, 2014, 12:46 PM
*
* Credits to http://zlib.net/zlib_how.html
*/
#include "Inflater.h"
Inflater::Inflater(FILE* f)
{
//printf("Constructor of Inflater\n");
source=f;
outOffset=0;
/*fseek(source,0,SEEK_END);
printf("Inflater file size: %ld\n",ftell(source));
fseek(source,0,SEEK_SET);*/
// allocate inflate state
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.zalloc = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if (ret != Z_OK)
{
printf("Inflater fail 1\n");
printf("There has been an error in Inflater (1)\n");
return;
}
strm.avail_in = fread(in, 1, CHUNK, source); //from the outer loop of the example
//Read CHUNK bytes from the file
strm.avail_in = fread(in, 1, CHUNK, source);
if (ferror(source)) {
(void)inflateEnd(&strm);
ret = Z_ERRNO;
......@@ -41,27 +38,30 @@ Inflater::Inflater(FILE* f)
ret = Z_ERRNO;
if (ret != Z_OK)
{
printf("Inflater fail 2\n");
if(VERBOSE)
printf("There has been an error in Inflater (2)\n");
return;
}
strm.next_in = in;
strm.avail_out = CHUNK; //from the inner loop of the example
strm.avail_out = CHUNK;
strm.next_out = out;
//Inflate the read chunk and write to the char Array out
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR); //state not clobbered
//Test for any problem that might have appeared
assert(ret != Z_STREAM_ERROR);
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; //and fall through
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
printf("Inflater fail 3\n");
printf("There has been an error in Inflater (3)\n");
return;
}
//printf("Constructor of Inflater: success\n");
}
Inflater::~Inflater()
......@@ -69,10 +69,16 @@ Inflater::~Inflater()
//clean up and return
(void)inflateEnd(&strm);
ret = Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
printf("Inflater Endresult: %d\n",ret);
if(VERBOSE)
printf("Inflater Endresult: %d\n",ret);
fclose(source);
}
/**
* inflates and reads one Char from the File
* @param Byte The char to be filled by reading
* @return returns Z_OK if no problem has occurred
*/
bool Inflater::readByte(char* Byte)
{
if(addedChars.size()>0)
......@@ -83,18 +89,21 @@ bool Inflater::readByte(char* Byte)
}
if (ret != Z_OK)
{
//printf("Video Inflation failed: %d\n",ret);
//There has been an error, but the function is still being called. To avoid random results we 'read' 0.
*Byte=0;
return Z_ERRNO;
}
while(outOffset >= CHUNK - strm.avail_out) //Array out has to be refilled
//If all chars from the inflated chunk in the char array have been read, out has to be refilled
while(outOffset >= CHUNK - strm.avail_out)
{
//printf("Refill Array out, %d\n", ret);
//If everything has been inflated already, nothing can be done anymore; return.
if (ret == Z_STREAM_END)
return ret;
if(strm.avail_out != 0) //Array in has to be refilled
//Check whether Array in has to be refilled
if(strm.avail_out != 0)
{
//printf("Refill Array in\n");
//Fill Array in with CHUNK bytes from the file
strm.avail_in = fread(in, 1, CHUNK, source);
if (ferror(source)) {
(void)inflateEnd(&strm);
......@@ -108,9 +117,12 @@ bool Inflater::readByte(char* Byte)
strm.avail_out = CHUNK;
strm.next_out = out;
//Inflate a part of the char array in and write to the char array out
ret = inflate(&strm, Z_NO_FLUSH);
//Test for any problem that might have appeared
if(ret == Z_STREAM_ERROR)
printf("this assert fails\n");
printf("There has been an error in Inflater (4)\n");
assert(ret != Z_STREAM_ERROR); //state not clobbered
switch (ret) {
case Z_NEED_DICT:
......@@ -124,12 +136,15 @@ bool Inflater::readByte(char* Byte)
}
*Byte = out[outOffset];
outOffset++;
//printf("%c",*Byte);