//! [static] factory Source * Source::create() { checkErrorStatus(__FILE__, __LINE__); uint32_t sourceId; alGenSources( 1, &sourceId ); if(!checkErrorStatus(__FILE__, __LINE__, "Could not create Source.") || sourceId==0 ){ return nullptr; } return new Source(sourceId); }
void Source::dequeueBuffers(int num){ checkErrorStatus(__FILE__, __LINE__); auto removedBuffers=new ALuint[num]; alSourceUnqueueBuffers(sourceId,num,removedBuffers); delete []removedBuffers; if(checkErrorStatus(__FILE__, __LINE__, "Source::dequeueBuffers")){ // successful? buffers.erase(buffers.begin(),buffers.begin()+num); } }
//! [static] bool initSoundSystem(){ if(myContext != nullptr) { // already initialized? return true; } // Open the default device myDevice = alcOpenDevice(nullptr); checkALCErrorStatus(__FILE__, __LINE__, myDevice); if(myDevice == nullptr) { return false; } // Create Context myContext = alcCreateContext(myDevice, nullptr); checkALCErrorStatus(__FILE__, __LINE__, myDevice); if(myContext == nullptr) { return false; } alcMakeContextCurrent(myContext); checkALCErrorStatus(__FILE__, __LINE__, myDevice); if(!checkErrorStatus(__FILE__, __LINE__, "initSoundSystem()")){ shutDownSoundSystem(); return false; } return true; }
//! [static] factory Buffer * createSilence(unsigned int freq,unsigned int size) { Util::Reference<Buffer> b(Buffer::create()); if(b.isNull()) return nullptr; const std::vector<uint8_t> data(size, 128); b->setData(AL_FORMAT_MONO8, data.data(), data.size(), freq); return checkErrorStatus(__FILE__, __LINE__) ? b.detachAndDecrease() : nullptr; }
int ShaderManager::compileShaders(GLuint type, std::string &directory) { //Open the file std::ifstream file; file.open(directory.c_str(), std::ios::in); if(!file) { std::cout << directory + " could not be opened, no shaders loaded!" << std::endl; return -1; } std::string filename; GLuint shaderObject; GLchar *source; int len; //Opened, now read in the type and file name while(file.good()) { //Load files into memory file >> filename; directory = project_mode_string + "Shaders\\" + filename; std::cout << "--------------------------------------------" << std::endl; std::cout << filename << ":" << std::endl; int err = loadFile(type, directory.c_str(), &source, &len); if(err) continue; //create shader objects shaderObject = glCreateShader(type); //Could be a vertex or fragment shader, it uses the TYPE //CONST STUFF so annoying const char *csource = source; const GLchar **p_source = &csource; glShaderSource(shaderObject, 1, p_source, &len); //Compile shader file glCompileShader(shaderObject); //Check if it compiled correctly err = checkErrorStatus(shaderObject, GL_COMPILE_STATUS); if(err) continue; std::cout << "Shader of type " << type << " compiled correctly: " << filename << std::endl; //Add it to the vector if(type == GL_VERTEX_SHADER) compiled_vertex_shaders.push_back(shaderObject); else compiled_fragment_shaders.push_back(shaderObject); } std::cout << "--------------------------------------------" << std::endl; file.close(); return 0; }
//! [static] factory Buffer * createNoise(unsigned int freq, unsigned int size) { Util::Reference<Buffer> b(Buffer::create()); if(b.isNull()) return nullptr; std::vector<uint8_t> noise; noise.reserve(size); unsigned int j = 1; for (unsigned int i = 0; i < size; ++i) { j = (j * 1234393 + i) % 0xffffff; noise.push_back(static_cast<uint8_t>(j) % 256); } b->setData(AL_FORMAT_MONO8, noise.data(), noise.size(),freq); return checkErrorStatus(__FILE__, __LINE__) ? b.detachAndDecrease() : nullptr; }
int ShaderManager::createPrograms() { std::string directory = project_mode_string + "programs.txt"; std::ifstream file; file.open(directory.c_str(), std::ios::in); if(!file) { std::cout << directory + " could not be opened, no shaders created!" << std::endl; return -1; } std::string prog_name; int v_shader, f_shader; while(file.good() && !file.eof()) { file >> prog_name; file >> v_shader; file >> f_shader; if(v_shader >= (int)(compiled_vertex_shaders.size()) || f_shader >= ((int)compiled_fragment_shaders.size())) { std::cout << "Could not link a program with values " << v_shader << ", " << f_shader << std::endl; continue; } GLuint program = glCreateProgram(); glAttachShader(program, compiled_vertex_shaders[v_shader]); glAttachShader(program, compiled_fragment_shaders[f_shader]); glLinkProgram(program); int err = checkErrorStatus(program, GL_LINK_STATUS); if(err) continue; std::cout << prog_name << " - " << v_shader << ", " << f_shader << " linked correctly" << std::endl; shader_programs[prog_name] = program; } for(unsigned i = 0; i < compiled_vertex_shaders.size(); i++) { glDeleteShader(compiled_vertex_shaders[i]); } for(unsigned i = 0; i < compiled_fragment_shaders.size(); i++) { glDeleteShader(compiled_fragment_shaders[i]); } compiled_vertex_shaders.clear(); compiled_fragment_shaders.clear(); return 0; }
//! [static] factory Buffer * createRectangleSound(unsigned int width, unsigned int freq, unsigned int size) { Util::Reference<Buffer> b(Buffer::create()); if(b.isNull()) return nullptr; std::vector<uint8_t> noise; noise.reserve(size); uint8_t value = 0; for (unsigned int i = 0; i < size; ++i) { if(i % width == 0) { value = 255 - value; } noise.push_back(value); } b->setData(AL_FORMAT_MONO8, noise.data(), noise.size(), freq); return checkErrorStatus(__FILE__, __LINE__) ? b.detachAndDecrease() : nullptr; }
//! [static] factory Buffer * loadWAV(const std::string & filename) { Util::Reference<Buffer> b(Buffer::create()); if(b.isNull()) return nullptr; SDL_AudioSpec wav_spec; Uint32 wav_length; Uint8 *wav_buffer; if ( SDL_LoadWAV(filename.c_str(), &wav_spec, &wav_buffer, &wav_length) == nullptr ) { WARN("Could not open : "+ filename+ std::string( SDL_GetError()) ); return nullptr; } unsigned int format=0; unsigned int freq = static_cast<unsigned int>(wav_spec.freq); if (wav_spec.format == AUDIO_U8 ) { if (wav_spec.channels==1) format=AL_FORMAT_MONO8; else format=AL_FORMAT_STEREO8; } else if (wav_spec.format == AUDIO_S16SYS ) { if (wav_spec.channels==1) format=AL_FORMAT_MONO16; else format=AL_FORMAT_STEREO16; } if (format==0) { WARN("Uninplemented WAV-format. Please add conversion routine ;)."); return nullptr; } else b->setData(format,wav_buffer,wav_length,freq); // b->setData(AL_FORMAT_MONO8,noise,size,freq); SDL_FreeWAV(wav_buffer); return checkErrorStatus(__FILE__, __LINE__) ? b.detachAndDecrease() : nullptr; }