Working very unpolished tic-tac-toe game.

This commit is contained in:
rexy712 2020-10-11 12:36:05 -07:00
parent 60ab28d37a
commit f66e20aa3d
40 changed files with 2143 additions and 345 deletions

67
TODO Normal file
View File

@ -0,0 +1,67 @@
game
basically handles the game logic and delegates audio/video to respective management tools
track current state and updates state
calls renderers to draw the scene to framebuffer(s)
calls a mixer to play audio for a scene
key functions:
render()
update()
holds any scenes required for gameplay
holds window for game to be drawn in
scene
basically a central repository for raw data in a scene
holds cameras that exist in a given scene
holds objects that exist in a given scene
holds textures that exists for a given scene
holds audio that exists for a given scene
renderer
basically takes a scene and draws it a specific way to a framebuffer
key functions:
set_vpmatrix(mat4&)
set up view-projection matrix for use in rendering
render(scene&)
draw the given scene the this renderer's framebuffer
vertex
basically describes a vertex
holds position
holds texture coordinates
holds vertex color
holds normals
mesh
basically holds vertex data for a given shape
holds an array of vertices that describe a shape to draw
key functions:
render(shader&)
draw this mesh onto the currently bound framebuffer using the given shader
model
basically handles a collection of meshes that constitute a single entity
hold an array of meshes that describe the composition of a complex model
key functions:
render(shader&)
draw this model onto the currently bound framebuffer using the given shader
instance
basically holds handles to a mesh/model/any other needed data for use by multiple objects
renderable
basically any type with a render(shader_program&) function
hold an instance object reffing to a model
hold a model matrix
square
is a renderable that specifically references a square model

86
game_state.TODO Normal file
View File

@ -0,0 +1,86 @@
observer
virtual on_notify(const event& e)
observer_subject
std::vector<observer*> m_observers
notify(const event& e)
for(observer : m_observers)
observer.on_notify(e);
void add_observer(observer* obs)
m_observers.push_back(obs);
void remove_observer(
game : public observer
game_state_manager
resource_manager
queue<input_event>
bool m_game_over
on_notify(const event& e)override{
if(e.type == should_close)
m_game_over = true;
}
game_state_manager : public observer_subject
stack<unique_ptr<game_state_iface>> stack
update(double time, queue<input_event>&){
stack.top()->update(time, queue);
}
draw(){
stack.top()->draw();
}
void pop_state()
void push_state()
void replace_state()
game_state_iface
game_state_manager* m_owner
virtual enter()
virtual leave()
virtual update(float time, queue<input_event>&)
virtual draw()
play_state : public game_state_iface
scene game_scene
renderer_handle
enter()override
leave()override
update(float time, queue<input_event>&)override{
if(event == PRESS_PAUSE_BUTTON)
m_owner->push_state(new pause_state(this))
}
draw()override
pause_state : public game_state_iface
scene pause_menu
play_state* under_state
renderer_handle
pause_state(play_state* last_state)
enter()override
leave()override
update(float time, queue<input_event>&)override{
if(event == PRESS_RESUME_BUTTON)
m_owner->pop_state()
else if(event == PRESS_EXIT_BUTTON)
m_owner->notify(should_close_event{});
}
draw()override{
under_state->draw();
//draw this state over that state
}
main_menu_state : public game_state_iface
scene main_menu
renderer handle
enter()override
leave()override
update(float time, queue<input_event>&)override{
if(event == CLICK_ON_START_BUTTON)
m_owner->replace_state(new play_state);
}
draw()override

57
include/engine/game.hpp Normal file
View File

@ -0,0 +1,57 @@
/**
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/>.
*/
#ifndef OUR_DICK_ENGINE_GAME_HPP
#define OUR_DICK_ENGINE_GAME_HPP
#include "observable.hpp"
#include "game_state.hpp"
#include "graphics/window.hpp"
#include "input.hpp"
#include <queue>
namespace egn{
class game : public observer<game_state_event>
{
protected:
gfx::window m_window;
game_state_manager m_states_manager;
std::queue<input_event> m_event_queue;
double m_last_time;
public:
game(int cver_maj, int cver_min, int width, int height, const char* title);
void update();
void render();
void mouse_button(int button, double xpos, double ypos, int action, int mods);
void keypress(int key, int action, int mods);
void resize(int width, int height);
void on_notify(const game_state_event& e)override;
private:
void setup_window_();
};
}
#endif

View File

@ -0,0 +1,84 @@
/**
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/>.
*/
#ifndef OUR_DICK_ENGINE_GAME_STATE_HPP
#define OUR_DICK_ENGINE_GAME_STATE_HPP
#include "observable.hpp"
#include "input.hpp"
#include <stack>
#include <memory>
#include <queue>
namespace egn{
struct game_state_event {
enum class state_type{
SHOULD_CLOSE,
RESIZE_WINDOW,
};
state_type type;
int value1;
int value2;
};
class game_state_iface;
class game_state_manager : public egn::observable<game_state_event>
{
private:
std::stack<std::unique_ptr<game_state_iface>> m_state_stack;
public:
template<typename... Args>
void emplace_state(Args&&... args);
void push_state(std::unique_ptr<game_state_iface>&& state);
void pop_state();
void replace_state(std::unique_ptr<game_state_iface>&& state);
void handle_input(std::queue<egn::input_event>& events);
void update(double time_diff);
void render();
};
class game_state_iface
{
protected:
game_state_manager* m_owner = nullptr;
protected:
game_state_iface(game_state_manager* owner);
game_state_iface(const game_state_iface& g) = default;
game_state_iface(game_state_iface&& g) = default;
game_state_iface& operator=(const game_state_iface&) = default;
game_state_iface& operator=(game_state_iface&&) = default;
public:
virtual ~game_state_iface() = default;
virtual void enter() = 0;
virtual void leave() = 0;
virtual void handle_input(const egn::input_event& event) = 0;
virtual void update(double time) = 0;
virtual void render() = 0;
};
}
#endif

41
include/engine/input.hpp Normal file
View File

@ -0,0 +1,41 @@
/**
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/>.
*/
#ifndef OUR_DICK_ENGINE_INPUT_HPP
#define OUR_DICK_ENGINE_INPUT_HPP
namespace egn{
struct input_event {
enum class type{
KEY,
MOUSE_BUTTON,
SCROLL,
RESIZE,
};
type ev_type;
double time;
int key;
int action;
int mods;
double x, y;
};
}
#endif

View File

@ -0,0 +1,73 @@
/**
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/>.
*/
#ifndef OUR_DICK_ENGINE_OBSERVABLE_HPP
#define OUR_DICK_ENGINE_OBSERVABLE_HPP
#include <vector>
namespace egn{
template<typename Event>
class observable;
template<typename Event>
class observer
{
public:
using event_type = Event;
using event_ref = event_type&;
using const_event_ref = const event_type&;
using observed_type = observable<event_type>;
using observed_ref = observed_type&;
using const_observed_ref = const observed_type&;
using observed_pointer = observed_type*;
using const_observed_pointer = const observed_type*;
public:
virtual ~observer() = default;
virtual void on_notify(const_event_ref e) = 0;
};
template<typename Event>
class observable
{
public:
using event_type = Event;
using event_ref = event_type&;
using const_event_ref = const event_type&;
using observer_type = observer<event_type>;
using observer_ref = observer_type&;
using const_observer_ref = const observer_type&;
using observer_pointer = observer_type*;
using const_observer_pointer = const observer_type*;
private:
std::vector<observer_pointer> m_observers;
public:
void notify(const_event_ref e);
void add_observer(observer_ref o);
void remove_observer(observer_ref o);
};
}
#include "observable.tpp"
#endif

View File

@ -0,0 +1,46 @@
/**
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/>.
*/
#ifndef OUR_DICK_ENGINE_OBSERVABLE_TPP
#define OUR_DICK_ENGINE_OBSERVABLE_TPP
namespace egn{
template<typename Event>
void observable<Event>::notify(const_event_ref e){
for(auto i = m_observers.begin();i != m_observers.end();++i){
(*i)->on_notify(e);
}
}
template<typename Event>
void observable<Event>::add_observer(observer_ref o){
m_observers.push_back(&o);
}
template<typename Event>
void observable<Event>::remove_observer(observer_ref o){
for(auto i = m_observers.begin();i != m_observers.end();++i){
if(*i == &o){
m_observers.erase(i);
break;
}
}
}
}
#endif

View File

@ -0,0 +1,30 @@
/**
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/>.
*/
#ifndef OUR_DICK_GRAPHICS_LIGHT_HPP
#define OUR_DICK_GRAPHICS_LIGHT_HPP
#include "lightable.hpp"
namespace gfx{
class light : public lightable{};
}
#endif

View File

@ -0,0 +1,49 @@
/**
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/>.
*/
#ifndef OUR_DICK_GRAPHICS_LIGHTABLE_HPP
#define OUR_DICK_GRAPHICS_LIGHTABLE_HPP
#include "math/vec.hpp"
#include "gl_include.hpp"
namespace gfx{
class lightable
{
protected:
math::vec3<GLfloat> m_ambient;
math::vec3<GLfloat> m_diffuse;
math::vec3<GLfloat> m_specular;
GLfloat m_shininess;
public:
void set_ambient(const math::vec3<GLfloat>& a);
void set_diffuse(const math::vec3<GLfloat>& d);
void set_specular(const math::vec3<GLfloat>& s);
void set_shininess(GLfloat s);
const math::vec3<GLfloat>& get_ambient()const;
const math::vec3<GLfloat>& get_diffuse()const;
const math::vec3<GLfloat>& get_specular()const;
GLfloat get_shininess()const;
};
}
#endif

View File

@ -0,0 +1,36 @@
/**
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/>.
*/
#ifndef OUR_DICK_GRAPHICS_MATERIAL_HPP
#define OUR_DICK_GRAPHICS_MATERIAL_HPP
#include "lightable.hpp"
#include "texture.hpp"
namespace gfx{
class material : public lightable, public texture
{
public:
using texture::texture;
using texture::operator=;
};
}
#endif

113
include/graphics/mesh.hpp Normal file
View File

@ -0,0 +1,113 @@
/**
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/>.
*/
#ifndef OUR_DICK_GRAPHICS_MESH_HPP
#define OUR_DICK_GRAPHICS_MESH_HPP
#include "math/vec.hpp"
#include "vao.hpp"
#include "vbo.hpp"
#include "shader_program.hpp"
#include "material.hpp"
#include <vector>
#include <cstdlib> //size_t
namespace gfx{
struct vertex {
math::vec3<float> position;
math::vec4<float> color;
math::vec2<float> tex_coords;
};
class vertex_mesh
{
protected:
std::vector<vertex> m_vertices;
gfx::vao m_vao;
gfx::vbo m_vbo;
public:
vertex_mesh(const std::vector<vertex>& verts);
vertex_mesh(std::vector<vertex>&& verts);
vertex_mesh(const vertex* verts, size_t nverts);
vertex_mesh(const vertex_mesh&) = delete;
vertex_mesh(vertex_mesh&&) = default;
~vertex_mesh() = default;
vertex_mesh& operator=(const vertex_mesh&) = delete;
vertex_mesh& operator=(vertex_mesh&&) = default;
void configure();
const vertex& get_vertex(size_t index)const;
void set_vertex(size_t index, const vertex& v);
void render(shader_program& shader);
};
class material_mesh
{
protected:
std::vector<const material*> m_materials;
public:
material_mesh(const std::vector<const material*>& textures);
material_mesh(std::vector<const material*>&& textures);
material_mesh(const material** texs, size_t ntexs);
const material* get_material(size_t index)const;
void set_material(size_t index, const material& new_texture);
void set_material(size_t index, const material* new_texture);
void render(shader_program& shader);
};
class unified_mesh
{
protected:
vertex_mesh m_v_mesh;
material_mesh m_m_mesh;
public:
unified_mesh(vertex_mesh&& v, const material_mesh& m);
unified_mesh(vertex_mesh&& v, material_mesh&& m);
unified_mesh(const unified_mesh&) = delete; //vao is uncopiable
unified_mesh(unified_mesh&&) = default;
~unified_mesh() = default;
unified_mesh& operator=(const unified_mesh&) = delete;
unified_mesh& operator=(unified_mesh&&) = default;
vertex_mesh& vertex();
const vertex_mesh& vertex()const;
material_mesh& material();
const material_mesh& material()const;
void configure();
void render(shader_program& shader);
};
}
#endif

View File

@ -0,0 +1,77 @@
/**
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/>.
*/
#ifndef OUR_DICK_GRAPHICS_MODEL_HPP
#define OUR_DICK_GRAPHICS_MODEL_HPP
#include "mesh.hpp"
#include "shader_program.hpp"
#include <vector>
#include <type_traits> //enable_if, decay, is_same
#include <utility> //forward, move
#include <cstdlib> //size_t
namespace gfx{
class model
{
protected:
std::vector<unified_mesh> m_meshes;
public:
model() = default;
template<typename... Args, std::enable_if_t<(std::is_same_v<std::decay_t<Args>,::gfx::unified_mesh> && ...),int> = 0>
model(Args&&... args);
model(std::vector<unified_mesh>&& meshes);
model(unified_mesh* meshes, size_t size);
model(const model&) = delete;
model(model&&) = default;
~model() = default;
model& operator=(const model&) = default;
model& operator=(model&&) = default;
void configure();
void add_mesh(unified_mesh&& m);
template<typename... Args>
void add_meshes(unified_mesh&& m, Args&&... args);
unified_mesh& mesh(size_t index);
const unified_mesh& mesh(size_t index)const;
void render(shader_program& s);
};
template<typename... Args, std::enable_if_t<(std::is_same_v<std::decay_t<Args>,::gfx::unified_mesh> && ...),int>>
model::model(Args&&... args){
m_meshes.reserve(sizeof...(args));
add_meshes(std::forward<Args>(args)...);
}
template<typename... Args>
void model::add_meshes(unified_mesh&& m, Args&&... args){
add_mesh(std::move(m));
if constexpr(sizeof...(args) > 0){
add_meshes(std::forward<Args>(args)...);
}
}
}
#endif

42
include/pause_state.hpp Normal file
View File

@ -0,0 +1,42 @@
/**
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/>.
*/
#ifndef OUR_DICK_PAUSE_STATE_HPP
#define OUR_DICK_PAUSE_STATE_HPP
#include "engine/game_state.hpp"
#include "engine/input.hpp"
#include <queue>
class pause_state : public egn::game_state_iface
{
private:
egn::game_state_iface* m_under_state;
public:
pause_state(egn::game_state_manager& owner, egn::game_state_iface* under);
void enter();
void leave();
void handle_input(const egn::input_event& event);
void update(double time);
void render();
};
#endif

55
include/play_state.hpp Normal file
View File

@ -0,0 +1,55 @@
/**
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/>.
*/
#ifndef OUR_DICK_PLAY_STATE_HPP
#define OUR_DICK_PLAY_STATE_HPP
#include "engine/game_state.hpp"
#include <queue>
#include "scene.hpp"
#include "sprite_renderer.hpp"
#include "screen_renderer.hpp"
#include "engine/camera.hpp"
#include "square.hpp"
class play_state : public egn::game_state_iface
{
private:
scene m_scene;
sprite_renderer m_sprite_renderer;
screen_renderer m_screen_renderer;
egn::ortho_camera* m_main_camera;
int m_current_player = 1;
double m_last_time;
public:
play_state(egn::game_state_manager& owner, int width, int height);
void enter()override;
void leave()override;
void handle_input(const egn::input_event& events)override;
void update(double time)override;
void render()override;
private:
square::value check_win_condition_();
bool is_board_full_();
};
#endif

35
include/renderable.hpp Normal file
View File

@ -0,0 +1,35 @@
/**
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/>.
*/
#ifndef OUR_DICK_RENDERABLE_HPP
#define OUR_DICK_RENDERABLE_HPP
#include "engine/object.hpp"
#include "graphics/shader_program.hpp"
class renderable_iface : public egn::object
{
public:
using egn::object::object;
using egn::object::operator=;
virtual ~renderable_iface() = default;
virtual void render(gfx::shader_program&) = 0;
};
#endif

38
include/scene.hpp Normal file
View File

@ -0,0 +1,38 @@
/**
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/>.
*/
#ifndef OUR_DICK_SCENE_HPP
#define OUR_DICK_SCENE_HPP
#include "square.hpp"
#include "graphics/texture.hpp" //TODO: use material interface instead of direct texture
#include "engine/camera.hpp"
#include "renderable.hpp"
#include "graphics/model.hpp"
#include <vector> //vector
#include <memory> //unique_ptr
struct scene {
std::vector<std::unique_ptr<renderable_iface>> renderables;
std::vector<gfx::material> textures;
std::vector<std::unique_ptr<egn::camera_iface>> cameras;
std::vector<std::unique_ptr<gfx::model>> models;
};
#endif

View File

@ -0,0 +1,58 @@
/**
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/>.
*/
#ifndef OUR_DICK_SCREEN_RENDERER_HPP
#define OUR_DICK_SCREEN_RENDERER_HPP
#include "graphics/vbo.hpp"
#include "graphics/vao.hpp"
#include "graphics/texture.hpp"
#include "math/math.hpp"
#include "screen_shader.hpp"
#include "scene.hpp"
class screen_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:
screen_shader m_screen_shader;
gfx::vbo m_vbo;
gfx::vao m_vao;
gfx::weak_texture_handle m_texture;
int m_screen_width, m_screen_height;
public:
screen_renderer(int width, int height, gfx::texture& base_tex);
void render(scene&);
void resize_viewport(int width, int height);
};
#endif

54
include/screen_shader.hpp Normal file
View File

@ -0,0 +1,54 @@
/**
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/>.
*/
#ifndef OUR_DICK_SCREEN_SHADER_HPP
#define OUR_DICK_SCREEN_SHADER_HPP
#include "graphics/shader_program.hpp"
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 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);"
//texture(texture1, frag_tex_coords);"
"}";
public:
screen_shader();
};
#endif

View File

@ -0,0 +1,49 @@
/**
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/>.
*/
#ifndef OUR_DICK_SPRITE_RENDERER_HPP
#define OUR_DICK_SPRITE_RENDERER_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
{
private:
gfx::texture m_fb_colorbuffer;
gfx::rbo m_fb_depthbuffer;
gfx::fbo m_framebuffer;
sprite_shader m_sprite_shader;
public:
sprite_renderer(int width, int height);
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;
};
#endif

59
include/sprite_shader.hpp Normal file
View File

@ -0,0 +1,59 @@
/**
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/>.
*/
#ifndef OUR_DICK_SPRITE_SHADER_HPP
#define OUR_DICK_SPRITE_SHADER_HPP
#include "graphics/shader_program.hpp"
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;"
"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;"
"frag_vertex_color = color;"
"}";
static constexpr char s_fragment_shader_text[] = "#version 430 core\n"
"out vec4 FragColor;"
"in vec2 frag_tex_coords;"
"in vec4 frag_vertex_color;"
"uniform sampler2D texture1;"
"void main(){"
"FragColor = texture(texture1, frag_tex_coords);"
"}";
public:
sprite_shader();
};
#endif

53
include/square.hpp Normal file
View File

@ -0,0 +1,53 @@
/**
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/>.
*/
#ifndef OUR_DICK_SQUARE_HPP
#define OUR_DICK_SQUARE_HPP
#include "graphics/mesh.hpp"
#include "graphics/model.hpp"
#include "graphics/material.hpp"
#include "graphics/shader_program.hpp"
#include "renderable.hpp"
gfx::unified_mesh square_mesh(const gfx::material& blank, const gfx::material& o, const gfx::material& x);
class square : public renderable_iface
{
public:
enum class value{
BLANK,
O,
X,
};
private:
gfx::model* m_model;
value m_active_value = value::BLANK;
public:
square(gfx::model* model);
void set_value(value v);
value get_value()const;
void render(gfx::shader_program&)override;
};
#endif

View File

@ -1,6 +1,6 @@
/**
This file is a part of our_dick
Copyright (C) 2020 r0nk, rexy712
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
@ -16,23 +16,26 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_GAME_STATE_HPP
#define OUR_DICK_GAME_STATE_HPP
#ifndef OUR_DICK_TIC_TAC_TOE_HPP
#define OUR_DICK_TIC_TAC_TOE_HPP
#define TILE_COUNT 9
#include "engine/game.hpp"
#include "engine/game_state.hpp"
#include "config.hpp"
struct game_state {
int turn;
char board[TILE_COUNT];
#include <queue>
class tic_tac_toe : public egn::game
{
private:
bool m_game_over = false;
public:
tic_tac_toe(int width, int height);
bool is_game_over()const;
void on_notify(const egn::game_state_event& e)override;
};
int get_player_input();
int check_win_condition(const game_state& gs, char player);
void display_game_state(const game_state& gs);
void player_turn(game_state& gs, char player, int input);
void game_turn(game_state& gs,int player_input);
int exists_empty_tile(const game_state& gs);
#endif

View File

@ -31,7 +31,6 @@ namespace egn{
m_update_flag |= VIEW_UPDATE;
}
void camera_iface::set_orientation(const math::quaternion<GLfloat>& orient){
debug_print("%f, %f, %f, %f\n", m_orientation.w(), m_orientation.x(), m_orientation.y(), m_orientation.z());
object::set_orientation(orient);
m_update_flag |= VIEW_UPDATE;
}

102
src/engine/game.cpp Normal file
View File

@ -0,0 +1,102 @@
/**
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 "engine/game.hpp"
#include "graphics/gl_include.hpp" //TODO
#include "config.hpp"
namespace egn{
static void handle_input_events(GLFWwindow* window, int key, int, int action, int mods){
if(action == GLFW_PRESS){
debug_print_verbose("Keycode %d pressed\n", key);
}else if(action == GLFW_RELEASE){
debug_print_verbose("Keycode %d released\n", key);
}else{
debug_print_verbose("Keycode %d repeated\n", key);
}
game* g = reinterpret_cast<game*>(glfwGetWindowUserPointer(window));
g->keypress(key, action, mods);
}
static void handle_resize_event(GLFWwindow* window, int width, int height){
debug_print_verbose("Window resized to %dx%d\n", width, height);
game* g = reinterpret_cast<game*>(glfwGetWindowUserPointer(window));
g->resize(width, height);
}
static void handle_mouse_button_events(GLFWwindow* window, int button, int action, int mods){
if(action == GLFW_PRESS){
debug_print_verbose("Mouse button %d pressed\n", button);
}else{
debug_print_verbose("Mouse button %d released\n", button);
}
double x, y;
glfwGetCursorPos(window, &x, &y);
game* g = reinterpret_cast<game*>(glfwGetWindowUserPointer(window));
g->mouse_button(button, x, y, action, mods);
}
game::game(int cver_maj, int cver_min, int width, int height, const char* title):
m_window(cver_maj, cver_min, width, height, title)
{
setup_window_();
m_last_time = glfwGetTime();
}
void game::update(){
double this_time = glfwGetTime();
double time_diff = this_time - m_last_time;
m_last_time = this_time;
m_states_manager.handle_input(m_event_queue);
m_states_manager.update(time_diff);
}
void game::render(){
m_window.make_current();
m_window.framebuffer().bind();
m_window.framebuffer().clear_color_buffer();
m_window.framebuffer().clear_depth_buffer();
m_states_manager.render();
m_window.swap_buffers();
}
void game::mouse_button(int button, double xpos, double ypos, int action, int mods){
m_event_queue.emplace(input_event{input_event::type::MOUSE_BUTTON, glfwGetTime(), button, action, mods, xpos, ypos});
}
void game::keypress(int key, int action, int mods){
double x, y;
glfwGetCursorPos(m_window.raw(), &x, &y);
m_event_queue.emplace(input_event{input_event::type::KEY, glfwGetTime(), key, action, mods, x, y});
}
void game::resize(int width, int height){
m_event_queue.emplace(input_event{input_event::type::RESIZE, glfwGetTime(), 0, 0, 0, (double)width, (double)height});
}
void game::on_notify(const game_state_event&){}
void game::setup_window_(){
glfwSetWindowUserPointer(m_window, this);
glfwSetKeyCallback(m_window.raw(), handle_input_events); //TODO
glfwSetFramebufferSizeCallback(m_window.raw(), handle_resize_event);
glfwSetMouseButtonCallback(m_window.raw(), handle_mouse_button_events);
m_window.set_visible(true);
}
}

64
src/engine/game_state.cpp Normal file
View File

@ -0,0 +1,64 @@
/**
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 "engine/game_state.hpp"
#include "engine/input.hpp"
#include <queue> //queue
#include <memory> //unique_ptr
namespace egn{
void game_state_manager::push_state(std::unique_ptr<game_state_iface>&& state){
if(!m_state_stack.empty())
m_state_stack.top()->leave();
m_state_stack.push (std::move(state));
m_state_stack.top()->enter();
}
void game_state_manager::pop_state(){
m_state_stack.top()->leave();
m_state_stack.pop();
if(!m_state_stack.empty())
m_state_stack.top()->enter();
}
void game_state_manager::replace_state(std::unique_ptr<game_state_iface>&& state){
m_state_stack.top()->leave();
m_state_stack.top() = std::move(state);
m_state_stack.top()->enter();
}
void game_state_manager::handle_input(std::queue<egn::input_event>& events){
while(!events.empty() && !m_state_stack.empty()){
m_state_stack.top()->handle_input(events.front());
events.pop();
}
}
void game_state_manager::update(double time_diff){
if(!m_state_stack.empty())
m_state_stack.top()->update(time_diff);
}
void game_state_manager::render(){
if(!m_state_stack.empty())
m_state_stack.top()->render();
}
game_state_iface::game_state_iface(game_state_manager* owner):
m_owner(owner){}
}

View File

@ -27,8 +27,9 @@ namespace gfx{
image::image(const char* file, bool flip, int req_chan){
stbi_set_flip_vertically_on_load(flip);
m_data = stbi_load(file, &m_width, &m_height, &m_channels, req_chan);
if(!m_data)
if(!m_data){
debug_print_error("Unable to load image file %s\n", file);
}
}
image::image(const image& i):
m_width(i.m_width), m_height(i.m_height), m_channels(i.m_channels),

View File

@ -0,0 +1,50 @@
/**
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 "graphics/lightable.hpp"
namespace gfx{
void lightable::set_ambient(const math::vec3<GLfloat>& a){
m_ambient = a;
}
void lightable::set_diffuse(const math::vec3<GLfloat>& d){
m_diffuse = d;
}
void lightable::set_specular(const math::vec3<GLfloat>& s){
m_specular = s;
}
void lightable::set_shininess(GLfloat s){
m_shininess = s;
}
const math::vec3<GLfloat>& lightable::get_ambient()const{
return m_ambient;
}
const math::vec3<GLfloat>& lightable::get_diffuse()const{
return m_diffuse;
}
const math::vec3<GLfloat>& lightable::get_specular()const{
return m_specular;
}
GLfloat lightable::get_shininess()const{
return m_shininess;
}
}

133
src/graphics/mesh.cpp Normal file
View File

@ -0,0 +1,133 @@
/**
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 "graphics/mesh.hpp"
#include "config.hpp"
#include <cstdlib> //offsetof
#include <utility> //move
namespace gfx{
vertex_mesh::vertex_mesh(const std::vector<vertex>& verts):
m_vertices(verts),
m_vbo(&verts[0], sizeof(vertex) * verts.size(), vbo::usage::STATIC_DRAW)
{
m_vao.bind_buffer(m_vbo, 0, 0, sizeof(vertex));
configure();
}
vertex_mesh::vertex_mesh(std::vector<vertex>&& verts):
m_vertices(std::move(verts)),
m_vbo(&verts[0], sizeof(vertex) * verts.size(), vbo::usage::STATIC_DRAW)
{
m_vao.bind_buffer(m_vbo, 0, 0, sizeof(vertex));
configure();
}
vertex_mesh::vertex_mesh(const vertex* verts, size_t nverts):
m_vbo(verts, sizeof(vertex) * nverts, vbo::usage::STATIC_DRAW)
{
m_vertices.reserve(nverts);
for(size_t i = 0;i < nverts;++i)
m_vertices.push_back(verts[i]);
m_vao.bind_buffer(m_vbo, 0, 0, sizeof(vertex));
configure();
}
void vertex_mesh::configure(){
auto attrib = m_vao.get_attribute(0);
attrib.set_float_array(3, 0);
attrib.associate_with(0);
attrib.enable();
attrib = m_vao.get_attribute(1);
attrib.set_float_array(2, offsetof(vertex, tex_coords));
attrib.associate_with(0);
attrib.enable();
attrib = m_vao.get_attribute(2);
attrib.set_float_array(4, offsetof(vertex, color));
attrib.associate_with(0);
attrib.enable();
}
const vertex& vertex_mesh::get_vertex(size_t index)const{
return m_vertices[index];
}
void vertex_mesh::set_vertex(size_t index, const vertex& v){
m_vertices[index] = v;
}
void vertex_mesh::render(shader_program&){
m_vao.bind();
glDrawArrays(GL_TRIANGLES, 0, m_vertices.size());
}
material_mesh::material_mesh(const std::vector<const material*>& textures):
m_materials(textures){}
material_mesh::material_mesh(std::vector<const material*>&& textures):
m_materials(std::move(textures)){}
material_mesh::material_mesh(const material** texs, size_t ntexs){
m_materials.reserve(ntexs);
for(size_t i = 0;i < ntexs;++i){
m_materials.push_back(texs[i]);
}
}
const material* material_mesh::get_material(size_t index)const{
return m_materials[index];
}
void material_mesh::set_material(size_t index, const material& new_texture){
m_materials[index] = &new_texture;
}
void material_mesh::set_material(size_t index, const material* new_texture){
m_materials[index] = new_texture;
}
void material_mesh::render(shader_program& shader){
size_t tex1_loc = shader.get_uniform_loc("texture1");
for(size_t i = 0;i < m_materials.size();++i){
m_materials[i]->bind_unit(i);
shader.set_uniform(tex1_loc + i, *m_materials[i]);
}
}
unified_mesh::unified_mesh(vertex_mesh&& v, const material_mesh& m):
m_v_mesh(std::move(v)),
m_m_mesh(m){}
unified_mesh::unified_mesh(vertex_mesh&& v, material_mesh&& m):
m_v_mesh(std::move(v)),
m_m_mesh(std::move(m)){}
vertex_mesh& unified_mesh::vertex(){
return m_v_mesh;
}
const vertex_mesh& unified_mesh::vertex()const{
return m_v_mesh;
}
material_mesh& unified_mesh::material(){
return m_m_mesh;
}
const material_mesh& unified_mesh::material()const{
return m_m_mesh;
}
void unified_mesh::configure(){
m_v_mesh.configure();
}
void unified_mesh::render(shader_program& shader){
m_m_mesh.render(shader);
m_v_mesh.render(shader);
}
}

53
src/graphics/model.cpp Normal file
View File

@ -0,0 +1,53 @@
/**
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 "graphics/model.hpp"
#include <utility> //move
namespace gfx{
model::model(std::vector<unified_mesh>&& meshes):
m_meshes(std::move(meshes)){}
model::model(unified_mesh* meshes, size_t size){
m_meshes.reserve(size);
for(size_t i = 0;i < size;++i){
m_meshes.push_back(std::move(meshes[i]));
}
}
void model::configure(){
for(size_t i = 0;i < m_meshes.size();++i){
m_meshes[i].configure();
}
}
void model::add_mesh(unified_mesh&& m){
m_meshes.push_back(std::move(m));
}
unified_mesh& model::mesh(size_t index){
return m_meshes[index];
}
const unified_mesh& model::mesh(size_t index)const{
return m_meshes[index];
}
void model::render(shader_program& s){
for(size_t i = 0;i < m_meshes.size();++i){
m_meshes[i].render(s);
}
}
}

View File

@ -23,4 +23,6 @@
#define STBI_MALLOC(size) our_dick_stb_malloc(size)
#define STBI_FREE(data) our_dick_stb_free(data)
#define STBI_REALLOC_SIZED(data, olds, news) our_dick_stb_realloc(data, olds, news)
//stb_image itself is not subject to the above license notice
#include <stb/stb_image.h>

View File

@ -1,98 +0,0 @@
/**
This file is a part of our_dick
Copyright (C) 2020 r0nk
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/>.
*/
// 0 | 1 | 2
// ---------
// 3 | 4 | 5
// ---------
// 6 | 7 | 8
#include "game_state.hpp"
#include <cstdlib> //rand
#include <cstdio> //printf, fprintf
#define TILE_COUNT 9
int get_player_input(){
//TODO get player input
return rand() % 9;
}
int check_win_condition(const game_state& gs, char player){
int i;
for(i = 0 ; i < 3; i++){
//rows
if(gs.board[(i*3)] == player && gs.board[(i*3)+1] == player && gs.board[(i*3)+2] == player)
return 1;
//column
if(gs.board[i] == player && gs.board[i+3] == player && gs.board[i+6] == player)
return 1;
}
if(gs.board[0] == player && gs.board[4] == player && gs.board[8] == player)
return 1;
if(gs.board[2] == player && gs.board[4] == player && gs.board[6] == player)
return 1;
return 0;
}
void display_game_state(const game_state& gs){
printf("turn %i:\n", gs.turn);
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(gs.board[(i*3)+j])
printf("%c", gs.board[(i*3)+j]);
else
printf("_");
}
printf("\n");
}
}
void player_turn(game_state& gs, char player, int input){
if(input > TILE_COUNT && input < 0)
fprintf(stderr,"ERR: player input not in range.");
if(!gs.board[input])
gs.board[input] = player;
if(check_win_condition(gs, player)){
printf("player %c wins!\n", player);
gs.turn = -1;
}
}
void game_turn(game_state& gs,int player_input){
gs.turn++;
player_turn(gs, 'O', player_input);
display_game_state(gs);
if(gs.turn == -1)
return;
player_turn(gs, 'X', rand() % TILE_COUNT);
display_game_state(gs);
}
int exists_empty_tile(const game_state& gs){
int i;
for(i = 0; i < TILE_COUNT; i++)
if(!gs.board[i])
return 1;
return 0;
}

View File

@ -1,6 +1,10 @@
/**
This file is a part of our_dick
Copyright (C) 2020 ATLAS_Moon, rexy712, r0nk
Copyright (C) 2020 rexy712, r0nk
r0nk owns 3 lines in this file
int main(){
srand(time(NULL));
}
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,243 +20,22 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cstring>
#include <cstdlib> //srand
#include <ctime> //time
#include "game_state.hpp"
#include "config.hpp"
#include "graphics/vao.hpp"
#include "graphics/vbo.hpp"
#include "graphics/shader_program.hpp"
#include "graphics/fbo.hpp"
#include "graphics/window.hpp"
#include "engine/camera.hpp"
#include "math/debug.hpp"
void handle_input_events(GLFWwindow* window, int key, int, int, int){
debug_print("[II] Recieved keycode %d\n", key);
if(key == 256)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
void handle_resize_event(GLFWwindow*, int width, int height){
debug_print("Window resized to %dx%d\n", width, height);
glViewportIndexedf(0, 0, 0, width, height);
}
gfx::shader_program create_screen_shader(){
static constexpr const char vertex_source[] = "#version 330 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 const char fragment_source[] = "#version 330 core\n"
"out vec4 FragColor;"
"in vec2 frag_tex_coords;"
"uniform sampler2D fbtexture;"
"void main(){"
"FragColor = mix(vec4(1,0,0,1),texture(fbtexture, frag_tex_coords), 0.9);"
"}";
gfx::shader_program prog;
gfx::shader vert(vertex_source, gfx::shader::type::VERTEX);
gfx::shader frag(fragment_source, gfx::shader::type::FRAGMENT);
bool error = false;
if(vert.has_compile_error()){
debug_print_error("%s\n", vert.get_error().c_str());
error = true;
}
if(frag.has_compile_error()){
debug_print_error("%s\n", frag.get_error().c_str());
error = true;
}
if(error)
return prog;
prog.attach_shaders(vert, frag);
prog.link();
if(prog.has_link_error()){
debug_print_error("%s\n", prog.get_error().c_str());
}
return prog;
}
class sprite_shader : public gfx::shader_program
{
private:
static constexpr char s_vertex_shader_text[] = "#version 330 core\n"
"layout (location = 0) in vec2 position;"
"layout (location = 1) in vec2 tex_coords;"
"layout (location = 2) in vec4 color;"
"out vec2 frag_tex_coords;"
"out vec4 frag_vertex_color;"
"uniform mat4 model_mat;"
"uniform mat4 vp_mat;"
"void main(){"
"gl_Position = vp_mat * model_mat * vec4(position, -1.0, 1.0);"
"frag_tex_coords = tex_coords;"
"frag_vertex_color = color;"
"}";
static constexpr char s_fragment_shader_text[] = "#version 330 core\n"
"out vec4 FragColor;"
"in vec2 frag_tex_coords;"
"in vec4 frag_vertex_color;"
"uniform sampler2D texture1;"
"void main(){"
"FragColor = texture(texture1, frag_tex_coords);"
"}";
public:
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();
}
};
struct vertex {
math::vec2<GLfloat> pos;
math::vec2<GLfloat> tex_coords;
math::vec4<GLfloat> color;
};
struct square : public egn::object{
vertex vertices[6];
gfx::texture& tex_handle;
gfx::vao vao;
gfx::vbo buffer;
explicit square(gfx::texture& tex):
tex_handle(tex),
buffer(sizeof(GLfloat) * 48, gfx::vbo::usage::DYNAMIC_DRAW)
{
vertices[0].pos = {-1.0f, 1.0f};
vertices[1].pos = {-1.0f, -1.0f};
vertices[2].pos = {1.0f, -1.0f};
vertices[3].pos = {1.0f, -1.0f};
vertices[4].pos = {1.0f, 1.0f};
vertices[5].pos = {-1.0f, 1.0f};
vertices[0].tex_coords = {0.0f, 1.0f};
vertices[1].tex_coords = {0.0f, 0.0f};
vertices[2].tex_coords = {1.0f, 0.0f};
vertices[3].tex_coords = {1.0f, 0.0f};
vertices[4].tex_coords = {1.0f, 1.0f};
vertices[5].tex_coords = {0.0f, 1.0f};
vertices[0].color = {0.0f, 0.0f, 0.0f, 0.0f};
vertices[1].color = {0.0f, 0.0f, 0.0f, 0.0f};
vertices[2].color = {0.0f, 0.0f, 0.0f, 0.0f};
vertices[3].color = {0.0f, 0.0f, 0.0f, 0.0f};
vertices[4].color = {0.0f, 0.0f, 0.0f, 0.0f};
vertices[5].color = {0.0f, 0.0f, 0.0f, 0.0f};
vao.bind_buffer(buffer, 0, 0, sizeof(GLfloat) * 8);
auto attrib = vao.get_attribute(0);
attrib.set_float_array(2, 0);
attrib.associate_with(0);
attrib.enable();
attrib = vao.get_attribute(1);
attrib.set_float_array(2, 2);
attrib.associate_with(0);
attrib.enable();
attrib = vao.get_attribute(2);
attrib.set_float_array(4, 4);
attrib.associate_with(0);
attrib.enable();
}
void render(gfx::shader_program& shader){
{
auto mapping = buffer.map(gfx::vbo::maptype::WRITE);
char* data = reinterpret_cast<char*>(mapping.raw());
for(size_t i = 0;i < sizeof(vertices) / sizeof(vertices[0]);++i){
memcpy(data, vertices[i].pos, sizeof(GLfloat) * 2);
data += sizeof(GLfloat) * 2;
memcpy(data, vertices[i].tex_coords, sizeof(GLfloat) * 2);
data += sizeof(GLfloat) * 2;
memcpy(data, vertices[i].color, sizeof(GLfloat) * 4);
data += sizeof(GLfloat) * 4;
}
}
vao.bind();
shader.set_uniform("texture1", tex_handle);
shader.set_uniform("model_mat", get_model_matrix());
glDrawArrays(GL_TRIANGLES, 0, 6);
buffer.clear();
}
};
struct scene {
std::vector<square> squares;
};
class renderer
{
private:
gfx::fbo m_framebuffer;
gfx::texture m_fb_colorbuffer;
gfx::rbo m_fb_depthbuffer;
sprite_shader m_sprite_shader;
egn::ortho_camera m_cam;
public:
renderer(int width, int height):
m_fb_colorbuffer(GL_RGBA, width, height, GL_UNSIGNED_BYTE),
m_fb_depthbuffer(640, 480, GL_DEPTH24_STENCIL8),
m_cam(10, 10, 1, 50){}
void render(scene& s){
//m_framebuffer.bind();
m_sprite_shader.use();
m_sprite_shader.set_uniform("vp_mat", m_cam.get_projection_matrix()*m_cam.get_view_matrix());
for(auto it = s.squares.begin();it != s.squares.end();++it){
it->render(m_sprite_shader);
}
}
};
#include "tic_tac_toe.hpp"
int main(){
srand(time(NULL));
game_state gs = {};
//window testing setup
gfx::window window(4, 5, 640, 480, "Tic-Tac-Gugh");
glfwSetKeyCallback(window.raw(), handle_input_events); //TODO
glfwSetFramebufferSizeCallback(window.raw(), handle_resize_event);
window.make_current();
window.set_visible(true);
tic_tac_toe game(640, 480);
gfx::texture tex2(gfx::image("assets/images/x.jpg"));
scene s;
s.squares.push_back(square{tex2});
renderer test(window.get_width(), window.get_height());
while(!game.is_game_over()){
game.update();
game.render();
while(!window.should_close()){
window.framebuffer().bind();
window.framebuffer().clear_color_buffer();
window.framebuffer().clear_depth_buffer();
test.render(s);
window.swap_buffers();
glfwPollEvents();
}
while(exists_empty_tile(gs) && gs.turn != -1){
game_turn(gs, get_player_input());
}
}

47
src/pause_state.cpp Normal file
View File

@ -0,0 +1,47 @@
/**
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 "pause_state.hpp"
#include "graphics/gl_include.hpp" //TODO
#include "config.hpp"
pause_state::pause_state(egn::game_state_manager& owner, egn::game_state_iface* under):
game_state_iface(&owner),
m_under_state(under){}
void pause_state::enter(){}
void pause_state::leave(){}
void pause_state::handle_input(const egn::input_event& event){
if(event.ev_type == egn::input_event::type::KEY){
if(event.key == GLFW_KEY_SPACE && !event.mods && event.action == GLFW_PRESS){
debug_print("Exiting pause state\n");
m_owner->pop_state();
return;
}else if(event.key == GLFW_KEY_ESCAPE && !event.mods && event.action == GLFW_PRESS){
m_owner->notify({egn::game_state_event::state_type::SHOULD_CLOSE, 0, 0});
}
}else if(event.ev_type == egn::input_event::type::RESIZE){
m_under_state->handle_input(event);
}
}
void pause_state::update(double){}
void pause_state::render(){
m_under_state->render();
}

180
src/play_state.cpp Normal file
View File

@ -0,0 +1,180 @@
/**
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 "play_state.hpp"
#include <queue>
#include "pause_state.hpp"
#include "graphics/gl_include.hpp" //TODO: separate key definitions from GLFW
#include "graphics/image.hpp" //TODO: move image to engine namespace
#include "math/math.hpp"
#include "square.hpp"
#include "config.hpp"
play_state::play_state(egn::game_state_manager& 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_current_player(rand() % 2)
{
debug_print("First player chosen is %c\n", m_current_player ? 'X' : 'O');
m_scene.textures.emplace_back(gfx::image("assets/images/blank.jpg", true));
m_scene.textures.emplace_back(gfx::image("assets/images/o.jpg", true));
m_scene.textures.emplace_back(gfx::image("assets/images/x.jpg", true));
m_scene.models.emplace_back(new gfx::model{gfx::unified_mesh(square_mesh(m_scene.textures[0],
m_scene.textures[1],
m_scene.textures[2]))});
gfx::model& sm = *m_scene.models[0];
for(int i = 0;i < 9;++i){
m_scene.renderables.emplace_back(new square(&sm));
}
m_scene.renderables[0]->set_position({-2, 2, -1});
m_scene.renderables[1]->set_position({-2, 0, -1});
m_scene.renderables[2]->set_position({-2, -2, -1});
m_scene.renderables[3]->set_position({ 0, 2, -1});
m_scene.renderables[4]->set_position({ 0, 0, -1});
m_scene.renderables[5]->set_position({ 0, -2, -1});
m_scene.renderables[6]->set_position({ 2, 2, -1});
m_scene.renderables[7]->set_position({ 2, 0, -1});
m_scene.renderables[8]->set_position({ 2, -2, -1});
m_scene.cameras.emplace_back(new egn::ortho_camera(10 * ((float)width / height), 10, 0.1, 50));
m_main_camera = static_cast<egn::ortho_camera*>(m_scene.cameras[0].get());
m_main_camera->set_position({0, 0, 10});
m_last_time = glfwGetTime();
}
void play_state::enter(){}
void play_state::leave(){}
void play_state::handle_input(const egn::input_event& ev){
if(ev.ev_type == egn::input_event::type::KEY){
if(ev.key == GLFW_KEY_SPACE && !ev.mods && ev.action == GLFW_PRESS){
debug_print("Entering pause state\n");
m_owner->push_state(std::unique_ptr<game_state_iface>(new pause_state(*m_owner, this)));
return;
}else if(ev.key == GLFW_KEY_ESCAPE && !ev.mods && ev.action == GLFW_PRESS){
m_owner->notify({egn::game_state_event::state_type::SHOULD_CLOSE, 0, 0});
}
}else if(ev.ev_type == egn::input_event::type::RESIZE){
if(ev.x > ev.y){
m_main_camera->set_projection_box(10 * (float)ev.x / ev.y, 10);
}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);
}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();
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();
math::vec3<GLfloat> projected1 = math::unproject(vp_mat, vp_coords, viewport);
vp_coords[2] = -1.0f;
//TODO actual 3D ray picking
//math::vec3<GLfloat> projected2 = math::unproject(vp_mat, vp_coords, viewport);
debug_print("projected click vector: (%f, %f, %f)\n",
projected1.x(), projected1.y(), projected1.z());
for(size_t i = 0;i < m_scene.renderables.size();++i){
square& sq = static_cast<square&>(*m_scene.renderables[i]);
const math::vec3<GLfloat>& sq_pos = sq.get_position();
if((projected1.x() > (sq_pos.x() - 1.0f) && projected1.x() < (sq_pos.x() + 1.0f)) &&
(projected1.y() > (sq_pos.y() - 1.0f) && projected1.y() < (sq_pos.y() + 1.0f)))
{
debug_print_succ("Clicked on square %lu\n", i);
if(sq.get_value() == square::value::BLANK)
sq.set_value(((++m_current_player) %= 2) ? square::value::O : square::value::X);
break;
}
}
}
}
}
void play_state::update(double time_diff){
square::value winner = check_win_condition_();
if(winner != square::value::BLANK){
printf("Winner is %c\n", winner == square::value::O ? 'O' : 'X');
m_owner->notify({egn::game_state_event::state_type::SHOULD_CLOSE, 0, 0});
}else if(is_board_full_()){
printf("Game is a tie\n");
m_owner->notify({egn::game_state_event::state_type::SHOULD_CLOSE, 0, 0});
}
}
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);
}
square::value play_state::check_win_condition_(){
square::value v = square::value::O;
for(int i = 0;i < 2;++i){
for(int j = 0;j < 3;++j){
//check columns
bool valid = true;
for(int l = 0;l < 3;++l){
if(static_cast<square&>(*m_scene.renderables[j*3 + l]).get_value() != v){
valid = false;
break;
}
}
if(valid)
return v;
//check rows
valid = true;
for(int l = 0;l < 3;++l){
if(static_cast<square&>(*m_scene.renderables[j + l*3]).get_value() != v){
valid = false;
break;
}
}
if(valid)
return v;
}
//check diagonals
int offset = 0;
int stride = 4;
for(int k = 0;k < 2;++k){
bool valid = true;
for(int l = 0;l < 3;++l){
if(static_cast<square&>(*m_scene.renderables[l * stride + offset]).get_value() != v){
valid = false;
break;
}
}
if(valid)
return v;
offset = 2;
stride = 2;
}
v = square::value::X;
}
return square::value::BLANK;
}
bool play_state::is_board_full_(){
for(size_t i = 0;i < m_scene.renderables.size();++i){
if(static_cast<square&>(*m_scene.renderables[i]).get_value() == square::value::BLANK)
return false;
}
return true;
}

74
src/screen_renderer.cpp Normal file
View File

@ -0,0 +1,74 @@
/**
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 "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()),
m_screen_width(width), m_screen_height(height)
{
//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);
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 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);
glDisable(GL_DEPTH_TEST);
glViewport(0, 0, m_screen_width, m_screen_height);
//Apply our shader and vao
m_screen_shader.use();
m_screen_shader.set_uniform("texture1", m_texture, 0);
m_vao.bind();
//Draw
glDrawArrays(GL_TRIANGLES, 0, 6);
}
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};
}

26
src/screen_shader.cpp Normal file
View File

@ -0,0 +1,26 @@
/**
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 "screen_shader.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))
{
link();
}

56
src/sprite_renderer.cpp Normal file
View File

@ -0,0 +1,56 @@
/**
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();
}

26
src/sprite_shader.cpp Normal file
View File

@ -0,0 +1,26 @@
/**
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();
}

54
src/square.cpp Normal file
View File

@ -0,0 +1,54 @@
/**
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 "square.hpp"
#include "graphics/gl_include.hpp"
#include <utility> //swap
gfx::unified_mesh square_mesh(const gfx::material& blank, const gfx::material& o, const gfx::material& x){
static constexpr gfx::vertex s_vertices[] = {
{{-1.0f, 1.0f, 0.0f}, {}, {0.0f, 1.0f}},
{{-1.0f, -1.0f, 0.0f}, {}, {0.0f, 0.0f}},
{{ 1.0f, -1.0f, 0.0f}, {}, {1.0f, 0.0f}},
{{ 1.0f, -1.0f, 0.0f}, {}, {1.0f, 0.0f}},
{{ 1.0f, 1.0f, 0.0f}, {}, {1.0f, 1.0f}},
{{-1.0f, 1.0f, 0.0f}, {}, {0.0f, 1.0f}}
};
const gfx::material* mats[] = {&blank, &o, &x};
return gfx::unified_mesh{{s_vertices, sizeof(s_vertices) / sizeof(s_vertices[0])}, {mats, sizeof(mats) / sizeof(mats[0])}};
}
square::square(gfx::model* model):
m_model(model){}
void square::set_value(value t){
m_active_value = t;
}
auto square::get_value()const -> value{
return m_active_value;
}
void square::render(gfx::shader_program& shader){
shader.set_uniform("model_mat", get_model_matrix());
shader.set_uniform("texture1", *m_model->mesh(0).material().get_material((int)m_active_value));
m_model->mesh(0).vertex().render(shader);
}

41
src/tic_tac_toe.cpp Normal file
View File

@ -0,0 +1,41 @@
/**
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 "tic_tac_toe.hpp"
#include "engine/game_state.hpp"
#include "play_state.hpp"
#include "config.hpp"
tic_tac_toe::tic_tac_toe(int width, int height):
egn::game(4, 5, width, height, "Tic-Tac-Gugh")
{
m_window.make_current();
m_states_manager.add_observer(*this);
m_states_manager.push_state(std::unique_ptr<egn::game_state_iface>(new play_state(m_states_manager, width, height)));
}
bool tic_tac_toe::is_game_over()const{
return m_game_over;
}
void tic_tac_toe::on_notify(const egn::game_state_event& e){
if(e.type == egn::game_state_event::state_type::SHOULD_CLOSE){
debug_print("Game recieved should close notification\n");
m_game_over = true;
}
}