Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - TheRabbit

Pages: [1]
1
General / How can I lock one thread from another?
« on: November 12, 2013, 04:44:06 pm »
I'm trying to combat a race condition that occurs when a user connects to my server. Because the connection function is spawned in a thread, the main thread continues to see an "incoming connection" until one of the threads latches, which often results in 4 or 5 extra threads being created. I thought three fix for this would be fairly simple, just lock a mutex right before the thread launches, and then unlock the mutex add soon as a connection latches. But, mutexs throw an exception when you try and unlock in a different thread than you locked in....

So instead I did something like this: (please ignore syntax, I'm typing this up on my phone from memory)
 bool connLock = false;
...
if (selector.isReady(listener) && !connLock)
{
    std::thread(login,this);
    connLock = true;
}

...
login()
{

    if (listener.accept(client->socket) != sf::Socket::Done)
    {
      connLock = false;
      Return;
    }
    connLock = false;
....
 
I probably messed up some of those lines of code, sorry.
Anyway, this works pretty good, it lets my sever thread keep chugging away on other things while the login thread does it thing, but there has to be a better way to do this, right?

Any suggestions?

2
General discussions / Converted server to running multithreaded today
« on: November 11, 2013, 07:35:18 am »
Sorry, I just wanted to brag really quick because there's nobody that I can show this to that would care, but I spent all day today converting my server software to run multithreaded and not break or explode.



There was a lot of frustration at the end, every time my thread returned it would cause a debug error. I had forgotten to .detach the threads and so they were disappearing and throwing exceptions. But now it all works! Time to make the rest of the server functions threaded!

3
Network / Container(list) of TcpSockets not behaving (NonCopyable)
« on: November 06, 2013, 05:17:04 am »
My server uses a list of TcpSockets called host:
std::list<sf::TcpSocket> host; //sockets we will use to communicate

When I go to add a new socket, I'm unable to:
host.push_back(sf::TcpSocket());
This code gives me an error regarding the 'NonCopyable' declaration.

Code: [Select]
c:\sfml-2.1-windows-vc11-32bits\sfml-2.1\include\sfml\network\socket.hpp(178): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          c:\sfml-2.1-windows-vc11-32bits\sfml-2.1\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          c:\sfml-2.1-windows-vc11-32bits\sfml-2.1\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::Socket::Socket(const sf::Socket &)'

Is there a way around this? Before, I was using a fixed array of TcpSockets:
sf::TcpSocket host[10];
and had no problems, but it wasn't very robust. I'm trying to make the server a bit more robust to allow it to create new connections and delete old ones without going though all of the hoops I went through using a fixed array.

What can I do to get around this?

I've tried making a new class that (for right now) only holds a sf::TcpSocket item in it. But it seems to be an inherent problem with the way that 'push' works on containers, in that it creates a copy of the item before putting it into the list. Since TcpSockets are special and can't be copied (I assume that would cause actual problems and I shouldn't just overload it) I can't add a new one into a list.

4
My game has a multiplayer chat box in it, and I'm having a tough time implementing a scrolling and word wrapping chatbox without using a hammer. It works, but I feel like I'm using way too many variables and some really messy arrays to get it done.

Here's the relevant code bits (the really relevant parts are down in Game::pushMessage()):
class Game
{
public:
        Game();
        void run();

private:
        //...
        void processEvents();
        void render();
        void processInput(); //handles what happens when the user has types something and presses enter
        void pushMessage(std::string str_in, sf::Color color=sf::Color::Black); //puts a new message onto the stack
        //...

private:
        //...
        sf::Font chatFont;
        int chatLineFocus; //integer showing which chat line the client is currently looking at (allows scrolling text)
        sf::Text chatText[20]; //holds the last 20 rows of text that shows up in the chat box
        //...
};
//...
Game::Game()
{
        //...
        chatFont.loadFromFile("C:/Windows/Fonts/arial.ttf");
        for (int i=0;i<20;i++) //load the chat window with fonts
        {
                chatText[i].setFont(chatFont);
                chatText[i].setCharacterSize(12);
                chatText[i].setColor(sf::Color::Black);
        }
        chatLineFocus = 19; //The client is focused on the very bottom line of text
        //...
}
//...
void Game::processEvents()
{
        //...
                case sf::Event::MouseWheelMoved:
                        if (event.mouseWheel.delta > 0) //mouse wheel up
                        {
                                if (chatLineFocus > 9)
                                {
                                        chatLineFocus--;
                                }
                        }
                        else //mouse wheel down
                        {
                                if (chatLineFocus < 19)
                                        chatLineFocus++;
                        }
                        break;
        //...
        dataPacketFrom.clear();
        if (socketStatus == sf::Socket::Done) //only check if we're connected
        {
                if (socket.receive(dataPacketFrom) != sf::Socket::NotReady)
                {
                        //if there is data available this will return something other than NotReady
                        std::string str_in;
                        dataPacketFrom >> str_in;
                        pushMessage(str_in);
                }
        }
        //...
}
//...
void Game::render()
{
        mWindow.clear();
        //...

        //draw the chatbox
        sf::RectangleShape chatBox(sf::Vector2f(580.f,175.f)); //create a white rectangle
        sf::RectangleShape chatInput(sf::Vector2f(580.f,40.f)); //create another white rectangle

        //position and fill the boxes
        chatBox.setFillColor(sf::Color(255,255,255,125));
        chatBox.setPosition(700.f,545.f);
        chatInput.setFillColor(sf::Color(255,255,255,125));
        chatInput.setPosition(700.f,680.f);

        mWindow.draw(chatBox);
        mWindow.draw(chatInput);
        for (int i = 0; i < 9; i++)
        {
                //draw each of the 9 "visible" texts
                chatText[chatLineFocus - (8 - i)].setPosition(705.f, 550.f + (13.f * float(i)));
                mWindow.draw(chatText[chatLineFocus - (8 - i)]);
                temp2 = chatText[chatLineFocus - (8 - i)].getString().toAnsiString();
                temp = chatText[chatLineFocus - (8 - i)].getPosition();
        }
        mWindow.draw(currentInput);
        //...
        mWindow.display();
}
//here's where it gets ugly...
void Game::pushMessage(std::string str_in, sf::Color color)
{
        unsigned int i;
        sf::String lastLine;
        sf::Glyph glyph; //used for calculating line length
        int lineSize = 0; //used to determine when a line would run off the page
        int lastSpace = -1; //used to keep track of the last space in a line

        for (i=1;i<20;i++)
                chatText[i-1] = chatText[i]; //shift all the messages down to accept a new message
        chatText[19].setString(sf::String(str_in)); //put the new message in
        chatText[19].setColor(color);
       
        //break the most recent chat line up with line breaks
        lastLine = chatText[19].getString();
        for (i = 0; i < lastLine.getSize(); i++)
        {
                glyph = chatFont.getGlyph(lastLine[i],chatText[0].getCharacterSize(),false); //get the current character
                if ((lineSize + glyph.advance) > 570) //check for runoff
                {
                        for (i=1;i<20;i++)
                                chatText[i-1] = chatText[i]; //shift all the messages down again
                        if (lastSpace != -1)
                        {
                                chatText[18].setString(lastLine.toAnsiString().substr(0,lastSpace));//put the first part of the string up a line
                                chatText[19].setString(lastLine.toAnsiString().substr(lastSpace+1));//put the remainder back into slot 19
                        }
                        else
                        {
                                chatText[18].setString(lastLine.toAnsiString().substr(0,i));//put the first part of the string up a line
                                chatText[19].setString(lastLine.toAnsiString().substr(i));//put the remainder back into slot 19
                        }
                        lastLine = chatText[19].getString(); //reset our tracking string and start over to see if this new line is long
                        lastSpace = -1;
                        i=0; //reset our counter
                        lineSize = 0; //reset our line counter
                }
                else
                        lineSize += glyph.advance; //increment linesize by the texture size of the current character
                if (lastLine[i] == '\n')
                        lineSize = 0;
                if (lastLine[i] == ' ')
                        lastSpace = i;
        }
}

 

The other problem I have is that the user's text input just keeps going right off the edge of the screen, I'd like to simply shift the text over so that the text is still visible, but I can't figure out how to do that using an sf::Text.

I've searched around and looked at the way some other people implement things, and it seems like other people's code does way more things than I need it to (like SFGUI and TGUI) or they're dead projects that aren't fully implemented (SFChat and TextArea).

Here's a screenshot of what it looks like when it's running:


Any thoughts on how I could make this be a bit more elegant?

5
General / Can't get SFML 2.1 to work in Visual Studio 2012
« on: October 21, 2013, 12:03:35 am »
Hi,

Im brand new to SFML, and I can't figure it out. I tried following this tutorial to get everything set up:

http://www.cplusplus.com/forum/beginner/95295/#msg511542

I followed it to the letter (except for the Doxygen part), and I'm getting 31 unresolved externals linking errors.

First, I created a brand new blank c++ project. I put examples/window/window.cpp into the project and tried to build:

1>  Source.cpp
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: float __thiscall sf::Time::asSeconds(void)const " (__imp_?asSeconds@Time@sf@@QBEMXZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::Clock::Clock(void)" (__imp_??0Clock@sf@@QAE@XZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class sf::Time __thiscall sf::Clock::getElapsedTime(void)const " (__imp_?getElapsedTime@Clock@sf@@QBE?AVTime@2@XZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::String::String(char const *,class std::locale const &)" (__imp_??0String@sf@@QAE@PBDABVlocale@std@@@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::String::~String(void)" (__imp_??1String@sf@@QAE@XZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::VideoMode::VideoMode(unsigned int,unsigned int,unsigned int)" (__imp_??0VideoMode@sf@@QAE@III@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::Window::Window(class sf::VideoMode,class sf::String const &,unsigned int,struct sf::ContextSettings const &)" (__imp_??0Window@sf@@QAE@VVideoMode@1@ABVString@1@IABUContextSettings@1@@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall sf::Window::~Window(void)" (__imp_??1Window@sf@@UAE@XZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::Window::close(void)" (__imp_?close@Window@sf@@QAEXXZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall sf::Window::isOpen(void)const " (__imp_?isOpen@Window@sf@@QBE_NXZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall sf::Window::pollEvent(class sf::Event &)" (__imp_?pollEvent@Window@sf@@QAE_NAAVEvent@2@@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class sf::Vector2<unsigned int> __thiscall sf::Window::getSize(void)const " (__imp_?getSize@Window@sf@@QBE?AV?$Vector2@I@2@XZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall sf::Window::setActive(bool)const " (__imp_?setActive@Window@sf@@QBE_N_N@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::Window::display(void)" (__imp_?display@Window@sf@@QAEXXZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glClearColor@16 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glClearDepth@8 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glColorPointer@16 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glDepthMask@4 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glDisable@4 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glDisableClientState@4 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glDrawArrays@12 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glEnable@4 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glEnableClientState@4 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glFrustum@48 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glLoadIdentity@0 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glMatrixMode@4 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glRotatef@16 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glTranslatef@12 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glVertexPointer@16 referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol __imp__glViewport@16 referenced in function _main
1>c:\users\xxx\documents\visual studio 2012\Projects\Default\Debug\Default.exe : fatal error LNK1120: 31 unresolved externals
 

As I said above, I followed the tutorial exactly, with linking to the correct folders for both Includes and Libraries, and it doesn't work.

I also tried just using the VisualC++11 (32 bit) download straight from the SFML page in case I didn't configure cmake properly, and I'm getting the exact same errors.

Secondly, and slightly less important, the tutorial I linked to above has you create a convenience header:
//sfml.h
#pragma once
#ifndef SFMLFULL_INCLUDED
#define SFMLFULL_INCLUDED
 
#define SFML_STATIC
 
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
 
#if defined(_DEBUG) || defined(DEBUG)
    #pragma comment(lib,"sfml-graphics-s-d.lib")
    #pragma comment(lib,"sfml-audio-s-d.lib")
    #pragma comment(lib,"sfml-network-s-d.lib")
    #pragma comment(lib,"sfml-window-s-d.lib")
    #pragma comment(lib,"sfml-system-s-d.lib")
    #pragma comment(lib,"sfml-main-d.lib")
#else
    #pragma comment(lib,"sfml-graphics-s.lib")
    #pragma comment(lib,"sfml-audio-s.lib")
    #pragma comment(lib,"sfml-network-s.lib")
    #pragma comment(lib,"sfml-window-s.lib")
    #pragma comment(lib,"sfml-system-s.lib")
    #pragma comment(lib,"sfml-main.lib")
#endif
 
 
#endif // SFMLFULL_INCLUDED

When I try and include the convenience header, I get errors like this:
1>c:\users\xxx\documents\visual studio 2012\projects\default\default\source.cpp(24): error C3861: 'glClearDepth': identifier not found
1>c:\users\xxx\documents\visual studio 2012\projects\default\default\source.cpp(25): error C3861: 'glClearColor': identifier not found
1>c:\users\xxx\documents\visual studio 2012\projects\default\default\source.cpp(28): error C2065: 'GL_DEPTH_TEST' : undeclared identifier
1>c:\users\xxx\documents\visual studio 2012\projects\default\default\source.cpp(28): error C3861: 'glEnable': identifier not found
1>c:\users\xxx\documents\visual studio 2012\projects\default\default\source.cpp(29): error C2065: 'GL_TRUE' : undeclared identifier
...
 
As if the compiler doesn't include any of the .h files that sfml.h says to include (but I don't get any errors in sfml.h itself.

So to get the link errors that I'm getting above, I have to use the includes exactly as they are written in the example.

Please help, I have no idea where to start, google searching for these terms turns up pages talking about linking to the correct libraries, but I thought that's what I already did when I told the project to use library directories .../lib/release and .../lib/debug?

Thanks!

(I'm using MS VisualStudio 2012 Update 2 V11.0.60315.01 running in Windows 7 Profession 64 bit)

Pages: [1]