Add pause screen image

This commit is contained in:
rexy712 2022-01-10 10:35:33 -08:00
parent 56e4599ea1
commit 46c22c6193
30 changed files with 364 additions and 232 deletions

BIN
assets/images/pause.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
Copyright (C) 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
@ -16,34 +16,31 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_SPRITE_RENDERER_HPP
#define OUR_DICK_SPRITE_RENDERER_HPP
#ifndef OUR_DICK_BASIC_FRAMEBUFFER_HPP
#define OUR_DICK_BASIC_FRAMEBUFFER_HPP
#include "graphics/texture.hpp"
#include "graphics/rbo.hpp"
#include "graphics/fbo.hpp"
#include "sprite_shader.hpp"
#include "math/math.hpp"
#include "scene.hpp"
class sprite_renderer
class basic_framebuffer : public gfx::fbo
{
private:
gfx::texture m_fb_colorbuffer;
gfx::rbo m_fb_depthbuffer;
gfx::fbo m_framebuffer;
sprite_shader m_sprite_shader;
gfx::texture m_colorbuffer;
gfx::rbo m_depthbuffer;
public:
sprite_renderer(int width, int height);
basic_framebuffer(int width, int height);
basic_framebuffer(basic_framebuffer&&) = default;
~basic_framebuffer(void) = default;
void set_vp_matrix(const math::mat4<GLfloat>& vp);
void render(scene& s);
gfx::texture& color_buffer();
void resize_viewport(int width, int height);
const math::vec4<GLfloat>& get_viewport()const;
basic_framebuffer& operator=(basic_framebuffer&&) = default;
gfx::texture& colorbuffer(void);
const gfx::texture& colorbuffer(void)const;
gfx::rbo& depthbuffer(void);
const gfx::rbo& depthbuffer(void)const;
};
#endif

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2021 rexy712
Copyright (C) 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

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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
@ -56,6 +56,9 @@ namespace egn{
void push_state(std::unique_ptr<game_state_iface>&&);
void pop_state(void);
int get_width(void)const;
int get_height(void)const;
private:
void setup_window_();
};

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2021 rexy712
Copyright (C) 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
@ -22,17 +22,21 @@
#include "engine/image.hpp"
#include "graphics/material.hpp"
#include "graphics/model.hpp"
#include "graphics/shader_program.hpp"
#include <map>
#include <utility> //pair, forward
#include <string>
#include <utility> //pair, forward, piecewise_construct
#include <tuple>
namespace egn{
class resource_manager
{
private:
std::map<const char*,gfx::material> m_textures;
std::map<const char*,gfx::model> m_models;
std::map<std::string,gfx::material> m_textures;
std::map<std::string,gfx::model> m_models;
std::map<std::string,gfx::shader_program> m_shaders;
public:
resource_manager(void) = default;
@ -45,16 +49,22 @@ namespace egn{
resource_manager& operator=(resource_manager&&) = default;
bool has_material(const char* key)const;
template<class... Args>
std::pair<gfx::material*,bool> emplace_material(const char* key, Args&&... args);
gfx::material* get_material(const char* file);
bool erase_material(const char* key);
bool has_model(const char* key)const;
template<class... Args>
std::pair<gfx::model*,bool> emplace_model(const char* key, Args&&... args);
gfx::model* get_model(const char* file);
bool erase_model(const char* key);
bool has_shader(const char* key)const;
template<class... Args>
std::pair<gfx::shader_program*,bool> emplace_shader(const char* key, Args&&... args);
gfx::shader_program* get_shader(const char* key);
bool erase_shader(const char* key);
};
@ -62,14 +72,21 @@ namespace egn{
std::pair<gfx::material*,bool> resource_manager::emplace_material(const char* key, Args&&... args){
if(auto it = m_textures.find(key);it != m_textures.end())
return {&(*it).second, false};
auto ret = m_textures.emplace(key, std::forward<Args>(args)...);
auto ret = m_textures.emplace(std::piecewise_construct, std::tuple(key), std::tuple(std::forward<Args>(args)...));
return {&(*(ret.first)).second, true};
}
template<class... Args>
std::pair<gfx::model*,bool> resource_manager::emplace_model(const char* key, Args&&... args){
if(auto it = m_models.find(key);it != m_models.end())
return {&(*it).second, false};
auto ret = m_models.emplace(key, std::forward<Args>(args)...);
auto ret = m_models.emplace(std::piecewise_construct, std::tuple(key), std::tuple(std::forward<Args>(args)...));
return {&(*(ret.first)).second, true};
}
template<class... Args>
std::pair<gfx::shader_program*,bool> resource_manager::emplace_shader(const char* key, Args&&... args){
if(auto it = m_shaders.find(key);it != m_shaders.end())
return {&(*it).second, false};
auto ret = m_shaders.emplace(std::piecewise_construct, std::tuple(key), std::tuple(std::forward<Args>(args)...));
return {&(*(ret.first)).second, true};
}

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
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
@ -343,6 +343,7 @@ namespace gfx{
shader_program()
{
attach_shaders(std::forward<Args>(args)...);
link();
}
template<class... Args>
void shader_program::attach_shaders(const shader& s, Args&&... args){

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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

63
include/main_renderer.hpp Normal file
View File

@ -0,0 +1,63 @@
/**
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_MAIN_RENDERER_HPP
#define OUR_DICK_MAIN_RENDERER_HPP
#include "graphics/shader_program.hpp"
#include "graphics/vao.hpp"
#include "graphics/vbo.hpp"
#include "math/math.hpp"
#include "scene.hpp"
#include "engine/resource_manager.hpp"
#include "basic_framebuffer.hpp"
class main_renderer
{
private:
struct vertex{
math::vec2<GLfloat> pos;
math::vec2<GLfloat> tex_coord;
};
static constexpr vertex s_vertices[] = {
{{-1.0f, 1.0f}, {0.0f, 1.0f}},
{{-1.0f, -1.0f}, {0.0f, 0.0f}},
{{ 1.0f, -1.0f}, {1.0f, 0.0f}},
{{ 1.0f, -1.0f}, {1.0f, 0.0f}},
{{ 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-1.0f, 1.0f}, {0.0f, 1.0f}}
};
private:
basic_framebuffer m_fb;
gfx::shader_program* m_sprite_shader;
gfx::shader_program* m_screen_shader;
gfx::vbo m_vbo;
gfx::vao m_vao;
public:
main_renderer(egn::resource_manager& res, int width, int height);
void set_vp_matrix(const math::mat4<GLfloat>& vp);
void render(scene&);
gfx::texture& color_buffer(void);
void resize_viewport(int width, int height);
const math::vec4<GLfloat>& get_viewport(void)const;
};
#endif

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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
@ -21,13 +21,14 @@
#include "engine/game_state.hpp"
#include "engine/input.hpp"
#include <queue>
#include "graphics/texture.hpp"
#include "screen_renderer.hpp"
class pause_state : public egn::game_state_iface
{
private:
egn::game_state_iface* m_under_state;
screen_renderer m_screen_renderer;
public:
pause_state(egn::game& owner, egn::game_state_iface* under);

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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
@ -23,16 +23,14 @@
#include <queue>
#include "scene.hpp"
#include "sprite_renderer.hpp"
#include "screen_renderer.hpp"
#include "main_renderer.hpp"
#include "engine/camera.hpp"
class play_state : public egn::game_state_iface
{
private:
scene m_scene;
sprite_renderer m_sprite_renderer;
screen_renderer m_screen_renderer;
main_renderer m_main_renderer;
egn::ortho_camera* m_main_camera;
int m_current_player = 1;
double m_last_time;

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
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
@ -23,7 +23,8 @@
#include "graphics/vao.hpp"
#include "graphics/texture.hpp"
#include "math/math.hpp"
#include "screen_shader.hpp"
#include "graphics/shader_program.hpp"
#include "engine/resource_manager.hpp"
#include "scene.hpp"
class screen_renderer
@ -42,14 +43,14 @@ private:
{{-1.0f, 1.0f}, {0.0f, 1.0f}}
};
private:
screen_shader m_screen_shader;
gfx::shader_program* m_screen_shader;
gfx::vbo m_vbo;
gfx::vao m_vao;
gfx::weak_texture_handle m_texture;
gfx::texture* m_texture;
int m_screen_width, m_screen_height;
public:
screen_renderer(int width, int height, gfx::texture& base_tex);
screen_renderer(egn::resource_manager& res, int width, int height, gfx::texture& base_tex);
void render(scene&);
void resize_viewport(int width, int height);

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
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
@ -19,36 +19,28 @@
#ifndef OUR_DICK_SCREEN_SHADER_HPP
#define OUR_DICK_SCREEN_SHADER_HPP
#include "graphics/shader_program.hpp"
namespace screen_shader{
static constexpr char vertex_shader_text[] = "#version 430 core\n"
"layout (location = 0) in vec2 position;"
"layout (location = 1) in vec2 tex_coords;"
class screen_shader : public gfx::shader_program
{
private:
static constexpr char s_vertex_shader_text[] = "#version 430 core\n"
"layout (location = 0) in vec2 position;"
"layout (location = 1) in vec2 tex_coords;"
"out vec2 frag_tex_coords;"
"void main(){"
"gl_Position = vec4(position, -1.0, 1.0);"
"frag_tex_coords = tex_coords;"
"}";
static constexpr char fragment_shader_text[] = "#version 430 core\n"
"out vec4 FragColor;"
"out vec2 frag_tex_coords;"
"in vec2 frag_tex_coords;"
"uniform sampler2D texture1;"
"void main(){"
"gl_Position = vec4(position, -1.0, 1.0);"
"frag_tex_coords = tex_coords;"
"}";
static constexpr char s_fragment_shader_text[] = "#version 430 core\n"
"out vec4 FragColor;"
"in vec2 frag_tex_coords;"
"uniform sampler2D texture1;"
"void main(){"
"FragColor = texture(texture1, frag_tex_coords);"
"FragColor = texture(texture1, frag_tex_coords);"
//texture(texture1, frag_tex_coords);"
"}";
public:
screen_shader();
};
"}";
}
#endif

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
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
@ -19,40 +19,33 @@
#ifndef OUR_DICK_SPRITE_SHADER_HPP
#define OUR_DICK_SPRITE_SHADER_HPP
#include "graphics/shader_program.hpp"
namespace sprite_shader{
static constexpr char vertex_shader_text[] = "#version 430 core\n"
"layout (location = 0) in vec3 position;"
"layout (location = 1) in vec2 tex_coords;"
"layout (location = 2) in vec4 color;"
class sprite_shader : public gfx::shader_program
{
private:
static constexpr char s_vertex_shader_text[] = "#version 430 core\n"
"layout (location = 0) in vec3 position;"
"layout (location = 1) in vec2 tex_coords;"
"layout (location = 2) in vec4 color;"
"out vec2 frag_tex_coords;"
"out vec4 frag_vertex_color;"
"out vec2 frag_tex_coords;"
"out vec4 frag_vertex_color;"
"uniform mat4 model_mat;"
"uniform mat4 vp_mat;"
"uniform mat4 model_mat;"
"uniform mat4 vp_mat;"
"void main(){"
"gl_Position = vp_mat * model_mat * vec4(position, 1.0);"
"frag_tex_coords = tex_coords;"
"}";
static constexpr char fragment_shader_text[] = "#version 430 core\n"
"out vec4 FragColor;"
"in vec2 frag_tex_coords;"
"uniform sampler2D texture1;"
"uniform vec4 model_color;"
"void main(){"
"gl_Position = vp_mat * model_mat * vec4(position, 1.0);"
"frag_tex_coords = tex_coords;"
"FragColor = mix(texture(texture1, frag_tex_coords), model_color, model_color.a);"
"}";
static constexpr char s_fragment_shader_text[] = "#version 430 core\n"
"out vec4 FragColor;"
"in vec2 frag_tex_coords;"
"uniform sampler2D texture1;"
"uniform vec4 model_color;"
"void main(){"
"FragColor = mix(texture(texture1, frag_tex_coords), model_color, model_color.a);"
"}";
public:
sprite_shader();
};
}
#endif

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
Copyright (C) 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
@ -16,11 +16,27 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "screen_shader.hpp"
#include "basic_framebuffer.hpp"
screen_shader::screen_shader():
shader_program(gfx::shader(s_fragment_shader_text, gfx::shader::type::FRAGMENT),
gfx::shader(s_vertex_shader_text, gfx::shader::type::VERTEX))
basic_framebuffer::basic_framebuffer(int width, int height):
gfx::fbo(),
m_colorbuffer(GL_RGBA, width, height, GL_UNSIGNED_BYTE),
m_depthbuffer(width, height, GL_DEPTH24_STENCIL8)
{
link();
attach(m_colorbuffer, GL_COLOR_ATTACHMENT0);
attach(m_depthbuffer, GL_DEPTH_STENCIL_ATTACHMENT);
}
gfx::texture& basic_framebuffer::colorbuffer(void){
return m_colorbuffer;
}
const gfx::texture& basic_framebuffer::colorbuffer(void)const{
return m_colorbuffer;
}
gfx::rbo& basic_framebuffer::depthbuffer(void){
return m_depthbuffer;
}
const gfx::rbo& basic_framebuffer::depthbuffer(void)const{
return m_depthbuffer;
}

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2021rexy712
Copyright (C) 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

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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
@ -103,6 +103,12 @@ namespace egn{
void game::pop_state(void){
m_states_manager.pop_state();
}
int game::get_width(void)const{
return m_window.get_width();
}
int game::get_height(void)const{
return m_window.get_height();
}
void game::setup_window_(){

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2021 rexy712
Copyright (C) 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
@ -31,6 +31,9 @@ namespace egn{
return &(*it).second;
return nullptr;
}
bool resource_manager::erase_material(const char* key){
return m_textures.erase(key) == 1;
}
bool resource_manager::has_model(const char* key)const{
@ -43,5 +46,23 @@ namespace egn{
return &(*it).second;
return nullptr;
}
bool resource_manager::erase_model(const char* key){
return m_models.erase(key) == 1;
}
bool resource_manager::has_shader(const char* key)const{
if(auto it = m_shaders.find(key);it != m_shaders.end())
return true;
return false;
}
gfx::shader_program* resource_manager::get_shader(const char* file){
if(auto it = m_shaders.find(file);it != m_shaders.end())
return &(*it).second;
return nullptr;
}
bool resource_manager::erase_shader(const char* key){
return m_shaders.erase(key) == 1;
}
}

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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

104
src/main_renderer.cpp Normal file
View File

@ -0,0 +1,104 @@
/**
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/>.
*/
#include "main_renderer.hpp"
#include "graphics/shader.hpp"
#include "sprite_shader.hpp"
#include "screen_shader.hpp"
#include <cstdlib> //size_t
#define MAX_WIDTH 3840
#define MAX_HEIGHT 2160
main_renderer::main_renderer(egn::resource_manager& res, int width, int height):
m_fb(MAX_WIDTH, MAX_HEIGHT),
m_vbo(s_vertices, sizeof(s_vertices), gfx::vbo::usage::DYNAMIC_DRAW)
{
if(res.has_shader("sprite_shader"))
m_sprite_shader = res.get_shader("sprite_shader");
else
m_sprite_shader = res.emplace_shader("sprite_shader", gfx::shader(sprite_shader::fragment_shader_text, gfx::shader::type::FRAGMENT),
gfx::shader(sprite_shader::vertex_shader_text, gfx::shader::type::VERTEX)).first;
m_sprite_shader->set_uniform("vp_mat", math::mat4<GLfloat>{});
if(res.has_shader("screen_shader"))
m_screen_shader = res.get_shader("screen_shader");
else
m_screen_shader = res.emplace_shader("screen_shader", gfx::shader(screen_shader::fragment_shader_text, gfx::shader::type::FRAGMENT),
gfx::shader(screen_shader::vertex_shader_text, gfx::shader::type::VERTEX)).first;
m_screen_shader->set_uniform("vp_mat", math::mat4<GLfloat>{});
m_vao.bind_buffer(m_vbo, 0, 0, sizeof(vertex));
auto attrib = m_vao.get_attribute(0);
attrib.set_float_array(2, 0);
attrib.associate_with(0);
attrib.enable();
attrib = m_vao.get_attribute(1);
attrib.set_float_array(2, offsetof(vertex, tex_coord));
attrib.associate_with(0);
attrib.enable();
resize_viewport(width, height);
}
void main_renderer::set_vp_matrix(const math::mat4<GLfloat>& vp){
m_sprite_shader->set_uniform("vp_mat", vp);
}
void main_renderer::render(scene& s){
const auto& vp = m_fb.get_viewport();
m_fb.bind();
m_fb.clear_color_buffer();
m_fb.clear_depth_buffer();
m_fb.apply_viewport();
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
m_sprite_shader->use();
for(auto it = s.renderables.begin();it != s.renderables.end();++it){
(*it)->render(*m_sprite_shader);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glViewport(0, 0, vp.z(), vp.w());
m_screen_shader->use();
m_screen_shader->set_uniform("texture1", m_fb.colorbuffer(), 0);
m_vao.bind();
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void main_renderer::resize_viewport(int width, int height){
m_fb.set_viewport(0, 0, width, height);
auto map = m_vbo.map(gfx::vbo::maptype::WRITE);
vertex* data = reinterpret_cast<vertex*>(map.raw());
GLfloat x1 = 0, y1 = 0;
GLfloat x2 = (GLfloat)width / m_fb.colorbuffer().get_width();
GLfloat y2 = (GLfloat)height / m_fb.colorbuffer().get_height();
data[0].tex_coord = {x1, y2};
data[1].tex_coord = {x1, y1};
data[2].tex_coord = {x2, y1};
data[3].tex_coord = {x2, y1};
data[4].tex_coord = {x2, y2};
data[5].tex_coord = {x1, y2};
}
const math::vec4<GLfloat>& main_renderer::get_viewport(void)const{
return m_fb.get_viewport();
}

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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
@ -25,9 +25,12 @@
pause_state::pause_state(egn::game& owner, egn::game_state_iface* under):
game_state_iface(&owner),
m_under_state(under){}
m_under_state(under),
m_screen_renderer(owner.resource_man(), owner.get_width(), owner.get_height(), *owner.resource_man().emplace_material("pause_screen", egn::deferred_image("assets/images/pause.png", true)).first){}
void pause_state::enter(){}
void pause_state::enter(){
m_screen_renderer.resize_viewport(m_owner->get_width(), m_owner->get_height());
}
void pause_state::leave(){}
void pause_state::handle_input(const egn::input_event& event){
if(event.ev_type == egn::input_event::type::KEY){
@ -40,9 +43,12 @@ void pause_state::handle_input(const egn::input_event& event){
}
}else if(event.ev_type == egn::input_event::type::RESIZE){
m_under_state->handle_input(event);
m_screen_renderer.resize_viewport(event.x, event.y);
}
}
void pause_state::update(double){}
void pause_state::render(){
scene tmp;
m_under_state->render();
m_screen_renderer.render(tmp);
}

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020-2021 rexy712
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
@ -29,8 +29,7 @@
play_state::play_state(egn::game& owner, int width, int height):
game_state_iface(&owner),
m_sprite_renderer(width, height),
m_screen_renderer(width, height, m_sprite_renderer.color_buffer()),
m_main_renderer(owner.resource_man(), width, height),
m_current_player(rand() % 2)
{
debug_print("First player chosen is %c\n", m_current_player ? 'X' : 'O');
@ -62,11 +61,10 @@ void play_state::handle_input(const egn::input_event& ev){
}else{
m_main_camera->set_projection_box(10, 10 * (float)ev.y / ev.x);
}
m_sprite_renderer.resize_viewport(ev.x, ev.y);
m_screen_renderer.resize_viewport(ev.x, ev.y);
m_main_renderer.resize_viewport(ev.x, ev.y);
}else if(ev.ev_type == egn::input_event::type::MOUSE_BUTTON){
if(ev.key == GLFW_MOUSE_BUTTON_1 && ev.action == GLFW_RELEASE){
const math::vec4<GLfloat>& viewport = m_sprite_renderer.get_viewport();
const math::vec4<GLfloat>& viewport = m_main_renderer.get_viewport();
math::vec3<GLfloat> vp_coords{ev.x - viewport[0], viewport[3] - (ev.y - viewport[1]), 1.0f};
math::mat4<GLfloat> vp_mat = m_main_camera->get_projection_matrix() * m_main_camera->get_view_matrix();
@ -99,7 +97,6 @@ void play_state::handle_input(const egn::input_event& ev){
void play_state::update(double time_diff){
}
void play_state::render(){
m_sprite_renderer.set_vp_matrix(m_main_camera->get_projection_matrix() * m_main_camera->get_view_matrix());
m_sprite_renderer.render(m_scene);
m_screen_renderer.render(m_scene);
m_main_renderer.set_vp_matrix(m_main_camera->get_projection_matrix() * m_main_camera->get_view_matrix());
m_main_renderer.render(m_scene);
}

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
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
@ -18,11 +18,23 @@
#include "screen_renderer.hpp"
screen_renderer::screen_renderer(int width, int height, gfx::texture& base_tex):
m_vbo(s_vertices, sizeof(s_vertices), gfx::vbo::usage::DYNAMIC_DRAW),
m_texture(base_tex.create_handle()),
#include "graphics/shader.hpp"
#include "screen_shader.hpp"
#include <tuple>
#include <utility> //piecewise_construct
screen_renderer::screen_renderer(egn::resource_manager& res, int width, int height, gfx::texture& base_tex):
m_vbo(s_vertices, sizeof(s_vertices), gfx::vbo::usage::STATIC_DRAW),
m_texture(&base_tex),
m_screen_width(width), m_screen_height(height)
{
if(res.has_shader("screen_shader")){
m_screen_shader = res.get_shader("screen_shader");
}else{
m_screen_shader = res.emplace_shader("screen_shader", gfx::shader(screen_shader::vertex_shader_text, gfx::shader::type::VERTEX),
gfx::shader(screen_shader::fragment_shader_text, gfx::shader::type::FRAGMENT)).first;
}
//attach a vbo to the vao and assign attribute specs
m_vao.bind_buffer(m_vbo, 0, 0, sizeof(vertex));
auto attrib = m_vao.get_attribute(0);
@ -40,13 +52,14 @@ screen_renderer::screen_renderer(int width, int height, gfx::texture& base_tex):
void screen_renderer::render(scene&){
//Clear the global state to that which this renderer prefers
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glViewport(0, 0, m_screen_width, m_screen_height);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Apply our shader and vao
m_screen_shader.use();
m_screen_shader.set_uniform("texture1", m_texture, 0);
m_screen_shader->use();
m_screen_shader->set_uniform("texture1", *m_texture, 0);
m_vao.bind();
//Draw
@ -56,19 +69,4 @@ void screen_renderer::render(scene&){
void screen_renderer::resize_viewport(int width, int height){
m_screen_width = width;
m_screen_height = height;
//Specify that we only want to render part of the texture using texture_coordinates in the shader
auto map = m_vbo.map(gfx::vbo::maptype::WRITE);
vertex* data = reinterpret_cast<vertex*>(map.raw());
GLfloat x1 = 0, y1 = 0;
GLfloat x2 = (GLfloat)width / 3840;
GLfloat y2 = (GLfloat)height / 2160;
data[0].tex_coord = {x1, y2};
data[1].tex_coord = {x1, y1};
data[2].tex_coord = {x2, y1};
data[3].tex_coord = {x2, y1};
data[4].tex_coord = {x2, y2};
data[5].tex_coord = {x1, y2};
}

View File

@ -1,56 +0,0 @@
/**
This file is a part of our_dick
Copyright (C) 2020 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/>.
*/
#include "sprite_renderer.hpp"
#include <cstdlib> //size_t
sprite_renderer::sprite_renderer(int width, int height):
m_fb_colorbuffer(GL_RGBA, 3840, 2160, GL_UNSIGNED_BYTE),
m_fb_depthbuffer(3840, 2160, GL_DEPTH24_STENCIL8),
m_framebuffer(m_fb_colorbuffer, GL_COLOR_ATTACHMENT0, m_fb_depthbuffer, GL_DEPTH_STENCIL_ATTACHMENT)
{
m_framebuffer.set_viewport(0, 0, width, height);
m_sprite_shader.set_uniform("model_mat", math::mat4<GLfloat>{});
}
void sprite_renderer::set_vp_matrix(const math::mat4<GLfloat>& vp){
m_sprite_shader.set_uniform("vp_mat", vp);
}
void sprite_renderer::render(scene& s){
m_framebuffer.bind();
m_framebuffer.clear_color_buffer();
m_framebuffer.clear_depth_buffer();
m_framebuffer.apply_viewport();
glEnable(GL_DEPTH_TEST);
m_sprite_shader.use();
for(auto it = s.renderables.begin();it != s.renderables.end();++it){
(*it)->render(m_sprite_shader);
}
}
gfx::texture& sprite_renderer::color_buffer(){
return m_fb_colorbuffer;
}
void sprite_renderer::resize_viewport(int width, int height){
m_framebuffer.set_viewport(0, 0, width, height);
}
const math::vec4<GLfloat>& sprite_renderer::get_viewport()const{
return m_framebuffer.get_viewport();
}

View File

@ -1,26 +0,0 @@
/**
This file is a part of our_dick
Copyright (C) 2020 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/>.
*/
#include "sprite_shader.hpp"
sprite_shader::sprite_shader():
shader_program(gfx::shader(s_fragment_shader_text, gfx::shader::type::FRAGMENT),
gfx::shader(s_vertex_shader_text, gfx::shader::type::VERTEX))
{
link();
}