The entire source:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <sstream>
struct player
{
sf::Image image_player;
sf::Font font_visitor;
sf::Sprite sprite;
sf::String score_string;
int score_int;
std::stringstream ss;
player();
void update_score(int);
};
player::player()
{
// Init player sprite and score string.
image_player.Create(15, 100, sf::Color(255,255,255));
font_visitor.LoadFromFile("visitor1.ttf", 72);
sprite.SetImage(image_player);
score_string.SetText("0");
score_string.SetFont(font_visitor);
score_string.SetSize(72);
score_int = 0;
}
void player::update_score(int x)
{
score_int += x;
ss << score_int;
score_string.SetText(ss.str());
ss.str("");
}
bool fps_enabled = true, in_menu = true;
int main()
{
// Create the main window
sf::RenderWindow App(sf::VideoMode(1280, 800), "Monotone", sf::Style::Fullscreen);
App.UseVerticalSync(true);
// Load font
sf::Font font_visitor;
if (!font_visitor.LoadFromFile("visitor1.ttf", 16))
{
return EXIT_FAILURE;
}
// Create the players, set positions
player p1, p2;
p1.sprite.SetPosition(0, floor(App.GetHeight()/2));
p2.sprite.SetPosition(floor(App.GetWidth()-15), floor(App.GetHeight())/2);
p1.score_string.SetPosition(floor((App.GetWidth()/2)-100), 10);
p2.score_string.SetPosition(floor((App.GetWidth()/2)+100), 10);
// Create fps counter
std::string framerate_string;
sf::String fps(framerate_string, font_visitor, 16);
fps.SetPosition(floor(App.GetWidth())-150,floor(App.GetHeight())-25);
// Initiate clock
sf::Clock Clock;
Clock.Reset();
// Init music
sf::Music title_music;
if (!title_music.OpenFromFile("audio/Feryl - Beakortolaris - Cerror.ogg"))
{
return EXIT_FAILURE;
}
// Init sounds
sf::SoundBuffer hit_sb, step_sb;
if (!hit_sb.LoadFromFile("audio/hit.wav") && !step_sb.LoadFromFile("audio/step.wav"))
{
return EXIT_FAILURE;
}
sf::Sound step(step_sb);
// Create menu
sf::Sprite logo_sprite;
sf::Image logo_image;
if (!logo_image.LoadFromFile("monotone.png"))
{
return EXIT_FAILURE;
}
logo_image.SetSmooth(false);
logo_sprite.SetImage(logo_image);
logo_sprite.SetPosition(App.GetWidth()/2-(logo_sprite.GetSize().x/2), (App.GetHeight()/10)*4);
// Start the game loop
while (App.IsOpened())
{
// Have fps counter update only every second, rather than several times a second
float Time = Clock.GetElapsedTime();
if (Time > 1 && fps_enabled)
{
// Framerate value
float framerate_float = floor(1.f / App.GetFrameTime());
std::stringstream ss;
ss << framerate_float;
framerate_string = ss.str();
fps.SetText(framerate_string + " FPS");
Clock.Reset();
}
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
{
App.Close();
}
// Escape key pressed : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
{
App.Close();
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F))
{
fps_enabled = !fps_enabled;
}
}
// Create an input event handler for keyboard presses
const sf::Input& Input = App.GetInput();
bool a_down = Input.IsKeyDown(sf::Key::A);
bool z_down = Input.IsKeyDown(sf::Key::Z);
bool slash_down = Input.IsKeyDown(sf::Key::Slash);
bool quote_down = Input.IsKeyDown(sf::Key::Quote);
bool space_down = Input.IsKeyDown(sf::Key::Space);
int mouse_x = Input.GetMouseX();
int mouse_y = Input.GetMouseY();
//Draw menu if that's where we are.
if (in_menu)
{
if (!title_music.GetLoop())
{
title_music.SetLoop(true);
title_music.Play();
}
App.Clear(sf::Color(255, 255, 255));
App.Draw(logo_sprite);
App.Display();
if ((Input.IsKeyDown(sf::Key::Space) || (Input.IsMouseButtonDown(sf::Mouse::Left))) && in_menu)
{
step.Play();
in_menu = false;
}
}
else
{
// Player 1 controls
if (space_down) //Meant to be used for testing.
{
std::cout << "Duration: " << step_sb.GetDuration() << "Status: " << step.GetStatus();
p1.update_score(1);
std::cout << "Space Pressed.\n";
step.Play();
}
if (a_down)
{
if (p1.sprite.GetPosition().y != 0)
{
p1.sprite.Move(0, -20);
}
}
if (z_down)
{
if (p1.sprite.GetPosition().y != App.GetHeight()-p1.sprite.GetSize().y)
{
p1.sprite.Move(0, 20);
}
}
// Player 2 controls
if (quote_down)
{
if (p2.sprite.GetPosition().y != 0)
{
p2.sprite.Move(0, -20);
}
}
if (slash_down)
{
if (p2.sprite.GetPosition().y != App.GetHeight()-p2.sprite.GetSize().y)
{
p2.sprite.Move(0, 20);
}
}
// Clear screen
App.Clear();
// Draw entities
App.Draw(p1.score_string);
App.Draw(p2.score_string);
App.Draw(p1.sprite);
App.Draw(p2.sprite);
if (fps_enabled)
{
App.Draw(fps);
}
// Update the window
App.Display();
}
}
return EXIT_SUCCESS;
}