moans merge.

This commit is contained in:
r0nk 2020-08-15 16:33:46 -05:00
commit ba07a9c4a4
7 changed files with 471 additions and 228 deletions

View File

@ -75,15 +75,9 @@ namespace math::detail{
protected: protected:
T* m_data = nullptr; T* m_data = nullptr;
public: public:
constexpr mat_ref_obj(T* d, size_type i): constexpr mat_ref_obj(T* d, size_type i);
m_data(d+i){} constexpr T& operator[](size_type i);
constexpr const T& operator[](size_type i)const;
constexpr T& operator[](size_type i){
return m_data[i*R];
}
constexpr const T& operator[](size_type i)const{
return m_data[i*R];
}
}; };
template<typename T, size_t W, size_t H> template<typename T, size_t W, size_t H>
@ -107,102 +101,58 @@ namespace math::detail{
protected: protected:
template<size_type... Ss> template<size_type... Ss>
constexpr matrix_base(std::integer_sequence<size_type,Ss...>): constexpr matrix_base(std::integer_sequence<size_type,Ss...>);
m_data{Ss...}{}
public: public:
//Default construct as identity when square, zero otherwise //Default construct as identity when square, zero otherwise
constexpr matrix_base(): constexpr matrix_base();
matrix_base(typename detail::default_initialization_matrix<Columns,Rows>::tuple{}){}
//Range initializing constructors //Range initializing constructors
constexpr explicit matrix_base(zero_initialize_t): constexpr explicit matrix_base(detail::zero_initialize_t);
m_data{}{} constexpr explicit matrix_base(detail::no_initialize_t);
constexpr explicit matrix_base(no_initialize_t){}
template<typename U = void> template<typename U = void>
constexpr explicit matrix_base(id_initialize_t): constexpr explicit matrix_base(detail::id_initialize_t);
matrix_base()
{
static_assert(Columns == Rows, "Identity initialization only supported on square matrices");
}
//Value initializing constructors //Value initializing constructors
constexpr explicit matrix_base(value_type v){ constexpr explicit matrix_base(value_type v);
for(size_type i = 0;i < Columns*Rows;++i)
m_data[i] = v;
}
template<typename... Args> template<typename... Args>
constexpr explicit matrix_base(Args&&... args): constexpr explicit matrix_base(Args&&... args);
m_data{std::forward<Args>(args)...}{}
//Copying constructors //Copying constructors
constexpr matrix_base(const matrix_base&) = default; constexpr matrix_base(const matrix_base&) = default;
constexpr matrix_base(matrix_base&&) = default; constexpr matrix_base(matrix_base&&) = default;
template<typename U> template<typename U>
constexpr matrix_base(const matrix_base<U,Columns,Rows>& m){ constexpr matrix_base(const matrix_base<U,Columns,Rows>& m);
using mat = decltype(m);
for(typename mat::size_type i = 0;i < mat::Columns*mat::Rows;++i)
m_data[i] = m.get(i);
}
~matrix_base() = default; ~matrix_base() = default;
//Assignement //Assignement
template<typename U> template<typename U>
constexpr matrix_base& operator=(const matrix_base<U,Columns,Rows>& m){ constexpr matrix_base& operator=(const matrix_base<U,Columns,Rows>& m);
using mat = decltype(m);
for(typename mat::size_type i = 0;i < mat::Columns*mat::Rows;++i)
m_data[i] = m.get(i);
return *this;
}
constexpr matrix_base& operator=(const matrix_base&) = default; constexpr matrix_base& operator=(const matrix_base&) = default;
constexpr matrix_base& operator=(matrix_base&&) = default; constexpr matrix_base& operator=(matrix_base&&) = default;
//Getters/Setters //Getters/Setters
constexpr auto operator[](size_type x){ constexpr auto operator[](size_type x);
return detail::mat_ref_obj<value_type,Rows>{m_data, x}; constexpr auto operator[](size_type x)const;
} constexpr reference get(size_type x, size_type y);
constexpr auto operator[](size_type x)const{ constexpr const_reference get(size_type x, size_type y)const;
return detail::mat_ref_obj<const value_type,Rows>{m_data, x}; constexpr reference get(size_type i);
} constexpr const_reference get(size_type i)const;
constexpr reference get(size_type x, size_type y){
return m_data[x+(y*Rows)];
}
constexpr const_reference get(size_type x, size_type y)const{
return m_data[x+(y*Rows)];
}
constexpr reference get(size_type i){
return m_data[i];
}
constexpr const_reference get(size_type i)const{
return m_data[i];
}
constexpr size_type columns()const{ constexpr size_type columns()const;
return Columns; constexpr size_type rows()const;
} constexpr size_type size()const;
constexpr size_type rows()const{
return Rows;
}
constexpr size_type size()const{
return Columns*Rows;
}
constexpr pointer raw(){ constexpr pointer raw();
return m_data; constexpr const_pointer raw()const;
} constexpr operator pointer();
constexpr const_pointer raw()const{ constexpr operator const_pointer()const;
return m_data;
}
constexpr operator pointer(){
return m_data;
}
constexpr operator const_pointer()const{
return m_data;
}
}; };
} }
#include "matrix.tpp"
#endif #endif

147
include/detail/matrix.tpp Normal file
View File

@ -0,0 +1,147 @@
/**
This file is a part of the rexy/r0nk/atlas project
Copyright (C) 2020 rexy712
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 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REXY_DETAIL_MATRIX_TPP
#define REXY_DETAIL_MATRIX_TPP
#include <cstdlib> //size_t
#include <utility> //integer_sequence
namespace math::detail{
template<typename T, size_t R>
constexpr mat_ref_obj<T,R>::mat_ref_obj(T* d, size_type i):
m_data(d+i){}
template<typename T, size_t R>
constexpr T& mat_ref_obj<T,R>::operator[](size_type i){
return m_data[i*R];
}
template<typename T, size_t R>
constexpr const T& mat_ref_obj<T,R>::operator[](size_type i)const{
return m_data[i*R];
}
template<typename T, size_t W, size_t H>
template<typename matrix_base<T,W,H>::size_type... Ss>
constexpr matrix_base<T,W,H>::matrix_base(std::integer_sequence<size_type,Ss...>):
m_data{Ss...}{}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::matrix_base():
matrix_base(typename detail::default_initialization_matrix<Columns,Rows>::tuple{}){}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::matrix_base(detail::zero_initialize_t):
m_data{}{}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::matrix_base(detail::no_initialize_t){}
template<typename T, size_t W, size_t H>
template<typename U>
constexpr matrix_base<T,W,H>::matrix_base(detail::id_initialize_t):
matrix_base()
{
static_assert(Columns == Rows, "Identity initialization only supported on square matrices");
}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::matrix_base(value_type v){
for(size_type i = 0;i < Columns*Rows;++i)
m_data[i] = v;
}
template<typename T, size_t W, size_t H>
template<typename... Args>
constexpr matrix_base<T,W,H>::matrix_base(Args&&... args):
m_data{std::forward<Args>(args)...}{}
template<typename T, size_t W, size_t H>
template<typename U>
constexpr matrix_base<T,W,H>::matrix_base(const matrix_base<U,Columns,Rows>& m){
using mat = decltype(m);
for(typename mat::size_type i = 0;i < mat::Columns*mat::Rows;++i)
m_data[i] = m.get(i);
}
template<typename T, size_t W, size_t H>
template<typename U>
constexpr matrix_base<T,W,H>& matrix_base<T,W,H>::operator=(const matrix_base<U,Columns,Rows>& m){
using mat = decltype(m);
for(typename mat::size_type i = 0;i < mat::Columns*mat::Rows;++i)
m_data[i] = m.get(i);
return *this;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::operator[](size_type x){
return detail::mat_ref_obj<value_type,Rows>{m_data, x};
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::operator[](size_type x)const{
return detail::mat_ref_obj<const value_type,Rows>{m_data, x};
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::get(size_type x, size_type y) -> reference{
return m_data[x+(y*Rows)];
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::get(size_type x, size_type y)const -> const_reference{
return m_data[x+(y*Rows)];
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::get(size_type i) -> reference{
return m_data[i];
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::get(size_type i)const -> const_reference{
return m_data[i];
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::columns()const -> size_type{
return Columns;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::rows()const -> size_type{
return Rows;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::size()const -> size_type{
return Columns*Rows;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::raw() -> pointer{
return m_data;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::raw()const -> const_pointer{
return m_data;
}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::operator pointer(){
return m_data;
}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::operator const_pointer()const{
return m_data;
}
}
#endif

View File

@ -21,8 +21,7 @@
#include <cstdlib> //size_t #include <cstdlib> //size_t
#include <utility> //integer_sequence #include <utility> //integer_sequence
#include <type_traits> //decay_t #include <type_traits> //decay_t, is_same, integral_constant
#include <cmath> //sin, cos
#include "detail/math.hpp" #include "detail/math.hpp"
#include "detail/matrix.hpp" #include "detail/matrix.hpp"
@ -61,21 +60,9 @@ namespace math{
using detail::matrix_base<T,3,3>::matrix_base; using detail::matrix_base<T,3,3>::matrix_base;
using detail::matrix_base<T,3,3>::operator=; using detail::matrix_base<T,3,3>::operator=;
template<typename U = void> static matrix rotation(value_type angle);
static constexpr matrix rotation(value_type angle){ static constexpr matrix rotation(value_type sin, value_type cos);
value_type c = std::cos(angle); static constexpr matrix rotation(value_type angle_x, value_type angle_y, value_type angle_z);
value_type s = std::sin(angle);
return rotation(s, c);
}
template<typename U = void>
static constexpr matrix rotation(value_type sin, value_type cos){
return matrix(cos, -sin, 0,
sin, cos, 0,
0, 0, 1);
}
static constexpr matrix rotation(value_type angle_x, value_type angle_y, value_type angle_z){
//TODO
}
}; };
namespace detail{ namespace detail{
@ -110,118 +97,40 @@ namespace math{
template<typename M1, typename M2> template<typename M1, typename M2>
using enable_if_eq_matrix = std::enable_if_t<are_same_size_matrix<M1,M2>::value,int>; using enable_if_eq_matrix = std::enable_if_t<are_same_size_matrix<M1,M2>::value,int>;
}
template<typename T, typename U, size_t W, size_t H> template<typename T, typename U, size_t W, size_t H>
constexpr bool operator==(const matrix<T,W,H>& left, const matrix<U,W,H> right){ constexpr bool operator==(const matrix<T,W,H>& left, const matrix<U,W,H> right);
for(size_t i = 0;i < left.size();++i){
if(left.get(i) != right.get(i))
return false;
}
return true;
}
template<typename T, typename U, size_t W, size_t H> template<typename T, typename U, size_t W, size_t H>
constexpr bool operator!=(const matrix<T,W,H>& left, const matrix<U,W,H> right){ constexpr bool operator!=(const matrix<T,W,H>& left, const matrix<U,W,H> right);
return !(left == right);
}
template<typename T, typename U, size_t R1, size_t C1, size_t C2> template<typename T, typename U, size_t R1, size_t C1, size_t C2>
constexpr auto operator*(const matrix<T,C1,R1>& left, const matrix<U,C2,C1>& right){ constexpr auto operator*(const matrix<T,C1,R1>& left, const matrix<U,C2,C1>& right);
using res_t = decltype(std::declval<T>() * std::declval<U>());
matrix<res_t,C2,R1> res(no_initialize);
size_t index = 0;
for(size_t i = 0;i < right.rows();++i){
for(size_t j = 0;j < left.rows();++j){
for(size_t k = 0;k < left.columns();++k){
res.get(index) += left[j][k] * right[i][k];
}
++index;
}
}
return res;
}
template<typename T, typename U, size_t C, size_t R> template<typename T, typename U, size_t C, size_t R>
constexpr auto operator*(const matrix<T,C,R>& left, U&& right){ constexpr auto operator*(const matrix<T,C,R>& left, U&& right);
using res_t = decltype(std::declval<T>() * std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) * std::forward<U>(right);
}
return res;
}
template<typename T, typename U, size_t C, size_t R> template<typename T, typename U, size_t C, size_t R>
constexpr auto operator/(const matrix<T,C,R>& left, U&& right){ constexpr auto operator/(const matrix<T,C,R>& left, U&& right);
using res_t = decltype(std::declval<T>() / std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) / std::forward<U>(right);
}
return res;
}
template<typename T, typename U, size_t C, size_t R> template<typename T, typename U, size_t C, size_t R>
constexpr auto operator+(const matrix<T,C,R>& left, const matrix<U,C,R>& right){ constexpr auto operator+(const matrix<T,C,R>& left, const matrix<U,C,R>& right);
using res_t = decltype(std::declval<T>() + std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) + right.get(i);
}
return res;
}
template<typename T, typename U, size_t C, size_t R> template<typename T, typename U, size_t C, size_t R>
constexpr auto operator-(const matrix<T,C,R>& left, const matrix<U,C,R>& right){ constexpr auto operator-(const matrix<T,C,R>& left, const matrix<U,C,R>& right);
using res_t = decltype(std::declval<T>() - std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) - right.get(i);
}
return res;
}
template<typename T, typename U, size_t C, size_t R> template<typename T, typename U, size_t C, size_t R>
constexpr auto operator-(const matrix<T,C,R>& left){ constexpr auto operator-(const matrix<T,C,R>& left);
using res_t = decltype(std::declval<T>() - std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = -left.get(i);
}
return res;
}
template<typename T, typename U, size_t R1, size_t C1, size_t C2> template<typename T, typename U, size_t R1, size_t C1, size_t C2>
constexpr decltype(auto) operator*=(matrix<T,C1,R1>& left, const matrix<U,C2,C1>& right){ constexpr decltype(auto) operator*=(matrix<T,C1,R1>& left, const matrix<U,C2,C1>& right);
//have to evaluate entire expression first since matrix multiplication depends on reusing many elements
//cannot be expression templatized, TODO
return (left = (left * right));
}
template<typename T, typename U, size_t C, size_t R> template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator*=(matrix<T,C,R>& left, U&& right){ constexpr decltype(auto) operator*=(matrix<T,C,R>& left, U&& right);
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) * std::forward<U>(right);
}
return left;
}
template<typename T, typename U, size_t C, size_t R> template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator/=(matrix<T,C,R>& left, U&& right){ constexpr decltype(auto) operator/=(matrix<T,C,R>& left, U&& right);
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) / std::forward<U>(right);
}
return left;
}
template<typename T, typename U, size_t C, size_t R> template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator+=(matrix<T,C,R>& left, const matrix<U,C,R>& right){ constexpr decltype(auto) operator+=(matrix<T,C,R>& left, const matrix<U,C,R>& right);
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) + right.get(i);
}
return left;
}
template<typename T, typename U, size_t C, size_t R> template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator-=(matrix<T,C,R>& left, const matrix<U,C,R>& right){ constexpr decltype(auto) operator-=(matrix<T,C,R>& left, const matrix<U,C,R>& right);
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) - right.get(i);
}
return left;
} }
} }
#include "mat.tpp"
#endif #endif

157
include/mat.tpp Normal file
View File

@ -0,0 +1,157 @@
/**
This file is a part of the rexy/r0nk/atlas project
Copyright (C) 2020 rexy712
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 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REXY_MAT_TPP
#define REXY_MAT_TPP
#include <cstdlib> //size_t
#include <cmath> //sin, cos
#include <type_traits> //decay_t, declval
namespace math{
template<typename T>
matrix<T,3,3> matrix<T,3,3>::rotation(value_type angle){
value_type c = std::cos(angle);
value_type s = std::sin(angle);
return rotation(s, c);
}
template<typename T>
constexpr matrix<T,3,3> matrix<T,3,3>::rotation(value_type sin, value_type cos){
return matrix(cos, -sin, 0,
sin, cos, 0,
0, 0, 1);
}
template<typename T>
constexpr matrix<T,3,3> matrix<T,3,3>::rotation(value_type angle_x, value_type angle_y, value_type angle_z){
//TODO
}
template<typename T, typename U, size_t W, size_t H>
constexpr bool operator==(const matrix<T,W,H>& left, const matrix<U,W,H> right){
for(size_t i = 0;i < left.size();++i){
if(left.get(i) != right.get(i))
return false;
}
return true;
}
template<typename T, typename U, size_t W, size_t H>
constexpr bool operator!=(const matrix<T,W,H>& left, const matrix<U,W,H> right){
return !(left == right);
}
template<typename T, typename U, size_t R1, size_t C1, size_t C2>
constexpr auto operator*(const matrix<T,C1,R1>& left, const matrix<U,C2,C1>& right){
using res_t = decltype(std::declval<T>() * std::declval<U>());
matrix<res_t,C2,R1> res(no_initialize);
size_t index = 0;
for(size_t i = 0;i < right.rows();++i){
for(size_t j = 0;j < left.rows();++j){
for(size_t k = 0;k < left.columns();++k){
res.get(index) += left[j][k] * right[i][k];
}
++index;
}
}
return res;
}
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator*(const matrix<T,C,R>& left, U&& right){
using res_t = decltype(std::declval<T>() * std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) * std::forward<U>(right);
}
return res;
}
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator/(const matrix<T,C,R>& left, U&& right){
using res_t = decltype(std::declval<T>() / std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) / std::forward<U>(right);
}
return res;
}
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator+(const matrix<T,C,R>& left, const matrix<U,C,R>& right){
using res_t = decltype(std::declval<T>() + std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) + right.get(i);
}
return res;
}
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator-(const matrix<T,C,R>& left, const matrix<U,C,R>& right){
using res_t = decltype(std::declval<T>() - std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) - right.get(i);
}
return res;
}
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator-(const matrix<T,C,R>& left){
using res_t = decltype(std::declval<T>() - std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
for(size_t i = 0;i < left.size();++i){
res.get(i) = -left.get(i);
}
return res;
}
template<typename T, typename U, size_t R1, size_t C1, size_t C2>
constexpr decltype(auto) operator*=(matrix<T,C1,R1>& left, const matrix<U,C2,C1>& right){
//have to evaluate entire expression first since matrix multiplication depends on reusing many elements
//cannot be expression templatized, TODO
return (left = (left * right));
}
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator*=(matrix<T,C,R>& left, U&& right){
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) * std::forward<U>(right);
}
return left;
}
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator/=(matrix<T,C,R>& left, U&& right){
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) / std::forward<U>(right);
}
return left;
}
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator+=(matrix<T,C,R>& left, const matrix<U,C,R>& right){
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) + right.get(i);
}
return left;
}
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator-=(matrix<T,C,R>& left, const matrix<U,C,R>& right){
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) - right.get(i);
}
return left;
}
}
#endif

View File

@ -2,18 +2,47 @@
#define OUR_DICK_RENDER_HPP #define OUR_DICK_RENDER_HPP
#include <stdint.h> #include <stdint.h>
#include <GLFW/glfw3.h>
#define OPENFL_VERSION 400 #define OPENFL_VERSION 400
#define GLSL_VERSION 400 #define GLSL_VERSION 400
class RenderManager class GLFWwindow;
{
public:
RenderManager (RenderManager&) = delete;
RenderManager (RenderManager&&) = delete;
RenderManager(); class render_manager
bool Init(uint16_t width, uint16_t height, const char* title); // Sets up the OpenGL environment {
private:
void (*m_windowCloseCallback)(void);
void (*m_inputHandler)(GLFWwindow* window, int key, int scancode, int action, int mods);
GLFWwindow* m_mainWindow;
public:
enum GLFW_ERROR
{
GLFW_OK,
GLFW_CONTEXT_ERROR,
GLFW_WINDOW_ERROR,
GLFW_INIT_ERROR,
};
render_manager (render_manager&) = delete;
render_manager (render_manager&&) = delete;
render_manager();
GLFW_ERROR init(int width, int height, const char* title); // Sets up the OpenGL environment
void update(); // Update the GL context and draw new frame
void request_exit();
template <typename T>
void handle_window_close_event(T handle){
m_windowCloseCallback = handle;
}
template <typename T>
void handle_keypress_event(T handle){
m_inputHandler = handle;
glfwSetKeyCallback(m_mainWindow, m_inputHandler);
}
}; };

View File

@ -4,6 +4,7 @@
#include "render.hpp" #include "render.hpp"
#include "game_state.hpp" #include "game_state.hpp"
#include "mat.hpp"
// 0 | 1 | 2 // 0 | 1 | 2
// --------- // ---------
@ -11,6 +12,14 @@
// --------- // ---------
// 6 | 7 | 8 // 6 | 7 | 8
#define TILE_COUNT 9
namespace
{
bool running = true;
render_manager manager;
}
int get_player_input(){ int get_player_input(){
//TODO get player input //TODO get player input
return rand()%9; return rand()%9;
@ -77,16 +86,31 @@ int exists_empty_tile(const game_state& gs){
} }
void play_audio(){ void play_audio(){
printf("playing audio...\n");
system("mpv assets/moans/$(ls assets/moans/ | sort -R | head -n 1) > /dev/null"); system("mpv assets/moans/$(ls assets/moans/ | sort -R | head -n 1) > /dev/null");
} }
void handle_window_close(){
printf ("[II] Window close event triggered\n");
running = false;
}
void handle_input_events(GLFWwindow*, int key, int, int, int)
{
printf ("[II] Recieved keycode %d\n", key);
if (key == 256)
manager.request_exit ();
}
int main(){ int main(){
srand(time(NULL)); srand(time(NULL));
game_state gs = {}; game_state gs = {};
RenderManager manager; manager.init(640, 480, "Tic-Tac-Gugh");
manager.Init(640, 480, "Tic-Tac-Gugh"); manager.handle_window_close_event(handle_window_close);
manager.handle_keypress_event (handle_input_events);
while (running)
manager.update();
while(exists_empty_tile(gs) && gs.turn != -1){ while(exists_empty_tile(gs) && gs.turn != -1){
game_turn(gs, get_player_input()); game_turn(gs, get_player_input());

View File

@ -1,33 +1,60 @@
#include <gl3w/GL/gl3w.h> // Must be included first
#include "render.hpp" #include "render.hpp"
#include <gl3w/GL/gl3w.h>
#include <GLFW/glfw3.h>
#include <stdio.h> #include <stdio.h>
RenderManager::RenderManager(){} namespace
{
void handle_resize_event(GLFWwindow*, int width, int height){
printf("Window resized\n");
glViewport(0, 0, width, height);
}
}
bool RenderManager::Init (uint16_t width, uint16_t height, const char* title){
render_manager::render_manager() :
m_windowCloseCallback (nullptr),
m_inputHandler (nullptr),
m_mainWindow (nullptr)
{}
render_manager::GLFW_ERROR render_manager::init (int width, int height, const char* title){
if (!glfwInit()) { if (!glfwInit()) {
printf("[EE] failed to initialize GLFW.\n"); fprintf(stdout, "[EE] failed to initialize GLFW.\n");
return false; return GLFW_INIT_ERROR;
} }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
auto window = glfwCreateWindow(width, height, title, nullptr, nullptr); m_mainWindow = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (!window) { if (!m_mainWindow) {
printf ("[EE] Could not create window\n"); fprintf (stdout, "[EE] Could not create window\n");
return false; return GLFW_WINDOW_ERROR;
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(m_mainWindow);
if (gl3wInit()) { if (gl3wInit()) {
printf("[EE] failed to initialize OpenGL\n"); fprintf(stdout, "[EE] failed to initialize OpenGL\n");
return false; return GLFW_CONTEXT_ERROR;
} }
glViewport(0, 0, width, height);
printf("[DD] OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION)); printf("[DD] OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));
return true;
glfwSetFramebufferSizeCallback(m_mainWindow, handle_resize_event);
return GLFW_OK;
}
void render_manager::update(){
glfwSwapBuffers(m_mainWindow);
glfwPollEvents();
}
void render_manager::request_exit()
{
if (m_windowCloseCallback) m_windowCloseCallback();
glfwSetWindowShouldClose(m_mainWindow, true);
} }