diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 4e7279fdc..741ff7621 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martiņo Figueroa +// Copyright (C) 2001-2008 Martio Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -149,6 +149,9 @@ Renderer::Renderer(){ gi.setFactory(fr.getGraphicsFactory(config.getString("FactoryGraphics"))); GraphicsFactory *graphicsFactory= GraphicsInterface::getInstance().getFactory(); + this->menu = NULL; + this->game = NULL; + modelRenderer= graphicsFactory->newModelRenderer(); textRenderer= graphicsFactory->newTextRenderer2D(); particleRenderer= graphicsFactory->newParticleRenderer(); @@ -184,7 +187,33 @@ Renderer &Renderer::getInstance(){ return renderer; } +void Renderer::reinitAll() { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + const Game *gamePtr = this->game; + const MainMenu *menuPtr = this->menu; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + //end(); + init(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + if(gamePtr != NULL) { + //endGame(); + initGame(gamePtr); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + if(menuPtr != NULL) { + //endMenu(); + initMenu(menuPtr); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); +} // ==================== init ==================== void Renderer::init(){ @@ -210,7 +239,10 @@ void Renderer::init(){ init2dList(); } -void Renderer::initGame(Game *game){ +void Renderer::initGame(const Game *game){ + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + this->game= game; //check gl caps @@ -220,6 +252,8 @@ void Renderer::initGame(Game *game){ shadowMapFrame= 0; waterAnim= 0; + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //shadows if(shadows==sProjected || shadows==sShadowMapping){ static_cast(modelRenderer)->setSecondaryTexCoordUnit(2); @@ -234,6 +268,8 @@ void Renderer::initGame(Game *game){ if(shadows==sShadowMapping){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //shadow mapping glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB); @@ -246,6 +282,8 @@ void Renderer::initGame(Game *game){ } else{ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //projected glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, shadowTextureSize, shadowTextureSize, @@ -255,21 +293,35 @@ void Renderer::initGame(Game *game){ shadowMapFrame= -1; } + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //texture init modelManager[rsGame]->init(); textureManager[rsGame]->init(); fontManager[rsGame]->init(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + init3dList(); } -void Renderer::initMenu(MainMenu *mm){ +void Renderer::initMenu(const MainMenu *mm){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + this->menu = mm; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + modelManager[rsMenu]->init(); textureManager[rsMenu]->init(); fontManager[rsMenu]->init(); //modelRenderer->setCustomTexture(CoreData::getInstance().getCustomTexture()); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + init3dListMenu(mm); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } void Renderer::reset3d(){ @@ -326,6 +378,7 @@ void Renderer::endGame(){ } void Renderer::endMenu(){ + this->menu = NULL; //delete resources modelManager[rsMenu]->end(); textureManager[rsMenu]->end(); @@ -2464,14 +2517,23 @@ void Renderer::checkExtension(const string &extension, const string &msg){ void Renderer::init3dList(){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + const Metrics &metrics= Metrics::getInstance(); assertGl(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + list3d= glGenLists(1); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + glNewList(list3d, GL_COMPILE_AND_EXECUTE); //need to execute, because if not gluPerspective takes no effect and gluLoadMatrix is wrong + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //misc glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); glClearColor(fowColor.x, fowColor.y, fowColor.z, fowColor.w); @@ -2479,6 +2541,8 @@ void Renderer::init3dList(){ glEnable(GL_CULL_FACE); loadProjectionMatrix(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //texture state glActiveTexture(shadowTexUnit); glDisable(GL_TEXTURE_2D); @@ -2492,6 +2556,8 @@ void Renderer::init3dList(){ glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //material state glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, defSpecularColor.ptr()); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, defAmbientColor.ptr()); @@ -2512,6 +2578,8 @@ void Renderer::init3dList(){ glDepthMask(GL_TRUE); glDepthFunc(GL_LESS); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //lighting state glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); @@ -2522,9 +2590,21 @@ void Renderer::init3dList(){ //stencil test glDisable(GL_STENCIL_TEST); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //fog - const Tileset *tileset= game->getWorld()->getTileset(); - if(tileset->getFog()){ + const Tileset *tileset= NULL; + if(game != NULL && game->getWorld() != NULL) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + tileset = game->getWorld()->getTileset(); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + if(tileset != NULL && tileset->getFog()){ + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + glEnable(GL_FOG); if(tileset->getFogMode()==fmExp){ glFogi(GL_FOG_MODE, GL_EXP); @@ -2537,8 +2617,12 @@ void Renderer::init3dList(){ glFogfv(GL_FOG_COLOR, tileset->getFogColor().ptr()); } + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + glEndList(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //assert assertGl(); @@ -2586,15 +2670,23 @@ void Renderer::init2dList(){ assertGl(); } -void Renderer::init3dListMenu(MainMenu *mm){ +void Renderer::init3dListMenu(const MainMenu *mm){ assertGl(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + const Metrics &metrics= Metrics::getInstance(); - const MenuBackground *mb= mm->getMenuBackground(); + const MenuBackground *mb= mm->getConstMenuBackground(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); list3dMenu= glGenLists(1); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + glNewList(list3dMenu, GL_COMPILE); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //misc glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); glClearColor(0.4f, 0.4f, 0.4f, 1.f); @@ -2637,15 +2729,23 @@ void Renderer::init3dListMenu(MainMenu *mm){ //stencil test glDisable(GL_STENCIL_TEST); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //fog - if(mb->getFog()){ + if(mb != NULL && mb->getFog()){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + glEnable(GL_FOG); glFogi(GL_FOG_MODE, GL_EXP2); glFogf(GL_FOG_DENSITY, mb->getFogDensity()); } + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + glEndList(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //assert assertGl(); } diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index 03c54d698..6fafeb30a 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martiņo Figueroa +// Copyright (C) 2001-2008 Martio Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -134,6 +134,7 @@ private: //game const Game *game; + const MainMenu *menu; //misc int triangleCount; @@ -174,10 +175,13 @@ private: public: static Renderer &getInstance(); + void reinitAll(); + + //init void init(); - void initGame(Game *game); - void initMenu(MainMenu *mm); + void initGame(const Game *game); + void initMenu(const MainMenu *mm); void reset3d(); void reset2d(); void reset3dMenu(); @@ -287,7 +291,7 @@ private: //gl init void init3dList(); void init2dList(); - void init3dListMenu(MainMenu *mm); + void init3dListMenu(const MainMenu *mm); //misc void loadProjectionMatrix(); diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index a88adeee0..2570b1a0e 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -35,12 +35,16 @@ namespace Glest{ namespace Game{ // ===================================================== BattleEnd::BattleEnd(Program *program, const Stats *stats): -ProgramState(program){ +ProgramState(program) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); this->stats= *stats; + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } BattleEnd::~BattleEnd(){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); SoundRenderer::getInstance().playMusic(CoreData::getInstance().getMenuMusic()); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } void BattleEnd::update(){ @@ -140,11 +144,15 @@ void BattleEnd::render(){ } void BattleEnd::keyDown(char key){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); program->setState(new MainMenu(program)); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } void BattleEnd::mouseDownLeft(int x, int y){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); program->setState(new MainMenu(program)); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } }}//end namespace diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 8119ed8cf..716b6f491 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -210,9 +210,9 @@ void MainWindow::eventKeyDown(char key){ if(keystate.mod & (KMOD_LALT | KMOD_RALT)) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] ALT-ENTER pressed\n",__FILE__,__FUNCTION__,__LINE__); - //Renderer &renderer= Renderer::getInstance(); + Renderer &renderer= Renderer::getInstance(); //renderer.reloadResources(); - //renderer.init(); + renderer.reinitAll(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/glest_game/menu/main_menu.cpp b/source/glest_game/menu/main_menu.cpp new file mode 100644 index 000000000..0dc9e8683 --- /dev/null +++ b/source/glest_game/menu/main_menu.cpp @@ -0,0 +1,209 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martio Figueroa +// +// You can redistribute this code 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 "main_menu.h" + +#include "renderer.h" +#include "sound.h" +#include "config.h" +#include "program.h" +#include "game_util.h" +#include "game.h" +#include "platform_util.h" +#include "sound_renderer.h" +#include "core_data.h" +#include "faction.h" +#include "metrics.h" +#include "network_manager.h" +#include "network_message.h" +#include "socket.h" +#include "menu_state_root.h" + +#include "leak_dumper.h" + +using namespace Shared::Sound; +using namespace Shared::Platform; +using namespace Shared::Util; +using namespace Shared::Graphics; +using namespace Shared::Xml; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class MainMenu +// ===================================================== + +// ===================== PUBLIC ======================== + +MainMenu::MainMenu(Program *program): + ProgramState(program) +{ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + mouseX=100; + mouseY=100; + + state= NULL; + this->program= program; + + fps= 0; + lastFps= 0; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + setState(new MenuStateRoot(program, this)); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); +} + +MainMenu::~MainMenu() { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + delete state; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + Renderer::getInstance().endMenu(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); +} + +void MainMenu::init(){ + Renderer::getInstance().initMenu(this); +} + +//asynchronus render update +void MainMenu::render(){ + + Config &config= Config::getInstance(); + Renderer &renderer= Renderer::getInstance(); + CoreData &coreData= CoreData::getInstance(); + + fps++; + + renderer.clearBuffers(); + + //3d + renderer.reset3dMenu(); + renderer.clearZBuffer(); + renderer.loadCameraMatrix(menuBackground.getCamera()); + renderer.renderMenuBackground(&menuBackground); + renderer.renderParticleManager(rsMenu); + + //2d + renderer.reset2d(); + state->render(); + renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); + + if(config.getBool("DebugMode")){ + renderer.renderText( + "FPS: " + intToStr(lastFps), + coreData.getMenuFontNormal(), Vec3f(1.f), 10, 10, false); + } + + renderer.swapBuffers(); +} + +//syncronus update +void MainMenu::update(){ + Renderer::getInstance().updateParticleManager(rsMenu); + mouse2dAnim= (mouse2dAnim +1) % Renderer::maxMouse2dAnim; + menuBackground.update(); + state->update(); +} + +void MainMenu::tick(){ + lastFps= fps; + fps= 0; +} + +//event magangement: mouse click +void MainMenu::mouseMove(int x, int y, const MouseState *ms){ + mouseX= x; mouseY= y; + state->mouseMove(x, y, ms); +} + +//returns if exiting +void MainMenu::mouseDownLeft(int x, int y){ + state->mouseClick(x, y, mbLeft); +} + +void MainMenu::mouseDownRight(int x, int y){ + state->mouseClick(x, y, mbRight); +} + +void MainMenu::keyDown(char key){ + state->keyDown(key); +} + +void MainMenu::keyPress(char c){ + state->keyPress(c); +} + +void MainMenu::setState(MenuState *state){ + delete this->state; + this->state= state; + GraphicComponent::resetFade(); + + menuBackground.setTargetCamera(state->getCamera()); +} + + +// ===================================================== +// class MenuState +// ===================================================== + +MenuState::MenuState(Program *program, MainMenu *mainMenu, const string &stateName){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + this->program= program; + this->mainMenu= mainMenu; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + //camera + XmlTree xmlTree; + xmlTree.load("data/core/menu/menu.xml"); + const XmlNode *menuNode= xmlTree.getRootNode(); + const XmlNode *cameraNode= menuNode->getChild("camera"); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + //position + const XmlNode *positionNode= cameraNode->getChild(stateName + "-position"); + Vec3f startPosition; + startPosition.x= positionNode->getAttribute("x")->getFloatValue(); + startPosition.y= positionNode->getAttribute("y")->getFloatValue(); + startPosition.z= positionNode->getAttribute("z")->getFloatValue(); + camera.setPosition(startPosition); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + //rotation + const XmlNode *rotationNode= cameraNode->getChild(stateName + "-rotation"); + Vec3f startRotation; + startRotation.x= rotationNode->getAttribute("x")->getFloatValue(); + startRotation.y= rotationNode->getAttribute("y")->getFloatValue(); + startRotation.z= rotationNode->getAttribute("z")->getFloatValue(); + camera.setOrientation(Quaternion(EulerAngles( + degToRad(startRotation.x), + degToRad(startRotation.y), + degToRad(startRotation.z)))); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); +} + +}}//end namespace diff --git a/source/glest_game/menu/main_menu.h b/source/glest_game/menu/main_menu.h index 3b3ab8ba8..dc426f7e3 100644 --- a/source/glest_game/menu/main_menu.h +++ b/source/glest_game/menu/main_menu.h @@ -77,6 +77,7 @@ public: ~MainMenu(); MenuBackground *getMenuBackground() {return &menuBackground;} + const MenuBackground *getConstMenuBackground() const {return &menuBackground;} virtual void render(); virtual void update(); diff --git a/source/shared_lib/include/sound/sound.h b/source/shared_lib/include/sound/sound.h new file mode 100644 index 000000000..85551a855 --- /dev/null +++ b/source/shared_lib/include/sound/sound.h @@ -0,0 +1,112 @@ +// ============================================================== +// This file is part of Glest Shared Library (www.glest.org) +// +// Copyright (C) 2001-2008 Martio Figueroa +// +// You can redistribute this code 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 +// ============================================================== + +#ifndef _SHARED_SOUND_SOUND_H_ +#define _SHARED_SOUND_SOUND_H_ + +#include +#include "sound_file_loader.h" + +using namespace std; +using namespace Shared::Platform; + +namespace Shared{ namespace Sound{ + +// ===================================================== +// class SoundInfo +// ===================================================== + +class SoundInfo{ +private: + uint32 channels; + uint32 samplesPerSecond; + uint32 bitsPerSample; + uint32 size; + +public: + SoundInfo(); + virtual ~SoundInfo(){}; + + uint32 getChannels() const {return channels;} + uint32 getSamplesPerSecond() const {return samplesPerSecond;} + uint32 getBitsPerSample() const {return bitsPerSample;} + uint32 getSize() const {return size;} + + void setChannels(uint32 channels) {this->channels= channels;} + void setsamplesPerSecond(uint32 samplesPerSecond) {this->samplesPerSecond= samplesPerSecond;} + void setBitsPerSample(uint32 bitsPerSample) {this->bitsPerSample= bitsPerSample;} + void setSize(uint32 size) {this->size= size;} +}; + +// ===================================================== +// class Sound +// ===================================================== + +class Sound{ +protected: + SoundFileLoader *soundFileLoader; + SoundInfo info; + float volume; + string fileName; + +public: + Sound(); + virtual ~Sound(){}; + + const SoundInfo *getInfo() const {return &info;} + float getVolume() const {return volume;} + + void setVolume(float volume) {this->volume= volume;} + string getFileName() {return fileName; } +}; + +// ===================================================== +// class StaticSound +// ===================================================== + +class StaticSound: public Sound{ +private: + int8 * samples; + +public: + StaticSound(); + virtual ~StaticSound(); + + int8 *getSamples() const {return samples;} + + void load(const string &path); + void close(); +}; + +// ===================================================== +// class StrSound +// ===================================================== + +class StrSound: public Sound{ +private: + StrSound *next; + +public: + StrSound(); + virtual ~StrSound(); + + StrSound *getNext() const {return next;} + void setNext(StrSound *next) {this->next= next;} + + void open(const string &path); + uint32 read(int8 *samples, uint32 size); + void close(); + void restart(); +}; + +}}//end namespace + +#endif diff --git a/source/shared_lib/include/sound/sound_file_loader.h b/source/shared_lib/include/sound/sound_file_loader.h new file mode 100644 index 000000000..8a7eb07c9 --- /dev/null +++ b/source/shared_lib/include/sound/sound_file_loader.h @@ -0,0 +1,108 @@ +// ============================================================== +// This file is part of Glest Shared Library (www.glest.org) +// +// Copyright (C) 2001-2008 Martio Figueroa +// +// You can redistribute this code 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 +// ============================================================== + +#ifndef _SHARED_SOUND_SOUNDFILELOADER_H_ +#define _SHARED_SOUND_SOUNDFILELOADER_H_ + +#include +#include + +#include "types.h" +#include "factory.h" + +struct OggVorbis_File; + +using std::string; +using std::ifstream; + +namespace Shared{ namespace Sound{ + +using Platform::uint32; +using Platform::int8; +using Util::MultiFactory; + +class SoundInfo; + +// ===================================================== +// class SoundFileLoader +// +/// Interface that all SoundFileLoaders will implement +// ===================================================== + +class SoundFileLoader{ +public: + virtual ~SoundFileLoader(){} + + virtual void open(const string &path, SoundInfo *soundInfo)= 0; + virtual uint32 read(int8 *samples, uint32 size)= 0; + virtual void close()= 0; + virtual void restart()= 0; + virtual string getFileName() = 0; +}; + +// ===================================================== +// class WavSoundFileLoader +// +/// Wave file loader +// ===================================================== + +class WavSoundFileLoader: public SoundFileLoader{ +private: + static const int maxDataRetryCount= 10; + string fileName; +private: + uint32 dataOffset; + uint32 dataSize; + uint32 bytesPerSecond; + ifstream f; + +public: + virtual void open(const string &path, SoundInfo *soundInfo); + virtual uint32 read(int8 *samples, uint32 size); + virtual void close(); + virtual void restart(); + virtual string getFileName() { return fileName; } +}; + +// ===================================================== +// class OggSoundFileLoader +// +/// OGG sound file loader, uses ogg-vorbis library +// ===================================================== + +class OggSoundFileLoader: public SoundFileLoader{ +private: + OggVorbis_File *vf; + FILE *f; + string fileName; + +public: + virtual void open(const string &path, SoundInfo *soundInfo); + virtual uint32 read(int8 *samples, uint32 size); + virtual void close(); + virtual void restart(); + virtual string getFileName() { return fileName; } +}; + +// ===================================================== +// class SoundFileLoaderFactory +// ===================================================== + +class SoundFileLoaderFactory: public MultiFactory{ +private: + SoundFileLoaderFactory(); +public: + static SoundFileLoaderFactory * getInstance(); +}; + +}}//end namespace + +#endif diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index f9788bac7..6b0397439 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -114,7 +114,7 @@ std::vector Socket::getLocalIPAddressList() { close(fd); sprintf(myhostaddr, "%s",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); - printf("%s\n",myhostaddr); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,myhostaddr); if(strlen(myhostaddr) > 0 && strncmp(myhostaddr,"127.",4) != 0) { if(std::find(ipList.begin(),ipList.end(),myhostaddr) == ipList.end()) { @@ -196,10 +196,7 @@ bool Socket::hasDataToRead(std::map &socketTriggeredList) int retval = select(imaxsocket + 1, &rfds, NULL, NULL, &tv); if(retval < 0) { - char szBuf[1024]=""; - sprintf(szBuf,"In [%s::%s] ERROR SELECTING SOCKET DATA retval = %d errno = %d [%s]",__FILE__,__FUNCTION__,retval,errno,strerror(errno)); - fprintf(stderr, "%s", szBuf); - + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] ERROR SELECTING SOCKET DATA retval = %d errno = %d [%s]\n",__FILE__,__FUNCTION__,retval,errno,strerror(errno)); } else if(retval) { @@ -291,10 +288,8 @@ int Socket::getDataToRead(){ if(err < 0 && errno != EAGAIN) { - char szBuf[1024]=""; - sprintf(szBuf,"In [%s::%s] ERROR PEEKING SOCKET DATA, err = %d errno = %d [%s]\n",__FILE__,__FUNCTION__,err,errno,strerror(errno)); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] ERROR PEEKING SOCKET DATA, err = %d errno = %d [%s]\n",__FILE__,__FUNCTION__,err,errno,strerror(errno)); //throwException(szBuf); - printf("%s",szBuf); } else if(err == 0) { @@ -313,14 +308,12 @@ int Socket::send(const void *data, int dataSize) { } if(bytesSent < 0 && errno != EAGAIN) { - char szBuf[1024]=""; - sprintf(szBuf,"In [%s::%s] ERROR WRITING SOCKET DATA, err = %d errno = %d [%s]\n",__FILE__,__FUNCTION__,bytesSent,errno,strerror(errno)); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] ERROR WRITING SOCKET DATA, err = %d errno = %d [%s]\n",__FILE__,__FUNCTION__,bytesSent,errno,strerror(errno)); //throwException(szBuf); - printf("%s",szBuf); } else if(bytesSent < 0 && errno == EAGAIN) { - printf("In [%s::%s] #1 EAGAIN during send, trying again...\n",__FILE__,__FUNCTION__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #1 EAGAIN during send, trying again...\n",__FILE__,__FUNCTION__); time_t tStartTimer = time(NULL); while((bytesSent < 0 && errno == EAGAIN) && (difftime(time(NULL),tStartTimer) <= 5)) @@ -329,7 +322,7 @@ int Socket::send(const void *data, int dataSize) { { bytesSent = ::send(sock, reinterpret_cast(data), dataSize, 0); - printf("In [%s::%s] #2 EAGAIN during send, trying again returned: %d\n",__FILE__,__FUNCTION__,bytesSent); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #2 EAGAIN during send, trying again returned: %d\n",__FILE__,__FUNCTION__,bytesSent); } } } @@ -338,9 +331,7 @@ int Socket::send(const void *data, int dataSize) { int iErr = errno; disconnectSocket(); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] DISCONNECTED SOCKET error while sending socket data, bytesSent = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,bytesSent,iErr,strerror(iErr)); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] DISCONNECTED SOCKET error while sending socket data, bytesSent = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,bytesSent,iErr,strerror(iErr)); //throwException(szBuf); } @@ -359,14 +350,12 @@ int Socket::receive(void *data, int dataSize) } if(bytesReceived < 0 && errno != EAGAIN) { - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] ERROR READING SOCKET DATA error while sending socket data, bytesSent = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,bytesReceived,errno,strerror(errno)); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] ERROR READING SOCKET DATA error while sending socket data, bytesSent = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,bytesReceived,errno,strerror(errno)); //throwException(szBuf); - printf("%s",szBuf); } else if(bytesReceived < 0 && errno == EAGAIN) { - printf("In [%s::%s] #1 EAGAIN during receive, trying again...\n",__FILE__,__FUNCTION__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #1 EAGAIN during receive, trying again...\n",__FILE__,__FUNCTION__); time_t tStartTimer = time(NULL); while((bytesReceived < 0 && errno == EAGAIN) && (difftime(time(NULL),tStartTimer) <= 5)) @@ -375,7 +364,7 @@ int Socket::receive(void *data, int dataSize) { bytesReceived = recv(sock, reinterpret_cast(data), dataSize, 0); - printf("In [%s::%s] #2 EAGAIN during receive, trying again returned: %d\n",__FILE__,__FUNCTION__,bytesReceived); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #2 EAGAIN during receive, trying again returned: %d\n",__FILE__,__FUNCTION__,bytesReceived); } } } @@ -385,9 +374,7 @@ int Socket::receive(void *data, int dataSize) int iErr = errno; disconnectSocket(); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] DISCONNECTED SOCKET error while receiving socket data, bytesReceived = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,bytesReceived,iErr,strerror(iErr)); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] DISCONNECTED SOCKET error while receiving socket data, bytesReceived = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,bytesReceived,iErr,strerror(iErr)); //throwException(szBuf); } return static_cast(bytesReceived); @@ -401,15 +388,14 @@ int Socket::peek(void *data, int dataSize){ } if(err < 0 && errno != EAGAIN) { - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] ERROR PEEKING SOCKET DATA error while sending socket data, bytesSent = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,err,errno,strerror(errno)); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] ERROR PEEKING SOCKET DATA error while sending socket data, bytesSent = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,err,errno,strerror(errno)); //throwException(szBuf); disconnectSocket(); } else if(err < 0 && errno == EAGAIN) { - printf("In [%s::%s] #1 EAGAIN during peek, trying again...\n",__FILE__,__FUNCTION__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #1 EAGAIN during peek, trying again...\n",__FILE__,__FUNCTION__); time_t tStartTimer = time(NULL); while((err < 0 && errno == EAGAIN) && (difftime(time(NULL),tStartTimer) <= 5)) @@ -418,7 +404,7 @@ int Socket::peek(void *data, int dataSize){ { err = recv(sock, reinterpret_cast(data), dataSize, MSG_PEEK); - printf("In [%s::%s] #2 EAGAIN during peek, trying again returned: %d\n",__FILE__,__FUNCTION__,err); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #2 EAGAIN during peek, trying again returned: %d\n",__FILE__,__FUNCTION__,err); } } } @@ -428,9 +414,7 @@ int Socket::peek(void *data, int dataSize){ int iErr = errno; disconnectSocket(); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] DISCONNECTED SOCKET error while peeking socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,err,iErr,strerror(iErr)); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] DISCONNECTED SOCKET error while peeking socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,err,iErr,strerror(iErr)); //throwException(szBuf); } @@ -468,9 +452,7 @@ bool Socket::isReadable() lastDebugEvent = time(NULL); //throwException("Error selecting socket"); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] error while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] error while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); } } //return (i == 1 && FD_ISSET(sock, &set)); @@ -499,9 +481,7 @@ bool Socket::isWritable(bool waitOnDelayedResponse) { lastDebugEvent = time(NULL); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] error while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] error while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); } waitOnDelayedResponse = false; @@ -512,9 +492,7 @@ bool Socket::isWritable(bool waitOnDelayedResponse) if(difftime(time(NULL),lastDebugEvent) >= 1) { lastDebugEvent = time(NULL); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] TIMEOUT while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] TIMEOUT while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); } if(waitOnDelayedResponse == false) @@ -546,30 +524,6 @@ bool Socket::isConnected() char tmp; int err = peek(&tmp, sizeof(tmp)); return (err > 0); - /* - int err = recv(sock, &tmp, sizeof(tmp), MSG_PEEK); - - if(err <= 0 && errno != EAGAIN) - { - int iErr = errno; - disconnectSocket(); - - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] DISCONNECTED SOCKET error while peeking isconnected socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,err,iErr,strerror(iErr)); - printf("%s",szBuf); - - return false; - } - else if(err <= 0) - { - int iErr = errno; - //disconnectSocket(); - - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] #2 DISCONNECTED SOCKET error while peeking isconnected socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,err,iErr,strerror(iErr)); - printf("%s",szBuf); - } - */ } //otherwise the socket is connected @@ -684,9 +638,7 @@ void ClientSocket::connect(const Ip &ip, int port) int err= ::connect(sock, reinterpret_cast(&addr), sizeof(addr)); if(err < 0) { - char szBuf[1024]=""; - sprintf(szBuf,"In [%s::%s] #2 Error connecting socket for IP: %s for Port: %d err = %d errno = %d [%s]\n",__FILE__,__FUNCTION__,ip.getString().c_str(),port,err,errno,strerror(errno)); - fprintf(stderr, "%s", szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #2 Error connecting socket for IP: %s for Port: %d err = %d errno = %d [%s]\n",__FILE__,__FUNCTION__,ip.getString().c_str(),port,err,errno,strerror(errno)); if (errno == EINPROGRESS) { @@ -695,7 +647,7 @@ void ClientSocket::connect(const Ip &ip, int port) int valopt; socklen_t lon; - fprintf(stderr, "In [%s::%s] EINPROGRESS in connect() - selecting\n",__FILE__,__FUNCTION__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] EINPROGRESS in connect() - selecting\n",__FILE__,__FUNCTION__); do { @@ -709,9 +661,8 @@ void ClientSocket::connect(const Ip &ip, int port) if (err < 0 && errno != EINTR) { - sprintf(szBuf, "In [%s::%s] Error connecting %d - [%s]\n",__FILE__,__FUNCTION__,errno, strerror(errno)); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Error connecting %d - [%s]\n",__FILE__,__FUNCTION__,errno, strerror(errno)); //throwException(szBuf); - fprintf(stderr, "%s", szBuf); break; } else if (err > 0) @@ -720,30 +671,27 @@ void ClientSocket::connect(const Ip &ip, int port) lon = sizeof(int); if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) { - sprintf(szBuf, "In [%s::%s] Error in getsockopt() %d - [%s]\n",__FILE__,__FUNCTION__,errno, strerror(errno)); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Error in getsockopt() %d - [%s]\n",__FILE__,__FUNCTION__,errno, strerror(errno)); //throwException(szBuf); - fprintf(stderr, "%s", szBuf); break; } // Check the value returned... if (valopt) { - sprintf(szBuf, "In [%s::%s] Error in delayed connection() %d - [%s]\n",__FILE__,__FUNCTION__,valopt, strerror(valopt)); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Error in delayed connection() %d - [%s]\n",__FILE__,__FUNCTION__,valopt, strerror(valopt)); //throwException(szBuf); - fprintf(stderr, "%s", szBuf); break; } errno = 0; - fprintf(stderr, "In [%s::%s] Apparent recovery for connection sock = %d, err = %d, errno = %d\n",__FILE__,__FUNCTION__,sock,err,errno); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Apparent recovery for connection sock = %d, err = %d, errno = %d\n",__FILE__,__FUNCTION__,sock,err,errno); break; } else { - sprintf(szBuf, "In [%s::%s] Timeout in select() - Cancelling!\n",__FILE__,__FUNCTION__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Timeout in select() - Cancelling!\n",__FILE__,__FUNCTION__); //throwException(szBuf); - fprintf(stderr, "%s", szBuf); disconnectSocket(); break; @@ -753,13 +701,13 @@ void ClientSocket::connect(const Ip &ip, int port) if(err < 0) { - fprintf(stderr, "In [%s::%s] Before END sock = %d, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,sock,err,errno,strerror(errno)); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Before END sock = %d, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,sock,err,errno,strerror(errno)); //throwException(szBuf); disconnectSocket(); } else { - fprintf(stderr, "In [%s::%s] Valid recovery for connection sock = %d, err = %d, errno = %d\n",__FILE__,__FUNCTION__,sock,err,errno); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Valid recovery for connection sock = %d, err = %d, errno = %d\n",__FILE__,__FUNCTION__,sock,err,errno); } } } diff --git a/source/shared_lib/sources/platform/win32/socket.cpp b/source/shared_lib/sources/platform/win32/socket.cpp index c940b2754..903fdd81f 100644 --- a/source/shared_lib/sources/platform/win32/socket.cpp +++ b/source/shared_lib/sources/platform/win32/socket.cpp @@ -304,9 +304,7 @@ bool Socket::hasDataToRead(std::map &socketTriggeredList) int retval = select(imaxsocket + 1, &rfds, NULL, NULL, &tv); if(retval < 0) { - char szBuf[1024]=""; - sprintf(szBuf,"In [%s::%s] ERROR SELECTING SOCKET DATA retval = %d WSAGetLastError() = %d",__FILE__,__FUNCTION__,retval,WSAGetLastError()); - fprintf(stderr, "%s", szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] ERROR SELECTING SOCKET DATA retval = %d WSAGetLastError() = %d",__FILE__,__FUNCTION__,retval,WSAGetLastError()); } else if(retval) { @@ -398,9 +396,7 @@ int Socket::getDataToRead(){ if(err < 0 && WSAGetLastError() != WSAEWOULDBLOCK) { - char szBuf[1024]=""; - sprintf(szBuf,"In [%s::%s] ERROR PEEKING SOCKET DATA, err = %d WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,err,WSAGetLastError()); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] ERROR PEEKING SOCKET DATA, err = %d WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,err,WSAGetLastError()); //throwException(szBuf); } else if(err == 0) @@ -420,10 +416,8 @@ int Socket::send(const void *data, int dataSize) { } if(bytesSent < 0 && WSAGetLastError() != WSAEWOULDBLOCK) { - char szBuf[1024]=""; - sprintf(szBuf,"In [%s::%s] ERROR WRITING SOCKET DATA, err = %d WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,bytesSent,WSAGetLastError()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] ERROR WRITING SOCKET DATA, err = %d WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,bytesSent,WSAGetLastError()); //throwException(szBuf); - printf("%s",szBuf); } else if(bytesSent < 0 && WSAGetLastError() == WSAEWOULDBLOCK) { @@ -445,9 +439,7 @@ int Socket::send(const void *data, int dataSize) { int iErr = WSAGetLastError(); disconnectSocket(); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] DISCONNECTED SOCKET error while sending socket data, bytesSent = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,bytesSent,iErr); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] DISCONNECTED SOCKET error while sending socket data, bytesSent = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,bytesSent,iErr); //throwException(szBuf); } @@ -466,14 +458,12 @@ int Socket::receive(void *data, int dataSize) } if(bytesReceived < 0 && WSAGetLastError() != WSAEWOULDBLOCK) { - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] ERROR READING SOCKET DATA error while sending socket data, bytesSent = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,bytesReceived,WSAGetLastError()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] ERROR READING SOCKET DATA error while sending socket data, bytesSent = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,bytesReceived,WSAGetLastError()); //throwException(szBuf); - printf("%s",szBuf); } else if(bytesReceived < 0 && WSAGetLastError() == WSAEWOULDBLOCK) { - printf("In [%s::%s] #1 WSAEWOULDBLOCK during receive, trying again...\n",__FILE__,__FUNCTION__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #1 WSAEWOULDBLOCK during receive, trying again...\n",__FILE__,__FUNCTION__); time_t tStartTimer = time(NULL); while((bytesReceived < 0 && WSAGetLastError() == WSAEWOULDBLOCK) && (difftime(time(NULL),tStartTimer) <= 5)) @@ -482,7 +472,7 @@ int Socket::receive(void *data, int dataSize) { bytesReceived = recv(sock, reinterpret_cast(data), dataSize, 0); - printf("In [%s::%s] #2 WSAEWOULDBLOCK during receive, trying again returned: %d\n",__FILE__,__FUNCTION__,bytesReceived); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #2 WSAEWOULDBLOCK during receive, trying again returned: %d\n",__FILE__,__FUNCTION__,bytesReceived); } } } @@ -492,9 +482,7 @@ int Socket::receive(void *data, int dataSize) int iErr = WSAGetLastError(); disconnectSocket(); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] DISCONNECTED SOCKET error while receiving socket data, bytesReceived = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,bytesReceived,iErr); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] DISCONNECTED SOCKET error while receiving socket data, bytesReceived = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,bytesReceived,iErr); //throwException(szBuf); } return static_cast(bytesReceived); @@ -508,14 +496,12 @@ int Socket::peek(void *data, int dataSize){ } if(err < 0 && WSAGetLastError() != WSAEWOULDBLOCK) { - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] ERROR PEEKING SOCKET DATA error while sending socket data, bytesSent = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,err,WSAGetLastError()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] ERROR PEEKING SOCKET DATA error while sending socket data, bytesSent = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,err,WSAGetLastError()); //throwException(szBuf); - printf("%s",szBuf); } else if(err < 0 && WSAGetLastError() == WSAEWOULDBLOCK) { - printf("In [%s::%s] #1 WSAEWOULDBLOCK during peek, trying again...\n",__FILE__,__FUNCTION__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #1 WSAEWOULDBLOCK during peek, trying again...\n",__FILE__,__FUNCTION__); time_t tStartTimer = time(NULL); while((err < 0 && WSAGetLastError() == WSAEWOULDBLOCK) && (difftime(time(NULL),tStartTimer) <= 5)) @@ -524,7 +510,7 @@ int Socket::peek(void *data, int dataSize){ { err = recv(sock, reinterpret_cast(data), dataSize, MSG_PEEK); - printf("In [%s::%s] #2 WSAEWOULDBLOCK during peek, trying again returned: %d\n",__FILE__,__FUNCTION__,err); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #2 WSAEWOULDBLOCK during peek, trying again returned: %d\n",__FILE__,__FUNCTION__,err); } } } @@ -534,9 +520,7 @@ int Socket::peek(void *data, int dataSize){ int iErr = WSAGetLastError(); disconnectSocket(); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] DISCONNECTED SOCKET error while peeking socket data, err = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,err,iErr); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] DISCONNECTED SOCKET error while peeking socket data, err = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,err,iErr); //throwException(szBuf); } @@ -571,9 +555,7 @@ bool Socket::isReadable() int i= select(sock+1, &set, NULL, NULL, &tv); if(i==SOCKET_ERROR) { - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] error while selecting socket data, err = %d, errno = %d\n",__FILE__,__FUNCTION__,i,WSAGetLastError()); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] error while selecting socket data, err = %d, errno = %d\n",__FILE__,__FUNCTION__,i,WSAGetLastError()); } //return (i == 1 && FD_ISSET(sock, &set)); return (i == 1); @@ -597,16 +579,12 @@ bool Socket::isWritable(bool waitOnDelayedResponse) int i= select(sock+1, NULL, &set, NULL, &tv); if(i==SOCKET_ERROR) { - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] error while selecting socket data, err = %d, errno = %d\n",__FILE__,__FUNCTION__,i,WSAGetLastError()); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] error while selecting socket data, err = %d, errno = %d\n",__FILE__,__FUNCTION__,i,WSAGetLastError()); waitOnDelayedResponse = false; } else if(i == 0) { - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] TIMEOUT while selecting socket data, err = %d, errno = %d\n",__FILE__,__FUNCTION__,i,WSAGetLastError()); - printf("%s",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] TIMEOUT while selecting socket data, err = %d, errno = %d\n",__FILE__,__FUNCTION__,i,WSAGetLastError()); if(waitOnDelayedResponse == false) { @@ -636,21 +614,6 @@ bool Socket::isConnected(){ char tmp; int err = peek(&tmp, sizeof(tmp)); return (err > 0); - /* - int err = recv(sock, &tmp, sizeof(tmp), MSG_PEEK); - - if(err <= 0 && WSAGetLastError() != WSAEWOULDBLOCK) - { - int iErr = WSAGetLastError(); - disconnectSocket(); - - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] DISCONNECTED SOCKET error while peeking isconnected socket data, err = %d, WSAGetLastError() = %d\n",__FILE__,__FUNCTION__,err,iErr); - printf("%s",szBuf); - - return false; - } - */ } //otherwise the socket is connected @@ -766,10 +729,7 @@ void ClientSocket::connect(const Ip &ip, int port) int err= ::connect(sock, reinterpret_cast(&addr), sizeof(addr)); if(err < 0) { - char szBuf[1024]=""; - sprintf(szBuf,"#2 Error connecting socket for IP: %s for Port: %d err = %d WSAGetLastError() = %d",ip.getString().c_str(),port,err,WSAGetLastError()); - fprintf(stderr, "%s\n", WSAGetLastErrorMessage(szBuf)); - //fprintf(stderr, "%s", szBuf); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"#2 Error connecting socket for IP: %s for Port: %d err = %d WSAGetLastError() = %d",ip.getString().c_str(),port,err,WSAGetLastError()); if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK) { @@ -778,7 +738,7 @@ void ClientSocket::connect(const Ip &ip, int port) int valopt; socklen_t lon; - fprintf(stderr, "WSAEINPROGRESS or WSAEWOULDBLOCK in connect() - selecting\n"); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"WSAEINPROGRESS or WSAEWOULDBLOCK in connect() - selecting\n"); do { tv.tv_sec = 10; tv.tv_usec = 0; @@ -790,9 +750,8 @@ void ClientSocket::connect(const Ip &ip, int port) if (err < 0 && WSAGetLastError() != WSAEWOULDBLOCK && WSAGetLastError() != WSAEWOULDBLOCK) { - sprintf(szBuf, "Error connecting %d\n", WSAGetLastError()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Error connecting %d\n", WSAGetLastError()); //throwException(szBuf); - fprintf(stderr, "%s", szBuf); break; } else if (err > 0) { @@ -800,29 +759,26 @@ void ClientSocket::connect(const Ip &ip, int port) lon = sizeof(int); if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)(&valopt), &lon) < 0) { - sprintf(szBuf, "Error in getsockopt() %d\n", WSAGetLastError()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Error in getsockopt() %d\n", WSAGetLastError()); //throwException(szBuf); - fprintf(stderr, "%s", szBuf); break; } // Check the value returned... if (valopt) { - sprintf(szBuf, "Error in delayed connection() %d\n", valopt); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Error in delayed connection() %d\n", valopt); //throwException(szBuf); - fprintf(stderr, "%s", szBuf); break; } - fprintf(stderr, "Apparent recovery for connection sock = %d, err = %d, WSAGetLastError() = %d\n",sock,err,WSAGetLastError()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Apparent recovery for connection sock = %d, err = %d, WSAGetLastError() = %d\n",sock,err,WSAGetLastError()); break; } else { - sprintf(szBuf, "Timeout in select() - Cancelling!\n"); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Timeout in select() - Cancelling!\n"); //throwException(szBuf); - fprintf(stderr, "%s", szBuf); disconnectSocket(); @@ -833,16 +789,16 @@ void ClientSocket::connect(const Ip &ip, int port) if(err < 0) { - fprintf(stderr, "In [%s::%s] Before END sock = %d, err = %d, errno = %d\n",__FILE__,__FUNCTION__,sock,err,WSAGetLastError()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Before END sock = %d, err = %d, errno = %d\n",__FILE__,__FUNCTION__,sock,err,WSAGetLastError()); //throwException(szBuf); disconnectSocket(); } - fprintf(stderr, "Valid recovery for connection sock = %d, err = %d, WSAGetLastError() = %d\n",sock,err,WSAGetLastError()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Valid recovery for connection sock = %d, err = %d, WSAGetLastError() = %d\n",sock,err,WSAGetLastError()); } else { - fprintf(stderr, "Connected to host [%s] on port = %d sock = %d err = %d", ip.getString().c_str(),port,err); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Connected to host [%s] on port = %d sock = %d err = %d", ip.getString().c_str(),port,err); } } diff --git a/source/shared_lib/sources/sound/openal/sound_player_openal.cpp b/source/shared_lib/sources/sound/openal/sound_player_openal.cpp index a5bc09000..e930ada5b 100644 --- a/source/shared_lib/sources/sound/openal/sound_player_openal.cpp +++ b/source/shared_lib/sources/sound/openal/sound_player_openal.cpp @@ -88,17 +88,17 @@ ALenum SoundSource::getFormat(Sound* sound) else if(sound->getInfo()->getBitsPerSample() == 8) return AL_FORMAT_STEREO8; else - throw std::runtime_error("Sample format not supported"); + throw std::runtime_error("[1] Sample format not supported in file: " + sound->getFileName()); } else if(sound->getInfo()->getChannels() == 1) { if(sound->getInfo()->getBitsPerSample() == 16) return AL_FORMAT_MONO16; else if(sound->getInfo()->getBitsPerSample() == 8) return AL_FORMAT_MONO8; else - throw std::runtime_error("Sample format not supported"); + throw std::runtime_error("[2] Sample format not supported in file: " + sound->getFileName()); } - throw std::runtime_error("Sample format not supported"); + throw std::runtime_error("[3] Sample format not supported in file: " + sound->getFileName()); } //--------------------------------------------------------------------------- @@ -218,10 +218,9 @@ void StreamSoundSource::update() // we might have to restart the source if we had a buffer underrun if(!playing()) { - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d] Restarting audio source because of buffer underrun.\n",__FILE__,__FUNCTION__,__LINE__); - std::cerr - << "Restarting audio source because of buffer underrun.\n"; + std::cerr << "Restarting audio source because of buffer underrun.\n"; alSourcePlay(source); SoundPlayerOpenAL::checkAlError("Couldn't restart audio source: "); } @@ -260,8 +259,7 @@ bool StreamSoundSource::fillBufferAndQueue(ALuint buffer) int8* bufferdata = new int8[STREAMFRAGMENTSIZE]; uint32 bytesread = 0; do { - bytesread += sound->read(bufferdata + bytesread, - STREAMFRAGMENTSIZE - bytesread); + bytesread += sound->read(bufferdata + bytesread,STREAMFRAGMENTSIZE - bytesread); if(bytesread < STREAMFRAGMENTSIZE) { StrSound* next = sound->getNext(); if(next == 0) diff --git a/source/shared_lib/sources/sound/sound.cpp b/source/shared_lib/sources/sound/sound.cpp new file mode 100644 index 000000000..56bb0c984 --- /dev/null +++ b/source/shared_lib/sources/sound/sound.cpp @@ -0,0 +1,128 @@ +// ============================================================== +// This file is part of Glest Shared Library (www.glest.org) +// +// Copyright (C) 2001-2008 Martio Figueroa +// +// You can redistribute this code 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 "sound.h" + +#include +#include + +#include "leak_dumper.h" + +namespace Shared{ namespace Sound{ +// ===================================================== +// class SoundInfo +// ===================================================== + +SoundInfo::SoundInfo(){ + channels= 0; + samplesPerSecond= 0; + bitsPerSample= 0; + size= 0; +} + +// ===================================================== +// class Sound +// ===================================================== + +Sound::Sound(){ + volume= 0.0f; + fileName = ""; +} + +// ===================================================== +// class StaticSound +// ===================================================== + +StaticSound::StaticSound(){ + samples= NULL; + soundFileLoader = NULL; + fileName = ""; +} + +StaticSound::~StaticSound(){ + close(); +} + +void StaticSound::close(){ + if(samples != NULL) { + delete [] samples; + samples = NULL; + } + + if(soundFileLoader!=NULL){ + soundFileLoader->close(); + delete soundFileLoader; + soundFileLoader= NULL; + } +} + +void StaticSound::load(const string &path){ + close(); + + fileName = path; + + string ext= path.substr(path.find_last_of('.')+1); + soundFileLoader= SoundFileLoaderFactory::getInstance()->newInstance(ext); + + soundFileLoader->open(path, &info); + samples= new int8[info.getSize()]; + soundFileLoader->read(samples, info.getSize()); + soundFileLoader->close(); + + if(soundFileLoader!=NULL){ + soundFileLoader->close(); + delete soundFileLoader; + soundFileLoader= NULL; + } +} + +// ===================================================== +// class StrSound +// ===================================================== + +StrSound::StrSound(){ + soundFileLoader= NULL; + next= NULL; + fileName = ""; +} + +StrSound::~StrSound(){ + close(); +} + +void StrSound::open(const string &path){ + close(); + + fileName = path; + + string ext= path.substr(path.find_last_of('.')+1); + + soundFileLoader= SoundFileLoaderFactory::getInstance()->newInstance(ext); + soundFileLoader->open(path, &info); +} + +uint32 StrSound::read(int8 *samples, uint32 size){ + return soundFileLoader->read(samples, size); +} + +void StrSound::close(){ + if(soundFileLoader!=NULL){ + soundFileLoader->close(); + delete soundFileLoader; + soundFileLoader= NULL; + } +} + +void StrSound::restart(){ + soundFileLoader->restart(); +} + +}}//end namespace diff --git a/source/shared_lib/sources/sound/sound_file_loader.cpp b/source/shared_lib/sources/sound/sound_file_loader.cpp new file mode 100644 index 000000000..a6bf73fea --- /dev/null +++ b/source/shared_lib/sources/sound/sound_file_loader.cpp @@ -0,0 +1,202 @@ +// ============================================================== +// This file is part of Glest Shared Library (www.glest.org) +// +// Copyright (C) 2001-2008 Martio Figueroa +// +// You can redistribute this code 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 "sound_file_loader.h" + +#include +#include + +#include "sound.h" +#include "leak_dumper.h" + +using namespace Shared::Platform; +using namespace std; + +namespace Shared{ namespace Sound{ + +// ===================================================== +// class WavSoundFileLoader +// ===================================================== + +void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){ + char chunkId[]={'-', '-', '-', '-', '\0'}; + uint32 size32= 0; + uint16 size16= 0; + int count; + fileName = path; + + f.open(path.c_str(), ios_base::in | ios_base::binary); + + if(!f.is_open()){ + throw runtime_error("Error opening wav file: "+ string(path)); + } + + //RIFF chunk - Id + f.read(chunkId, 4); + + if(strcmp(chunkId, "RIFF")!=0){ + throw runtime_error("Not a valid wav file (first four bytes are not RIFF):" + path); + } + + //RIFF chunk - Size + f.read((char*) &size32, 4); + + //RIFF chunk - Data (WAVE string) + f.read(chunkId, 4); + + if(strcmp(chunkId, "WAVE")!=0){ + throw runtime_error("Not a valid wav file (wave data don't start by WAVE): " + path); + } + + // === HEADER === + + //first sub-chunk (header) - Id + f.read(chunkId, 4); + + if(strcmp(chunkId, "fmt ")!=0){ + throw runtime_error("Not a valid wav file (first sub-chunk Id is not fmt): "+ path); + } + + //first sub-chunk (header) - Size + f.read((char*) &size32, 4); + + //first sub-chunk (header) - Data (encoding type) - Ignore + f.read((char*) &size16, 2); + + //first sub-chunk (header) - Data (nChannels) + f.read((char*) &size16, 2); + soundInfo->setChannels(size16); + + //first sub-chunk (header) - Data (nsamplesPerSecond) + f.read((char*) &size32, 4); + soundInfo->setsamplesPerSecond(size32); + + //first sub-chunk (header) - Data (nAvgBytesPerSec) - Ignore + f.read((char*) &size32, 4); + + //first sub-chunk (header) - Data (blockAlign) - Ignore + f.read((char*) &size16, 2); + + //first sub-chunk (header) - Data (nsamplesPerSecond) + f.read((char*) &size16, 2); + soundInfo->setBitsPerSample(size16); + + if (soundInfo->getBitsPerSample() != 8 && soundInfo->getBitsPerSample()!=16){ + throw runtime_error("Bits per sample must be 8 or 16: " + path); + } + bytesPerSecond= soundInfo->getBitsPerSample()*8*soundInfo->getSamplesPerSecond()*soundInfo->getChannels(); + + count=0; + do{ + count++; + + // === DATA === + //second sub-chunk (samples) - Id + f.read(chunkId, 4); + if(strncmp(chunkId, "data", 4)!=0){ + continue; + } + + //second sub-chunk (samples) - Size + f.read((char*) &size32, 4); + dataSize= size32; + soundInfo->setSize(dataSize); + } + while(strncmp(chunkId, "data", 4)!=0 && count (samples), size); + return f.gcount(); +} + +void WavSoundFileLoader::close(){ + f.close(); +} + +void WavSoundFileLoader::restart(){ + f.seekg(dataOffset, ios_base::beg); +} + +// ======================================= +// Ogg Sound File Loader +// ======================================= + +void OggSoundFileLoader::open(const string &path, SoundInfo *soundInfo){ + fileName = path; + + f= fopen(path.c_str(), "rb"); + if(f==NULL){ + throw runtime_error("Can't open ogg file: "+path); + } + + vf= new OggVorbis_File(); + ov_open(f, vf, NULL, 0); + + vorbis_info *vi= ov_info(vf, -1); + + soundInfo->setChannels(vi->channels); + soundInfo->setsamplesPerSecond(vi->rate); + soundInfo->setBitsPerSample(16); + soundInfo->setSize(static_cast(ov_pcm_total(vf, -1))*2); +} + +uint32 OggSoundFileLoader::read(int8 *samples, uint32 size){ + int section; + int totalBytesRead= 0; + + while(size>0){ + int bytesRead= ov_read(vf, reinterpret_cast (samples), size, + 0, 2, 1, §ion); + if(bytesRead==0){ + break; + } + size-= bytesRead; + samples+= bytesRead; + totalBytesRead+= bytesRead; + } + return totalBytesRead; +} + +void OggSoundFileLoader::close(){ + if(vf!=NULL){ + ov_clear(vf); + delete vf; + vf= 0; + } +} + +void OggSoundFileLoader::restart(){ + ov_raw_seek(vf, 0); +} + +// ===================================================== +// class SoundFileLoaderFactory +// ===================================================== + +SoundFileLoaderFactory::SoundFileLoaderFactory(){ + registerClass("wav"); + registerClass("ogg"); +} + +SoundFileLoaderFactory *SoundFileLoaderFactory::getInstance(){ + static SoundFileLoaderFactory soundFileLoaderFactory; + return &soundFileLoaderFactory; +} + +}}//end namespace