Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stepulak committed Oct 15, 2020
1 parent 806870f commit f8ed71b
Show file tree
Hide file tree
Showing 22 changed files with 402 additions and 987 deletions.
64 changes: 0 additions & 64 deletions Interpolation_Clipping_Rasterization/AppSelector.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions Interpolation_Clipping_Rasterization/AppSelector.h

This file was deleted.

93 changes: 45 additions & 48 deletions Interpolation_Clipping_Rasterization/BitmapFont.cpp
Original file line number Diff line number Diff line change
@@ -1,64 +1,61 @@
#include "BitmapFont.h"
#include "Utils.h"

#include <cstring>

BitmapFont::BitmapFont(SDL_Renderer* rend, const std::string& path, int charWidth, const std::string& alphabet)
: m_renderer(rend),
m_bitmapCharWidth(charWidth)
: m_renderer(rend)
, m_bitmapCharWidth(charWidth)
, m_bitmap(Utils::LoadTexture(rend, path, nullptr, &m_bitmapHeight))
{
m_bitmap = Utils::LoadTexture(rend, path, nullptr, &m_bitmapHeight);

for (auto& p : m_alphabet) {
p = -1;
}

int currPos = 0;
for (const auto& c : alphabet) {
if (InsideAlphabet(c)) {
m_alphabet[c - ALPHABET_START] = currPos;
currPos += charWidth;
}
}
std::memset(m_alphabet.data(), -1, m_alphabet.size() * sizeof(m_alphabet.front()));

int pos = 0;
for (const auto& c : alphabet) {
if (InsideAlphabet(c)) {
m_alphabet[c - ALPHABET_START] = pos;
pos += charWidth;
}
}
}

BitmapFont::~BitmapFont()
{
SDL_DestroyTexture(m_bitmap);
SDL_DestroyTexture(m_bitmap);
}

int BitmapFont::GetLineWidth(const std::string& line, unsigned int fontSize) const
int BitmapFont::GetLineWidth(const std::string& line, int fontSize) const
{
unsigned int width = 0;
for (const auto& c : line) {
if (InsideAlphabet(c)) {
width += GetCharWidthFontSize(fontSize);
}
}
return width;
int width = 0;
for (const auto& c : line) {
if (InsideAlphabet(c)) {
width += GetCharWidthFontSize(fontSize);
}
}
return width;
}

void BitmapFont::DrawLine(const std::string& line, unsigned int fontSize, int posX, int posY, bool isCentered) const
void BitmapFont::DrawLine(const std::string& line, int fontSize, int posX, int posY, bool isCentered) const
{
Utils::PushColor(m_renderer);
SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255);

SDL_Rect dest = { posX, posY, GetCharWidthFontSize(fontSize), static_cast<int>(fontSize) };
SDL_Rect src = { 0, 0, m_bitmapCharWidth, m_bitmapHeight };

for (const auto& c : line) {
// In range and inside alphabet?
if (InsideAlphabet(c) && m_alphabet[c - ALPHABET_START] != -1) {
src.x = m_alphabet[c - ALPHABET_START];
SDL_RenderCopyEx(m_renderer, m_bitmap, &src, &dest, 0, nullptr, SDL_FLIP_NONE);
dest.x += fontSize;
}
else if (c == ' ') {
dest.x += fontSize;
}
else if (c == '\n') {
dest.y += fontSize;
dest.x = posX;
}
}

Utils::PopColor(m_renderer);
Utils::PushColor(m_renderer);
SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255);

SDL_Rect dest = { posX, posY, GetCharWidthFontSize(fontSize), fontSize };
SDL_Rect src = { 0, 0, m_bitmapCharWidth, m_bitmapHeight };

for (const auto& c : line) {
// In range and inside alphabet?
if (InsideAlphabet(c) && m_alphabet[c - ALPHABET_START] != -1) {
src.x = m_alphabet[c - ALPHABET_START];
SDL_RenderCopyEx(m_renderer, m_bitmap, &src, &dest, 0, nullptr, SDL_FLIP_NONE);
dest.x += fontSize;
} else if (c == ' ') {
dest.x += fontSize;
} else if (c == '\n') {
dest.y += fontSize;
dest.x = posX;
}
}

Utils::PopColor(m_renderer);
}
68 changes: 42 additions & 26 deletions Interpolation_Clipping_Rasterization/BitmapFont.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,58 @@
#pragma once

#include <array>
#include <SDL2/SDL.h>
#include <array>

// Simple bitmap font with variable alphabet loaded from "one-line" image
// Simple bitmap font with alphabet loaded from "one-line" image
class BitmapFont {
private:

static constexpr int CHAR_WIDTH_OFFSET = -8;
static constexpr int ALPHABET_START = ' ';
static constexpr int ALPHABET_END = '~';
static constexpr int ALPHABET_SIZE = ALPHABET_END - ALPHABET_START + 1;

std::array<int, ALPHABET_SIZE> m_alphabet;
int m_bitmapHeight;
int m_bitmapCharWidth;
SDL_Texture* m_bitmap;
SDL_Renderer* m_renderer;

bool InsideAlphabet(char c) const { return c >= ALPHABET_START && c <= ALPHABET_END; }
int GetCharWidthFontSize(int fontSize) const { return fontSize + CHAR_WIDTH_OFFSET; }

public:

BitmapFont(SDL_Renderer* rend, const std::string& path, int charWidth, const std::string& alphabet);

BitmapFont(const BitmapFont&) = delete;
BitmapFont(BitmapFont&&) = delete;

BitmapFont& operator=(const BitmapFont&) = delete;
BitmapFont& operator=(BitmapFont&&) = delete;

BitmapFont(SDL_Renderer* rend, const std::string& path, int charWidth, const std::string& alphabet);

virtual ~BitmapFont();

int GetBitmapCharWidth() const { return m_bitmapCharWidth; }
SDL_Renderer* GetRenderer() { return m_renderer; }
void SetRenderer(SDL_Renderer* rend) { m_renderer = rend; }
int GetBitmapCharWidth() const
{
return m_bitmapCharWidth;
}

SDL_Renderer* GetRenderer()
{
return m_renderer;
}

void SetRenderer(SDL_Renderer* rend)
{
m_renderer = rend;
}

int GetLineWidth(const std::string& line, int fontSize) const;
void DrawLine(const std::string& line, int fontSize, int posX, int posY, bool isCentered = false) const;

private:
static constexpr auto CHAR_WIDTH_OFFSET = -8;
static constexpr auto ALPHABET_START = ' ';
static constexpr auto ALPHABET_END = '~';
static constexpr auto ALPHABET_SIZE = ALPHABET_END - ALPHABET_START + 1u;

std::array<int, ALPHABET_SIZE> m_alphabet;
int m_bitmapHeight;
int m_bitmapCharWidth;
SDL_Texture* m_bitmap;
SDL_Renderer* m_renderer;

bool InsideAlphabet(char c) const
{
return c >= ALPHABET_START && c <= ALPHABET_END;
}

int GetLineWidth(const std::string& line, unsigned int fontSize) const;
void DrawLine(const std::string& line, unsigned int fontSize, int posX, int posY, bool isCentered = false) const;
int GetCharWidthFontSize(int fontSize) const
{
return fontSize + CHAR_WIDTH_OFFSET;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ CohenSutherlandClipping::CollisionCode CohenSutherlandClipping::GetPositionCode(
return MID;
}

CohenSutherlandClipping::Point CohenSutherlandClipping::ClipSecondEndPoint(Point p, Point q, unsigned int& steps) const
CohenSutherlandClipping::Point CohenSutherlandClipping::ClipSecondEndPoint(Point p, Point q, uint& steps) const
{
Point p1 = p;
if (GetPositionCode(q) == MID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CohenSutherlandClipping : public RasterGridRunnable {
CollisionCode GetPositionCode(const Point& p) const;

// Clip line from q point
Point ClipSecondEndPoint(Point p, Point q, unsigned int& steps) const;
Point ClipSecondEndPoint(Point p, Point q, uint& steps) const;
Line ClipLine(const Point& p, const Point& q, bool stepMode) const;

void DrawAppInfo() const;
Expand Down
4 changes: 2 additions & 2 deletions Interpolation_Clipping_Rasterization/CyrusBeckClipping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void CyrusBeckClipping::CreateClippingLines()
{
m_clippingLines.reserve(m_clippingPolygon.size() - 1);

for (unsigned int i = 0; i < m_clippingPolygon.size() - 1; i++) {
for (uint i = 0; i < m_clippingPolygon.size() - 1; i++) {
const auto& p = m_clippingPolygon[i];
const auto& q = m_clippingPolygon[i + 1];
auto&& mid = p.CountPointMiddle(q);
Expand All @@ -54,7 +54,7 @@ CyrusBeckClipping::Line CyrusBeckClipping::ClipLine(const Point & p, const Point
const auto pqVec = Vec(p, q);
auto steps = stepMode ? GetCurrentStep() : INT_MAX;

for (unsigned int i = 0; i < m_clippingPolygon.size() - 1 && tmin < tmax && steps > 0; i++) {
for (uint i = 0; i < m_clippingPolygon.size() - 1 && tmin < tmax && steps > 0; i++) {
const auto clipLineNormal = Vec(m_clippingPolygon[i + 1], m_clippingPolygon[i]).CountNormal();
const auto normalsResult = clipLineNormal.Dot(pqVec);

Expand Down
8 changes: 4 additions & 4 deletions Interpolation_Clipping_Rasterization/FramebufferRunnable.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "FramebufferRunnable.h"

FramebufferRunnable::FramebufferRunnable(SDL_Window* w, SDL_Renderer* r, unsigned int pointSize, unsigned int maxPoints)
FramebufferRunnable::FramebufferRunnable(SDL_Window* w, SDL_Renderer* r, uint pointSize, uint maxPoints)
: RasterGridRunnable(w, r, pointSize, maxPoints)
{
m_drawArea = { 0, 0, GetWindowWidth(), GetWindowHeight() };
ClearFramebuffer();
}

FramebufferRunnable::FramebufferRunnable(SDL_Window* w, SDL_Renderer* r, const SDL_Rect& drawArea,
unsigned int pointSize, unsigned int maxPoints)
uint pointSize, uint maxPoints)
: RasterGridRunnable(w, r, pointSize, maxPoints),
m_drawArea(drawArea)
{
Expand Down Expand Up @@ -63,7 +63,7 @@ bool FramebufferRunnable::HandleMouseMotion(Sint32 x, Sint32 y)
return false;
}

void FramebufferRunnable::DrawLinesFan(bool connectFirstLast, unsigned int lastPointSize) const
void FramebufferRunnable::DrawLinesFan(bool connectFirstLast, uint lastPointSize) const
{
Utils::PushColor(GetRenderer());
SDL_SetRenderDrawColor(GetRenderer(), GetR(FILL_COLOR), GetG(FILL_COLOR), GetB(FILL_COLOR), GetA(FILL_COLOR));
Expand All @@ -72,7 +72,7 @@ void FramebufferRunnable::DrawLinesFan(bool connectFirstLast, unsigned int lastP
Utils::DrawPoint(rend, x + size - (x % size), y + size - (y % size), size);
};

for (unsigned int i = 1; i <= NumberOfFilledPoints(); i++) {
for (uint i = 1; i <= NumberOfFilledPoints(); i++) {
auto p0 = GetPoint(i - 1).ToPoint();
auto p1 = GetPoint(i).ToPoint();
Utils::DrawLineGeneric(GetRenderer(), drawPoint, p0.x, p0.y, p1.x, p1.y, GetPointSize(), 1.f);
Expand Down
12 changes: 6 additions & 6 deletions Interpolation_Clipping_Rasterization/FramebufferRunnable.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class FramebufferRunnable : public RasterGridRunnable {
protected:

bool PolygonLinesFilled() const { return m_polygonLinesFilled; }
unsigned int FramebufferWidth() const { return m_framebuffer.size(); }
unsigned int FramebufferHeight() const { return m_framebuffer[0].size(); }
uint FramebufferWidth() const { return m_framebuffer.size(); }
uint FramebufferHeight() const { return m_framebuffer[0].size(); }
const SDL_Rect& GetDrawArea() const { return m_drawArea; }

Color GetColor(int x, int y) const { return m_framebuffer[x][y]; }
Expand All @@ -49,7 +49,7 @@ class FramebufferRunnable : public RasterGridRunnable {
bool HandleKeyPress(const SDL_Keycode& kc) override;
bool HandleMouseClick(Uint8 button, Sint32 x, Sint32 y) override;
bool HandleMouseMotion(Sint32 x, Sint32 y) override;
void DrawLinesFan(bool connectFirstLast = false, unsigned int lastPointSize = 0) const override;
void DrawLinesFan(bool connectFirstLast = false, uint lastPointSize = 0) const override;

void FillPolygonLinesIntoFramebuffer();
void ClearFramebuffer();
Expand All @@ -58,9 +58,9 @@ class FramebufferRunnable : public RasterGridRunnable {

public:

FramebufferRunnable(SDL_Window* w, SDL_Renderer* r, unsigned int pointSize,
unsigned int maxPoints = UINT_MAX);
FramebufferRunnable(SDL_Window* w, SDL_Renderer* r, uint pointSize,
uint maxPoints = UINT_MAX);

FramebufferRunnable(SDL_Window* w, SDL_Renderer* r, const SDL_Rect& drawArea,
unsigned int pointSize, unsigned int maxPoints = UINT_MAX);
uint pointSize, uint maxPoints = UINT_MAX);
};
1 change: 0 additions & 1 deletion Interpolation_Clipping_Rasterization/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
- Pineda's algorithm for triangle rasterization (min-max mechanism)
*/

#include "AppSelector.h"
#include <iostream>

namespace {
Expand Down
Loading

0 comments on commit f8ed71b

Please sign in to comment.