Commit c851a7b2 authored by gerstrong@gmail.com's avatar gerstrong@gmail.com

More cleanups

parent 3de8e5e3
......@@ -6,7 +6,7 @@
*/
#include <SDL.h>
#include <stdio.h>
//#include <cstdio>
#include "InputEvents.h"
#include "CInput.h"
......@@ -19,12 +19,6 @@
#include "fileio/CConfiguration.h"
// Input Events
bool pollLocked = false;
SDL_sem *pollSem = nullptr;
#if defined(CAANOO) || defined(WIZ) || defined(GP2X)
#include "sys/wizgp2x.h"
#endif
......@@ -33,6 +27,11 @@ SDL_sem *pollSem = nullptr;
#define MOUSEWRAPPER 1
#endif
CInput::~CInput()
{
SDL_DestroySemaphore(mpPollSem);
}
CInput::CInput()
{
#if defined(WIZ) || defined(GP2X)
......@@ -52,9 +51,7 @@ CInput::CInput()
startJoyDriver(); // not for iPhone for now, could cause trouble (unwanted input events)
#endif
//Create the semaphor
pollSem = SDL_CreateSemaphore(1);
mpPollSem = SDL_CreateSemaphore(1);
}
/**
......@@ -557,7 +554,7 @@ void CInput::waitForAnyInput()
while(1)
{
const float logicLatency = gTimer.LogicLatency();
const auto logicLatency = gTimer.LogicLatency();
curr = timerTicks();
......@@ -587,7 +584,7 @@ void CInput::waitForAnyInput()
elapsed = timerTicks() - start;
int waitTime = logicLatency - elapsed;
const auto waitTime = int(logicLatency - elapsed);
// wait time remaining in current loop
if( waitTime > 0 )
......@@ -680,7 +677,7 @@ void CInput::flushFingerEvents()
void CInput::pollEvents()
{
// Semaphore
SDL_SemWait( pollSem );
SDL_SemWait( mpPollSem );
#ifdef VIRTUALPAD
if(mpVirtPad && mVPadConfigState)
......@@ -693,7 +690,7 @@ void CInput::pollEvents()
if(remapper.mappingInput)
{
readNewEvent();
SDL_SemPost( pollSem );
SDL_SemPost( mpPollSem );
return;
}
......@@ -1010,7 +1007,7 @@ void CInput::pollEvents()
WIZ_AdjustVolume( volume_direction );
#endif
SDL_SemPost( pollSem );
SDL_SemPost( mpPollSem );
}
/**
......@@ -1917,11 +1914,11 @@ void CInput::shutdown()
bool CInput::readSDLEventVec(std::vector<SDL_Event> &evVec)
{
SDL_SemWait( pollSem );
SDL_SemWait( mpPollSem );
if(mSDLEventVec.empty())
{
SDL_SemPost( pollSem );
SDL_SemPost( mpPollSem );
return false;
}
......@@ -1929,18 +1926,18 @@ bool CInput::readSDLEventVec(std::vector<SDL_Event> &evVec)
mSDLEventVec.clear();
SDL_SemPost( pollSem );
SDL_SemPost( mpPollSem );
return true;
}
void CInput::pushBackButtonEventExtEng()
{
SDL_SemWait( pollSem );
SDL_SemWait( mpPollSem );
if(mBackEventBuffer.empty())
{
SDL_SemPost( pollSem );
SDL_SemPost( mpPollSem );
return;
}
......@@ -1956,6 +1953,6 @@ void CInput::pushBackButtonEventExtEng()
mBackEventBuffer.clear();
SDL_SemPost( pollSem );
SDL_SemPost( mpPollSem );
}
......@@ -160,6 +160,8 @@ class CInput : public GsSingleton<CInput>
public:
CInput();
~CInput();
/**
* @brief transMouseRelCoord transforms a mouse click from the screen coordinates to the relative coordinates
* @param Pos Resulting relative coordinate to handle
......@@ -311,6 +313,16 @@ public:
private:
bool processKeys(int value);
void processJoystickAxis();
void processJoystickHat();
void processJoystickButton(int value);
void processMouse();
void processMouse(SDL_Event& ev);
void processMouse(int x, int y, bool down, int index);
// Input Events
CEventContainer m_EventList;
......@@ -348,22 +360,13 @@ private:
struct rm_type
{
rm_type():
mappingInput(false) {}
// For mapping new Commands
bool mappingInput;
Uint8 mapDevice;
int mapPosition;
bool mappingInput = false;
Uint8 mapDevice = 0;
int mapPosition = 0;
} remapper;
bool processKeys(int value);
void processJoystickAxis();
void processJoystickHat();
void processJoystickButton(int value);
void processMouse();
void processMouse(SDL_Event& ev);
void processMouse(int x, int y, bool down, int index);
SDL_sem *mpPollSem = nullptr;
};
......
......@@ -6,7 +6,7 @@
#include <set>
class stInputCommand;
struct stInputCommand;
class TouchButton
{
......
......@@ -273,16 +273,12 @@ but it is getting deprecated and will be removed in future.
Boost libraries. I plan to remove that in future, but right now we need it.
libogg and libvorbis. I also plan to remove those since SDL2_mixer does the job.
Curl and Zlib, so you can fully use the Game store CG provides.
How to compile it under Ubuntu:
* Install through Synaptic or "sudo apt install" the following packages:
Common dependencies:
- build-essential
- libvorbis-dev
- libogg-dev
- libgl1-mesa-dev
- libcurl4-openssl-dev
- zlib1g-dev
......@@ -311,7 +307,7 @@ How to compile it under Fedora:
dnf install -y \
SDL2-devel cmake cmake-data gcc-c++ python3-devel \
boost-devel mesa-libGL-devel libogg-devel libvorbis-devel libcurl-devel \
boost-devel mesa-libGL-devel libcurl-devel \
SDL2_image-devel SDL2_mixer-devel
* Extract my source code into the "CGeniusSrc" directory.
......@@ -331,7 +327,7 @@ Compiling CG on any Linux/UNIX (other than Ubuntu or Fedora):
* If you want to compile the game, you need to have all the standard
development libraries and GNU C Compiler (at least version 4.8) installed
on your system.
* You need to install the "vorbis", "ogg", "SDL", "curl", "boost" and
* You need to install the "SDL_mixer", "SDL_image", "SDL", "curl", "boost" and
"Mesa/OpenGL" development libraries to get it to compile successfully.
* Extract my source code into the "CGeniusSrc" directory .
* cd CGeniusSrc
......
......@@ -65,7 +65,7 @@ IF(CREATE_DEBS)
ELSEIF(BUILD_ARCH STREQUAL i386)
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE i386)
ENDIF(BUILD_ARCH STREQUAL amd64)
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libvorbis0a (>= 1.2.3), libvorbisenc2 (>> 1.2.3), libvorbisfile3 (>= 1.2.3), libsdl2-2.0-0 (>= 2.0.0), libgl1-mesa-glx (>= 7.7.1), libsdl2-image-2.0-0 (>= 2.0), libsdl2-mixer-2.0-0 (>= 2.0), libpython3.5 (>= 3.5.2)")
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libsdl2-2.0-0 (>= 2.0.0), libgl1-mesa-glx (>= 7.7.1), libsdl2-image-2.0-0 (>= 2.0), libsdl2-mixer-2.0-0 (>= 2.0), libpython3.5 (>= 3.5.2)")
SET(CPACK_DEBIAN_PACKAGE_SECTION "games")
SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}")
ENDIF(WIN32)
......@@ -91,7 +91,7 @@ IF(CREATE_RPMS)
SET(CPACK_RPM_PACKAGE_GROUP "Amusements/Games")
SET(CPACK_RPM_PACKAGE_VENDOR "${CPACK_PACKAGE_VENDOR}")
SET(CPACK_RPM_PACKAGE_DESCRIPTION "${CPACK_PACKAGE_DESCRIPTION_SUMMARY}\n .\n ${CPACK_PACKAGE_DESCRIPTION}")
SET(CPACK_RPM_PACKAGE_REQUIRES "SDL >= 2.0, libvorbis >= 1.3, mesa-libGL >= 7.7")
SET(CPACK_RPM_PACKAGE_REQUIRES "SDL >= 2.0, SDL_image >= 2.0, SDL_mixer >= 2.0, mesa-libGL >= 7.7")
ENDIF(WIN32)
ENDIF(CREATE_RPMS)
......
......@@ -11,9 +11,6 @@ endif()
OPTION(DOWNLOADER "Internal Downloader" Yes)
#OPTION(OGG "Ogg/Vorbis support" Yes)
#OPTION(TREMOR "Tremor support" No)
IF(WIN32)
SET(BUILD_TARGET WIN32)
......
......@@ -70,9 +70,8 @@ mp_levels_completed(player.mp_levels_completed)
m_playingmode = WORLDMAP;
playpushed_decreasetimer = 0;
inventory = stInventory();
// Set every value in the class to zero.
memset(&inventory, 0, sizeof(stInventory));
setDefaultStartValues();
setDatatoZero();
pinertia_y = 0;
......@@ -95,7 +94,7 @@ CPlayer& CPlayer::operator=(const CPlayer &player)
// Set every value in the class to zero.
memset(&inventory, 0, sizeof(stInventory));
inventory = stInventory();
setDefaultStartValues();
setDatatoZero();
......
......@@ -4,25 +4,7 @@ include_directories(${SDL_INCLUDE_DIR})
add_library(sdl_extensions_audio_music CIMFPlayer.cpp CIMFPlayer.h
CMusic.cpp CMusic.h
CMusicPlayer.cpp CMusicPlayer.h
COGGPlayer.cpp COGGPlayer.h)
# TODO: Does not work with MacOS. Where is the pkgconfig or cmake script
IF(OGG)
if(WIN32)
TARGET_LINK_LIBRARIES(sdl_extensions_audio_music vorbisfile vorbis ogg)
else(WIN32)
TARGET_LINK_LIBRARIES(sdl_extensions_audio_music vorbis vorbisfile)
endif(WIN32)
ENDIF(OGG)
IF(TREMOR)
TARGET_LINK_LIBRARIES(sdl_extensions_audio_music vorbisidec)
ENDIF(TREMOR)
CMusicPlayer.cpp CMusicPlayer.h)
if(USE_SDL2)
LINK_DIRECTORIES(${SDL_MIXER_LIBRARY_DIRS})
......
/*
* COGGPlayer.cpp
*
* Created on: 17.02.2011
* Author: Gerstrong
* NOTE: if you use SDL_Mixer this is not required!
*/
#if defined(OGG) || defined(TREMOR)
#include "COGGPlayer.h"
#include <base/utils/FindFile.h>
#include "sdl/audio/base/Sampling.h"
#include "fileio/ResourceMgmt.h"
#include <base/GsLogging.h>
#include "fileio/KeenFiles.h"
COGGPlayer::COGGPlayer() :
m_pcm_size(0),
m_music_pos(0),
m_bitStream(0)
{
m_Audio_cvt.buf = nullptr;
}
COGGPlayer::COGGPlayer(const std::string& filename) :
m_filename(filename),
m_pcm_size(0),
m_music_pos(0),
m_bitStream(0)
{
m_Audio_cvt.buf = nullptr;
}
COGGPlayer::~COGGPlayer()
{
if(!m_filename.empty())
{
close(true);
}
}
#if defined(TREMOR)
int ov_fopen(char *path,OggVorbis_File *vf)
{
int result;
FILE *fp = fopen(path, "rb");
if((result = ov_open(fp, vf, NULL, 0)) < 0)
fclose(fp);
return result;
}
#endif
bool COGGPlayer::loadMusicFromFile(const std::string& filename)
{
m_filename = getResourceFilename(JoinPaths("music", filename), gKeenFiles.gameDir, false, false);
if(m_filename.empty())
{
m_filename = filename;
if(m_filename.empty())
{
return false;
}
}
return true;
}
bool COGGPlayer::open(const bool lock)
{
if(m_Audio_cvt.buf)
{
close(lock);
}
if(lock) SDL_LockAudio();
auto &audioSpec = gSound.getAudioSpec();
// If Ogg detected, decode it into the stream psound->sound_buffer.
// It must fit into the Audio_cvt structure, so that it can be converted
mHasCommonFreqBase = true;
if(ov_fopen((char*)GetFullFileName(m_filename).c_str(), &m_oggStream) != 0)
{
if(lock) SDL_UnlockAudio();
return false;
}
mVorbisInfo = ov_info(&m_oggStream, -1);
ov_comment(&m_oggStream, -1);
m_AudioFileSpec.format = AUDIO_S16LSB; // Ogg Audio seems to always use this format
m_AudioFileSpec.channels = mVorbisInfo->channels;
m_AudioFileSpec.freq = mVorbisInfo->rate;
// Since I cannot convert with a proper quality from 44100 to 48000 Ogg wave output
// we set m_AudioFileSpec frequency to the same as the one of the SDL initialized AudioSpec
// scale just the buffer using readOGGStreamAndResample.
// This is base problem, but we have workarounds for that...
if( (m_AudioFileSpec.freq%audioSpec.freq != 0) &&
(audioSpec.freq%m_AudioFileSpec.freq != 0) )
{
#if SDL_VERSION_ATLEAST(2, 0, 0)
m_AudioFileSpec.freq = audioSpec.freq;
#endif
mHasCommonFreqBase = false;
}
m_pcm_size = ov_pcm_total(&m_oggStream,-1);
m_pcm_size *= (mVorbisInfo->channels*sizeof(Sint16));
m_music_pos = 0;
gLogging.ftextOut("OGG-Player: File \"%s\" has been opened successfully!<br>", m_filename.c_str());
int ret = SDL_BuildAudioCVT(&m_Audio_cvt,
m_AudioFileSpec.format, m_AudioFileSpec.channels, m_AudioFileSpec.freq,
audioSpec.format, audioSpec.channels, audioSpec.freq);
if(ret == -1)
{
if(lock) SDL_UnlockAudio();
return false;
}
const size_t length = audioSpec.size;
#if SDL_VERSION_ATLEAST(2, 0, 0)
#else
m_Audio_cvt.len_cvt = 0;
#endif
m_Audio_cvt.len = (length)/m_Audio_cvt.len_ratio;
m_Audio_cvt.len = (m_Audio_cvt.len>>2)<<2;
m_Audio_cvt.buf = new Uint8[m_Audio_cvt.len*m_Audio_cvt.len_mult];
if(lock) SDL_UnlockAudio();
return true;
}
bool COGGPlayer::loadMusicTrack(const int track)
{
m_filename = "slot" + itoa(track) + ".ogg";
m_filename = getResourceFilename(JoinPaths("music", m_filename), gKeenFiles.gameDir, false, false);
if(m_filename.empty())
return false;
return open(true);
}
bool COGGPlayer::readOGGStream(char *buffer, const size_t &size, const SDL_AudioSpec &OGGAudioSpec )
{
long bytes = 0;
unsigned long pos = 0;
while( pos<size )
{
if(m_pcm_size<=0)
break;
// Read up to a buffer's worth of decoded sound data
#if defined(OGG)
bytes = ov_read(&m_oggStream, buffer+pos, size-pos, 0, 2, 1, &m_bitStream);
#elif defined(TREMOR)
bytes = ov_read(&m_oggStream, buffer+pos, size-pos, &m_bitStream);
#endif
pos += bytes;
m_music_pos += bytes;
if( bytes <= 0 || m_music_pos >= m_pcm_size )
{
memset( buffer+pos, OGGAudioSpec.silence, size-pos );
pos = size;
m_bitStream = 0;
m_music_pos = 0;
return true;
}
}
return false;
}
bool COGGPlayer::readOGGStreamAndResample( Uint8 *buffer,
const int output_size,
const size_t input_size,
const SDL_AudioSpec &OGGAudioSpec )
{
mResampleBuf.resize(input_size);
// In case format has not been converted yet...
if(output_size<0)
return false;
bool eof = readOGGStream( reinterpret_cast<char*>(mResampleBuf.data()), input_size, OGGAudioSpec );
resample( buffer, mResampleBuf.data(), output_size, input_size, OGGAudioSpec.format, OGGAudioSpec.channels);
return eof;
}
void COGGPlayer::readBuffer(Uint8* buffer, Uint32 length)
{
if(!m_playing || !m_Audio_cvt.buf)
return;
auto &audioSpec = gSound.getAudioSpec();
auto freq = audioSpec.freq;
bool rewind = false;
// read the ogg stream
if( !mHasCommonFreqBase )
{
Uint64 insize = (m_Audio_cvt.len*mVorbisInfo->rate)/freq;
Uint8 mult = m_AudioFileSpec.channels;
if(m_AudioFileSpec.format == AUDIO_S16)
mult <<= 1;
insize /= mult;
insize++;
insize *= mult;
rewind = readOGGStreamAndResample(m_Audio_cvt.buf,
m_Audio_cvt.len_cvt,
insize,
m_AudioFileSpec);
}
else
{
rewind = readOGGStream(reinterpret_cast<char*>(m_Audio_cvt.buf),
m_Audio_cvt.len,
m_AudioFileSpec);
}
if(m_Audio_cvt.buf == nullptr)
{
close(false);
open(false);
play(true);
return;
}
// then convert it into SDL Audio buffer
// Conversion to SDL Format
SDL_ConvertAudio(&m_Audio_cvt);
memcpy(buffer, m_Audio_cvt.buf, length);
if(rewind)
{
close(false);
open(false);
play(true);
}
}
void COGGPlayer::close(const bool lock)
{
if(lock) SDL_LockAudio();
if(m_Audio_cvt.buf)
{
delete [] m_Audio_cvt.buf;
}
m_Audio_cvt.buf = nullptr;
m_playing = false;
m_music_pos = 0;
m_pcm_size = 0;
ov_clear(&m_oggStream);
if(lock) SDL_UnlockAudio();
}
#endif
/*
* COGGPlayer.h
*
* Created on: 17.02.2011
* Author: gerstrong
*/
#ifndef COGGPLAYER_H_
#define COGGPLAYER_H_
#if defined(OGG) || defined(TREMOR)
#include <cstdio>
#include <iostream>
#include "CMusicPlayer.h"
#ifdef OGG
#define OV_EXCLUDE_STATIC_CALLBACKS // Will reduce some nasty warning, since we don't use those callbacks
#include <vorbisfile.h>
#elif defined TREMOR
#include <ivorbisfile.h>
#endif
#include <SDL.h>
#include <string>
#include <fileio/CExeFile.h>
class COGGPlayer : public CMusicPlayer
{
public:
COGGPlayer();
COGGPlayer(const std::string& filename);
virtual ~COGGPlayer();
bool loadMusicTrack(const int track) override;
bool loadMusicFromFile(const std::string& filename);
bool open(const bool lock);
void readBuffer(Uint8* buffer, Uint32 length);
void close(const bool lock);
private:
bool readOGGStream(char *buffer, const size_t &size, const SDL_AudioSpec &OGGAudioSpec );
bool readOGGStreamAndResample(Uint8 *buffer, const int output_size, const size_t input_size, const SDL_AudioSpec &OGGAudioSpec );
OggVorbis_File m_oggStream;
std::string m_filename;
SDL_AudioSpec m_AudioFileSpec;
SDL_AudioCVT m_Audio_cvt;
Uint32 m_pcm_size;
Uint32 m_music_pos;
int m_bitStream;
vorbis_info* mVorbisInfo; // some formatting data
bool mHasCommonFreqBase;
std::vector<Uint8> mResampleBuf;
};
#if defined(TREMOR)
int ov_fopen(char *path,OggVorbis_File *vf);
#endif
#endif
#endif /* COGGPLAYER_H_ */
......@@ -3,12 +3,3 @@ include_directories(${SDL_INCLUDE_DIR})
add_library(sdl_extensions_audio_sound CSoundChannel.cpp CSoundChannel.h
CSoundSlot.cpp CSoundSlot.h)
# TODO: Does not work with MacOS. Where is the pkgconfig or cmake script
IF(OGG)
if(WIN32)
TARGET_LINK_LIBRARIES(sdl_extensions_audio_sound vorbisfile vorbis ogg)
else(WIN32)
TARGET_LINK_LIBRARIES(sdl_extensions_audio_sound vorbis vorbisfile)
endif(WIN32)
ENDIF(OGG)
......@@ -46,9 +46,6 @@ public:
private:
std::vector<byte> mSounddata;
unsigned int m_soundlength = 0;
bool mHasCommonFreqBase;
int mOggFreq;
Mix_Chunk *mpWaveChunk = nullptr;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment