Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Jul 3, 2024
1 parent bb2d8e8 commit 033c6c4
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 57 deletions.
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ project(unlogic)
set(CMAKE_CXX_STANDARD 23)

# External Libs
find_package(SFML 2.6.1 REQUIRED)
find_package(GTest REQUIRED)
find_package(BISON 3.8.2 REQUIRED)
find_package(FLEX 2.6.4 REQUIRED)
Expand Down Expand Up @@ -64,16 +63,15 @@ add_library(unlogic-graphic
src/graphic/Shape.h
src/graphic/Math.h
src/graphic/Color.h
src/graphic/Color.cpp
)
target_link_libraries(unlogic-graphic PUBLIC unlogic sfml-graphics)
target_link_libraries(unlogic-graphic PUBLIC unlogic)

add_executable(unlogic-calculator
src/calculator/main.cpp
src/calculator/ui/Window.cpp
src/calculator/ui/Window.h
src/calculator/ui/SourceEditor.h
src/calculator/ui/SFMLWidget.h
src/calculator/ui/GlAreaEx.h
)
target_link_libraries(unlogic-calculator PUBLIC unlogic unlogic-graphic ${GTKMM_LIBRARIES})
target_link_directories(unlogic-calculator PUBLIC ${GTKMM_LIBRARY_DIRS})
Expand Down
2 changes: 0 additions & 2 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[requires]
benchmark/1.8.3
gtest/1.14.0
sfml/2.6.1

[generators]
CMakeDeps
Expand Down
23 changes: 23 additions & 0 deletions src/calculator/ui/GlAreaEx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by nathan on 7/3/24.
//

#ifndef UNLOGIC_GLAREAEX_H
#define UNLOGIC_GLAREAEX_H

#include <gtkmm.h>
#include "graphic/RenderTarget.h"

namespace unlogic
{
class GlAreaEx : public Gtk::GLArea, public mf::RenderTarget
{
public:
mf::Vector2u GetSize() const override
{
return {(unsigned)this->get_width(), (unsigned)this->get_height()};
}
};
} // unlogic

#endif //UNLOGIC_GLAREAEX_H
23 changes: 0 additions & 23 deletions src/calculator/ui/SFMLWidget.h

This file was deleted.

8 changes: 4 additions & 4 deletions src/calculator/ui/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <gtkmm.h>
#include <epoxy/gl.h>
#include "SourceEditor.h"
#include "SFMLWidget.h"
#include "GlAreaEx.h"
#include "graphic/Graph.h"

namespace unlogic
Expand All @@ -16,14 +16,14 @@ namespace unlogic
Gtk::Paned divider_;

SourceEditor source_editor_;
SFMLWidget renderer_;
GlAreaEx renderer_;

Graph graph_;

bool render(const Glib::RefPtr<Gdk::GLContext>& context)
{
this->renderer_.clear(sf::Color::White);
this->renderer_.draw(this->graph_);
this->renderer_.Clear(mf::Color::White);
this->renderer_.Draw(this->graph_);

return true;
}
Expand Down
4 changes: 0 additions & 4 deletions src/graphic/Color.cpp

This file was deleted.

9 changes: 9 additions & 0 deletions src/graphic/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ namespace mf

static const Color White;
static const Color Black;
static const Color Red;
static const Color Green;
static const Color Blue;

constexpr Color(float r, float g, float b, float a = 1.f) : r(r), g(g), b(b), a(a) {}
};

inline constexpr Color Color::White(1.f, 1.f, 1.f, 1.f);
inline constexpr Color Color::Black(0.f, 0.f, 0.f, 1.f);
inline constexpr Color Color::Red(1.f, 0.f, 0.f, 1.f);
inline constexpr Color Color::Green(0.f, 1.f, 0.f, 1.f);
inline constexpr Color Color::Blue(0.f, 0.f, 1.f, 1.f);
}

#endif //UNLOGIC_COLOR_H
4 changes: 2 additions & 2 deletions src/graphic/Drawable.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef UNLOGIC_DRAWABLE_H
#define UNLOGIC_DRAWABLE_H

#include "graphic/RenderTarget.h"

namespace mf
{
class RenderTarget;

class Drawable
{
public:
Expand Down
10 changes: 6 additions & 4 deletions src/graphic/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ using namespace unlogic;
void Graph::Draw(mf::RenderTarget &target) const
{
// Create new view
mf::View const &view = target.getView();
auto [width, height] = view.getSize();
mf::View const &view = target.GetView();
auto [width, height] = view.GetSize();

mf::Vector2f const &center = view.getCenter();
mf::Vector2f const &center = view.GetCenter();
mf::Vector2f domain(center.x - (width / 2), center.x + (width / 2));
mf::Vector2f range(center.y - (height / 2), center.y + (height / 2));

/*
// Draw Axis
sf::RectangleShape x_axis({width, this->axis_thickness_});
x_axis.setFillColor(sf::Color::Black);
Expand Down Expand Up @@ -51,6 +52,7 @@ void Graph::Draw(mf::RenderTarget &target) const
target.Draw(gridline);
}
*/

// Draw Plots
for(auto const &plot : this->plots_)
Expand All @@ -61,7 +63,7 @@ void Graph::Draw(mf::RenderTarget &target) const

void Plot::Draw(mf::RenderTarget &target) const
{
target.Draw(this->vertices_.data(), this->vertices_.size(), sf::TrianglesStrip);
//target.Draw(this->vertices_.data(), this->vertices_.size(), sf::TrianglesStrip);
}

void Plot::update()
Expand Down
22 changes: 11 additions & 11 deletions src/graphic/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ namespace unlogic
class Plot : public mf::Drawable
{
unlogic::Callable<double> function_;
sf::Color color_;
mf::Color color_;
double precision_ = 0.1;
double thickness_ = 0.1;
std::string title_ = "My Plot";

sf::Vector2f domain_;
mf::Vector2f domain_;

std::vector<sf::Vertex> vertices_;
std::vector<sf::Vector2f> points_;
std::vector<mf::Vertex> vertices_;
std::vector<mf::Vector2f> points_;

protected:
void update();
void Draw(mf::RenderTarget &target) const override;

public:
Plot(unlogic::Callable<double> function, sf::Vector2f domain, sf::Color color = sf::Color::Red) : function_(std::move(function)), domain_(domain), color_(color)
Plot(unlogic::Callable<double> function, mf::Vector2f domain, mf::Color color = mf::Color::Red) : function_(std::move(function)), domain_(domain), color_(color)
{
this->update();
}
Expand All @@ -51,11 +51,11 @@ namespace unlogic
{
unlogic::Compiler compiler_;
std::vector<Plot> plots_;
sf::Vector2f domain_;
sf::View view_;
mf::Vector2f domain_;
mf::View view_;
float axis_thickness_ = 0.1f;

sf::Color color_wheel_[4] = {sf::Color::Red, sf::Color::Blue, sf::Color::Cyan, sf::Color::Green};
mf::Color color_wheel_[3] = {mf::Color::Red, mf::Color::Blue, mf::Color::Green};
int wheel_index_ = 0;

protected:
Expand All @@ -68,17 +68,17 @@ namespace unlogic
this->plots_.emplace_back(this->compiler_.CompileFunction<double>(function), this->domain_, color);
}

Graph(std::initializer_list<std::string> functions, sf::Vector2f domain = {-10.0, 10.0}) : domain_(domain)
Graph(std::initializer_list<std::string> functions, mf::Vector2f domain = {-100.0, 100.0}) : domain_(domain)
{
for(auto const &function : functions)
{
this->AddPlot(function);
}
}

Graph(std::string const &function, sf::Vector2f domain = {-10.0, 10.0}) : Graph(std::initializer_list<std::string>{function}, domain) {}
Graph(std::string const &function, mf::Vector2f domain = {-100.0, 100.0}) : Graph(std::initializer_list<std::string>{function}, domain) {}

Graph(sf::Vector2f domain = {-10.0, 10.0}) : domain_(domain) {};
Graph(mf::Vector2f domain = {-100.0, 100.0}) : domain_(domain) {};
};
}

Expand Down
12 changes: 12 additions & 0 deletions src/graphic/Math.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
#ifndef UNLOGIC_MATH_H
#define UNLOGIC_MATH_H

#include "graphic/Color.h"

namespace mf
{
template<typename T>
struct Vector
{
T x;
T y;

Vector(T x, T y) : x(x), y(y) {}
};

typedef Vector<float> Vector2f;
typedef Vector<double> Vector2d;
typedef Vector<int> Vector2i;
typedef Vector<unsigned> Vector2u;

struct Vertex
{
Vector2f position;
Color color;

Vertex(Vector2f position, Color color = Color::Red) : position(std::move(position)), color(std::move(color)) {}
};
}

#endif //UNLOGIC_MATH_H
54 changes: 51 additions & 3 deletions src/graphic/RenderTarget.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,66 @@
#ifndef UNLOGIC_RENDERTARGET_H
#define UNLOGIC_RENDERTARGET_H

#include "graphic/Drawable.h"
#include <epoxy/gl.h>
#include "graphic/View.h"
#include "graphic/Math.h"
#include "graphic/Color.h"

namespace mf
{
class Drawable;

enum class Primitive
{
kTriangleStrip = GL_TRIANGLE_STRIP,
};

class RenderTarget
{
View view_;

protected:
void UpdateViewport()
{
// TODO: Implement
}

void Initialize()
{
this->UpdateViewport();
}

public:
void Draw(Drawable const &drawable);
void Draw();
void Draw(Drawable const &drawable) {};

void Draw(std::vector<Vertex> const &vertices, Primitive primitive)
{
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(vertices.size() * sizeof(Vertex)), vertices.data(), GL_STATIC_DRAW);


};

void Clear(mf::Color color)
{
glClearColor(color.r, color.g, color.b, color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

virtual Vector2u GetSize() const = 0;

void SetView(View const &view)
{
this->view_ = view;
this->UpdateViewport();
}

View const &GetView() const
{
return this->view_;
}
};
}

Expand Down
27 changes: 27 additions & 0 deletions src/graphic/View.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
#ifndef UNLOGIC_VIEW_H
#define UNLOGIC_VIEW_H

#include "graphic/Math.h"

namespace mf
{
struct Viewport
{
Vector2f position;
Vector2f size;
};

class View
{
Vector2f size_;
Vector2f center_;

public:
Vector2f const &GetCenter() const
{
this->center_;
}

Vector2f const &GetSize() const
{
return this->size_;
}

Viewport CalculateViewport() const
{

}

View(Vector2f size, Vector2f center) : size_(size), center_(center) {}
};
}

Expand Down

0 comments on commit 033c6c4

Please sign in to comment.