our_dick/include/gfx/ogl/buffer_map.tpp
2022-02-10 16:56:01 -08:00

92 lines
2.6 KiB
C++

/**
This file is a part of our_dick
Copyright (C) 2020-2022 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_GRAPHICS_OGL_BUFFER_MAP_TPP
#define OUR_DICK_GRAPHICS_OGL_BUFFER_MAP_TPP
#include <utility> //exchange, swap
#include "gl_include.hpp"
namespace gfx::ogl{
template<typename T>
scoped_buffer_map<T>::scoped_buffer_map(GLuint bid, buffer::maptype m):
m_buffer(bid)
{
m_data = reinterpret_cast<pointer>(glMapNamedBuffer(m_buffer, static_cast<GLenum>(m)));
}
template<typename T>
scoped_buffer_map<T>::scoped_buffer_map(scoped_buffer_map&& m):
m_data(std::exchange(m.m_data, nullptr)),
m_buffer(std::exchange(m.m_buffer, 0)){}
template<typename T>
scoped_buffer_map<T>::~scoped_buffer_map(void){
if(m_data){
glUnmapNamedBuffer(m_buffer);
}
}
template<typename T>
scoped_buffer_map<T>& scoped_buffer_map<T>::operator=(scoped_buffer_map&& m){
std::swap(m_data, m.m_data);
std::swap(m_buffer, m.m_buffer);
return *this;
}
template<typename T>
auto scoped_buffer_map<T>::length(void)const -> size_type{
GLint64 retval;
glGetNamedBufferParameteri64v(m_buffer, GL_BUFFER_MAP_LENGTH, &retval);
return retval;
}
template<typename T>
auto scoped_buffer_map<T>::release(void) -> pointer{
m_buffer = 0;
return std::exchange(m_data, nullptr);
}
template<typename T>
scoped_buffer_map<T>::operator pointer(void){
return m_data;
}
template<typename T>
scoped_buffer_map<T>::operator const_pointer(void)const{
return m_data;
}
template<typename T>
auto scoped_buffer_map<T>::raw(void) -> pointer{
return m_data;
}
template<typename T>
auto scoped_buffer_map<T>::raw(void)const -> const_pointer{
return m_data;
}
template<typename T>
bool scoped_buffer_map<T>::valid(void)const{
return m_data;
}
template<typename T>
auto scoped_buffer_map<T>::operator[](size_type i) -> reference{
return m_data[i];
}
template<typename T>
auto scoped_buffer_map<T>::operator[](size_type i)const -> const_reference{
return m_data[i];
}
}
#endif