Exemple #1
0
//-----------------
bool CBarrier::init( const TPoint2 & pos, const TPoint2 & size ) {
  m_texture.loadFromFile( "data/textures/blank.png" );
  m_texture.setPosition( pos );
  m_texture.setClip( SDL_Rect( 0, 0, size.x, size.y ) );

  colors[ 2 ] = SDL_Color( 0, 255, 0, 255 );
  colors[ 1 ] = SDL_Color( 255, 255, 0, 255 );
  colors[ 0 ] = SDL_Color( 255, 0, 0, 255 );

  reset( );

  return true;
}
Exemple #2
0
//-----------------
void CBarrier::update( float ) {
  if( m_cur_lives - 1 >= 0 ) {
    m_texture.setTintColor( colors[ m_cur_lives - 1 ] );
  } else {
    m_texture.setTintColor( SDL_Color( 0, 0, 0, 255 ) );
  }
}
Exemple #3
0
SDL_Color Color::getSDLColor(void)
{
    SDL_Color color = SDL_Color();
    color.r = red;
    color.g = green;
    color.b = blue;
    return color;
}
Exemple #4
0
SimpleButton::SimpleButton(SDL_Renderer* pRenderer, SimpleTexture* pBtnTexture, SimpleTexture* pBtnHLTexture, SimpleTexture* pFaceTexture, SDL_Color pColor, Uint32 pX, Uint32 pY, Uint32 pW, Uint32 pH)
{
    rect = SDL_Rect{ pX, pY, pW, pH };
    color = SDL_Color(pColor);

    btnTexture = pBtnTexture;

    btnTextureHighlighted = pBtnHLTexture;

    btnFaceTexture = pFaceTexture;
}
Exemple #5
0
glm::vec2 UIRenderer::getTextDimensions(const std::string &text)
{
	assert(font);

	if (text.empty())
		return glm::vec2(0.f);

	SDL_Surface *sText = TTF_RenderUTF8_Blended(font, text.c_str(), SDL_Color());

	assert(sText);

	glm::vec2 out((float)sText->w, (float)sText->h);

	SDL_FreeSurface(sText);

	return out;
}
Exemple #6
0
Font::Font() : _d( new Impl )
{
  _d->ttfFont = 0;
  _d->color = SDL_Color();
}
int main( int argc, char* args[] )
{
    SDL_Color BACKGROUND_COLOR  = SDL_Color();  BACKGROUND_COLOR.r  = 0;    BACKGROUND_COLOR.g  = 0;    BACKGROUND_COLOR.b  = 0;     BACKGROUND_COLOR.a = 255;
    SDL_Color HUNTER_COLOR      = SDL_Color();  HUNTER_COLOR.r      = 255;  HUNTER_COLOR.g      = 0;    HUNTER_COLOR.b      = 0;     HUNTER_COLOR.a     = 255;
    SDL_Color BOUND_COLOR       = SDL_Color();  BOUND_COLOR.r       = 0;    BOUND_COLOR.g       = 255;  BOUND_COLOR.b       = 0;    BOUND_COLOR.a       = 255;
    SDL_Color PREY_COLOR        = SDL_Color();  PREY_COLOR.r        = 0;    PREY_COLOR.g        = 0;    PREY_COLOR.b        = 255;  PREY_COLOR.a        = 255;
    
    
    // Init GUI
    SDL_Window *screen = NULL;
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
        std::cout << "Error on SDL_Init:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }
    
    screen = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
    if( screen == NULL ) {
        std::cout << "Error on SDL_CreateWindow:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }
    
    SDL_Renderer* bgRenderer = NULL;
    bgRenderer =  SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if( bgRenderer == NULL ) {
        std::cout << "Error on SDL_CreateRenderer:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }

    
    // Define QLAgent
    QLAgent<QLAction,QLState>::qlt table;
    if(std::ifstream(FILENAME)) { loadQLTable(table,FILENAME); }
    const std::vector<QLAction> actionVec { Entity::UP, Entity::DOWN, Entity::LEFT, Entity::RIGHT, Entity::IDLE };
    QLAgent<QLAction,QLState> preyAgent = QLAgent<QLAction,QLState>([&actionVec](QLState u){return actionVec;} , table);
    preyAgent.setQLParameters(CAP, LR, DF, GVR);

    // Inizialization
    SDL_Rect bounds = SDL_Rect();
    bounds.x = BOUND_MIN_X;
    bounds.y = BOUND_MIN_Y;
    bounds.w = BOUND_MAX_X - BOUND_MIN_X;
    bounds.h = BOUND_MAX_Y - BOUND_MIN_Y;
    
    Entity hunter   = Entity( HUNTER_WIDTH, HUNTER_HEIGHT,  HUNTER_SPEED,   HUNTER_START_X, HUNTER_START_Y, bounds, HUNTER_COLOR );
    Entity prey     = Entity( PREY_WIDTH,   PREY_HEIGHT,    PREY_SPEED,     PREY_START_X,   PREY_START_Y,   bounds, PREY_COLOR );
    
    preyAgent.setCurrentState(dataToQLState(hunter,prey));
    
    // Render
    clr(bgRenderer,BACKGROUND_COLOR);
    renderRect(bounds, BOUND_COLOR, bgRenderer, false);
    renderEntity(hunter, bgRenderer);
    renderEntity(prey, bgRenderer);
    SDL_RenderPresent(bgRenderer);



    // TIMERS
    SDL_TimerID renderTimerId = SDL_AddTimer(TIME_GAP_RENDER, render_callback, NULL);
    if( renderTimerId == 0 ) {
        std::cout << "Error on SDL_AddTimer:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }
    
    SDL_TimerID movementTimerId = SDL_AddTimer(TIME_GAP_MOVEMENT, movement_callback, NULL);
    if( movementTimerId == 0 ) {
        std::cout << "Error on SDL_AddTimer:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }


    // main cycle
    //// structure to read user events
    SDL_Event event;
    //// to check when the key is pressed or released
    bool isKeyPressed[4] = { false, false, false, false };
    
    bool quit = false;
    int matchCounter = 0;
    while( quit == false ) {
        
        bool matchIsOver = false;
        matchCounter++;
        // int moveCounter = 0;
        // int succesCounter = 0;
        while(!matchIsOver) {
        
            // event handling
            while( SDL_PollEvent( &event ) ) {
                switch( event.type ) {
                    case SDL_QUIT:
                        quit = true;
                    break;
                    case SDL_KEYDOWN:
                        // Keyboard input handling - keydown
                        checkKeyPressed( event.key.keysym.sym, isKeyPressed, true );
                    break;
                    case SDL_KEYUP:
                        // Keyboard input handling - keyup
                        checkKeyPressed( event.key.keysym.sym, isKeyPressed, false );
                    break;
                    case SDL_USEREVENT:

                        switch(event.user.code) {
                            case RENDER_CB:
                                // Render
                                clr(bgRenderer,BACKGROUND_COLOR);
                                renderRect(bounds, BOUND_COLOR, bgRenderer, false);
                                renderEntity(hunter, bgRenderer);
                                renderEntity(prey, bgRenderer);
                                SDL_RenderPresent(bgRenderer);
                            break;
                            case MOVEMENT_CB:
                                // Entities movement
                                hunter.move( actHunter(hunter,prey) );
                                //userMovement(prey, isKeyPressed);

                                prey.move( preyAgent.chooseAction() );

                                // check contact
                                if( checkContact( hunter, prey ) ) {
                                    hunter.reset();
                                    prey.reset();
                                    preyAgent.update(dataToQLState(hunter,prey), CATCH_REWARD);
                                    matchIsOver = true;
                                }
                                else {
                                    preyAgent.update(dataToQLState(hunter,prey), SURVIVE_REWARD);
                                }

                            break;
                        }


                    break;
                    default:
                    break;
                }
            }

            
            // 
// moveCounter++;
//
//             hunter.move( actHunter(hunter,prey) );
//             //userMovement(prey, isKeyPressed);
//
//             prey.move( preyAgent.chooseAction() );
//
//             // check contact
//             if( checkContact( hunter, prey ) ) {
//                 hunter.reset();
//                 prey.reset();
//                 preyAgent.update(dataToQLState(hunter,prey), CATCH_REWARD);
//                 matchIsOver = true;
//             }
//                else {
//                    preyAgent.update(dataToQLState(hunter,prey), SURVIVE_REWARD);
//                }
//
//             if(matchCounter%10000 == 0) { std::cout << matchCounter << std::endl; }
//             if(matchCounter%100000 == 0) { quit = true; }
//             if(moveCounter == 1000) { succesCounter++; matchIsOver = true; }
//             if(succesCounter == 100) { std::cout << "SUCCESS!!!" << std::endl; }
                
        }
        
    }
    
    std::cout << "states in table: " << table.size() << std::endl;
    saveQLTable(table,FILENAME);
    SDL_Quit();
    return 0;
}
int main( int argc, char* args[] )
{
    SDL_Color BACKGROUND_COLOR  = SDL_Color();  BACKGROUND_COLOR.r  = 0;    BACKGROUND_COLOR.g  = 0;    BACKGROUND_COLOR.b  = 0;     BACKGROUND_COLOR.a = 255;
    SDL_Color HUNTER_COLOR      = SDL_Color();  HUNTER_COLOR.r      = 255;  HUNTER_COLOR.g      = 0;    HUNTER_COLOR.b      = 0;     HUNTER_COLOR.a     = 255;
    SDL_Color BOUND_COLOR       = SDL_Color();  BOUND_COLOR.r       = 0;    BOUND_COLOR.g       = 255;  BOUND_COLOR.b       = 0;    BOUND_COLOR.a       = 255;
    SDL_Color PREY_COLOR        = SDL_Color();  PREY_COLOR.r        = 0;    PREY_COLOR.g        = 0;    PREY_COLOR.b        = 255;  PREY_COLOR.a        = 255;
    
    
    // Init GUI
    SDL_Window *screen = NULL;
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
        std::cout << "Error on SDL_Init:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }
    
    screen = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
    if( screen == NULL ) {
        std::cout << "Error on SDL_CreateWindow:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }
    
    SDL_Renderer* bgRenderer = NULL;
    bgRenderer =  SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if( bgRenderer == NULL ) {
        std::cout << "Error on SDL_CreateRenderer:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }

    
    // Define neural network
    LinearNeuralNetwork nn = LinearNeuralNetwork( 0, { 1 }, NULL, NULL );
    if(std::ifstream(FILENAME)) { nn = LinearNeuralNetwork( FILENAME, Neuron::SIGMOID, Neuron::SIGMOID_DER ); }
    else                        { nn = LinearNeuralNetwork( ALPHA, NEURAL_NETWORK_DESCRIPTOR, Neuron::SIGMOID, Neuron::SIGMOID_DER ); }
    

    // Inizialization
    SDL_Rect bounds = SDL_Rect();
    bounds.x = BOUND_MIN_X;
    bounds.y = BOUND_MIN_Y;
    bounds.w = BOUND_MAX_X - BOUND_MIN_X;
    bounds.h = BOUND_MAX_Y - BOUND_MIN_Y;
    
    Entity hunter   = Entity( HUNTER_WIDTH, HUNTER_HEIGHT,  HUNTER_SPEED,   HUNTER_START_X, HUNTER_START_Y, bounds, HUNTER_COLOR );
    Entity prey     = Entity( PREY_WIDTH,   PREY_HEIGHT,    PREY_SPEED,     PREY_START_X,   PREY_START_Y,   bounds, PREY_COLOR );
    
    // Render
    clr(bgRenderer,BACKGROUND_COLOR);
    renderRect(bounds, BOUND_COLOR, bgRenderer, false);
    renderEntity(hunter, bgRenderer);
    renderEntity(prey, bgRenderer);
    SDL_RenderPresent(bgRenderer);



    // TIMERS
    SDL_TimerID renderTimerId = SDL_AddTimer(TIME_GAP_RENDER, render_callback, NULL);
    if( renderTimerId == 0 ) {
        std::cout << "Error on SDL_AddTimer:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }
    
    SDL_TimerID movementTimerId = SDL_AddTimer(TIME_GAP_MOVEMENT, movement_callback, NULL);
    if( movementTimerId == 0 ) {
        std::cout << "Error on SDL_AddTimer:" << std::endl << SDL_GetError() << std::endl;
        return 1;
    }


    // main cycle
    //// structure to read user events
    SDL_Event event;
    //// to check when the key is pressed or released
    bool isKeyPressed[4] = { false, false, false, false };
    
    bool quit = false;
    while( quit == false ) {
        
        bool matchIsOver = false;
        while(!matchIsOver) {
        
            // event handling
            while( SDL_PollEvent( &event ) ) {
                switch( event.type ) {
                    case SDL_QUIT:
                        quit = true;
                    break;
                    case SDL_KEYDOWN:
                        // Keyboard input handling - keydown
                        checkKeyPressed( event.key.keysym.sym, isKeyPressed, true );
                    break;
                    case SDL_KEYUP:
                        // Keyboard input handling - keyup
                        checkKeyPressed( event.key.keysym.sym, isKeyPressed, false );
                    break;
                    case SDL_USEREVENT:
                    
                        switch(event.user.code) {
                            case RENDER_CB:
                                // Render
                                clr(bgRenderer,BACKGROUND_COLOR);
                                renderRect(bounds, BOUND_COLOR, bgRenderer, false);
                                renderEntity(hunter, bgRenderer);
                                renderEntity(prey, bgRenderer);
                                SDL_RenderPresent(bgRenderer);
                            break;
                            case MOVEMENT_CB:
                                int initialDistance = dist(hunter,prey);
                                // Entities movement
                                hunter.move( actHunter(hunter,prey) );
                                //userMovement(prey, isKeyPressed);
                            
                                // DEBUG
                                std::vector<float> outputNn = outPrey( hunter, prey, nn );
                                Entity::Action preyAction   = actPrey( outputNn );
                                prey.move( preyAction );
                            
                                // check contact
                                if( checkContact( hunter, prey ) ) {
                                    std::vector<float> err (5, 0);
                                    err[preyAction] = - REWARD_CAPTURE * outputNn[preyAction];
                                    nn.learn( err );
                                    matchIsOver = true;
                                }
                                else {
                                    int distance = dist(hunter, prey);
                                    // std::cout << "distances" << distance << " " << initialDistance << std::endl;
                                    std::vector<float> err (5, 0);
                                    if( distance > initialDistance ) {
                                        err[preyAction] = ( 1 - outputNn[preyAction] ) * REWARD_DISTANCE;
                                    }
                                    else if( distance < initialDistance ) {
                                        err[preyAction] = - outputNn[preyAction] * REWARD_DISTANCE;
                                    }
                                    nn.learn( err );
                                }
                            break;
                            // default:
    //                             std::cout << "!!! UserEvent code not defined!!!" << std::endl;
    //                             return 1;
    //break;
                        }
                
                    
                    break;
                    default:
                    break;
                }
                
                
            }
        }
        
        hunter.reset();
        prey.reset();
        
    }

    SDL_Quit();
    return 0;
}