#include "GameWindow.h"
#include "GLES2/gl2.h"

#include "TileInterfaces.h"
#include "GEAudioBuffer.h"
#include "GEAudioOut.h"
#include "GEInterfaces.h"


#define AUDIO_SAMPLE_COUNT 10



class QtTileGameEngine: public GE::GameWindow, public ITileRenderer
{
    Q_OBJECT

public:
    explicit QtTileGameEngine(QWidget *parent = 0);
    virtual ~QtTileGameEngine();

    void update(const float fFrameDelta);
    void onRender();
    void onPause();
    void onResume();

    void setSize(int w, int h);


    // ITileRenderer
    void renderTile( int fixedx, int fixedy,
                     int fixedWidth, int fixedHeight,
                     int angle,
                     int mode,
                     unsigned int tileIndex, unsigned int arg );

    void renderBackground( unsigned int index );
    void renderForeground( unsigned int index ) ;
    void effectNotify( eTILEGAME_EFFECT effect, unsigned int arg1, unsigned int arg2 );
    int run( int fixedFrameTime16Bit );


protected:
    GLuint loadGLTexture( const char *fn );
    GameTexture *bind(TextureID id);
    void load( TextureID id );

    float m_mousePressPos[2];
    float m_mousePressTime;
    bool m_mouseOn;

    void toggleMute();

    void coordsToScreen( QMouseEvent *event, float &x, float &y );

    void mousePressEvent ( QMouseEvent * event );
    void mouseReleaseEvent ( QMouseEvent * event );
    void mouseMoveEvent ( QMouseEvent * event );

    virtual int onCreate();
    virtual void onDestroy();

    void keyPressEvent ( QKeyEvent * event );

    ITileGame *theGame;

    int previousMode;
    GLuint pixelShader;
    GLuint vertexShader;
    GLuint program;
    GLuint vbo;

    GLuint angleAlphaLocation;
    GLuint sizeTScaleLocation;
    GLuint tposPositionLocation;



    // audio samples.
    GE::CAudioBuffer *m_audioBuffers[ AUDIO_SAMPLE_COUNT ];


};
