<repo_name>Mikea15/Minerals<file_sep>/Minerals/MineralEntity.h
//
//  MineralEntity.hpp
//  Minerals
//
//  Created by <NAME> on 27/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef MineralEntity_hpp
#define MineralEntity_hpp

#include <iostream>
#include <string>
#include <cmath>

#include "GameObject.h"
#include "Rect.h"
#include "Texture.h"

using namespace Skybelow;

class MineralEntity : public GameObject
{
public:
    MineralEntity( );
    ~MineralEntity( );
    
    void LoadTexture( SDL_Renderer *renderer, std::string path );
    
    void Start();
    void Update();
    void Cleanup();
    
private:
    
};



#endif /* MineralEntity_hpp */
<file_sep>/Engine/Timer.h
//
//  Timer.hpp
//  Minerals
//
//  Created by <NAME> on 27/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef Timer_hpp
#define Timer_hpp

#include <iostream>

#include <SDL2/SDL.h>

namespace Skybelow
{
    class Timer
    {
    public:
        Timer();
        
        void Start();
        void Stop();
        void Pause();
        void Resume();
        
        Uint32 GetTicks();
        
        bool IsStarted( ) const {
            return m_isStarted;
        }
        
        bool IsPaused( ) const {
            return m_isStarted && m_isPaused;
        }
        
    private:
        bool m_isPaused;
        bool m_isStarted;
        
        Uint32 m_startTicks;
        Uint32 m_pausedTicks;
    };
}

#endif /* Timer_hpp */
<file_sep>/Engine/Rect.h
//
//  Rect.hpp
//  Minerals
//
//  Created by <NAME> on 23/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef Rect_hpp
#define Rect_hpp

#include <iostream>

#include <SDL2/SDL.h>

#include "Vector2.h"

namespace Skybelow
{
    class Rect {
    public:
        Rect();
        Rect( Vector2 position, int width, int height );
        Rect( SDL_Rect rect );
        
        Vector2 GetCenter();
        
        bool IsColliding( SDL_Rect a, SDL_Rect b );
        bool Contains( float x, float y );
        
    private:
        Vector2 m_Position;
        int m_Height;
        int m_Width;
        
        SDL_Rect rect;
    };
}

#endif /* Rect_hpp */
<file_sep>/Minerals/GameState.h
//
//  GameState.h
//  Minerals
//
//  Created by <NAME> on 25/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef GameState_h
#define GameState_h

#include "GameEngine.h"


class GameState
{
public:

    virtual void Init(GameEngine *game) = 0;
    virtual void HandleEvent( GameEngine *game, SDL_Event event ) = 0;
    virtual void Update( GameEngine *game ) = 0;
    virtual void Render( GameEngine *game ) = 0;
    virtual void Cleanup() = 0;
    
    virtual void Pause() = 0;
    virtual void Resume() = 0;
    
    void ChangeState( GameEngine *game, GameState *state )
    {
        game->ChangeState(state);
    }
        
protected:
    GameState() {};
};



#endif /* GameState_h */
<file_sep>/Minerals/GameplayState.h
//
//  GameplayState.hpp
//  Minerals
//
//  Created by <NAME> on 26/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef GameplayState_hpp
#define GameplayState_hpp

#include <iostream>
#include <vector>
#include <sstream>

#include "GameState.h"
#include "IntroState.h"

#include "GameObject.h"
#include "MineralEntity.h"
#include "Timer.h"

#include "Grid.h"
#include "Texture.h"

#define SCORE_BONUS 2
#define TIME_TILL_NEW_BLOCK 1
#define TIME_TILL_NEW_GAME 2

class GameplayState : public GameState
{
    
public:

    void Init(GameEngine *game);
    void HandleEvent( GameEngine *game, SDL_Event event );
    void Update( GameEngine *game );
    void Render( GameEngine *game );
    void Cleanup();
    
    void ClickedCell( Vector2 position );
    
    void Pause();
    void Resume();
    
    static GameplayState* Instance() {
        return &m_GameplayState;
    }
    
protected:
    GameplayState();
    ~GameplayState();
    
private:
    static GameplayState m_GameplayState;
    
    Grid *m_grid;
    
    Texture m_board;
    Vector2 m_boardPosition;
    
    SDL_Texture *m_fontTexture;
    
    Texture m_scoreRect;
    
    Skybelow::Timer m_time;
    float m_timeToPushBlock;
    float m_currentTime;
    
    float m_timeToBeginNew;
    
    Skybelow::Timer m_fpsTimer;
    std::stringstream m_timeText;
    
    int m_score;
    std::stringstream m_scoreText;
    Texture m_scoreTexture;
    
    bool m_gameOver;
    Texture m_gameOverText;
    
    int m_frameCount;
    
    bool m_showDebugInfo;
    
    bool m_mouseClickDownPrevious;
};


#endif /* GameplayState_hpp */
<file_sep>/Engine/Thread.h
//
//  Thread.hpp
//  Minerals
//
//  Created by <NAME> on 28/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef Thread_hpp
#define Thread_hpp

#include <stdio.h>
#include <pthread.h>

class Thread
{
public:
    Thread();
    virtual ~Thread();
    
    int Start();
    int Join();
    int Detach();
    
    pthread_t Self();
    
    virtual void* Run() = 0;
    
private:
    pthread_t  m_tid;
    int        m_running;
    int        m_detached;
};

#endif /* Thread_hpp */
<file_sep>/Engine/Vector2.h
//
//  Vector2.hpp
//  Minerals
//
//  Created by <NAME> on 23/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef Vector2_hpp
#define Vector2_hpp

#include <cmath>
#include <stdio.h>

#include "Math.h"

namespace Skybelow
{
    struct Vector2
    {
        float x;
        float y;
    
        Vector2( ) : x(0), y(0)
        {
        }
        
        Vector2( float x, float y ): x(x), y(y)
        {
        }

        Vector2( const Vector2& v ) : x(v.x), y(v.y)
        {
        }
        
        float Dot( Vector2 other )
        {
            return x * other.x + y * other.y;
        }
        
        float LengthSqr( )
        {
            return x * x + y * y;
        }
        
        float Length( )
        {
            return sqrtf( Length() );
        }
        
        Vector2 Lerp( Vector2 a, Vector2 b, float t )
        {
            return Vector2( Math::Lerp(a.x, b.x, t), Math::Lerp(a.y, b.y, t) );
        }
        
        Vector2 EaseInOutQuad( Vector2 a, Vector2 b, float t, float duration )
        {
            return Vector2( Math::EaseInOutQuad(t, a.x, b.x, duration), Math::EaseInOutQuad(t, a.y, b.y, duration) );
        }
        
        Vector2 Normalized( )
        {
            return Vector2( x / Length(), y / Length() );
        }
        
        Vector2 operator+( const Vector2& a ) const {
            return Vector2( x + a.x, y + a.y );
        }
        
        Vector2 operator-( const Vector2& a ) const {
            return Vector2( x - a.x, y - a.y );
        }
        
        Vector2 operator+=( const Vector2& a ) {
            x += a.x;
            y += a.y;
            return *this;
        }
        
        Vector2 operator-=( const Vector2& a ) {
            x -= a.x;
            y -= a.y;
            return *this;
        }
        
        Vector2 operator*( const float scale ) {
            return Vector2( x * scale, y * scale );
        }
        
        Vector2 operator*=( const float scale ) {
            x *= scale;
            y *= scale;
            return *this;
        }
        
        bool operator==(const Vector2 other ) const {
            return ( x == other.x && y == other.y );
        }
        
        
    };
}

#endif /* Vector2_hpp */
<file_sep>/readme.txt
=================
# Minerals v001 #
=================

- Executable can be found in ./Build folder. 

Enjoy.

<NAME>
<EMAIL>
<file_sep>/Engine/WorkQueue.cpp
//
//  WorkQueue.cpp
//  Minerals
//
//  Created by <NAME> on 28/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#include "WorkQueue.h"

template <typename T>
WorkQueue<T>::WorkQueue()
{
    pthread_mutex_init(&m_mutex, NULL);
    pthread_cond_init(&m_condv, NULL);
}

template <typename T>
WorkQueue<T>::~WorkQueue()
{
    pthread_mutex_destroy(&m_mutex);
    pthread_cond_destroy(&m_condv);
}

template <typename T>
void WorkQueue<T>::Add(T item)
{
    pthread_mutex_lock(&m_mutex);
    
    m_queue.push_back( item );
    
    pthread_cond_signal(&m_condv);
    
    pthread_mutex_unlock(&m_mutex);
}

template <typename T>
T WorkQueue<T>::Remove()
{
    pthread_mutex_lock(&m_mutex);
    
    while( m_queue.size() == 0 ) {
        pthread_cond_wait(&m_condv, &m_mutex);
    }
    
    T item = m_queue.front();
    
    m_queue.pop_front();
    
    pthread_mutex_unlock(&m_mutex);
    return item;
}

template <typename T>
int WorkQueue<T>::Size()
{
    pthread_mutex_lock(&m_mutex);
    
    int size = m_queue.size();
    
    pthread_mutex_unlock(&m_mutex);
    
    return size;
}

<file_sep>/Engine/ConsumerThread.cpp
//
//  ConsumerThread.cpp
//  Minerals
//
//  Created by <NAME> on 28/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#include "ConsumerThread.h"
<file_sep>/Engine/Texture.h
//
//  Texture.hpp
//  Minerals
//
//  Created by <NAME> on 27/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef Texture_hpp
#define Texture_hpp

#include <iostream>
#include <string>

#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
#include <SDL2_ttf/SDL_ttf.h>

#include "Vector2.h"

namespace Skybelow
{
    class Texture
    {
    public:
        Texture();
        ~Texture();
        
        bool LoadFromFile( SDL_Renderer *renderer, std::string path );
        bool LoadFromRenderedText(TTF_Font *font, SDL_Renderer *renderer, std::string textureText, SDL_Color textColor );
        
        void Clear();
        
        void SetColor( Uint8 red, Uint8 green, Uint8 blue );
        void SetBlendMode( SDL_BlendMode blending );
        void SetAlpha( Uint8 alpha );
        
        void Render( SDL_Renderer *renderer, Vector2 position, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE );
        
        int GetWidth( ) { return m_width; }
        int GetHeight( ) { return m_height; }
        
        void SetWidth( float width ) {
            m_width = width;
        }
        
        void SetHeight( float height ) {
            m_height = height;
        }
        
        SDL_Texture* GetTexture( ) const {
            return m_texture;
        }
                
    private:
        SDL_Texture* m_texture;
        
        int m_width;
        int m_height;
    };
}

#endif /* Texture_hpp */
<file_sep>/Minerals/GameEngine.h
//
//  GameEngine.h
//  Minerals
//
//  Created by <NAME> on 23/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef _GameEngine_h_
#define _GameEngine_h_

#if __APPLE__
    #include <SDL2_image/SDL_image.h>
    #include <SDL2_ttf/SDL_ttf.h>
    #include <SDL2/SDL.h>

#elif _WIN32 || _WIN64
    #include <SDL/SDL_image.h>
    #include <SDL/SDL_ttf.h>
    #include <SDL/SDL.h>

#endif

#include <iostream>
#include <string>
#include <vector>

class GameState;

class GameEngine
{
public:
    GameEngine( );
    
    int Run();
    
    void Init( std::string windowName, int width, int height );
    void Cleanup();
    
    void ChangeState( GameState * state );
    void PushState( GameState *state );
    void PopState( );
    
    void HandleEvents( );
    void Update( );
    void Render( );
    
    bool IsRunning();
    void Load();
    void Save();
    void Quit();
    
    // exposing this for lack of time.
    // going rambo style.. :/
    int m_time;
    int m_timeNow;
    int m_timeBefore;
    SDL_Event m_event;
    
    SDL_Window* MainWindow( ) {
        return m_window;
    }
    
    SDL_Renderer* MainRenderer( ) {
        return m_renderer;
    }
    
    TTF_Font* MainFont() {
        return m_font;
    }
    
    
private:
    std::string m_windowName;
    
    int m_windowHeight;
    int m_windowWidth;
    int m_windowBits;
    
    bool m_isRunning;
    bool m_fullscreen;
    
    std::vector<GameState*> m_gameStates;
    
    SDL_Window *m_window;
    
    // SDL 2.0. Hardware Accelerated.
    SDL_Renderer *m_renderer;
    
    TTF_Font *m_font;
};


#endif /* GameEngine */
<file_sep>/Minerals/IntroState.h
//
//  IntroState.hpp
//  Minerals
//
//  Created by <NAME> on 26/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef IntroState_hpp
#define IntroState_hpp

#include <iostream>

#include "GameState.h"

#include "Texture.h"

using namespace Skybelow;

class IntroState : public GameState
{
    
public:

    void Init(GameEngine *game);
    void HandleEvent( GameEngine *game, SDL_Event event );
    void Update( GameEngine *game );
    void Render( GameEngine *game );
    void Cleanup();
    
    void Pause();
    void Resume();
    
    static IntroState* Instance() {
        return &m_IntroState;
    }
    
protected:
    IntroState();
    ~IntroState();
    
private:
    static IntroState m_IntroState;
    
    Texture m_text;
    SDL_Rect textDest;
    SDL_Texture *m_fontTexture;
    
    int m_time;
    int m_startTime;
    int m_timeToNextScreen;
};


#endif /* IntroState_hpp */
<file_sep>/Engine/WorkItem.h
//
//  WorkItem.hpp
//  Minerals
//
//  Created by <NAME> on 28/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef WorkItem_hpp
#define WorkItem_hpp

#include <iostream>
#include <string>

// #include "thread.h"
#include "WorkQueue.h"

class WorkItem
{
    string m_message;
    int    m_number;
    
public:
    WorkItem(const char* message, int number)
    : m_message(message), m_number(number) {}
    ~WorkItem() {}
    
    const char* getMessage() { return m_message.c_str(); }
    int getNumber() { return m_number; }
};

#endif /* WorkItem_hpp */
<file_sep>/Minerals/main.cpp
//
//  main.cpp
//  Minerals
//
//  Created by <NAME> on 23/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//
//
// For Future Reference.. I'll be needing this info.
// /*
// * This is a simple C program to demonstrate the usage of callbacks
// * The callback function is in the same file as the calling code.
// * The callback function can later be put into external library like
// * e.g. a shared object to increase flexibility.
// *
// */
//
//#include <stdio.h>
//#include <string.h>
//
//typedef struct _MyMsg {
//    int appId;
//    char msgbody[32];
//} MyMsg;
//
//void myfunc(MyMsg *msg)
//{
//    if (strlen(msg->msgbody) > 0 )
//        printf("App Id = %d \nMsg = %s \n",msg->appId, msg->msgbody);
//    else
//        printf("App Id = %d \nMsg = No Msg\n",msg->appId);
//}
//
///*
// * Prototype declaration
// */
//void (*callback)(MyMsg *);
//
//int main(void)
//{
//    MyMsg msg1;
//    msg1.appId = 100;
//    strcpy(msg1.msgbody, "This is a test\n");
//    
//    /*
//     * Assign the address of the function "myfunc" to the function
//     * pointer "callback" (may be also written as "callback = &myfunc;")
//     */
//    callback = myfunc;
//    
//    /*
//     * Call the function (may be also written as "(*callback)(&msg1);")
//     */
//    callback(&msg1);
//    
//    return 0;
//}
//
//
//
//
//
//

#include <iostream>
#include <string>

#if __APPLE__
#include <SDL2/SDL.h>
#endif

#include "GameEngine.h"
#include "IntroState.h"


std::string GetResourcePath( const std::string &subDir = "" )
{
#ifdef _WIN32 
    const char PATH_SEPARATOR = '\\';
#else
    const char PATH_SEPARATOR = '/';
#endif
    
    static std::string baseRes;
    if( baseRes.empty() )
    {
        char *basePath = SDL_GetBasePath();
        if( basePath != NULL )
        {
            baseRes = basePath;
            SDL_free(basePath);
        }
        else
        {
            std::cout << "Error getting resource path: " << SDL_GetError() << std::endl;
            return "";
        }
        
        size_t position = baseRes.rfind("bin");
        baseRes = baseRes.substr(0, position) + "res" + PATH_SEPARATOR;
    }
    
    return subDir.empty() ? baseRes : baseRes + subDir + PATH_SEPARATOR;
}


int main(int argc, char * arg[])
{
    GameEngine *game = new GameEngine();
    game->Init("Minerals", 800, 600);
    
    game->ChangeState(IntroState::Instance());
    
    while( game->IsRunning() )
    {
        SDL_PollEvent( &game->m_event );
        
        game->HandleEvents();
        
        game->m_timeNow = SDL_GetTicks();
        if( game->m_timeNow - game->m_timeBefore > game->m_time )
        {
            game->Update();
            game->m_timeBefore = game->m_timeNow;
        }
        else
        {
            SDL_Delay( game->m_time - ( game->m_timeNow - game->m_timeBefore ) );
        }
        
        game->Render();
    }
    
    game->Cleanup();
    
    return 0;
}<file_sep>/README.md
Minerals
========

## Project
- Language: C++
- 3rd Party Libraries: SDL2, SDL_image, SDL_mixer, SDL_ttf

<file_sep>/Engine/State.h
//
//  State.hpp
//  Minerals
//
//  Created by <NAME> on 26/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//

#ifndef State_hpp
#define State_hpp

#include <stdio.h>

namespace Skybelow
{
    template <class entity_type>
    class State
    {
    public:
        virtual ~State();
        
        virtual void Enter( entity_type* ) = 0;
        virtual void Execute( entity_type* ) = 0;
        virtual void Exit( entity_type* ) = 0;
    };
}

#endif /* State_hpp */
<file_sep>/Engine/WorkQueue.h
//
//  WorkQueue.hpp
//  Minerals
//
//  Created by <NAME> on 28/09/15.
//  Copyright © 2015 SkyBelow. All rights reserved.
//
//  http://vichargrave.com/multithreaded-work-queue-in-c/
//

#ifndef WorkQueue_hpp
#define WorkQueue_hpp

#include <pthread.h>
#include <list>


using namespace std;

template <typename T>
class WorkQueue
{
    
public:
    WorkQueue();
    ~WorkQueue();
    
    void Add(T item);
    T Remove( );
    
    int Size();
    
    list<T> m_queue;
    
    pthread_mutex_t m_mutex;
    pthread_cond_t m_condv;
    
private:
};

#endif /* WorkQueue_hpp */
