Skip to content

Commit

Permalink
Correction timing de l'interface + sons
Browse files Browse the repository at this point in the history
  • Loading branch information
lfgaleota committed Jul 4, 2018
1 parent 9420f76 commit 3bf2e36
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 42 deletions.
26 changes: 15 additions & 11 deletions othello/inc/ui/games/gameallegroui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <GL/glu.h>
#include <alleggl.h>
#include <unordered_map>
#include <chrono>
#include "../../../../imgui/imgui.h"
#include "../../../../imgui/imgui_impl_agl.h"
#include "gameui.hpp"
Expand All @@ -39,8 +40,6 @@
#define PAUSE_BUTTON_HEIGHT 40
#define PAUSE_BLENDING_FACTOR 130

#define COMPUTE_DT( dt ) dt = (float) ( ( clock() - before ) * 30 ) / ( CLOCKS_PER_SEC );

/**
* @namespace Othello
* @details Espace de nommage du jeu.
Expand Down Expand Up @@ -74,7 +73,7 @@
friend class ErrorBar;

protected:
float t = 0;
double t = 0;
bool in = false, doAnimateIn = false, doAnimateOut = false, shouldAnimateIn = false;
int xIcon, xBar, y;
BITMAP* page;
Expand All @@ -84,7 +83,7 @@
constexpr static int widthIcon = 20, outXIcon = -16, inX = 0, height = 22, marginX = 4, marginY = 2, marginXIcon = 8, marginYIcon = 2;
int colorBarFront = makecol( 0, 0, 0 ), colorBarBack = makecol( 200, 200,
200 ), colorIconFront = colorBarBack, colorIconBack = colorBarFront, widthBar = SCREEN_W, outXBar = -SCREEN_W;
float inTime = 15, outTime = 15;
double inTime = 0.5, outTime = 0.5;

/**
* @brief Afficheur de barre réel
Expand All @@ -108,7 +107,7 @@
* @brief Afficheur de barre
* @param dt Temps entre cette frame et la frame précédente
*/
virtual void render( float dt );
virtual void render( double dt );

/**
* @brief Animateur d'entrée
Expand All @@ -133,9 +132,9 @@
private:
MessageBar* infoBar;

constexpr static time_t stayTime = CLOCKS_PER_SEC * 4;
time_t before, next;
int diff;
constexpr static std::chrono::seconds stayTime = std::chrono::seconds( 4 );
std::chrono::time_point<std::chrono::high_resolution_clock> before, next;
double diff;
int colorBarFront = makecol( 255, 255, 255 ), colorBarBack = makecol( 190, 0, 0 ), colorIconFront = makecol( 255, 255, 255 ), colorIconBack = makecol( 100, 0, 0 ), colorBarBackNoTime = makecol( 170, 0, 0 );

/**
Expand Down Expand Up @@ -170,7 +169,7 @@
* @brief Afficheur de barre
* @param dt Temps entre cette frame et la frame précédente
*/
void render( float dt );
void render( double dt );
};

/**
Expand All @@ -185,8 +184,8 @@
ImGuiIO& m_io;
bool prevKeyEsc = false, keyEsc = false, prevKeyG = false, keyG = false;
unsigned char x = 0, y = 0;
float dt;
time_t before;
double dt;
std::chrono::time_point<std::chrono::high_resolution_clock> before;
Othello::Board::GameBoard& m_oboard;
MessageBar infoBar;
ErrorBar errorBar;
Expand Down Expand Up @@ -285,6 +284,11 @@
*/
void gameDisplay();

/**
* @brief Calcul l'intervalle de temps entre deux frames
*/
double inline computeDt();

public:
/**
* @brief Constructeur
Expand Down
24 changes: 15 additions & 9 deletions othello/inc/ui/main/mainallegroui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <loadpng.h>
#include <vector>
#include <ios>
#include <chrono>
#include <fstream>
#include <sstream>
#include <stdlib.h>
Expand All @@ -43,7 +44,7 @@
#define ERROR_WIDTH 300
#define ERROR_HEIGHT 100

#define SPLASHSCREEN_DURATION_MINIMUM 2 * CLOCKS_PER_SEC
#define SPLASHSCREEN_DURATION_MINIMUM 2

/**
* @namespace Othello
Expand Down Expand Up @@ -94,11 +95,11 @@
*/
class AnimatedRectangle {
protected:
float t = 0;
float& dt;
double t = 0;
double& dt;
GLuint texture;
bool show = false, hoverIn = false, hoverOut = true, doAnimateEntry = false, doAnimateHoverIn = false, doAnimateHoverOut = false, sound = false;
constexpr static float defaultEntryRY = 50.0f, defaultEntryTime = 10.0f, defaultNormalX = 1.0f, defaultHoverX = 1.0f, defaultHoverZ = 0.5f, defaultHoverTime = 10.0f, defaultClickZ = -defaultHoverZ;
constexpr static float defaultEntryRY = 50.0f, defaultEntryTime = 0.3f, defaultNormalX = 1.0f, defaultHoverX = 1.0f, defaultHoverZ = 0.5f, defaultHoverTime = 0.3f, defaultClickZ = -defaultHoverZ;
float entryRY = defaultEntryRY, entryTime = defaultEntryTime, normalX = defaultNormalX, hoverX = defaultHoverX, hoverZ = defaultHoverZ, hoverTime = defaultHoverTime, clickZ = defaultClickZ;
Othello::UI::Audio::FMOD& m_fmod;

Expand All @@ -125,7 +126,7 @@
* @param dt Référence vers le temps entre cette frame et la frame précédente
* @param fmod Référence vers un objet de gestion #FMOD
*/
AnimatedRectangle( float& dt, Othello::UI::Audio::FMOD& fmod );
AnimatedRectangle( double& dt, Othello::UI::Audio::FMOD& fmod );

/**
* @brief Chargeur de texture du rectangle
Expand Down Expand Up @@ -194,7 +195,7 @@
* @param dt Référence vers le temps entre cette frame et la frame précédente
* @param fmod Référence vers un objet de gestion #FMOD
*/
MenuRectangle( float& dt, Othello::UI::Audio::FMOD& fmod );
MenuRectangle( double& dt, Othello::UI::Audio::FMOD& fmod );

/**
* @brief Chargeur de texture d'arrière-plan du rectangle
Expand Down Expand Up @@ -231,7 +232,7 @@
* @param dt Référence vers le temps entre cette frame et la frame précédente
* @param fmod Référence vers un objet de gestion #FMOD
*/
ButtonRectangle( float& dt, Othello::UI::Audio::FMOD& fmod );
ButtonRectangle( double& dt, Othello::UI::Audio::FMOD& fmod );
};

/**
Expand All @@ -246,8 +247,8 @@
bool keyEsc = false, prevKeyEsc = false, quit = false;
int choice;
std::vector<Othello::Players::Player*> m_players;
float t = 0, dt = 0;
clock_t before;
double t = 0, dt = 0;
std::chrono::time_point<std::chrono::high_resolution_clock> before;
GLdouble inx, iny, inz;
Othello::UI::Audio::FMOD& m_fmod;
AnimatedRectangle rectPVP, rectPVAI, rectPVPNet, rectAI1, rectAI2, rectAI3;
Expand Down Expand Up @@ -493,6 +494,11 @@
*/
void hideConsole();

/**
* @brief Calcul l'intervalle de temps entre deux frames
*/
double inline computeDt();

public:
/**
* @brief Constructeur
Expand Down
22 changes: 13 additions & 9 deletions othello/ui/games/gameallegroui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void MessageBar::draw() {
textprintf_ex( page, font, xIcon + marginXIcon, y + marginYIcon, colorIconFront, -1, "i" );
}

void MessageBar::render( float dt ) {
void MessageBar::render( double dt ) {
float ease;
if( in ) {
if( doAnimateIn ) {
Expand Down Expand Up @@ -85,10 +85,10 @@ ErrorBar::ErrorBar() {}

ErrorBar::ErrorBar( BITMAP* page, FONT* font, MessageBar* infoBar ) : MessageBar( page, font ), infoBar( infoBar ) {}

void ErrorBar::render( float dt ) {
void ErrorBar::render( double dt ) {
float ease;
if( in ) {
diff = next - clock();
diff = std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>( next - std::chrono::high_resolution_clock::now() ).count();
if( doAnimateIn ) {
t += dt;
if( t < inTime - 0.1f ) {
Expand Down Expand Up @@ -132,14 +132,14 @@ void ErrorBar::render( float dt ) {

void ErrorBar::draw() {
rectfill( page, xBar, y, xBar + widthBar, y + height, colorBarBackNoTime );
rectfill( page, xBar, y, (int) ( ( xBar + widthBar ) * (float) diff / stayTime ), y + height, colorBarBack );
rectfill( page, xBar, y, (int) ( ( xBar + widthBar ) * diff / stayTime.count() ), y + height, colorBarBack );
textprintf_ex( page, font, xBar + widthIcon + marginX, y + marginY, colorBarFront, -1, "%s", message.c_str() );
rectfill( page, xIcon, y, xIcon + widthIcon, y + height, colorIconBack );
textprintf_ex( page, font, xIcon + marginXIcon, y + marginYIcon, colorIconFront, -1, "!" );
}

void ErrorBar::animateIn( std::string msg, bool force ) {
before = clock();
before = std::chrono::high_resolution_clock::now();
next = before + stayTime;
MessageBar::animateIn( msg, force );
}
Expand Down Expand Up @@ -474,7 +474,7 @@ void Allegro::victory( Player* player ) {
m_fmod.playSoundWait( "victory" );
m_fmod.playMusic( "victory" );

before = clock() - 1;
before = std::chrono::high_resolution_clock::now();
for( bool loop = true; loop; ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
allegro_gl_set_allegro_mode();
Expand All @@ -491,11 +491,15 @@ void Allegro::victory( Player* player ) {
m_fmod.stopMusic();
}

double inline Allegro::computeDt() {
return std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>( std::chrono::high_resolution_clock::now() - before ).count();
}

void Allegro::newFrame( bool skipDT ) {
if( !skipDT ) {
COMPUTE_DT( dt )
dt = computeDt();
ImGui_ImplAGL_NewFrame( dt );
before = clock();
before = std::chrono::high_resolution_clock::now();
} else {
ImGui_ImplAGL_NewFrame( 1 / 30.0f );
}
Expand Down Expand Up @@ -568,4 +572,4 @@ void Allegro::showError( std::string msg ) {

void Allegro::forceQuit() {
m_forceQuit = true;
}
}
39 changes: 26 additions & 13 deletions othello/ui/main/mainallegroui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void Othello::UI::Main::GL_windowToObjectf( GLfloat x, GLfloat y, GLdouble* retx
*rety = 0;
}

AnimatedRectangle::AnimatedRectangle( float& dt, Othello::UI::Audio::FMOD& fmod ) : dt( dt ), m_fmod( fmod ) {}
AnimatedRectangle::AnimatedRectangle( double& dt, Othello::UI::Audio::FMOD& fmod ) : dt( dt ), m_fmod( fmod ) {}

bool AnimatedRectangle::loadTexture( string path ) {
return AGL_loadTexture( texture, path );
Expand Down Expand Up @@ -191,7 +191,7 @@ void AnimatedRectangle::playHover() {
m_fmod.playSound( "hoverCard" );
}

MenuRectangle::MenuRectangle( float& dt, Othello::UI::Audio::FMOD& fmod ) : AnimatedRectangle( dt, fmod ) {
MenuRectangle::MenuRectangle( double& dt, Othello::UI::Audio::FMOD& fmod ) : AnimatedRectangle( dt, fmod ) {
entryRY = 0.0f, entryTime = defaultEntryTime, normalX = defaultNormalX, hoverX = 1.1f, hoverZ = 0.1f, hoverTime = defaultHoverTime, clickZ = 0.04f;
}

Expand Down Expand Up @@ -225,7 +225,7 @@ void MenuRectangle::playHover() {
m_fmod.playSound( "hoverButton" );
}

ButtonRectangle::ButtonRectangle( float& dt, Othello::UI::Audio::FMOD& fmod ) : AnimatedRectangle( dt, fmod ) {
ButtonRectangle::ButtonRectangle( double& dt, Othello::UI::Audio::FMOD& fmod ) : AnimatedRectangle( dt, fmod ) {
entryRY = 0.0f, entryTime = defaultEntryTime, normalX = 0.0f, hoverX = 0.0f, hoverZ = 0.05f, hoverTime = defaultHoverTime, clickZ = -hoverZ;
}

Expand Down Expand Up @@ -740,10 +740,14 @@ void Allegro::handleMouse() {
}
}

double inline Allegro::computeDt() {
return std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>( std::chrono::high_resolution_clock::now() - before ).count();
}

void Allegro::newFrame() {
COMPUTE_DT( dt );
dt = computeDt();
ImGui_ImplAGL_NewFrame( dt );
before = clock();
before = std::chrono::high_resolution_clock::now();
t += dt;
GL_windowToObjectf( mouse_x, mouse_y, &inx, &iny, &inz );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
Expand Down Expand Up @@ -784,6 +788,7 @@ void Allegro::back() {
stage = Stage::Menu;
break;
case Stage::NewPlayer:
m_fmod.playSound( "back" );
stage = Stage::New;
break;
case Stage::NewAISelect:
Expand All @@ -795,6 +800,7 @@ void Allegro::back() {
stage = Stage::New;
break;
case Stage::NewAI:
m_fmod.playSound( "back" );
stage = Stage::NewAISelect;
break;
case Stage::ContinueParty:
Expand Down Expand Up @@ -834,6 +840,12 @@ void Allegro::forward( Stage newStage ) {
stage = newStage;
break;

case NewPlayer:
case NewAI:
m_fmod.playSound( "clickNextCard" );
stage = newStage;
break;

case Stage::Options:
prevSelWinMode = selWinMode;
prevSelWinResI = selWinResI;
Expand Down Expand Up @@ -920,7 +932,7 @@ void Allegro::renderNew() {
rectPVAI.animateEntry();

//if( t > 6.0f )
if( t > 3.0f )
if( t > 0.1f )
rectPVP.animateEntry();
}
}
Expand All @@ -941,10 +953,10 @@ void Allegro::renderNewAISelect() {

rectAI3.animateEntry();

if( t > 3.0f )
if( t > 0.1f )
rectAI2.animateEntry();

if( t > 6.0f )
if( t > 0.2f )
rectAI1.animateEntry();
}
}
Expand Down Expand Up @@ -1117,20 +1129,21 @@ void Allegro::renderLoading() {
}

void Allegro::renderSplashscreen() {
glClear( GL_COLOR_BUFFER_BIT );
BITMAP* bg = load_jpg( "images/splashscreen.jpg", NULL );
int w = bg->w, h = bg->h;
allegro_gl_set_allegro_mode();
blit( bg, screen, 0, 0, ( SCREEN_W - w ) / 2, ( SCREEN_H - h ) / 2, w, h );
allegro_gl_unset_allegro_mode();
allegro_gl_flip();
destroy_bitmap( bg );
before = time( NULL );
before = std::chrono::high_resolution_clock::now();
}

void Allegro::splashscreenEndLoading() {
clock_t diff = time( NULL ) - before;
double diff = computeDt();
if( diff < SPLASHSCREEN_DURATION_MINIMUM ) {
std::this_thread::sleep_for( std::chrono::duration<double>( (double) ( SPLASHSCREEN_DURATION_MINIMUM - diff ) / CLOCKS_PER_SEC ) );
std::this_thread::sleep_for( std::chrono::duration<double>( SPLASHSCREEN_DURATION_MINIMUM - diff ) );
}
}

Expand All @@ -1153,7 +1166,7 @@ void Allegro::menu() {
m_fmod.playMusic( "menu" );

isSave = Othello::Save::SaveManager::check();
before = clock() - 1;
before = std::chrono::high_resolution_clock::now();

while( !quit ) {
newFrame();
Expand Down Expand Up @@ -1224,7 +1237,7 @@ void Allegro::newAIGame() {
}

void Allegro::error( string message ) {
before = clock() - 1;
before = std::chrono::high_resolution_clock::now();
for( bool loop = true; loop; ) {
newFrame();

Expand Down

0 comments on commit 3bf2e36

Please sign in to comment.