Beispiel #1
0
	void GUIWindow::open()
	{
		enableBit(state, GUIRECT_OPEN);
		setRenderState(true);
		refresh();
		std::cout << "\nWindow opened: " << header->getString();
	}
Beispiel #2
0
	int getStringAsInt(const std::string& string)
	{
		int8_t stringState = 0;
		int intValue = 0;
		for (unsigned i = 0; i < string.size(); i++)
		{
			if (string[i] >= '0' && string[i] <= '9')
			{//Add numerical value
				{
					intValue *= 10;
					intValue += int(string[i] - 48);
				}
			}
			else if (string[i] == '-')
			{
				if (checkBit(stringState, 1))
				{//Second '-' character, return
					return 0.0f;
				}
				enableBit(stringState, 1);//Negative
			}
			else if (string[i] == '.' || string[i] == ',')
			{
				if (checkBit(stringState, 2))
				{//Second ',/.' character, return
					return 0.0f;
				}
				enableBit(stringState, 2);//Begin decimal part
			}
			else
			{//Character is unknown
				return 0.0f;
			}
		}

		//negate if needed
		if (checkBit(stringState, 1))
			intValue *= -1;

		return intValue;
	}
Beispiel #3
0
	void GUIWindow::update()
	{

		//Clicking within window -> gaining focus
		//if (checkState(GUIRECT_ENABLED_BIT) && !isFocused() && isOpen() && inputManager->isKeyPressed(MOUSE_BUTTON_LEFT) && (strech->updateMouseHover() || getMouseHover()))
			//gainFocus();

		//Run update in all members
		GUIRectangleContainer::update();
		header->update();
		exit->update();
		strech->update();
		if (strech->getMouseHover())
		{
			enableBit(state, GUIRECT_MOUSE_HOVER);
			enableBit(state, GUIRECT_MOUSE_HOVER_CONTAINER);
		}

		////DRAGGING
		//Handle previous frame dragging
		if (checkState(GUIRECT_DRAGGING_BIT) && inputManager->isKeyDown(MOUSE_BUTTON_LEFT))
			translate(inputManager->getMouseMovementX(), inputManager->getMouseMovementY());
		//Set dragging boolean
		if (checkState(GUIRECT_ENABLED_BIT) && !checkState(GUIRECT_DRAGGING_BIT) && header->getMouseHover() && inputManager->isKeyPressed(MOUSE_BUTTON_LEFT))
			enableState(GUIRECT_DRAGGING_BIT);//Begin dragging
		else if (checkState(GUIRECT_DRAGGING_BIT) && !inputManager->isKeyDown(MOUSE_BUTTON_LEFT))
		{//Stop dragging

#ifdef DOCK_BORDER
			//Check docking
			if (inputManager->getMouseX() < DOCK_BORDER)
			{//Dock left
				setSize(minSize.x, applicationData->getWindowHeight() - upBorder - downBorder - header->getHeight() - 2 * strechWidth);
				setPositionLocal(strechWidth + leftBorder, strechWidth + downBorder);
			}
			else if (inputManager->getMouseX() > applicationData->getWindowWidth() - DOCK_BORDER)
			{//Dock right
				setSize(minSize.x, applicationData->getWindowHeight() - upBorder - downBorder - header->getWidth() - 2 * strechWidth);
				setPositionLocal(applicationData->getWindowWidth() - size.x - strechWidth - rightBorder, strechWidth + downBorder);
			}

			if (inputManager->getMouseY() < DOCK_BORDER)
			{//Dock down
				setSize(applicationData->getWindowWidth() - 2 * strechWidth - leftBorder - rightBorder, minSize.y);
				setPositionLocal(strechWidth + leftBorder, strechWidth + downBorder);
			}
			else if (inputManager->getMouseY() > applicationData->getWindowHeight() - DOCK_BORDER)
			{//Dock up
				setSize(applicationData->getWindowWidth() - 2 * strechWidth - leftBorder - rightBorder, minSize.y);
				setPositionLocal(strechWidth + leftBorder, applicationData->getWindowHeight() - header->getHeight() - strechWidth - size.y - upBorder);
			}
#endif

			limitWithinMainWindow();
			disableState(GUIRECT_DRAGGING_BIT);
		}

		////STRECHING
		//Handle previous frame strech
		if (checkState(GUIRECT_STRECHING_BIT) && inputManager->isKeyDown(MOUSE_BUTTON_LEFT))
		{
			//Take record of current dimensions
			float w = size.x;
			float h = size.y;
			bool breakFromStrech = false;

			//Check horizontal strech movement
			if (checkBit(strechState, STRECH_STATE_HORIZONTAL))
			{
				//Check if mouse has crossed over to the opposite side of the window -> break from strech
				if (checkBit(strechState, STRECH_STATE_W))
				{//Strech began from left side of the window
					if (inputManager->getMouseX() > getXGlobal() + size.x / 2.0f)
						breakFromStrech = true;
				}
				else
				{//Strech began from the right side of the window
					if (inputManager->getMouseX() < getXGlobal() + size.x / 2.0f)
						breakFromStrech = true;
				}

				//Strech window based on mouse movement
				if (!breakFromStrech)
				{
					if (inputManager->getMouseX() > position.x + size.x / 2.0f)
					{
						w += inputManager->getMouseMovementX();
					}
					else
					{
						w -= inputManager->getMouseMovementX();
						if (abs(size.x - minSize.x) > 0.01f)
							translate(inputManager->getMouseMovementX(), 0);
					}
				}
			}

			//Check vertical strech movement
			if (!breakFromStrech && checkBit(strechState, STRECH_STATE_VERTICAL))
			{
				//Check if mouse has crossed over to the opposite side of the window -> break from strech
				if (checkBit(strechState, STRECH_STATE_N))
				{//Strech began from upper side of the window
					if (inputManager->getMouseY() < getYGlobal() + size.y / 2.0f + header->getHeight() / 2.0f)
						breakFromStrech = true;
				}
				else
				{//Strech began from the lower side of the window
					if (inputManager->getMouseY() > getYGlobal() + size.y / 2.0f + header->getHeight() / 2.0f)
						breakFromStrech = true;
				}

				//Strech window based on mouse movement
				if (!breakFromStrech)
				{
					if (inputManager->getMouseY() > position.y + size.y / 2.0f)
					{
						h += inputManager->getMouseMovementY();
					}
					else
					{
						h -= inputManager->getMouseMovement().y;
						if (abs(size.y - minSize.y) > 0.01f)
						translate(0, inputManager->getMouseMovementY());
					}
				}
			}

			//Set size to the calculated dimensions
			setSize(w, h);

			//position within main application window
			limitWithinMainWindow();
		}

		//Handle strech state bit
		if (checkState(GUIRECT_ENABLED_BIT) &&
			!checkState(GUIRECT_STRECHING_BIT) &&
			inputManager->isKeyPressed(MOUSE_BUTTON_LEFT) &&
			mouseOverStrechArea())
		{//Begin streching

			////Define state variables
			enableState(GUIRECT_STRECHING_BIT);
			strechState = 0;
			//Horizontal
			if (inputManager->getMouseX() < getXGlobal() + size.x * STRECH_CORNER_PERCENTAGE)
			{
				//Enable horizontal streching
				enableBit(strechState, STRECH_STATE_HORIZONTAL);
				//Take record that the streching began from the western side of the window
				enableBit(strechState, STRECH_STATE_W);
			}
			else if (inputManager->getMouseX() > getXGlobal() + size.x * (1.0 - STRECH_CORNER_PERCENTAGE))
			{
				//Enable horizontal streching
				enableBit(strechState, STRECH_STATE_HORIZONTAL);
				//Take record that the streching began from the eastern side of the window
				enableBit(strechState, STRECH_STATE_E);
			}
			//Vertical
			if (inputManager->getMouseY() < getYGlobal() + size.y * STRECH_CORNER_PERCENTAGE)
			{
				//Enable vertical streching
				enableBit(strechState, STRECH_STATE_VERTICAL);
				//Take record that the streching began from the southern side of the window
				enableBit(strechState, STRECH_STATE_S);
			}
			else if (inputManager->getMouseY() > getYGlobal() + size.y * (1.0 - STRECH_CORNER_PERCENTAGE))
			{
				//Enable vertical streching
				enableBit(strechState, STRECH_STATE_VERTICAL);
				//Take record that the streching began from the northern side of the window
				enableBit(strechState, STRECH_STATE_N);
			}
		}
		else if (checkState(GUIRECT_STRECHING_BIT) &&
			(!checkState(GUIRECT_ENABLED_BIT) || !inputManager->isKeyDown(MOUSE_BUTTON_LEFT)))
		{//Conditions not met to continue streching, set to false
			disableState(GUIRECT_STRECHING_BIT);
			strechState = 0;
		}

		////Header double clicking
		//Timer decrease
		if (doubleClickTimer > 0)
			doubleClickTimer -= time::getDeltaTimeAsMilliseconds();
		//Check header double click
		if (checkState(GUIRECT_ENABLED_BIT) && inputManager->isKeyPressed(MOUSE_BUTTON_LEFT) && header->getMouseHover())
		{//Header has been clicked
			if (doubleClickTimer > 0)
			{//Header double click detected
				disableState(GUIRECT_DRAGGING_BIT);

				if (size == minSize)
				{//Set to full window

					//Set size according to application data's window dimensions
					setSize(applicationData->getWindowWidth() - rightBorder - leftBorder, applicationData->getWindowHeight() - upBorder - downBorder - header->getHeight());
					setPositionLocal(0, 0);

					//Position window so that it won't go out of the application window
					limitWithinMainWindow();
				}
				else
				{//Set to min size

					//Take record of the window's old center position so that resized window can be nicely centered at the same position
					glm::vec2 oldCenterPos(getXGlobal() + size.x / 2.0f, getYGlobal() + size.y / 2.0f);

					//Rezise to min size
					setSize(minSize);

					//Reposition relative to old center position (recorded earlier)
					setPositionLocal(oldCenterPos.x - size.x / 2.0f, oldCenterPos.y - size.y / 2.0f);

					//Position window so that it won't go out of the application window
					limitWithinMainWindow();
				}
			}
			else
				doubleClickTimer = DOUBLE_CLICK_TIME;
		}

		//Check exit button
		if (checkState(GUIRECT_ENABLED_BIT) && inputManager->isKeyPressed(MOUSE_BUTTON_LEFT) && exit->getMouseHover())
			close();

	}
Beispiel #4
0
void Game::run()
{

	//Connect TCP
	bool success = false;
	do
	{
		try
		{
			serverEndpointTCP = *resolverTCP.resolve(queryTCP);
			socketTCP.open(boost::asio::ip::tcp::v4());
			socketTCP.connect(serverEndpointTCP);

			//Send enter packet to server
			boost::array<unsigned char, 1> enterPacket = { packet::enter };
			socketTCP.send(boost::asio::buffer(enterPacket));
			socketTCP.send(boost::asio::buffer(enterPacket));
			socketTCP.send(boost::asio::buffer(enterPacket));

			//Wait for receiving return data
			size_t bytes = socketTCP.receive(boost::asio::buffer(receiveBufferTCP));
			boost::system::error_code e;
			receiveHandlerTCP(e, bytes);
			success = true;
		}
		catch (std::exception& e)
		{
			std::cout << "\n" << e.what();
		}
	} while (!success);

	//Connect UDP
	success = false;
	do
	{
		try
		{
			serverEndpointUDP = *resolverUDP.resolve(queryUDP);
			socketUDP.open(boost::asio::ip::udp::v4());
			socketUDP.connect(serverEndpointUDP);
			boost::array<unsigned char, sizeof(CLIENT_ID_TYPE) + sizeof(packet::PacketType)> enterUDP;
			enterUDP[0] = packet::enterUdpEndpoint;
			memcpy(&enterUDP[sizeof(packet::PacketType)], &ID, sizeof(ID));
			socketUDP.send(boost::asio::buffer(enterUDP));
			socketUDP.receive(boost::asio::buffer(enterUDP));//Wait for server response
			success = true;
		}
		catch (std::exception& e)
		{
			std::cout << "\n" << e.what();
		}
	} while (!success);
	

	std::thread ioServiceThread(boost::bind(&boost::asio::io_service::run, &ioService));
	while (!checkBit(state, GAME_EXIT_BIT))
	{
		mainWindow->clearBuffer();
		spehs::beginFPS();

		//Update
		spehs::console::update();
		inputManager->update();
		update();

		//Render
		render();
		spehs::console::render();

		spehs::endFPS();
		spehs::drawFPS();
		mainWindow->swapBuffers();

		if (inputManager->isKeyDown(KEYBOARD_Q))
			enableBit(state, GAME_EXIT_BIT);
	}

	//Notify server
	exitGame();
	ioServiceThread.join();
}