]> Dogcows Code - chaz/yoink/blobdiff - src/Main.cc
better broadcast support
[chaz/yoink] / src / Main.cc
index 2cd0f0c08aaf87f62fe3a2ba99c17684a0309a60..4d88a709e69bb39a71e739a574823e254ff2f0ac 100644 (file)
@@ -1,32 +1,16 @@
 
-/*******************************************************************************
-
- Copyright (c) 2009, Charles McGarvey
- All rights reserved.
- Redistribution   and   use  in  source  and  binary  forms,  with  or  without
- modification, are permitted provided that the following conditions are met:
-   * Redistributions  of  source  code  must retain the above copyright notice,
-     this list of conditions and the following disclaimer.
-   * Redistributions  in binary form must reproduce the above copyright notice,
-     this  list of conditions and the following disclaimer in the documentation
-     and/or other materials provided with the distribution.
- THIS  SOFTWARE  IS  PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND  ANY  EXPRESS  OR  IMPLIED  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED.  IN  NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- DAMAGES  (INCLUDING,  BUT  NOT  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- SERVICES;  LOSS  OF  USE,  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- CAUSED  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- OR  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-*******************************************************************************/
+/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
+**]  All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
 
 #include <cstdlib>             // atexit, getenv
+#include <functional>
 #include <iostream>
 #include <string>
 #include <unistd.h>            // access
@@ -36,7 +20,6 @@
 #include <Moof/OpenGL.hh>
 #include <Moof/Resource.hh>
 #include <Moof/Settings.hh>
-#include <Moof/Transition.hh>
 #include <Moof/Video.hh>
 
 #include "ErrorHandler.hh"
 #include "version.h"
 
 
-Main::Main()
+Main::Main(Mf::Settings& settings, Mf::Video& video) :
+       Mf::View(settings, video)
 {
-       mDispatchHandler = Mf::core.addHandler("video.newcontext",
-                       boost::bind(&Main::contextRecreated));
+       Mf::Dispatch& dispatch = Mf::Dispatch::global();
+       mNewContextDispatch = dispatch.addTarget("video.newcontext",
+                                                                                        boost::bind(&Main::setupGL));
        setupGL();
 }
 
-void Main::addedToCore()
-{
-       //Mf::Scalar coeff[] = {0.0, 1.0};
-       //Mf::Lerp interp(coeff, 0.25);
-
-       //Mf::LayerP gameLayer = GameLayer::alloc();
-       //Mf::Transition<Mf::Lerp>::Ptr transition =
-               //Mf::Transition<Mf::Lerp>::alloc(gameLayer, Mf::LayerP(), interp);
-       //core.push(transition);
-       //core.push(GameLayer::alloc());
-       Mf::core.push(TitleLayer::alloc());
-}
-
 
 void Main::update(Mf::Scalar t, Mf::Scalar dt)
 {
-       if (Mf::core.getSize() == 1)
+       if (children().size() == 0)
        {
-               // this is the only layer left on the stack
-               //Mf::core.push(TitleLayer::alloc());
-               Mf::core.clear();
+               //Mf::logWarning("main view has no children");
+               //stop();
+               //return;
+               addChild(TitleLayer::alloc());
        }
+
+       Mf::View::update(t, dt);
 }
 
 void Main::draw(Mf::Scalar alpha) const
@@ -90,25 +65,25 @@ void Main::draw(Mf::Scalar alpha) const
 
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
+
+       Mf::View::draw(alpha);
 }
 
 bool Main::handleEvent(const Mf::Event& event)
 {
+       if (Mf::View::handleEvent(event)) return true;
+
        switch (event.type)
        {
                case SDL_KEYUP:
-                       if (event.key.keysym.sym == SDLK_ESCAPE)
-                       {
-                               Mf::core.clear();
-                       }
-                       else if (event.key.keysym.sym == SDLK_f)
+                       if (event.key.keysym.sym == SDLK_f)
                        {
-                               Mf::video->toggleFull();
+                               video().toggleFull();
                        }
                        else if (event.key.keysym.sym == SDLK_l)
                        {
-                               Mf::video->toggleCursorGrab();
-                               Mf::video->toggleCursorVisible();
+                               video().toggleCursorGrab();
+                               video().toggleCursorVisible();
                        }
                        break;
 
@@ -117,8 +92,8 @@ bool Main::handleEvent(const Mf::Event& event)
                        break;
 
                case SDL_QUIT:
-                       Mf::core.clear();
-                       break;
+                       stop();
+                       return true;
        }
 
        return false;
@@ -152,9 +127,10 @@ std::string Main::getConfigPath()
        // 3. $HOME/.yoinkrc
        // 4. YOINKRC (environment)
 
-       std::string path = Mf::Resource::getPath("yoinkrc");
+       std::string path("yoinkrc");
+       Mf::Resource::getPath(path);
 
-#if !defined(_WIN32) && !defined(__WIN32__)
+#if !defined(_WIN32)
        path += ":/etc/yoinkrc";
 #endif
        path += ":$HOME/.yoinkrc";
@@ -195,18 +171,11 @@ void Main::setupGL()
        //glMatrixMode(GL_MODELVIEW);
 }
 
-void Main::contextRecreated()
-{
-       // whenever the context is destroyed and a new one created, it probably
-       // won't contain our state so we need to set that up again
-       setupGL();
-}
-
-
 
 void Main::printUsage()
 {
-       std::cout << "Usage: "PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
+       std::cout << "Usage: "
+                         << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
                          << std::endl
                          << "The alien-smashing action game." << std::endl
                          << std::endl
@@ -220,7 +189,7 @@ void Main::printUsage()
                          << "  fullscreen=true|false" << std::endl
                          << "      if true, uses the entire display" << std::endl
                          << "  framerate=num" << std::endl
-                         << "      the target number of frames per second" << std::endl
+                         << "      number of frames to draw per second" << std::endl
                          << std::endl
                          << "See documentation for more options." << std::endl;
 }
@@ -252,11 +221,18 @@ void Main::printInfo(int argc, char* argv[])
        }
 
        std::cout << "   Executable: " << argv[0] << std::endl
+#ifdef YOINK_GITHEAD
+                         << "       Commit: "YOINK_GITHEAD << std::endl
+#endif
                          << "      Version: "VERSION << std::endl
                          << "        Built: " << COMPILE_TIME << std::endl
                          << "     Compiler: "COMPILER_STRING << std::endl
                          << "       Assets: " << assets << std::endl
                          << "Build options: "
+#ifndef HAVE_CLOCK_GETTIME
+                         << "-"
+#endif
+                         << "clock_gettime "
 #ifdef NDEBUG
                          << "-"
 #endif
@@ -299,8 +275,91 @@ void goodbye()
        std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
 }
 
+
+#include <Moof/Socket.hh>
+
 int main(int argc, char* argv[])
 {
+       Mf::ResolverTask task("4950", "compy", SOCK_DGRAM);
+       task.run();
+
+       int i = task.wait();
+       Mf::logWarning << "task ended with code: " << i << std::endl;
+
+       std::vector<Mf::SocketAddress>::const_iterator it;
+       for (it = task.addresses().begin(); it != task.addresses().end(); ++it)
+       {
+               Mf::SocketAddress addr = *it;
+
+               Mf::logInfo << "address: " << addr.name() << ":" << addr.port() 
+                                       << " (" << addr.type() << ")" << std::endl;
+       }
+
+
+       Mf::Packet packet(1000000);
+
+       packet << 5.1234f;
+       Mf::logInfo << "packet size: " << packet.size() << std::endl;
+
+       float meh3;
+       packet >> meh3;
+       Mf::logInfo << "float: " << meh3 << std::endl;
+       Mf::logInfo << "packet size: " << packet.size() << std::endl;
+
+
+       packet << uint16_t(45);
+       Mf::logInfo << "packet size: " << packet.size() << std::endl;
+       packet << uint64_t(1234567812345679ULL);
+       Mf::logInfo << "packet size: " << packet.size() << std::endl;
+
+       packet << true << false << false << true << false << true << true;
+       Mf::logInfo << "packet size: " << packet.size() << std::endl;
+
+       std::vector<short> hi;
+       hi.push_back(34);
+       hi.push_back(-12345);
+       hi.push_back(7734);
+
+       for (int a = 0; a < 15900; a++)
+       {
+               hi.push_back(a);
+       }
+
+       packet << hi;
+       Mf::logInfo << "packet size: " << packet.size() << std::endl;
+
+       packet << "hello world";
+       Mf::logInfo << "packet size: " << packet.size() << std::endl;
+
+       packet << false << false << false << true << false;
+       Mf::logInfo << "packet size: " << packet.size() << std::endl;
+
+
+       Mf::SocketAddress addr = Mf::SocketAddress::broadcast("4950");
+       //Mf::logInfo << "local addr: " << addr.name() << std::endl;
+
+       //for (it = task.addresses().begin(); it != task.addresses().end(); ++it)
+       //{
+               int bcast = 0;
+
+               //Mf::SocketAddress addr = *it;
+               Mf::Socket sock(addr);
+
+               sock.get(SO_BROADCAST, bcast);
+               Mf::logInfo << "bcast: " << bcast << std::endl;
+
+               sock.set(SO_BROADCAST, 1);
+
+               sock.get(SO_BROADCAST, bcast);
+               Mf::logInfo << "bcast: " << bcast << std::endl;
+
+               sock.write(packet);
+       //}
+
+
+       return 0;
+
+
        if (argc > 1)
        {
                std::string arg(argv[1]);
@@ -321,34 +380,32 @@ int main(int argc, char* argv[])
 
        Mf::Resource::addSearchPaths(Main::getSearchPath());
 
-       Mf::settings.loadFromFiles(Main::getConfigPath());
-       Mf::settings.parseArgs(argc, argv);
+       Mf::Settings settings(argc, argv, Main::getConfigPath());
 
-       Mf::Log::Level logLevel;
-       if (Mf::settings.get("loglevel", logLevel)) Mf::Log::setLevel(logLevel);
+       Mf::Log::Level logLevel = Mf::Log::INFO;
+       settings.get("loglevel", logLevel);
+       Mf::Log::setLevel(logLevel);
 
        try
        {
-               Mf::Video video(PACKAGE_STRING, Mf::Resource::getPath(PACKAGE".png"));
-               MainP app = Main::alloc();
-               Mf::core.push(app);
-               Mf::core.run();
+               Mf::Video::Attributes attributes(settings);
+               attributes.caption = PACKAGE_STRING;
+               attributes.icon = Mf::Resource::getPath(PACKAGE".png");
+
+               Mf::Video       video(attributes);
+               Main            mainView(settings, video);
+
+               mainView.run();
+               return 0;
        }
        catch (const Mf::Error& error)
        {
-               Mf::ModalDialog dialog;
-               dialog.title = PACKAGE_STRING;
-               dialog.text1 = "Unhandled Exception";
-               dialog.text2 = getErrorString(error);
-               dialog.type = Mf::ModalDialog::CRITICAL;
-               dialog.run();
+               Mf::ModalDialog dialog(Mf::ModalDialog::CRITICAL,
+                                                          PACKAGE_STRING, "Unhandled Exception",
+                                                          getErrorString(error));
 
+               dialog.run();
                return 1;
        }
-
-       return 0;
 }
 
-
-/** vim: set ts=4 sw=4 tw=80: *************************************************/
-
This page took 0.029593 seconds and 4 git commands to generate.