Skip to content

Commit

Permalink
Started working on the game menu, needed to make gui, cascaded into n…
Browse files Browse the repository at this point in the history
…eeding to make an event system.
  • Loading branch information
Avopeac committed Nov 21, 2017
1 parent 92d820d commit 18a0667
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 68 deletions.
Binary file modified project/.vs/project/v15/.suo
Binary file not shown.
5 changes: 5 additions & 0 deletions project/project.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<ClCompile Include="..\src\font_renderer.cpp" />
<ClCompile Include="..\src\frame_buffer.cpp" />
<ClCompile Include="..\src\graphics.cpp" />
<ClCompile Include="..\src\gui_canvas.cpp" />
<ClCompile Include="..\src\keymap.cpp" />
<ClCompile Include="..\src\main.cpp" />
<ClCompile Include="..\src\map_parser.cpp" />
Expand All @@ -161,8 +162,12 @@
<ClInclude Include="..\src\component.h" />
<ClInclude Include="..\src\entity_manager.h" />
<ClInclude Include="..\src\entity_types.h" />
<ClInclude Include="..\src\event.h" />
<ClInclude Include="..\src\event_test.h" />
<ClInclude Include="..\src\font.h" />
<ClInclude Include="..\src\font_renderer.h" />
<ClInclude Include="..\src\abstract_gui_object.h" />
<ClInclude Include="..\src\gui_canvas.h" />
<ClInclude Include="..\src\map_data.h" />
<ClInclude Include="..\src\map_parser.h" />
<ClInclude Include="..\src\map_view.h" />
Expand Down
24 changes: 21 additions & 3 deletions project/project.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<ClCompile Include="..\src\map_view.cpp">
<Filter>Source\Game</Filter>
</ClCompile>
<ClCompile Include="..\src\gui_canvas.cpp">
<Filter>Source\Graphics.Gui</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\graphics.h">
Expand Down Expand Up @@ -175,9 +178,6 @@
<ClInclude Include="..\src\text_data_pipe.h">
<Filter>Source\Graphics.Data</Filter>
</ClInclude>
<ClInclude Include="..\src\font_renderer.h">
<Filter>Source\Graphics</Filter>
</ClInclude>
<ClInclude Include="..\src\text_component.h">
<Filter>Source\Entity.Component</Filter>
</ClInclude>
Expand All @@ -193,6 +193,21 @@
<ClInclude Include="..\src\map_data.h">
<Filter>Source\Game</Filter>
</ClInclude>
<ClInclude Include="..\src\font_renderer.h">
<Filter>Source\Graphics</Filter>
</ClInclude>
<ClInclude Include="..\src\abstract_gui_object.h">
<Filter>Source\Graphics.Gui</Filter>
</ClInclude>
<ClInclude Include="..\src\gui_canvas.h">
<Filter>Source\Graphics.Gui</Filter>
</ClInclude>
<ClInclude Include="..\src\event.h">
<Filter>Source\Util</Filter>
</ClInclude>
<ClInclude Include="..\src\event_test.h">
<Filter>Source\Util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Source">
Expand Down Expand Up @@ -243,5 +258,8 @@
<Filter Include="Source\Game">
<UniqueIdentifier>{f1157bd4-5711-4098-9f9f-66e0021a15f9}</UniqueIdentifier>
</Filter>
<Filter Include="Source\Graphics.Gui">
<UniqueIdentifier>{6acb3dec-15d1-4a17-9ae4-9ffb78466e30}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
30 changes: 30 additions & 0 deletions src/abstract_gui_object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <vector>
#include <memory>

namespace graphics {

class AbstractGuiObject
{

protected:

std::shared_ptr<AbstractGuiObject> parent_;

std::vector<std::shared_ptr<AbstractGuiObject>> children_;

AbstractGuiObject() = default;

public:

virtual ~AbstractGuiObject() = default;

virtual void Initialize() = 0;

virtual void Draw() = 0;

virtual void Destroy() = 0;
};
}

54 changes: 54 additions & 0 deletions src/event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <vector>
#include <memory>

namespace util {

template <typename EventData> class EventSubscriber
{

public:

EventSubscriber() = default;

virtual ~EventSubscriber() = default;

virtual void Update(const EventData &data) = 0;

};

template <typename EventData> class Event
{

std::vector<std::weak_ptr<EventSubscriber<EventData>>> subscribers_;

public:

Event() = default;

~Event() = default;

void Publish(const EventData &data)
{

for (auto it = subscribers_.begin(); it != subscribers_.end(); ++it)
{
if (auto shared = it->lock())
{
shared->Update(data);
}
else
{
it = subscribers_.erase(it);
}
}
}

void Subscribe(const std::shared_ptr<EventSubscriber<EventData>> &subscriber)
{
subscribers_.push_back(subscriber);
}

};
}
72 changes: 72 additions & 0 deletions src/event_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once

#include <string>

#include <iostream>

#include "event.h"

namespace util {

struct EventTestData
{
float a;
uint32_t b;
std::string c;
};

class EventTestSubscriber : public EventSubscriber<EventTestData>
{
std::string name_;

public:

EventTestSubscriber(const std::string &name) : name_(name) {}

virtual ~EventTestSubscriber() = default;

virtual void Update(const EventTestData &data) override
{
std::cout << "Published to " << name_ << ": " << data.a << " " << data.b << " " << data.c << "." << std::endl;
}
};

class EventTest
{

public:

EventTest() = default;

~EventTest() = default;

void Run()
{
auto sub_0 = std::make_shared<EventTestSubscriber>("Olaf");
auto sub_1 = std::make_shared<EventTestSubscriber>("Jens");
auto sub_2 = std::make_shared<EventTestSubscriber>("Urban");
auto sub_3 = std::make_shared<EventTestSubscriber>("Ronja");

EventTestData data_0;
data_0.a = 2.0f;
data_0.b = 5;
data_0.c = "Urgent Message";

Event<EventTestData> e;

e.Subscribe(sub_0);
e.Subscribe(sub_1);

e.Publish(data_0);

e.Subscribe(sub_2);
e.Subscribe(sub_3);

data_0.a = 1.0;
data_0.b = 1000;
data_0.c = "Unimportant Message";

e.Publish(data_0);
}
};
}
32 changes: 6 additions & 26 deletions src/font_renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "font_renderer.h"

#include "glm/gtc/matrix_transform.hpp"

#include "data_pipe_hub.h"

#include "renderer.h"

#include "glm/gtc/matrix_transform.hpp"
#include "fullscreen_quad.h"

using namespace graphics;

Expand All @@ -26,31 +28,10 @@ FontRenderer::FontRenderer(const GraphicsBase & graphics_base) :
0.0f,
(float)graphics_base_.GetBackbufferHeight(),
-1.0f, 1.0f);

glGenBuffers(1, &vbo_);
glGenBuffers(1, &ebo_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_);
glBufferData(GL_ARRAY_BUFFER, 4 * vertex_size, &quad_vertices_[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * index_size, &quad_indices_[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &vao_);
glBindVertexArray(vao_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, GLsizei(vertex_size), (void *)0);
glVertexAttribDivisor(0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}

FontRenderer::~FontRenderer()
{
glDeleteVertexArrays(1, &vao_);
glDeleteBuffers(1, &vbo_);
glDeleteBuffers(1, &ebo_);
}

void FontRenderer::Draw(float delta_time)
Expand All @@ -60,8 +41,7 @@ void FontRenderer::Draw(float delta_time)

render_target->BindDraw(0, 0, 0, 0, 0);


glBindVertexArray(vao_);
FullscreenQuad::Get().Begin();

pipeline_.Bind();

Expand Down Expand Up @@ -123,7 +103,7 @@ void FontRenderer::Draw(float delta_time)
default_frag_program_->SetUniform("u_texture", &texture_index);
}

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
FullscreenQuad::Get().DrawElements();

position.x += glyph->advance;

Expand All @@ -140,5 +120,5 @@ void FontRenderer::Draw(float delta_time)

pipeline_.Unbind();

glBindVertexArray(0);
FullscreenQuad::Get().End();
}
17 changes: 0 additions & 17 deletions src/font_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ namespace graphics
{
class FontRenderer
{

const glm::vec2 quad_vertices_[4] =
{
glm::vec2(0.0f, 0.0f),
glm::vec2(1.0f, 0.0f),
glm::vec2(0.0f, 1.0f),
glm::vec2(1.0f, 1.0f),
};

const Uint32 quad_indices_[6] =
{
0, 1, 2,
2, 1, 3
};

const GraphicsBase &graphics_base_;

ProgramPipeline pipeline_;
Expand All @@ -35,8 +20,6 @@ namespace graphics

Program * default_frag_program_;

GLuint vao_, vbo_, ebo_;

glm::mat4 proj_;

public:
Expand Down
Empty file added src/gui_canvas.cpp
Empty file.
18 changes: 18 additions & 0 deletions src/gui_canvas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "glm/glm.hpp"

#include "abstract_gui_object.h"

namespace graphics {

class GuiCanvas : public AbstractGuiObject
{
public:

GuiCanvas(const glm::ivec2 &offset, const glm::ivec2 &size);

~GuiCanvas();

};
}
Loading

0 comments on commit 18a0667

Please sign in to comment.