Example #1
0
void Container::onDraw(sf::RenderWindow& render)
{
    sf::View original = render.GetView();
    sf::View view = render.GetView();
    view.Move(-rect.Left,-rect.Top);
    render.SetView(view);
    for(std::list<Widget*>::iterator i = childs.begin(); i != childs.end() ; ++i)
    {
        (*i)->draw(render);
        render.SetView(layout->adjust(render.GetView(),**i));
    }
    render.SetView(original);
}
Example #2
0
void ParticleSystem::PushOpenGLStates(sf::RenderWindow& target) const
{
	// Switch to manual OpenGL handling, save SFML's state
	target.SaveGLStates();
	target.SetActive();

	// Switch blend mode, depending on glow effect	
	if (mGlow)
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	else
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);

	// Store attributes and assign them
	glPushAttrib(GL_DEPTH_BITS | GL_TEXTURE_BIT);
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, mTexture->id);

	// Initialize projection matrix (2D view)
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	detail::SetupOpenGL2DView(target.GetView());

	// Initialize modelview matrix (will be edited in Particle::Render())
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
}
void AddHelpText(sf::RenderWindow &App, sf::Font &f, GroupManager &gm)
{
    sf::View v = App.GetView();
    sf::Vector2f center = v.GetCenter(), half = v.GetHalfSize();

    string s;
    s = "Operations available:\n";
    s += "Move:              Left, Right, Up, Down\n";
    s += "Speed:             s (decrease), d (increase)\n";
    s += "Step size:         w (decrease), e (increase)\n";
    s += "Zoom:              z (in), x (out)\n";
    s += "Steps mode:        a (switch between modes)\n";
    s += "Quit:              Esc\n";
    sf::String text(s, f, 12);
    text.SetColor(sf::Color(128, 200, 40));
    text.Move(center.x - half.x + half.x/20, center.y - half.y + half.y/20);
    App.Draw(text);


    ostringstream step_per_second_str;
    step_per_second_str << "~" << step_per_second << "\n";
    ostringstream step_time_str;
    step_time_str << "~" << step_time << "\n";
    ostringstream framerate_str;
    framerate_str << 1.0/App.GetFrameTime() << "\n";
    ostringstream step_size_str;
    step_size_str << gm.GetStepSize() << "\n";

    s = "Steps per second: ";
    s += step_per_second_str.str() ;
    s += "Step time: ";
    s += step_time_str.str();
    s += "Framerate: ";
    s += framerate_str.str();
    s += "Step size: ";
    s += step_size_str.str();
    s += "Steps mode: ";
    switch (step_mode)
    {
        case SM_HARD_STEPS_CNT: s += "SM_HARD_STEPS_CNT\n"; break;
        case SM_FREE_STEPS_CNT: s += "SM_FREE_STEPS_CNT\n"; break;
        default: break;
    }

    sf::String inf(s.c_str(), f, 20);
    inf.SetColor(sf::Color(230, 200, 180));
    inf.Move(center.x - half.x + half.x/20, center.y + half.y - half.y/2);
    App.Draw(inf);
}
Example #4
0
void MainMenuHandler::run(sf::RenderWindow& window) {
	ImageCache mainMenuImages;
	const sf::Image& logoImage = mainMenuImages.get(Path::findDataPath("graphics/logo.png"));

	// Entering main menu, no game should be running.
	GameHandler::instance.reset();

	// Make view that is as close to 640x480 as possible and centered.
	view = window.GetDefaultView();
	view.Zoom(std::min(view.GetRect().GetWidth() / 640, view.GetRect().GetHeight() / 480));
	view.SetCenter(view.GetHalfSize());
	window.SetView(view);

	window.SetFramerateLimit(30);

	// Position at the top of the window.
	sf::Sprite logoSprite(logoImage);
	logoSprite.SetCenter(logoImage.GetWidth() / 2, 0);
	logoSprite.SetPosition(window.GetView().GetRect().GetWidth() / 2, 1);

	// Build the main menu GUI.
	GUI::Container gui;
	gui.insert(boost::shared_ptr<GUI::Object>(new GUI::Button("New game", 200, 100, 240, 50, boost::bind(&MainMenuHandler::startGame, this))));
	gui.insert(boost::shared_ptr<GUI::Object>(new GUI::Button("Exit", 250, 170, 140, 50, boost::bind(&sf::RenderWindow::Close, boost::ref(window)))));

	menuClosed = false;
	while (window.IsOpened() && !menuClosed) {
		sf::Event e;
		if (window.GetEvent(e)) {
			if (gui.handleEvent(e, window)) {
				continue;
			}
			if (e.Type == sf::Event::Closed || (e.Type == sf::Event::KeyPressed && e.Key.Code == sf::Key::Escape)) {
				window.Close();
				continue;
			}
		} else {
			window.Clear(sf::Color(0xcc, 0x66, 0x33));
			window.Draw(logoSprite);
			gui.draw(window);
			window.Display();
		}
	}
}
Example #5
0
int Screen_Select::Run (sf::RenderWindow &App, Model* _model, Controleur* _controleur)
{
    sf::Event Event;
    bool Running = true;
    sf::Image Image;
    sf::Sprite Sprite;
    int alpha = 0;
    sf::Font Font;
	sf::String Menu0;
    sf::String Menu1;
    sf::String Menu2;
    sf::String Menu3;
    sf::String Menu4;
    int menu = 2;
	std::string ip = "";
	
	sf::SoundBuffer Buffer;
	if (!Buffer.LoadFromFile("../../Images/theme.wav"))
	{
		std::cout << "Musique pas trouvée" << std::endl;
	}
	
	sf::Sound Sound;
	Sound.SetBuffer(Buffer); // Buffer est un sfSoundBuffer
	
    if (!Image.LoadFromFile("../../Images/bf2.jpg"))
    {
        std::cerr << "Error loading bf2.jpg" << std::endl;
        return (-1);
    }
	
	Sprite.SetScale((App.GetView().GetRect().GetWidth() / 500), (App.GetView().GetRect().GetHeight() / 375));
    Sprite.SetImage(Image);
	Sprite.SetPosition(-1,-1);
	
	if (!Font.LoadFromFile("../../Images/GUNPLA3D.ttf"))
    {
        std::cerr << "Error loading font" << std::endl;
    }
	
    Menu0.SetFont(Font);
    Menu0.SetSize(25);
    Menu0.SetText("Pour jouer en Client, tapez l'ip en remplacant les . de l'adresse par des ;");
    Menu0.SetX(30);
    Menu0.SetY(App.GetView().GetRect().GetHeight() / 2 - 120);
    Menu1.SetFont(Font);
    Menu1.SetSize(25);
    Menu1.SetText("Client");
    Menu1.SetX(App.GetView().GetRect().GetWidth() / 2 - 30);
    Menu1.SetY(App.GetView().GetRect().GetHeight() / 2 - 42);
    Menu2.SetFont(Font);
    Menu2.SetSize(25);
    Menu2.SetText("Serveur");
    Menu2.SetX(App.GetView().GetRect().GetWidth() / 2 - 30);
    Menu2.SetY(App.GetView().GetRect().GetHeight() / 2);
    Menu3.SetFont(Font);
    Menu3.SetSize(25);
    Menu3.SetText("Retour");
    Menu3.SetX(App.GetView().GetRect().GetWidth() / 2 - 30);
    Menu3.SetY(App.GetView().GetRect().GetHeight() / 2 + 100);
	Menu4.SetFont(Font);
    Menu4.SetSize(25);
    Menu4.SetText(ip);
    Menu4.SetX(App.GetView().GetRect().GetWidth() / 2 - 30);
    Menu4.SetY(App.GetView().GetRect().GetHeight() / 2 + 50);
	
	App.Clear();
	
    if (playing)
    {
        alpha = alpha_max;
    }
	
	Sound.Play();
	Sound.SetLoop(true);
	
    while (Running)
    {
		
        //Verifying events
        while (App.GetEvent(Event))
        {
            // Window closed
            if (Event.Type == sf::Event::Closed)
            {
                return (-1);
            }
            //Key pressed
            if (Event.Type == sf::Event::KeyPressed)
            {
                switch (Event.Key.Code)
                {
                    case sf::Key::Up:
						if(menu == 1)
							menu++;
						if(menu == 0)
							menu++;
                        break;
                    case sf::Key::Down:
						if(menu == 1)
							menu--;
						if(menu == 2)
							menu--;
                        break;
					case sf::Key::Num0 :
						ip = ip + "O";
						break;
					case sf::Key::Num1 :
						ip = ip + "1";
						break;
					case sf::Key::Num2 :
						ip = ip + "2";
						break;
					case sf::Key::Num3 :
						ip = ip + "3";
						break;
					case sf::Key::Num4 :
						ip = ip + "4";
						break;
					case sf::Key::Num5 :
						ip = ip + "5";
						break;
					case sf::Key::Num6 :
						ip = ip + "6";
						break;
					case sf::Key::Num7 :
						ip = ip + "7";
						break;
					case sf::Key::Num8 :
						ip = ip + "8";
						break;
					case sf::Key::Num9 :
						ip = ip + "9";
						break;
					case sf::Key::SemiColon :
						ip = ip + ".";
						break;
					case sf::Key::Back :
						if(ip.size() > 0)
							ip.erase(ip.end()-1);
						break;
                    case sf::Key::Return:
						Sound.Stop();
                        if (menu == 2)
                        {
							Screen_Multi2* s4 = new Screen_Multi2(ip);
							return (s4->Run(App,_model,_controleur));
                        }
						if (menu == 1)
						{
							//Serveur
							return 3;
						}
                        else
                        {
                            return 0;
                        }
                        break;
                    default :
                        break;
                }
            }
        }
        //When getting at alpha_max, we stop modifying the sprite
        if (alpha<alpha_max)
        {
            alpha++;
        }
        Sprite.SetColor(sf::Color(255, 255, 255, alpha/alpha_div));
        if (menu == 2)
        {
            Menu1.SetColor(sf::Color(255, 0, 0, 255));
            Menu2.SetColor(sf::Color(0, 0, 0, 255));
            Menu3.SetColor(sf::Color(0, 0, 0, 255));
        }
		else if (menu == 1)
        {
            Menu1.SetColor(sf::Color(0, 0, 0, 255));
            Menu2.SetColor(sf::Color(255, 0, 0, 255));
            Menu3.SetColor(sf::Color(0, 0, 0, 255));
        }
        else
        {
            Menu1.SetColor(sf::Color(0, 0, 0, 255));
            Menu2.SetColor(sf::Color(0, 0, 0, 255));
            Menu3.SetColor(sf::Color(255, 0, 0, 255));
        }
		
        //Drawing
        App.Draw(Sprite);
		Menu4.SetText(ip);
		App.Draw(Menu0);
		App.Draw(Menu3);
		App.Draw(Menu4); 
		App.Draw(Menu1);
		App.Draw(Menu2);
		
        App.Display();
    }
	
    //Never reaching this point normally, but just in case, exit the application
    return (-1);
}
void ProcessAppEvents(sf::RenderWindow &App, sf::View &v, GroupManager &gm)
{
    sf::Event Event;
    while (App.GetEvent(Event))
    {
        switch (Event.Type)
        {
            case sf::Event::Closed: App.Close(); break;
            case sf::Event::KeyPressed:
            {
                switch (Event.Key.Code)
                {
                    case sf::Key::Escape: App.Close(); break;
                    case sf::Key::Right:
                    case sf::Key::Left:
                    case sf::Key::Up:
                    case sf::Key::Down:
                    {
                        const int step = 5;
                        v = App.GetView();
                        int hor = 0, ver = 0;
                        if (Event.Key.Code == sf::Key::Left)
                            hor+=step;
                        if (Event.Key.Code == sf::Key::Right)
                            hor-=step;
                        if (Event.Key.Code == sf::Key::Up)
                            ver+=step;
                        if (Event.Key.Code == sf::Key::Down)
                            ver-=step;
                        v.Move(hor, ver);
                        App.SetView(v);
                        break;
                    }
                    case sf::Key::S:
                    case sf::Key::D:
                    {
                        //if (Event.Key.Code == sf::Key::S)
                        //    wanna_step_per_second-= wanna_step_per_second/10 - 1;
                        //if (Event.Key.Code == sf::Key::D)
                        //    wanna_step_per_second+= wanna_step_per_second/10 + 1;
                        //if (wanna_step_per_second <= 0)
                        //    step_per_second = 1;
                        //if (wanna_step_per_second >= 250)
                        //    step_per_second = 250;
                        //step_time = 1.0/wanna_step_per_second;
                        break;
                    }
                    case sf::Key::W:
                    case sf::Key::E:
                    {
                        if (Event.Key.Code == sf::Key::E)
                            gm.SetStepSize(gm.GetStepSize() + 0.005);
                        if (Event.Key.Code == sf::Key::W)
                            gm.SetStepSize(gm.GetStepSize() - 0.005);
                        break;
                    }
                    case sf::Key::Z:
                    case sf::Key::X:
                    {
                        if (Event.Key.Code == sf::Key::Z)
                            v.Zoom(1.01);
                        if (Event.Key.Code == sf::Key::X)
                            v.Zoom(1.0/1.01);
                        break;
                    }
                    case sf::Key::A:
                    {
                        step_mode = (STEP_MODE) ((int)SM_HARD_STEPS_CNT + (int)SM_FREE_STEPS_CNT - step_mode);
                        break;
                    }
                    default: break;
                }
                break;
            }
            case sf::Event::Resized:
            {
                int h = Event.Size.Height;
                int w = Event.Size.Width;
                v.SetHalfSize(w/2, h/2);
                break;
            }
            default: break;
        }
    }
}