mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
Displaying a fraction
This commit is contained in:
17
kandinsky/fonts/Makefile
Normal file
17
kandinsky/fonts/Makefile
Normal file
@@ -0,0 +1,17 @@
|
||||
FREETYPE_PATH := /usr/local/Cellar/freetype/2.5.5
|
||||
LIBPNG_PATH = /usr/local/Cellar/libpng/1.6.17
|
||||
#LIBPNG_PATH is optional, PNGs won't be generated
|
||||
|
||||
CC := clang
|
||||
CFLAGS := -I$(FREETYPE_PATH)/include/freetype2
|
||||
LDFLAGS := -L$(FREETYPE_PATH)/lib -lfreetype
|
||||
ifdef LIBPNG_PATH
|
||||
CFLAGS += -I$(LIBPNG_PATH)/include -DGENERATE_PNG=1
|
||||
LDFLAGS += -L$(LIBPNG_PATH)/lib -lpng
|
||||
endif
|
||||
|
||||
default:
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) rasterizer.c -o rasterizer
|
||||
|
||||
clean:
|
||||
rm rasterizer
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
#include <kandinsky/color.h>
|
||||
#include <kandinsky/line.h>
|
||||
#include <kandinsky/point.h>
|
||||
#include <kandinsky/rect.h>
|
||||
#include <kandinsky/text.h>
|
||||
#include <kandinsky/types.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef KANDINSKY_LINE_H
|
||||
#define KANDINSKY_LINE_H
|
||||
|
||||
#include <kandinsky/point.h>
|
||||
#include <kandinsky/types.h>
|
||||
|
||||
void KDDrawLine(KDPoint p1, KDPoint p2);
|
||||
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
#ifndef KANDINSKY_POINT_H
|
||||
#define KANDINSKY_POINT_H
|
||||
|
||||
#include <kandinsky/coordinate.h>
|
||||
|
||||
typedef struct {
|
||||
KDCoordinate x;
|
||||
KDCoordinate y;
|
||||
} KDPoint;
|
||||
|
||||
#endif
|
||||
26
kandinsky/include/kandinsky/rect.h
Normal file
26
kandinsky/include/kandinsky/rect.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef KANDINSKY_RECT_H
|
||||
#define KANDINSKY_RECT_H
|
||||
|
||||
#include <kandinsky/color.h>
|
||||
#include <kandinsky/types.h>
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
KDCoordinate x;
|
||||
KDCoordinate y;
|
||||
};
|
||||
KDPoint origin;
|
||||
};
|
||||
union {
|
||||
struct {
|
||||
KDCoordinate width;
|
||||
KDCoordinate height;
|
||||
};
|
||||
KDPoint size;
|
||||
};
|
||||
} KDRect;
|
||||
|
||||
void KDFillRect(KDRect rect, KDColor color);
|
||||
|
||||
#endif
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef KANDINSKY_TEXT_H
|
||||
#define KANDINSKY_TEXT_H
|
||||
|
||||
#include <kandinsky/point.h>
|
||||
#include <kandinsky/types.h>
|
||||
|
||||
void KDDrawChar(char character, KDPoint p);
|
||||
void KDDrawString(char * text, KDPoint p);
|
||||
|
||||
18
kandinsky/include/kandinsky/types.h
Normal file
18
kandinsky/include/kandinsky/types.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef KANDINSKY_TYPES_H
|
||||
#define KANDINSKY_TYPES_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef uint16_t KDCoordinate;
|
||||
|
||||
typedef struct {
|
||||
KDCoordinate x;
|
||||
KDCoordinate y;
|
||||
} KDPoint;
|
||||
|
||||
typedef struct {
|
||||
KDCoordinate width;
|
||||
KDCoordinate height;
|
||||
} KDSize;
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,8 @@
|
||||
#include <kandinsky/line.h>
|
||||
#include <framebuffer.h>
|
||||
|
||||
void KDDrawLine(KDPoint p1, KDPoint p2) {
|
||||
for (KDCoordinate x = p1.x; x<p2.x; x++) {
|
||||
PIXEL(x, p1.y) = 0xC7;
|
||||
}
|
||||
}
|
||||
|
||||
9
kandinsky/src/rect.c
Normal file
9
kandinsky/src/rect.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <kandinsky/rect.h>
|
||||
#include <framebuffer.h>
|
||||
#include <string.h>
|
||||
|
||||
void KDFillRect(KDRect rect, KDColor color) {
|
||||
for (KDCoordinate y = rect.y ; y < (rect.y + rect.height); y++) {
|
||||
memset(PIXEL_ADDRESS(rect.x, y), color, rect.width);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
SFLAGS += -Ipoincare/include
|
||||
objs += $(addprefix poincare/src/, expression.o number.o)
|
||||
objs += $(addprefix poincare/src/, expression.o number.o fraction.o)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define POINCARE_H
|
||||
|
||||
#include <poincare/expression.h>
|
||||
#include <poincare/fraction.h>
|
||||
#include <poincare/number.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
#ifndef POINCARE_EXPRESSION_H
|
||||
#define POINCARE_EXPRESSION_H
|
||||
|
||||
extern "C" {
|
||||
#include <kandinsky.h>
|
||||
}
|
||||
|
||||
class Expression {
|
||||
public:
|
||||
virtual void draw() = 0;
|
||||
virtual void layout() = 0;
|
||||
KDRect m_frame;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
17
poincare/include/poincare/fraction.h
Normal file
17
poincare/include/poincare/fraction.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef POINCARE_FRACTION_H
|
||||
#define POINCARE_FRACTION_H
|
||||
|
||||
#include <poincare/expression.h>
|
||||
|
||||
class Fraction : public Expression {
|
||||
public:
|
||||
Fraction(Expression * numerator, Expression * denominator);
|
||||
virtual void draw();
|
||||
// protected:
|
||||
virtual void layout();
|
||||
private:
|
||||
Expression * m_numerator;
|
||||
Expression * m_denominator;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -3,9 +3,12 @@
|
||||
|
||||
#include <poincare/expression.h>
|
||||
|
||||
class Number : Expression {
|
||||
class Number : public Expression {
|
||||
public:
|
||||
Number(int v);
|
||||
virtual void draw();
|
||||
protected:
|
||||
virtual void layout();
|
||||
private:
|
||||
int m_value;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,2 @@
|
||||
#include <poincare/expression.h>
|
||||
/*
|
||||
Expression::Expression(int v) : value(v) {
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
45
poincare/src/fraction.cpp
Normal file
45
poincare/src/fraction.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <poincare/fraction.h>
|
||||
|
||||
static inline KDCoordinate max(KDCoordinate a, KDCoordinate b) {
|
||||
return (a > b ? a : b);
|
||||
}
|
||||
|
||||
#define FRACTION_BORDER_LENGTH 2
|
||||
#define FRACTION_LINE_MARGIN 2
|
||||
#define FRACTION_LINE_HEIGHT 1
|
||||
|
||||
Fraction::Fraction(Expression * numerator, Expression * denominator) :
|
||||
m_numerator(numerator),
|
||||
m_denominator(denominator) {
|
||||
}
|
||||
|
||||
void Fraction::layout() {
|
||||
m_numerator->layout();
|
||||
m_denominator->layout();
|
||||
|
||||
KDRect numFrame = m_numerator->m_frame;
|
||||
KDRect denFrame = m_denominator->m_frame;
|
||||
|
||||
m_frame.width = max(numFrame.width, denFrame.width) + 2*FRACTION_BORDER_LENGTH;
|
||||
m_frame.height = numFrame.height + FRACTION_LINE_MARGIN + FRACTION_LINE_HEIGHT + FRACTION_LINE_MARGIN + denFrame.height;
|
||||
|
||||
m_numerator->m_frame.origin = {
|
||||
.x = (KDCoordinate)((m_frame.width - numFrame.width)/2),
|
||||
.y = 0
|
||||
};
|
||||
|
||||
m_denominator->m_frame.origin = {
|
||||
.x = (KDCoordinate)((m_frame.width - denFrame.width)/2),
|
||||
.y = (KDCoordinate)(numFrame.height + 2*FRACTION_LINE_MARGIN + FRACTION_LINE_HEIGHT)
|
||||
};
|
||||
}
|
||||
|
||||
void Fraction::draw() {
|
||||
m_numerator->draw();
|
||||
m_denominator->draw();
|
||||
|
||||
KDCoordinate fractionLineY = m_numerator->m_frame.height + FRACTION_LINE_MARGIN;
|
||||
|
||||
KDDrawLine((KDPoint){.x = 0, .y = fractionLineY},
|
||||
(KDPoint){.x = m_frame.width, .y = fractionLineY});
|
||||
}
|
||||
@@ -2,3 +2,12 @@
|
||||
|
||||
Number::Number(int v) : m_value(v) {
|
||||
}
|
||||
|
||||
void Number::layout() {
|
||||
m_frame.width = 20;
|
||||
m_frame.height = 10;
|
||||
}
|
||||
|
||||
void Number::draw() {
|
||||
KDFillRect(m_frame, 0x7F);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user