Add geometry handling.
This commit is contained in:
parent
9c116b1efd
commit
c1389218f2
37 changed files with 294 additions and 278 deletions
|
@ -4,33 +4,7 @@ set(platform_HEADERS "")
|
|||
set(platform_LIBS "")
|
||||
|
||||
if (UNIX)
|
||||
find_package(PkgConfig)
|
||||
PKG_CHECK_MODULES(PC_CAIRO cairo)
|
||||
FIND_PATH(CAIRO_INCLUDE_DIRS
|
||||
NAMES cairo.h
|
||||
HINTS ${PC_CAIRO_INCLUDEDIR}
|
||||
${PC_CAIRO_INCLUDE_DIRS}
|
||||
PATH_SUFFIXES cairo
|
||||
)
|
||||
|
||||
FIND_LIBRARY(CAIRO_LIBRARIES
|
||||
NAMES cairo
|
||||
HINTS ${PC_CAIRO_LIBDIR}
|
||||
${PC_CAIRO_LIBRARY_DIRS}
|
||||
)
|
||||
list(APPEND platform_LIB_INCLUDES
|
||||
cairo/CairoInterface.cpp
|
||||
cairo/CairoDrawingSurface.cpp
|
||||
cairo/CairoDrawingContext.cpp
|
||||
)
|
||||
list(APPEND platform_INCLUDE_DIRS
|
||||
${CAIRO_INCLUDE_DIRS}
|
||||
)
|
||||
list(APPEND platform_HEADERS
|
||||
cairo/CairoInterface.h
|
||||
)
|
||||
list(APPEND platform_LIBS
|
||||
${CAIRO_LIBRARIES}
|
||||
list(APPEND platform_LIBSs
|
||||
GL
|
||||
)
|
||||
else()
|
||||
|
@ -43,6 +17,7 @@ list(APPEND graphics_LIB_INCLUDES
|
|||
DrawingContext.cpp
|
||||
DrawingManager.cpp
|
||||
DrawingSurface.cpp
|
||||
Rasterizer.cpp
|
||||
opengl/OpenGlInterface.cpp
|
||||
${platform_LIB_INCLUDES}
|
||||
)
|
||||
|
@ -50,6 +25,7 @@ list(APPEND graphics_LIB_INCLUDES
|
|||
list(APPEND graphics_HEADERS
|
||||
opengl/OpenGlInterface.h
|
||||
${platform_HEADERS}
|
||||
Rasterizer.h
|
||||
INativeDrawingSurface.h
|
||||
INativeDrawingContext.h)
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "DrawingContext.h"
|
||||
|
||||
#include "INativeDrawingContext.h"
|
||||
#include "AbstractGeometricItem.h"
|
||||
|
||||
std::unique_ptr<DrawingContext> DrawingContext::Create()
|
||||
{
|
||||
|
@ -16,3 +17,18 @@ INativeDrawingContext* DrawingContext::GetNativeContext()
|
|||
{
|
||||
return mNativeDrawingContext.get();
|
||||
}
|
||||
|
||||
void DrawingContext::AddDrawable(AbstractGeometricItemPtr item)
|
||||
{
|
||||
mItems.push_back(std::move(item));
|
||||
}
|
||||
|
||||
unsigned DrawingContext::GetNumItems() const
|
||||
{
|
||||
return mItems.size();
|
||||
}
|
||||
|
||||
AbstractGeometricItem* DrawingContext::GetDrawable(unsigned idx) const
|
||||
{
|
||||
return mItems[idx].get();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class INativeDrawingContext;
|
||||
class AbstractGeometricItem;
|
||||
using AbstractGeometricItemPtr = std::unique_ptr<AbstractGeometricItem>;
|
||||
|
||||
class DrawingContext
|
||||
{
|
||||
|
@ -15,8 +18,15 @@ public:
|
|||
|
||||
INativeDrawingContext* GetNativeContext();
|
||||
|
||||
unsigned GetNumItems() const;
|
||||
|
||||
void AddDrawable(AbstractGeometricItemPtr item);
|
||||
|
||||
AbstractGeometricItem* GetDrawable(unsigned idx) const;
|
||||
|
||||
private:
|
||||
|
||||
std::vector<std::unique_ptr<AbstractGeometricItem> > mItems;
|
||||
std::unique_ptr<INativeDrawingContext> mNativeDrawingContext;
|
||||
};
|
||||
|
||||
|
|
|
@ -3,15 +3,12 @@
|
|||
#include "INativeDrawingContext.h"
|
||||
#include "DrawingSurface.h"
|
||||
#include "DrawingContext.h"
|
||||
#ifdef __linux__
|
||||
#include "CairoInterface.h"
|
||||
#endif
|
||||
#include "Rasterizer.h"
|
||||
|
||||
DrawingManager::DrawingManager()
|
||||
: mRasterizer(std::make_unique<Rasterizer>())
|
||||
{
|
||||
#ifdef __linux__
|
||||
mCairoInterface = CairoInterface::Create();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<DrawingManager> DrawingManager::Create()
|
||||
|
@ -23,23 +20,11 @@ void DrawingManager::InitalizeSurface(unsigned width, unsigned height)
|
|||
{
|
||||
mDrawingSurface = DrawingSurface::Create();
|
||||
mDrawingSurface->SetSize(width, height);
|
||||
#ifdef __linux__
|
||||
if (mCairoInterface)
|
||||
{
|
||||
mCairoInterface->InitializeSurface(mDrawingSurface.get());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void DrawingManager::InitializeContext()
|
||||
{
|
||||
mDrawingContext = DrawingContext::Create();
|
||||
#ifdef __linux__
|
||||
if (mCairoInterface && mDrawingSurface)
|
||||
{
|
||||
mCairoInterface->InitializeContext(mDrawingContext.get(), mDrawingSurface.get());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void DrawingManager::AddText(TextElement* text)
|
||||
|
@ -53,20 +38,9 @@ void DrawingManager::AddText(TextElement* text)
|
|||
{
|
||||
return;
|
||||
}
|
||||
#ifdef __linux__
|
||||
if (mCairoInterface)
|
||||
{
|
||||
mCairoInterface->AddText(text, mDrawingContext.get());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void DrawingManager::RenderToFile(const std::string& path)
|
||||
{
|
||||
#ifdef __linux__
|
||||
if (mDrawingSurface && mCairoInterface)
|
||||
{
|
||||
mCairoInterface->RenderToFile(mDrawingSurface.get(), path);
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
|
|
@ -4,9 +4,6 @@
|
|||
#include <string>
|
||||
#include "INativeDrawingContext.h"
|
||||
#include "INativeDrawingSurface.h"
|
||||
#ifdef __linux__
|
||||
#include "CairoInterface.h"
|
||||
#endif
|
||||
|
||||
class TextElement;
|
||||
|
||||
|
@ -15,6 +12,8 @@ using DrawingSurfacePtr = std::unique_ptr<DrawingSurface>;
|
|||
class DrawingContext;
|
||||
using DrawingContextPtr = std::unique_ptr<DrawingContext>;
|
||||
|
||||
class Rasterizer;
|
||||
|
||||
class DrawingManager
|
||||
{
|
||||
public:
|
||||
|
@ -26,11 +25,9 @@ public:
|
|||
void RenderToFile(const std::string& path);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Rasterizer> mRasterizer;
|
||||
DrawingSurfacePtr mDrawingSurface {nullptr};
|
||||
DrawingContextPtr mDrawingContext {nullptr};
|
||||
#ifdef __linux__
|
||||
CairoInterfacePtr mCairoInterface {nullptr};
|
||||
#endif
|
||||
};
|
||||
|
||||
using DrawingManagerPtr = std::unique_ptr<DrawingManager>;
|
||||
|
|
|
@ -38,3 +38,8 @@ unsigned DrawingSurface::GetHeight() const
|
|||
{
|
||||
return mHeight;
|
||||
}
|
||||
|
||||
void DrawingSurface::Paint(Grid* grid)
|
||||
{
|
||||
mImageBuffer = std::make_unique<Image>();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,9 @@
|
|||
|
||||
#include "INativeDrawingSurface.h"
|
||||
|
||||
class Grid;
|
||||
class Image;
|
||||
|
||||
class DrawingSurface
|
||||
{
|
||||
public:
|
||||
|
@ -21,9 +24,14 @@ public:
|
|||
|
||||
unsigned GetHeight() const;
|
||||
|
||||
void Paint(Grid* grid);
|
||||
|
||||
Image* GetAsImage() const;
|
||||
|
||||
private:
|
||||
|
||||
unsigned mWidth = 0;
|
||||
unsigned mHeight = 0;
|
||||
std::unique_ptr<Image> mImageBuffer;
|
||||
std::unique_ptr<INativeDrawingSurface> mNativeDrawingSurface;
|
||||
};
|
||||
|
|
25
src/graphics/Rasterizer.cpp
Normal file
25
src/graphics/Rasterizer.cpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include "Rasterizer.h"
|
||||
|
||||
#include "DrawingSurface.h"
|
||||
#include "DrawingContext.h"
|
||||
#include "Grid.h"
|
||||
|
||||
Rasterizer::Rasterizer()
|
||||
: mGrid(std::make_unique<Grid>(Rectangle(Point(0, 0), Point(100, 100))))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Rasterizer::Paint(DrawingSurface* surface, DrawingContext* context)
|
||||
{
|
||||
const auto width = surface->GetWidth();
|
||||
const auto height = surface->GetHeight();
|
||||
mGrid->ResetBounds(Rectangle(Point(0, 0), Point(width, height)));
|
||||
|
||||
for (unsigned idx=0; idx< context->GetNumItems(); idx++)
|
||||
{
|
||||
context->GetDrawable(idx)->Sample(mGrid.get());
|
||||
}
|
||||
|
||||
surface->Paint(mGrid.get());
|
||||
}
|
18
src/graphics/Rasterizer.h
Normal file
18
src/graphics/Rasterizer.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
class DrawingSurface;
|
||||
class DrawingContext;
|
||||
class Grid;
|
||||
|
||||
class Rasterizer
|
||||
{
|
||||
public:
|
||||
Rasterizer();
|
||||
|
||||
void Paint(DrawingSurface* surface, DrawingContext* context);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Grid> mGrid;
|
||||
};
|
|
@ -1,31 +0,0 @@
|
|||
#include "CairoDrawingContext.h"
|
||||
|
||||
cairo_t* CairoDrawingContext::GetNativeContext()
|
||||
{
|
||||
return mNativeContext;
|
||||
}
|
||||
|
||||
CairoDrawingContext::CairoDrawingContext(cairo_t* context)
|
||||
: mNativeContext(context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CairoDrawingContext::~CairoDrawingContext()
|
||||
{
|
||||
DestroyNativeContext();
|
||||
}
|
||||
|
||||
std::unique_ptr<CairoDrawingContext> CairoDrawingContext::Create(cairo_t* context)
|
||||
{
|
||||
return std::make_unique<CairoDrawingContext>(context);
|
||||
}
|
||||
|
||||
void CairoDrawingContext::DestroyNativeContext()
|
||||
{
|
||||
if (mNativeContext)
|
||||
{
|
||||
cairo_destroy (mNativeContext);
|
||||
mNativeContext = nullptr;
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "INativeDrawingContext.h"
|
||||
|
||||
#include <memory>
|
||||
#include <cairo.h>
|
||||
|
||||
class CairoDrawingContext : public INativeDrawingContext
|
||||
{
|
||||
public:
|
||||
|
||||
CairoDrawingContext(cairo_t* context);
|
||||
|
||||
virtual ~CairoDrawingContext();
|
||||
|
||||
static std::unique_ptr<CairoDrawingContext> Create(cairo_t* context);
|
||||
|
||||
cairo_t* GetNativeContext();
|
||||
|
||||
void DestroyNativeContext();
|
||||
|
||||
private:
|
||||
cairo_t* mNativeContext;
|
||||
};
|
||||
|
||||
using CairoDrawingContextPtr = std::unique_ptr<CairoDrawingContext>;
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
#include "CairoDrawingSurface.h"
|
||||
|
||||
CairoDrawingSurface::CairoDrawingSurface(cairo_surface_t* surface)
|
||||
: mNativeSurface(surface)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CairoDrawingSurface::~CairoDrawingSurface()
|
||||
{
|
||||
DestroyNativeSurface();
|
||||
}
|
||||
|
||||
std::unique_ptr<CairoDrawingSurface> CairoDrawingSurface::Create(cairo_surface_t* surface)
|
||||
{
|
||||
return std::make_unique<CairoDrawingSurface>(surface);
|
||||
}
|
||||
|
||||
cairo_surface_t* CairoDrawingSurface::GetNativeSurface()
|
||||
{
|
||||
return mNativeSurface;
|
||||
}
|
||||
|
||||
void CairoDrawingSurface::DestroyNativeSurface()
|
||||
{
|
||||
if (mNativeSurface)
|
||||
{
|
||||
cairo_surface_destroy (mNativeSurface);
|
||||
mNativeSurface = nullptr;
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "INativeDrawingSurface.h"
|
||||
|
||||
#include <cairo.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class CairoDrawingSurface : public INativeDrawingSurface
|
||||
{
|
||||
public:
|
||||
|
||||
CairoDrawingSurface(cairo_surface_t* surface);
|
||||
|
||||
virtual ~CairoDrawingSurface();
|
||||
|
||||
static std::unique_ptr<CairoDrawingSurface> Create(cairo_surface_t* surface);
|
||||
|
||||
cairo_surface_t* GetNativeSurface();
|
||||
|
||||
void DestroyNativeSurface() override;
|
||||
|
||||
private:
|
||||
cairo_surface_t* mNativeSurface;
|
||||
};
|
||||
|
||||
using CairoDrawingSurfacePtr = std::unique_ptr<CairoDrawingSurface>;
|
|
@ -1,51 +0,0 @@
|
|||
#include "CairoInterface.h"
|
||||
|
||||
#include "DrawingSurface.h"
|
||||
#include "DrawingContext.h"
|
||||
#include "CairoDrawingSurface.h"
|
||||
#include "CairoDrawingContext.h"
|
||||
#include "TextElement.h"
|
||||
|
||||
#include <cairo.h>
|
||||
|
||||
|
||||
std::unique_ptr<CairoInterface> CairoInterface::Create()
|
||||
{
|
||||
return std::make_unique<CairoInterface>();
|
||||
}
|
||||
|
||||
void CairoInterface::InitializeSurface(DrawingSurface* surface)
|
||||
{
|
||||
auto native_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
|
||||
surface->GetWidth(), surface->GetHeight());
|
||||
|
||||
auto cairo_surface = CairoDrawingSurface::Create(native_surface);
|
||||
surface->SetNativeSurface(std::move(cairo_surface));
|
||||
}
|
||||
|
||||
void CairoInterface::InitializeContext(DrawingContext* context, DrawingSurface* surface)
|
||||
{
|
||||
auto cairo_surface = dynamic_cast<CairoDrawingSurface*>(surface->GetNativeSurface());
|
||||
auto native_context = cairo_create(cairo_surface->GetNativeSurface());
|
||||
|
||||
auto cairo_context = CairoDrawingContext::Create(native_context);
|
||||
context->SetNativeContext(std::move(cairo_context));
|
||||
}
|
||||
|
||||
void CairoInterface::RenderToFile(DrawingSurface* surface, const std::string& path)
|
||||
{
|
||||
auto cairo_surface = dynamic_cast<CairoDrawingSurface*>(surface->GetNativeSurface());
|
||||
cairo_surface_write_to_png (cairo_surface->GetNativeSurface(), path.c_str());
|
||||
}
|
||||
|
||||
void CairoInterface::AddText(TextElement* text, DrawingContext* context)
|
||||
{
|
||||
auto cairo_context = dynamic_cast<CairoDrawingContext*>(context->GetNativeContext());
|
||||
auto native_context = cairo_context->GetNativeContext();
|
||||
|
||||
cairo_select_font_face (native_context, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
|
||||
cairo_set_font_size (native_context, 32.0);
|
||||
cairo_set_source_rgb (native_context, 0.0, 0.0, 1.0);
|
||||
cairo_move_to(native_context, 10.0, 50.0);
|
||||
cairo_show_text(native_context, text->GetContent().c_str());
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class DrawingSurface;
|
||||
class DrawingContext;
|
||||
class TextElement;
|
||||
|
||||
class CairoInterface
|
||||
{
|
||||
public:
|
||||
CairoInterface() = default;
|
||||
|
||||
static std::unique_ptr<CairoInterface> Create();
|
||||
|
||||
void InitializeSurface(DrawingSurface* surface);
|
||||
|
||||
void InitializeContext(DrawingContext* context, DrawingSurface* surface);
|
||||
|
||||
void AddText(TextElement* text, DrawingContext* context);
|
||||
|
||||
void RenderToFile(DrawingSurface* surface, const std::string& path);
|
||||
};
|
||||
|
||||
using CairoInterfacePtr = std::unique_ptr<CairoInterface>;
|
Loading…
Add table
Add a link
Reference in a new issue