Move actual code to Internal/ and add barebones of ZFun

This commit is contained in:
Esteban I. Ruiz Moreno 2018-06-06 14:05:47 -03:00
parent 12913c2031
commit f055ba82e6
28 changed files with 257 additions and 36 deletions

View File

@ -310,15 +310,16 @@ int main(int argc, char** argv) {
std::vector<std::string> sources = {
"main.cxx",
"src/Color.cxx",
"src/DoubleBuffering.cxx",
"src/FontManager.cxx",
"src/Pos.cxx",
"src/RawFB.cxx",
"src/Toolkit.cxx",
"src/Exceptions.cxx",
"src/TextScreen.cxx",
"src/UIEvents.cxx"
"src/Internal/Color.cxx",
"src/Internal/DoubleBuffering.cxx",
"src/Internal/FontManager.cxx",
"src/Internal/Pos.cxx",
"src/Internal/RawFB.cxx",
"src/Internal/Toolkit.cxx",
"src/Internal/Exceptions.cxx",
"src/Internal/TextScreen.cxx",
"src/Internal/UIEvents.cxx",
"src/Apps/demo1.cxx",
};
man.add(std::shared_ptr<Task>(new TaskFonts()));

View File

@ -1,6 +1,8 @@
#pragma once
#include <cstdint>
namespace Internal {
class Color {
public:
int r;
@ -11,3 +13,5 @@ public:
uint32_t toCode();
Color blend(Color other, double mix);
};
}

View File

@ -1,9 +1,11 @@
#pragma once
#include "Drawer.h"
#include "RawFB.h"
#include "Internal/Drawer.h"
#include "Internal/RawFB.h"
#include <memory>
#include <cstdint>
namespace Internal {
class DoubleBuffering : public Drawer {
private:
std::shared_ptr<uint8_t> local_buffer;
@ -24,3 +26,5 @@ public:
void refreshScreen();
void clear();
};
}

View File

@ -1,6 +1,9 @@
#pragma once
#include "Color.h"
#include "Pos.h"
#include "Internal/Color.h"
#include "Internal/Pos.h"
namespace Internal {
class Drawer {
public:
@ -9,3 +12,5 @@ public:
virtual int screenWidth() = 0;
virtual int screenHeight() = 0;
};
}

View File

@ -2,7 +2,11 @@
#include <stdexcept>
namespace Internal {
class FramebufferException : public std::runtime_error {
public:
FramebufferException() : std::runtime_error("FramebufferException") {};
};
}

View File

@ -1,12 +1,14 @@
#pragma once
#include "Drawer.h"
#include "Internal/Drawer.h"
#include <string>
#include <vector>
#define FONT_HEIGHT 16
#define FONT_WIDTH 8
namespace Internal {
class FontManager {
private:
Drawer& backend;
@ -17,3 +19,5 @@ public:
void write(Pos pos, char c, int scale = 1, Color col = Color(255,255,255), double blend = 1);
void write(Pos pos, const std::string& string, int scale = 1, int sep = 4, Color col = Color(255,255,255), double blend = 1);
};
}

View File

@ -1,8 +1,13 @@
#pragma once
namespace Internal {
class Pos {
public:
int x;
int y;
Pos(int x, int y);
};
}

View File

@ -1,8 +1,10 @@
#pragma once
#include "Drawer.h"
#include "Internal/Drawer.h"
#include <string>
namespace Internal {
class RawFB : public Drawer {
private:
uint8_t *fbp;
@ -27,3 +29,5 @@ public:
uint32_t getLineLen();
uint32_t getBufferSize();
};
}

View File

@ -1,10 +1,12 @@
#pragma once
#include "FontManager.h"
#include "Internal/FontManager.h"
#include <string>
#include <deque>
namespace Internal {
class TextScreen {
std::deque<std::string> buffers;
uint32_t buffer_limit;
@ -23,3 +25,5 @@ public:
void render();
void clear();
};
}

View File

@ -1,6 +1,8 @@
#include "Drawer.h"
namespace Internal {
class Toolkit {
private:
Drawer& backend;
@ -9,3 +11,5 @@ public:
void line(Pos p1, Pos p2, Color col = Color(255,255,255));
};
}

View File

@ -5,6 +5,9 @@
#include <vector>
#include <string>
namespace Internal {
struct ClearHistory {};
struct AddLine {
std::vector<std::string> vec;
@ -28,3 +31,5 @@ struct SendInput {
using UIEvent = boost::variant<ClearHistory,AddLine,ChangePrompt,InsertChar,SendInput>;
}

21
include/ZFun/Color.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include <cstdint>
namespace ZFun {
struct Color {
int8_t r;
int8_t g;
int8_t b;
int8_t a;
Color() : r(0), g(0), b(0), a(255) {};
Color(int8_t all) : r(all), g(all), b(all), a(255) {};
Color(int8_t r, int8_t g, int8_t b) : r(r), g(g), b(b), a(255) {};
Color(int8_t r, int8_t g, int8_t b, int8_t a) : r(r), g(g), b(b), a(a) {};
};
}

16
include/ZFun/Context.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <memory>
#include "ZFun/Window.h"
namespace ZFun {
class Context {
public:
std::shared_ptr<ZFun::Window> Window(const std::string& title, const Resolution& res);
};
};

14
include/ZFun/Resolution.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <cstdint>
#include "ZFun/Vector.h"
namespace ZFun {
struct ResI {
using type = int32_t;
};
using Resolution = Vector2<ResI>;
}

54
include/ZFun/Vector.h Normal file
View File

@ -0,0 +1,54 @@
#pragma once
#include <cstdint>
namespace ZFun {
template <typename T>
struct Vector2 {
typename T::type x;
typename T::type y;
Vector2(typename T::type x, typename T::type y) : x(x), y(y) {};
Vector2(typename T::type a) : x(a), y(a) {};
Vector2() : x(0), y(0) {};
};
template <typename T>
Vector2<T> operator+(const Vector2<T>& a, const Vector2<T>& b) {
return Vector2<T>(a.x + b.x, a.y + b.y);
}
template <typename T>
Vector2<T> operator-(const Vector2<T>& a, const Vector2<T>& b) {
return Vector2<T>(a.x - b.x, a.y - b.y);
}
template <typename T>
Vector2<T>& operator+=(Vector2<T>& self, const Vector2<T>& other) {
self.x += other.x;
self.y += other.y;
}
template <typename T>
Vector2<T>& operator-=(Vector2<T>& self, const Vector2<T>& other) {
self.x -= other.x;
self.y -= other.y;
}
struct Vi {
using type = int32_t;
};
struct Vf {
using type = double;
};
using Vector2i = Vector2<Vi>;
using Vector2f = Vector2<Vf>;
}

25
include/ZFun/Window.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include "ZFun/Resolution.h"
#include "ZFun/Vector.h"
#include "ZFun/Color.h"
namespace ZFun {
class Window {
private:
Window();
public:
bool isOpen();
Resolution getResolution();
void clear();
void render();
void putPixel(const Vector2i &pos, const Color& col);
};
}

View File

@ -1,21 +1,22 @@
#include <iostream>
#include <atomic>
#include "conc.h"
#include <UIEvents.h>
#include <Internal/UIEvents.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include "Color.h"
#include "Drawer.h"
#include "RawFB.h"
#include "DoubleBuffering.h"
#include "FontManager.h"
#include "Toolkit.h"
#include "TextScreen.h"
#include "Internal/Color.h"
#include "Internal/Drawer.h"
#include "Internal/RawFB.h"
#include "Internal/DoubleBuffering.h"
#include "Internal/FontManager.h"
#include "Internal/Toolkit.h"
#include "Internal/TextScreen.h"
#include "libs/inline_variant.hpp"
using namespace Internal;
void render_thread(Conc::Chan<UIEvent> &evs,Conc::Chan<std::string> &input, std::atomic<bool> &stop) {

18
src/Apps/demo1.cxx Normal file
View File

@ -0,0 +1,18 @@
#include "ZFun/Context.h"
#include "ZFun/Window.h"
int demo_main(ZFun::Context ctx) {
std::shared_ptr<ZFun::Window> w = ctx.Window("Hello", ZFun::Resolution(640, 480));
int idx = 0;
while (w->isOpen()) {
w->clear();
for (int i=0; i < 50; i++) {
w->putPixel(ZFun::Vector2i(idx + i, i), ZFun::Color(5 * i, 5 * i, 5 * i));
}
w->render();
}
}

View File

@ -1,4 +1,6 @@
#include "Color.h"
#include "Internal/Color.h"
namespace Internal {
Color::Color(int r, int g, int b) : r(r), g(g), b(b) {};
Color::Color(uint32_t c) {
@ -16,3 +18,5 @@ Color Color::blend(Color other, double mix) {
g * (1-mix) + other.g * mix,
b * (1-mix) + other.b * mix);
}
}

View File

@ -1,6 +1,8 @@
#include "DoubleBuffering.h"
#include "Internal/DoubleBuffering.h"
#include <cstring>
namespace Internal {
DoubleBuffering::DoubleBuffering(std::shared_ptr<RawFB> backend) : backend(backend) {
ww = backend->screenWidth();
wh = backend->screenHeight();
@ -31,3 +33,5 @@ Color DoubleBuffering::getPixel(Pos pos) {
if (pos.x < 0 || pos.y < 0 || pos.x >= ww || pos.y >= wh) return Color(0,0,0);
return Color(*((uint32_t*)(local_buffer.get()+(pos.x*bpp+pos.y*lineLen))));
}
}

View File

@ -1,6 +1,7 @@
#include "FontManager.h"
#include "Internal/FontManager.h"
namespace Internal {
FontManager::FontManager(Drawer& backend) : backend(backend) {
#include "font.h"
@ -40,3 +41,5 @@ void FontManager::write(Pos pos, const std::string& string, int scale, int sep,
write(Pos(pos.x + i * (16 * scale + sep), pos.y), string[i], scale, col, blend);
}
}
}

8
src/Internal/Pos.cxx Normal file
View File

@ -0,0 +1,8 @@
#include "Internal/Pos.h"
namespace Internal {
Pos::Pos(int x, int y) : x(x), y(y) {};
}

View File

@ -7,10 +7,13 @@
#include <fcntl.h>
#include <stropts.h>
#include <sys/mman.h>
#include "RawFB.h"
#include "Exceptions.h"
#include "Internal/RawFB.h"
#include "Internal/Exceptions.h"
#include <cstring>
namespace Internal {
RawFB::RawFB(const std::string& fb_file) {
struct fb_fix_screeninfo finfo;
struct fb_var_screeninfo vinfo;
@ -67,3 +70,5 @@ Color RawFB::getPixel(Pos pos) {
if (pos.x < 0 || pos.y < 0 || pos.x >= ww || pos.y >= wh) return Color(0,0,0);
return Color(*((uint32_t*)(fbp+(pos.x*bpp+pos.y*lineLen))));
}
}

View File

@ -1,5 +1,7 @@
#include "TextScreen.h"
#include "Toolkit.h"
#include "Internal/TextScreen.h"
#include "Internal/Toolkit.h"
namespace Internal {
TextScreen::TextScreen(Drawer& backend, uint32_t blimit) : backend(backend), fm(backend) {
buffer_limit = blimit;
@ -84,3 +86,5 @@ std::string TextScreen::getInput(bool clear) {
return input_line;
}
}
}

View File

@ -1,7 +1,9 @@
#include "Toolkit.h"
#include "Internal/Toolkit.h"
#include <math.h>
#include <algorithm>
namespace Internal {
Toolkit::Toolkit(Drawer& backend) : backend(backend) {};
// stolen from Rosetta Code
@ -46,3 +48,5 @@ void Toolkit::line(Pos p0, Pos p1, Color col) {
}
}
};
}

View File

@ -1,4 +0,0 @@
#include "Pos.h"
Pos::Pos(int x, int y) : x(x), y(y) {};