Commit c2f63e7f by Anselme

added keyrepeat in the menu

parent fec907cb
......@@ -17,8 +17,9 @@
#include "spriteatlas.h"
#include "menu.h"
Client::Client(Graphics* graphics, Menu* menu, const std::string &address, unsigned short port):
Client::Client(sf::RenderWindow* window, Graphics* graphics, Menu* menu, const std::string &address, unsigned short port):
m_graphics(graphics),
m_window(window),
m_menu(menu),
m_address(address),
m_port(port),
......@@ -26,7 +27,7 @@ Client::Client(Graphics* graphics, Menu* menu, const std::string &address, unsig
m_connected(false),
m_authenticated(false)
{
m_graphics->initMenu(m_menu);
setMode(MENU);
}
bool Client::safeReceive(sf::Packet& packet)
......@@ -50,14 +51,7 @@ bool Client::safeReceive(sf::Packet& packet)
case sf::Socket::Disconnected:
std::cout << "Connexion to the server closed" << std::endl;
default:
m_server.disconnect();
m_connected = false;
m_authenticated = false;
m_menu->setConnecting(false);
m_menu->setStartPressed(false);
m_graphics->initMenu(m_menu);
m_graphics->setMessage("");
m_map = MapKoban();
setMode(MENU);
return false;
}
}while(retry);
......@@ -77,22 +71,13 @@ void Client::handlePackets()
{
case AUTH_ANSWER:
{
m_graphics->setMessage("");
AuthAnswer msg;
packet >> msg;
m_authenticated = msg.isOk;
if(m_authenticated)
{
m_graphics->decrementZoomLevel();
m_graphics->decrementZoomLevel();
}
setMode(PLAYING);
else
{
m_menu->updateTextSprite(true);
m_server.disconnect();
m_connected = false;
}
m_menu->setConnecting(false);
setMode(MENU);
break;
}
case MAP_INIT:
......@@ -134,6 +119,47 @@ void Client::handlePackets()
}
}
void Client::setMode(Mode mode)
{
switch(mode)
{
case MENU:
m_window->setKeyRepeatEnabled(true);
m_server.disconnect();
m_connected = false;
m_authenticated = false;
m_menu->setConnecting(false);
m_menu->setStartPressed(false);
m_graphics->initMenu(m_menu);
m_graphics->setMessage("");
m_map = MapKoban();
break;
case CONNECTING:
{
m_menu->startClickedReset();
m_name = m_menu->getText();
unsigned int color = m_menu->getColor();
m_menu->setConnecting(true);
std::string msg = "Connecting to ";
msg.append(m_address);
msg.append(" . . .");
m_graphics->setMessage(msg);
new std::thread(connectToServer, this, m_address, m_port, m_name, color);
SpriteAtlas::setIconColor(sf::Color(color));
m_window->setIcon(32, 32, SpriteAtlas::getIcon());
break;
}
case PLAYING:
m_window->setKeyRepeatEnabled(false);
m_graphics->setMessage("");
m_graphics->decrementZoomLevel();
m_graphics->decrementZoomLevel();
break;
default:
break;
}
}
void Client::connectToServer(Client* client, const std::string &address, unsigned short port, const sf::String &name, unsigned int color)
{
// add an artificial waiting time ?
......@@ -156,21 +182,21 @@ void Client::connectToServer(Client* client, const std::string &address, unsigne
}
}
void Client::update(sf::RenderWindow& window)
void Client::update()
{
if(window.isOpen())
if(m_window->isOpen())
{
handlePackets();
// Process events
sf::Event event;
while (window.pollEvent(event))
while (m_window->pollEvent(event))
{
if(m_authenticated)
{
// Close window: exit
if (event.type == sf::Event::Closed)
window.close();
m_window->close();
else if (event.type == sf::Event::Resized)
m_graphics->resize(event.size.width, event.size.height);
else if (event.type == sf::Event::KeyPressed)
......@@ -209,14 +235,7 @@ void Client::update(sf::RenderWindow& window)
hasMoved = false;
break;
case sf::Keyboard::Escape:
m_server.disconnect();
m_connected = false;
m_authenticated = false;
m_menu->setConnecting(false);
m_menu->setStartPressed(false);
m_graphics->initMenu(m_menu);
m_graphics->setMessage("");
m_map = MapKoban();
setMode(MENU);
break;
default:
hasMoved = false;
......@@ -246,7 +265,7 @@ void Client::update(sf::RenderWindow& window)
{
switch(event.type)
{
case sf::Event::Closed: window.close(); break;
case sf::Event::Closed: m_window->close(); break;
case sf::Event::Resized:
m_graphics->resize(event.size.width, event.size.height);
m_menu->resize(event.size.width, event.size.height);
......@@ -271,7 +290,7 @@ void Client::update(sf::RenderWindow& window)
case sf::Keyboard::Delete: m_menu->textDelete(); break;
case sf::Keyboard::Right: m_menu->textRight(); break;
case sf::Keyboard::Left: m_menu->textLeft(); break;
case sf::Keyboard::Escape: window.close(); break;
case sf::Keyboard::Escape: m_window->close(); break;
default: break;
}
}
......@@ -283,30 +302,18 @@ void Client::update(sf::RenderWindow& window)
}
if(!m_authenticated && m_menu->isStartClicked())
{
m_menu->startClickedReset();
m_name = m_menu->getText();
unsigned int color = m_menu->getColor();
m_menu->setConnecting(true);
std::string msg = "Connecting to ";
msg.append(m_address);
msg.append(" . . .");
m_graphics->setMessage(msg);
new std::thread(connectToServer, this, m_address, m_port, m_name, color);
SpriteAtlas::setIconColor(sf::Color(color));
window.setIcon(32, 32, SpriteAtlas::getIcon());
}
setMode(CONNECTING);
// Clear screen
window.clear(sf::Color::Black);
m_window->clear(sf::Color::Black);
if(m_authenticated)
m_graphics->renderMap(window, m_map);
m_graphics->renderMap(*m_window, m_map);
else
m_graphics->renderMenu(window, m_menu);
m_graphics->renderMenu(*m_window, m_menu);
// Update the window
window.display();
m_window->display();
}else{
m_running = false;
m_server.disconnect();
......
......@@ -17,6 +17,7 @@ class Client
{
sf::TcpSocket m_server;
Graphics* m_graphics;
sf::RenderWindow* m_window;
Menu* m_menu;
std::string m_address;
......@@ -32,9 +33,17 @@ class Client
bool safeReceive(sf::Packet& packet);
void handlePackets();
enum Mode {
MENU,
CONNECTING,
PLAYING
};
void setMode(Mode mode);
public:
Client(Graphics* graphics, Menu* menu, const std::string &address, unsigned short port);
void update(sf::RenderWindow &window);
Client(sf::RenderWindow* window, Graphics* graphics, Menu* menu, const std::string &address, unsigned short port);
void update();
bool isRunning() { return m_running; }
bool isConnected() { return m_connected; }
};
......
......@@ -131,8 +131,8 @@ void Graphics::renderMap(sf::RenderWindow &window, MapKoban& map)
m_cameraPos = sf::Vector2f(m_width/m_zoomLevel, m_height/m_zoomLevel)/2.f - (sf::Vector2f(16.f, 16.f) + m_main_player->getRenderPosition(currentTick));
sf::IntRect mapSize;
mapSize.width = (m_width/m_zoomLevel)/32 +2;
mapSize.height = (m_height/m_zoomLevel)/32 +2;
mapSize.width = (m_width/m_zoomLevel)/32 +3;
mapSize.height = (m_height/m_zoomLevel)/32 +3;
mapSize.left = - (int(m_cameraPos.x)/32 +1);
mapSize.top = - (int(m_cameraPos.y)/32 +1);
......
......@@ -48,12 +48,13 @@ int main(int argc, char** argv)
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "MMOkoban");
Graphics* graphics = new Graphics(WINDOW_WIDTH, WINDOW_HEIGHT);
Menu* menu = new Menu(WINDOW_WIDTH, WINDOW_HEIGHT);
window.setKeyRepeatEnabled(false);
SpriteAtlas::setIconColor(sf::Color(0xe8a71bFF));
window.setIcon(32, 32, SpriteAtlas::getIcon());
Client client(graphics, menu, address, port);
Client client(&window, graphics, menu, address, port);
while(client.isRunning())
client.update(window);
client.update();
delete menu;
delete graphics;
return EXIT_SUCCESS;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment