Skip to content

Commit

Permalink
Minor code refactoring and improved gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
pzaino committed Mar 1, 2024
1 parent b0d61f4 commit 8019177
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 62 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
*.orig
.DS_Store
ux-src/
**/o/
**/*.o
**/*.a

21 changes: 14 additions & 7 deletions src/cpp/fb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ FrameBuffer::FrameBuffer(unsigned int h_dim, unsigned int v_dim, std::shared_ptr
: renderStrategy(strategy), width(h_dim), height(v_dim) {}

// Add a layer to the framebuffer
void FrameBuffer::addLayer(const std::shared_ptr<Layer>& layer) {
void FrameBuffer::addLayer(const std::shared_ptr<Layer>& layer)
{
layers.push_back(layer);
}

Expand All @@ -45,19 +46,22 @@ void FrameBuffer::addLayer(std::shared_ptr<Layer>&& layer) {
}

// Add a layer to the framebuffer
void FrameBuffer::addLayer() {
void FrameBuffer::addLayer()
{
layers.push_back(std::make_shared<Layer>(width, height, renderStrategy, currentMemoryManager));
}

// Remove a layer from the framebuffer by index
void FrameBuffer::removeLayer(size_t index) {
void FrameBuffer::removeLayer(size_t index)
{
if (index < layers.size()) {
layers.erase(layers.begin() + index);
}
}

// Accessor for layer by index
std::shared_ptr<Layer> FrameBuffer::getLayer(size_t index) const {
std::shared_ptr<Layer> FrameBuffer::getLayer(size_t index) const
{
// Simple bounds checking, throw std::out_of_range if index is invalid
if (index >= layers.size()) {
throw std::out_of_range("Layer index is out of range.");
Expand All @@ -66,7 +70,8 @@ std::shared_ptr<Layer> FrameBuffer::getLayer(size_t index) const {
}

// Set the render strategy for the framebuffer and all its layers
void FrameBuffer::setRenderStrategy(std::shared_ptr<RenderStrategy> strategy) {
void FrameBuffer::setRenderStrategy(std::shared_ptr<RenderStrategy> strategy)
{
renderStrategy = strategy;
// Update the render strategy for all existing layers
for (auto& layer : layers) {
Expand All @@ -75,7 +80,8 @@ void FrameBuffer::setRenderStrategy(std::shared_ptr<RenderStrategy> strategy) {
}

// Set the memory manager for the framebuffer and all its layers
void FrameBuffer::setMemoryManager(std::shared_ptr<MemoryManager> manager) {
void FrameBuffer::setMemoryManager(std::shared_ptr<MemoryManager> manager)
{
currentMemoryManager = manager;
// Update the memory manager for all existing layers
for (auto& layer : layers) {
Expand All @@ -84,7 +90,8 @@ void FrameBuffer::setMemoryManager(std::shared_ptr<MemoryManager> manager) {
}

// Placeholder for the actual implementation of merging all layers
void FrameBuffer::mergeLayers() {
void FrameBuffer::mergeLayers()
{
// This could involve blending pixels from each layer based on their opacity,
// and visibility, or simply stacking them in order.
// TODO: This is a placeholder for the actual implementation
Expand Down
98 changes: 66 additions & 32 deletions src/cpp/layer
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* please refer to the official license documentation.
*/

#include <stdexcept>
#include <memory> // For std::shared_ptr

// Include MicroFB libraries
Expand All @@ -31,93 +32,126 @@

// Constructor implementation
Layer::Layer(pixel_t width, pixel_t height, std::shared_ptr<RenderStrategy> strategy, std::shared_ptr<MemoryManager> currMemoryManager)
: currentRenderStrategy(strategy), opacity(1.0f), visible(true), width(width), height(height) {

: currentRenderStrategy(strategy), opacity(1.0f), visible(true), width(width), height(height)
{
if (currentMemoryManager == nullptr) {
if (currMemoryManager == nullptr) {
throw std::runtime_error("MemoryManager cannot be null");
}
currentMemoryManager = currMemoryManager;
}

// Initialize pixel buffer with default color (0)
pixels = static_cast<color_t*>(currentMemoryManager->allocate(width * height * sizeof(color_t)));

}

// Destructor implementation
Layer::~Layer() {
Layer::~Layer()
{
// Not much for now
}

// Set the render strategy
void Layer::setRenderStrategy(std::shared_ptr<RenderStrategy> strategy) {
currentRenderStrategy = strategy; // Set the render strategy
void Layer::setRenderStrategy(std::shared_ptr<RenderStrategy> strategy)
{
// Set the render strategy
currentRenderStrategy = strategy;
}

// Set the memory manager
void Layer::setMemoryManager(std::shared_ptr<MemoryManager> manager) {
currentMemoryManager = manager; // Set the memory manager
void Layer::setMemoryManager(std::shared_ptr<MemoryManager> manager)
{
// Set the memory manager
currentMemoryManager = manager;
}

// Clear the layer with a specific color
void Layer::clear(color_t color) {
currentRenderStrategy->clear(*this, color); // Use the render strategy to clear the layer
void Layer::clear(color_t color)
{
// Use the render strategy to clear the layer
currentRenderStrategy->clear(*this, color);
}

// Direct pixel manipulation
void Layer::setPixel(pixel_t x, pixel_t y, color_t color) {
currentRenderStrategy->setPixel(*this, x, y, color); // Use the render strategy to set the pixel
// Use the render strategy to set the pixel
currentRenderStrategy->setPixel(*this, x, y, color);
}

// Draw a line
void Layer::drawLine(pixel_t x1, pixel_t y1, pixel_t x2, pixel_t y2, color_t color) {
currentRenderStrategy->drawLine(*this, x1, y1, x2, y2, color); // Use the render strategy to draw the line
void Layer::drawLine(pixel_t x1, pixel_t y1, pixel_t x2, pixel_t y2, color_t color)
{
// Use the render strategy to draw the line
currentRenderStrategy->drawLine(*this, x1, y1, x2, y2, color);
}

// Draw a rectangle
void Layer::drawRect(pixel_t x, pixel_t y, pixel_t w, pixel_t h, color_t color) {
currentRenderStrategy->drawRect(*this, x, y, w, h, color); // Use the render strategy to draw the rectangle
void Layer::drawRect(pixel_t x, pixel_t y, pixel_t w, pixel_t h, color_t color)
{
// Use the render strategy to draw the rectangle
currentRenderStrategy->drawRect(*this, x, y, w, h, color);
}

// Fill a rectangle
void Layer::fillRect(pixel_t x, pixel_t y, pixel_t w, pixel_t h, color_t color) {
currentRenderStrategy->fillRect(*this, x, y, w, h, color); // Use the render strategy to fill the rectangle
void Layer::fillRect(pixel_t x, pixel_t y, pixel_t w, pixel_t h, color_t color)
{
// Use the render strategy to fill the rectangle
currentRenderStrategy->fillRect(*this, x, y, w, h, color);
}

// Draw a circle
void Layer::drawCircle(pixel_t x, pixel_t y, pixel_t r, color_t color) {
currentRenderStrategy->drawCircle(*this, x, y, r, color); // Use the render strategy to draw the circle
void Layer::drawCircle(pixel_t x, pixel_t y, pixel_t r, color_t color)
{
// Use the render strategy to draw the circle
currentRenderStrategy->drawCircle(*this, x, y, r, color);
}

// Fill a circle
void Layer::fillCircle(pixel_t x, pixel_t y, pixel_t r, color_t color) {
currentRenderStrategy->fillCircle(*this, x, y, r, color); // Use the render strategy to fill the circle
void Layer::fillCircle(pixel_t x, pixel_t y, pixel_t r, color_t color)
{
// Use the render strategy to fill the circle
currentRenderStrategy->fillCircle(*this, x, y, r, color);
}

// Draw an ellipse
void Layer::drawEllipse(pixel_t x, pixel_t y, pixel_t rx, pixel_t ry, color_t color) {
currentRenderStrategy->drawEllipse(*this, x, y, rx, ry, color); // Use the render strategy to draw the ellipse
void Layer::drawEllipse(pixel_t x, pixel_t y, pixel_t rx,
pixel_t ry, color_t color)
{
// Use the render strategy to draw the ellipse
currentRenderStrategy->drawEllipse(*this, x, y, rx, ry, color);
}

// Fill an ellipse
void Layer::fillEllipse(pixel_t x, pixel_t y, pixel_t rx, pixel_t ry, color_t color) {
currentRenderStrategy->fillEllipse(*this, x, y, rx, ry, color); // Use the render strategy to fill the ellipse
void Layer::fillEllipse(pixel_t x, pixel_t y, pixel_t rx,
pixel_t ry, color_t color)
{
// Use the render strategy to fill the ellipse
currentRenderStrategy->fillEllipse(*this, x, y, rx, ry, color);
}

// Draw a polygon
void Layer::drawPolygon(const std::vector<pixel_t>& points, color_t color) {
currentRenderStrategy->drawPolygon(*this, points, color); // Use the render strategy to draw the polygon
// Use the render strategy to draw the polygon
currentRenderStrategy->drawPolygon(*this, points, color);
}

// Fill a polygon
void Layer::fillPolygon(const std::vector<pixel_t>& points, color_t color) {
currentRenderStrategy->fillPolygon(*this, points, color); // Use the render strategy to fill the polygon
void Layer::fillPolygon(const std::vector<pixel_t>& points, color_t color)
{
// Use the render strategy to fill the polygon
currentRenderStrategy->fillPolygon(*this, points, color);
}

// Draw text
void Layer::drawText(pixel_t x, pixel_t y, const char* text, color_t color) {
currentRenderStrategy->drawText(*this, x, y, text, color); // Use the render strategy to draw the text
void Layer::drawText(pixel_t x, pixel_t y, const char* text, color_t color)
{
// Use the render strategy to draw the text
currentRenderStrategy->drawText(*this, x, y, text, color);
}

// Draw an image
void Layer::drawImage(pixel_t x, pixel_t y, const Layer& image) {
currentRenderStrategy->drawImage(*this, x, y, image); // Use the render strategy to draw the image
void Layer::drawImage(pixel_t x, pixel_t y, const Layer& image)
{
// Use the render strategy to draw the image
currentRenderStrategy->drawImage(*this, x, y, image);
}
3 changes: 2 additions & 1 deletion src/h/fb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
#include "render_strategy.h"
#include "mem_manager.h"

class FrameBuffer {
class FrameBuffer
{
private:
std::shared_ptr<MemoryManager> currentMemoryManager;
std::vector<std::shared_ptr<Layer>> layers; // Use shared pointers for layers
Expand Down
3 changes: 2 additions & 1 deletion src/h/layer
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
typedef uint32_t color_t; // 32-bit color
typedef uint32_t pixel_t; // 32-bit pixel, assuming it represents dimensions

class Layer {
class Layer
{
private:
std::shared_ptr<MemoryManager> currentMemoryManager;
std::shared_ptr<RenderStrategy> currentRenderStrategy;
Expand Down
21 changes: 11 additions & 10 deletions src/h/mem_manager
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* This file is part of:
* This file is part of:
* MicroFB, a minimal framebuffer library for RISC OS
*
* Description:
* Description:
* This project aims to provide a simple and efficient way to manage
* the framebuffer on RISC OS using C++11. It abstracts the direct
* interaction with the GraphicsV API, offering a higher-level
* the framebuffer on RISC OS using C++11. It abstracts the direct
* interaction with the GraphicsV API, offering a higher-level
* interface for drawing operations and framebuffer manipulation.
*
* Author:
* Paolo Fabio Zaino, all rights reserved.
* Distributed under License:
* CDDL v1.1 (Common Development and Distribution License Version
* 1.1) The use of this project is subject to the terms of the
* CDDL v1.1. This project can be used and distributed according
* to the terms of this license. For details on the CDDL v1.1,
* Distributed under License:
* CDDL v1.1 (Common Development and Distribution License Version
* 1.1) The use of this project is subject to the terms of the
* CDDL v1.1. This project can be used and distributed according
* to the terms of this license. For details on the CDDL v1.1,
* please refer to the official license documentation.
*/

Expand All @@ -24,7 +24,8 @@
#include <cstddef>

// Abstracted memory manager interface
class MemoryManager {
class MemoryManager
{
public:
virtual ~MemoryManager() {}
virtual void* allocate(size_t size) = 0;
Expand Down
23 changes: 12 additions & 11 deletions src/h/render_strategy
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* This file is part of:
* This file is part of:
* MicroFB, a minimal framebuffer library for RISC OS
*
* Description:
* Description:
* This project aims to provide a simple and efficient way to manage
* the framebuffer on RISC OS using C++11. It abstracts the direct
* interaction with the GraphicsV API, offering a higher-level
* the framebuffer on RISC OS using C++11. It abstracts the direct
* interaction with the GraphicsV API, offering a higher-level
* interface for drawing operations and framebuffer manipulation.
*
* Author:
* Paolo Fabio Zaino, all rights reserved.
* Distributed under License:
* CDDL v1.1 (Common Development and Distribution License Version
* 1.1) The use of this project is subject to the terms of the
* CDDL v1.1. This project can be used and distributed according
* to the terms of this license. For details on the CDDL v1.1,
* Distributed under License:
* CDDL v1.1 (Common Development and Distribution License Version
* 1.1) The use of this project is subject to the terms of the
* CDDL v1.1. This project can be used and distributed according
* to the terms of this license. For details on the CDDL v1.1,
* please refer to the official license documentation.
*/

Expand All @@ -31,9 +31,10 @@ class Layer;
typedef uint32_t color_t; // 32-bit color, assuming it represents ARGB or similar
typedef uint32_t pixel_t; // 32-bit pixel, for dimensions

class RenderStrategy {
class RenderStrategy
{
public:
virtual ~RenderStrategy() {}
virtual ~RenderStrategy() {};

// Pure virtual functions for rendering operations
virtual void clear(Layer& layer, color_t color) = 0;
Expand Down

0 comments on commit 8019177

Please sign in to comment.