Show client.cpp syntax highlighted
/***************************************************************************
freepop.cpp - description
-------------------
begin : Sat Sep 14 2002
copyright : (C) 2002 by Brendon Higgins
email : freepop-devel@lists.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <client.h>
#include <connectstate.h>
#include <ClanLib/core.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>
#include <ClanLib/gui.h>
#include <ClanLib/guistylesilver.h>
#include <boost/filesystem/operations.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <stdexcept>
Client app;
Client::Client():
screenWidth(640),
screenHeight(480),
fullscreen(false),
address(Network::Session::HOST),
port(Network::Session::PORT),
resources(0),
styleManager(0),
guiManager(0) {
}
Client::~Client() {
}
CL_ResourceManager* Client::getResources() {
return resources;
}
CL_StyleManager* Client::getStyleManager() {
return styleManager;
}
CL_GUIManager* Client::getGuiManager() {
return guiManager;
}
Tempo& Client::getTempo() {
return tempo;
}
void Client::initCL() {
FreePopApp::initCL();
CL_SetupDisplay::init();
CL_SetupGL::init();
CL_SetupGUI::init();
// CL_SetupSound::init();
// CL_SetupVorbis::init();
}
void Client::deinitCL() {
// CL_SetupVorbis::deinit();
// CL_SetupSound::deinit();
CL_SetupGUI::deinit();
CL_SetupGL::deinit();
CL_SetupDisplay::deinit();
FreePopApp::deinitCL();
}
bool Client::args(int n, char** a) {
bool ret = true;
CL_CommandLine cl;
enum Opts { HELP = 256 };
cl.add_option('a', "address", "ADDRESS", "IP Address to connect to.");
cl.add_option('p', "port", "PORT", "IP Port to connect to.");
cl.add_option('w', "width", "WIDTH", "Screen width.");
cl.add_option('h', "height", "HEIGHT", "Screen height.");
cl.add_option('f', "fullscreen", "", "Enter in fullscreen mode.");
cl.add_option(HELP, "help", "", "This screen.");
cl.add_option('v', "version", "", "Get the version of this client.");
cl.parse_args(n, a);
while (cl.next()) {
switch (cl.get_key()) {
case 'a':
address = cl.get_argument();
break;
case 'p':
port = cl.get_argument();
break;
case 'w':
screenWidth = boost::lexical_cast<int>(cl.get_argument());
break;
case 'h':
screenHeight = boost::lexical_cast<int>(cl.get_argument());
break;
case 'f':
fullscreen = true;
break;
case HELP:
ret = false;
cl.print_help();
break;
case 'v':
ret = false;
std::cout << "FreePop client version " << PACKAGE_VERSION <<
" (protocol " << FREEPOP_NETPROTOCOL << ")." << std::endl;
break;
}
}
return ret;
}
void Client::run() {
CL_DisplayWindow display(PACKAGE_STRING, screenWidth, screenHeight,
fullscreen);
CL_Display::clear(CL_Color(127, 0, 0));
CL_Display::flip();
// Load the resources lists
boost::filesystem::path resourcesPath = findResourcesPath();
resources = new CL_ResourceManager(
(resourcesPath / "resources.xml").native_file_string());
resources->add_resources(CL_ResourceManager(
(resourcesPath / "guistyle.xml").native_file_string()));
resources->add_resources(CL_ResourceManager(
(resourcesPath / "gui.xml").native_file_string()));
styleManager = new CL_StyleManager_Silver(resources);
guiManager = new CL_GUIManager(styleManager);
// Keep a counter of how many states we've been through
int stateNum = 1;
// The current state
ClientState* s = new ConnectState(CL_IPAddress(address, port));
// The next state
ClientState* ns;
tempo.start();
while (s) {
std::cout << "Entering state number " << stateNum << std::endl;
ns = s->go(this);
delete s;
s = ns;
stateNum++;
}
std::cout << "Quitting on state number " << stateNum << std::endl;
CL_System::keep_alive();
tempo.stop();
// CL_SoundOutput soundOut(44100);
// CL_SoundBuffer beatSample((resourcesPath / "heartbeat.ogg").native_file_string());
// displayOffset = CL_Point(0, 0);
// int beat = 0;
//
// while (state != QUITTING) {
// if (state != NOT_READY) {
// // 143 ms per 1/7th beat => 60 bpm
// // if (tempo.getStamp() - startTime > 143) {
// // if (beat == 0 || beat == 2) {
// // Play a heartbeat
// // beatSample.play();
// //std::cout << "beat" << std::endl;
// // }
// // if (beat == 6) {
// // beat = 0;
// // } else {
// // beat ++;
// // }
// // startTime = tempo.getStamp();
// // }
// }
delete guiManager;
delete styleManager;
delete resources;
}
boost::filesystem::path Client::findResourcesPath() const {
boost::filesystem::path p = "../../resources";
if (boost::filesystem::exists(p / "resources.xml")) {
return p;
}
p = boost::filesystem::path(FREEPOP_DEFAULTRESOURCESPATH,
boost::filesystem::native);
if (boost::filesystem::exists(p / "resources.xml")) {
return p;
}
// Couldn't find it. We'd better die.
throw std::runtime_error("Unable to locate resources.");
}
See more files for this project here