예제 #1
0
//Checks for a collision between the character and the passed window:
Collision Character::checkCollision(sf::Window& window) const
{	
	//Gets the sides of the character:
	float topA, bottomA, leftA, rightA;
	topA = _sprite.getGlobalBounds().top;
	bottomA = _sprite.getGlobalBounds().height + topA;
	leftA = _sprite.getGlobalBounds().left;
	rightA = _sprite.getGlobalBounds().width + leftA;

	//Top:
	if(topA < 0)
		return COLLISION_TOP;

	//Bottom:
	if(bottomA > window.getSize().y)
		return COLLISION_BOTTOM;

	//Left:
	if(leftA < 0) 
		return COLLISION_LEFT;

	//Right:
	if(rightA > window.getSize().x)
		return COLLISION_RIGHT;

	//Otherwise there is no collision:
	return COLLISION_NONE;
}
예제 #2
0
void video::saveImage(sf::Window &app, int framerate)
{
  if (!app.isOpen())
    return ;
  sf::Vector2u size = app.getSize();
  if (videoTexture.getSize().y != size.y)
    videoTexture.create(size.x, size.y);

  if (videoTimer.getElapsedTime().asMilliseconds() > 1000/framerate)
    {
      videoTimer.restart();

      std::ostringstream videoFramePath;

      videoFramePath << "/tmp/gl-engine-"
		     << std::setw(5)
		     << std::setfill('0')
		     << currentVideoFrame
		     << ".jpg";
      videoTexture.update(app);
      sf::Image videoImage = videoTexture.copyToImage();
      videoImage.saveToFile(videoFramePath.str());
      currentVideoFrame++;
    }
}
예제 #3
0
파일: camera.cpp 프로젝트: rokn/GEAR
	void Camera::FreeFlyControl(const sf::Window& window, sf::Time elapsedTime)
	{
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
		{
			Move(GEAR::LEFT, elapsedTime.asSeconds());
		}
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
		{
			Move(GEAR::RIGHT, elapsedTime.asSeconds());
		}
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
		{
			Move(GEAR::BACKWARD, elapsedTime.asSeconds());
		}
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
		{
			Move(GEAR::FORWARD, elapsedTime.asSeconds());
		}
		
		sf::Vector2u screenSize = window.getSize();
		sf::Vector2i windowCenterPos(screenSize.x/2,screenSize.y/2);
		
		sf::Vector2i mouseOffset = sf::Mouse::getPosition(window) - windowCenterPos;
		
		Turn(mouseOffset.x * SENSITIVITY, -mouseOffset.y * SENSITIVITY);
	}
예제 #4
0
파일: Engine.cpp 프로젝트: sindney/fury3d
	bool Engine::Initialize(sf::Window &window, int numThreads, LogLevel level, const char* logfile,
		bool console, const LogFormatter &formatter, bool append)
	{
		Log<0>::Initialize(std::move(level), std::move(logfile), std::move(console), formatter, std::move(append));

		ThreadUtil::Initialize(std::move(numThreads));
		ThreadUtil::Instance()->SetMainThread();

		FURYD << ThreadUtil::Instance()->GetWorkerCount() << " thread launched!";

		MeshUtil::m_UnitQuad = MeshUtil::CreateQuad("quad_mesh", Vector4(-1.0f, -1.0f, 0.0f), Vector4(1.0f, 1.0f, 0.0f));
		MeshUtil::m_UnitCube = MeshUtil::CreateCube("cube_mesh", Vector4(-1.0f), Vector4(1.0f));
		MeshUtil::m_UnitIcoSphere = MeshUtil::CreateIcoSphere("ico_sphere_mesh", 1.0f, 2);
		MeshUtil::m_UnitSphere = MeshUtil::CreateSphere("sphere_mesh", 1.0f, 20, 20);
		MeshUtil::m_UnitCylinder = MeshUtil::CreateCylinder("cylinder_mesh", 1.0f, 1.0f, 1.0f, 4, 10);
		MeshUtil::m_UnitCone = MeshUtil::CreateCylinder("cone_mesh", 0.0f, 1.0f, 1.0f, 4, 10);

		InputUtil::Initialize(window.getSize().x, window.getSize().y);

#ifdef _FURY_FBXPARSER_IMP_
		FbxParser::Initialize();
#endif

		int flag = gl::LoadGLFunctions();

		RenderUtil::Initialize();

		BufferManager::Initialize();

#ifdef _FURY_GUI_IMP_
		Gui::Initialize(&window);
#endif

		if (flag == 1)
			return true;

		if (flag < 1)
		{
			FURYE << "Failed to load gl functions.";
		}
		else
		{
			FURYE << "Failed to load " << flag - 1 << " gl functions.";
		}

		return false;
	}
예제 #5
0
파일: main.cpp 프로젝트: mimaun/Rose
void saveBuffer(sf::Window const & window)
{
    unsigned char *dest;
    unsigned int w = window.getSize().x;
    unsigned int h = window.getSize().y;
    glPixelStorei(GL_PACK_ALIGNMENT, 1);
    dest = (unsigned char *) malloc( sizeof(unsigned char)*w*h*3);
    glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, dest);
    
    simplePNG_write("_program1.png", w, h, dest);
    free(dest);
}
예제 #6
0
Controller::Controller(sf::Window &theWindow, sfg::Desktop &theDesktop, CallbackSystem &callbackSystem):
    desktop(theDesktop),
    boardWindow(sfg::Window::Create(sfg::Window::BACKGROUND)),
    settingsButton(sfg::Button::Create("Settings")),
    canvas(theWindow),
    m_whiteClock(Clock::Create()),
    m_blackClock(Clock::Create()),
    settingsWindow(desktop),
    premove({{0,0},{0,0}}), premoveOn(false),
    netWindow(m_currentEvent),
    player1(sfg::Label::Create()),
    player2(sfg::Label::Create())
{
    boardWindow->SetRequisition(static_cast<sf::Vector2f>(theWindow.getSize()));

    canvas.requestMove.connect(boost::bind(&Controller::requestMove, this,_1));
    settingsWindow.settingsDone.connect([this](const PieceToTexPos& pieceToTexPos){
        settingsWindow.enable(false);
        canvas.setPieceColors(pieceToTexPos);
    });
    messages.connect(Messages::ID::TextReady, [this](const Messages::Message& message){
        auto received = boost::polymorphic_downcast<const Messages::TextReady*>(&message);
        netWindow.addLine(received->text);
    });

    ButtonBox buttons;
    buttons.flip->GetSignal(sfg::Button::OnLeftClick).Connect(std::bind(&Canvas::flipBoard, &canvas));
    buttons.settings->GetSignal(sfg::Button::OnLeftClick).Connect([]{
        //settingsWindow.enable(true);
    });
    buttons.resign->GetSignal(sfg::Button::OnLeftClick).Connect([this]{
        messages.triggerEvent(Messages::TextToClient("resign"));
    });
    buttons.draw->GetSignal(sfg::Button::OnLeftClick).Connect([this]{
        messages.triggerEvent(Messages::TextToClient("draw"));
    });

    auto promotionLayout = sfg::Box::Create(sfg::Box::Orientation::VERTICAL);
    auto promotionGroup = sfg::RadioButtonGroup::Create();

    auto createPromotionButton = [this, promotionGroup, promotionLayout](const std::string& name, char letter){
        auto promotionButton = sfg::RadioButton::Create(name, promotionGroup);
        promotionLayout->Pack(promotionButton);
        promotionButton->GetSignal(sfg::ToggleButton::OnToggle).Connect([this, promotionButton, letter]{
            if (promotionButton->IsActive()){
                std::string promote("promote ");
                promote += letter;
                messages.triggerEvent(Messages::TextToClient(promote));
            }
        });
        return promotionButton;
    };

    auto promotionQueen = createPromotionButton("Queen", 'q');
    createPromotionButton("Bishop", 'b');
    createPromotionButton("Knight", 'n');
    createPromotionButton("Rook", 'r');

    auto promotionFrame = sfg::Frame::Create("Promotion");
    promotionFrame->Add(promotionLayout);

    auto sideLayout = sfg::Box::Create(sfg::Box::Orientation::VERTICAL);

    sideLayout->Pack(player1);
    sideLayout->Pack(m_whiteClock);
    sideLayout->Pack(player2);
    sideLayout->Pack(m_blackClock);
    sideLayout->Pack(status.getView());

    sfg::Table::Ptr mainLayout(sfg::Table::Create());
    mainLayout->SetRowSpacings(2.f);
    mainLayout->SetColumnSpacings(2.f);
    mainLayout->Attach(canvas.getBoardWidget(),{0, 0, 3, 2},sfg::Table::EXPAND, sfg::Table::EXPAND, sf::Vector2f( 10.f, 0.f ));
    mainLayout->Attach(promotionFrame, {0, 2, 1, 1});
    mainLayout->Attach(sideLayout, {3,0, 3, 1});
    mainLayout->Attach(moveList.getView(),{3, 1, 3, 3});
    mainLayout->Attach(buttons.layout,{0,4,6,1});

    desktop.Add(settingsWindow.getWidget());

    sfg::Notebook::Ptr notebook(sfg::Notebook::Create());
    notebook->AppendPage(mainLayout,sfg::Label::Create("Board"));
    notebook->AppendPage(netWindow.getWidget(),sfg::Label::Create("Server"));
    notebook->GetSignal(sfg::Notebook::OnTabChange).Connect([this]{
        netWindow.grabEntryFocus();
    });

    callbackSystem.connect(Action::Tab, [notebook](thor::ActionContext<Action> context){
        if (context.event->key.control){
            if (context.event->key.shift)
                notebook->PreviousPage();
            else
                notebook->NextPage();
        }
    });

    boardWindow->Add(notebook);

    desktop.Add(boardWindow);

    buttons.connect->GetSignal(sfg::Button::OnLeftClick).Connect([this, notebook]{
        client.connect();
        notebook->SetCurrentPage(1);
    });

    messages.connect(Messages::ID::GameState, [this](const Messages::Message& message){
        auto received = boost::polymorphic_downcast<const Messages::GameState*>(&message);
        m_whiteClock->setTimeLeft(received->white_time);
        m_blackClock->setTimeLeft(received->black_time);
        if (received->turnColor == Color::White){
            m_blackClock->stop();
            m_whiteClock->start();
        }else{
            assert(received->turnColor==Color::Black);
            m_whiteClock->stop();
            m_blackClock->start();
        }


        if (premoveOn)
        {
            premoveOn = false;
            canvas.clearArrows();
            requestMove(premove);
        }
    });

    messages.connect(Messages::ID::GameStart, [this, promotionQueen, notebook](const Messages::Message& message){
        auto received = boost::polymorphic_downcast<const Messages::GameStart*>(&message);
        player1->SetText(received->p1);
        player2->SetText(received->p2);
        premoveOn = false;
        promotionQueen->SetActive(true);
        updateClocks();
        notebook->SetCurrentPage(0);
    });

    messages.connect(Messages::ID::GameEnd, [this](const Messages::Message&){
        //auto received = boost::polymorphic_downcast<const EndGameMessage*>(&message);
        premoveOn = false;
        canvas.clearArrows();
        m_whiteClock->stop();
        m_blackClock->stop();
    });

    callbackSystem.connect(Action::Scroll, [this](thor::ActionContext<Action> context){
        int delta = context.event->mouseWheelScroll.delta;
        netWindow.scroll(delta);
    });
}
    void draw() {
        //Activate the window's context
        window.setActive();

        //Various settings
        glClearColor(0.5, 0.5, 0.5, 0.0f);
        glShadeModel(GL_SMOOTH);
        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);
        glClearDepth(1.0f);
        glDepthFunc(GL_LEQUAL);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        glEnable(GL_TEXTURE_2D);
        
        //Lighting
        GLfloat light_color[] = {0.9, 0.9, 0.9, 1.f};
        glMaterialfv(GL_FRONT, GL_DIFFUSE, light_color);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        
        //Setup projection matrix
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        GLfloat ratio = float(window.getSize().x) / window.getSize().y;
        glFrustum(-ratio, ratio, -1.0, 1.0, 1.0, 500.0);
        
        //Store start time
        std::chrono::time_point<std::chrono::system_clock> startTime=std::chrono::system_clock::now();
        
        //The rendering loop
        glMatrixMode(GL_MODELVIEW);
        while (window.isOpen()) {
            std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - startTime;
            double time=elapsed_seconds.count();
            
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glLoadIdentity();

            glTranslatef(0.0f, -20.0f, -45.0f);

            glPushMatrix();
            glRotatef(-time*50.0, 0.0, 1.0, 0.0);
            glTranslatef(20.0, 0.0, 0.0);            
            unanimatedAstroBoy.draw();
            glPopMatrix();
            
            glPushMatrix();
            glRotatef(-time*50.0+90.0, 0.0, 1.0, 0.0);
            glTranslatef(20.0, 0.0, 0.0);            
            astroBoy.drawFrame(time);
            glPopMatrix();
            
            glPushMatrix();
            glRotatef(-time*50.0+180.0, 0.0, 1.0, 0.0);
            glTranslatef(20.0, 0.0, 0.0);            
            astroBoyMovingGlasses.drawFrame(time);
            glPopMatrix();
            
            glRotatef(-time*50.0+270.0, 0.0, 1.0, 0.0);
            glTranslatef(20.0, 0.0, 0.0);            
            astroBoyHeadBanging.drawFrame(time);
            
            //Swap buffer (show result)
            window.display();
        }
    }
예제 #8
0
Desktop::Desktop( const sf::Window& window ) :
	m_view( sf::FloatRect( 0.f, 0.f, static_cast<float>( window.getSize().x ), static_cast<float>( window.getSize().y ) ) ),
	m_engine( 0 ),
	m_last_mouse_pos( 0, 0 )
{
}