cleanup prototype code

This commit is contained in:
j3d1 2018-11-05 21:09:01 +01:00
parent d958452e48
commit bfef6fdc1e
17 changed files with 272 additions and 243 deletions

View file

@ -1,7 +1,7 @@
TARGET = main TARGET = main
SRC = col.cpp hal.cpp main.cpp example.cpp SRC = hal.cpp main.cpp example.cpp
SRC += gfx/screen.cpp gfx/canvas.cpp gfx/font.cpp SRC += gfx/screen.cpp gfx/canvas.cpp gfx/font.cpp gfx/layout.cpp
SRC += fonts/basic_5x4.cpp SRC += fonts/basic_5x4.cpp

View file

@ -1,87 +0,0 @@
#ifndef _COL_H_
#define _COL_H_
class canvas;
#include "gfx/canvas.h"
class Node;
typedef void (*drawfun_t)(int offset, int size, Node*, canvas& c);
void draw_menu(int, int, Node*, canvas& c);
class Drawable{
public:
const int minsize;
const int maxsize;
drawfun_t render;
Drawable(drawfun_t fun, int min = 0, int max=0): minsize(min), maxsize(max), render(fun){
}
};
class Node{
private:
Node* parent_ = 0;
Node* child_ = 0;
Node* next_ = 0;
void addNodes(bool b){
}
template<class ...Us>
void addNodes(bool b, Node* n, Us ... ns){
next_ = n;
next_->parent_ = parent_;
next_->addNodes(!b,ns...);
}
public:
Drawable view;
const char* title_ = 0;
Node(int min = 24, int max=48): next_(nullptr), child_(nullptr), parent_(nullptr), view(nullptr, min, max){
}
Node(const char* t, drawfun_t fun=draw_menu, int min = 24, int max=48) : title_(t), view(fun, min, max), next_(nullptr), child_(nullptr), parent_(nullptr){
}
template<typename ... Args>
Node(const char* t, Node* n, Args ... ns) : title_(t), view(draw_menu, 24, 48){
child_ = n;
n->parent_=this;
child_->addNodes(true,ns...);
}
Node* getChild(){
return child_;
}
};
class Value: public Node{
public:
const char* header;
int state;
Value(const char* t, int val, drawfun_t fun): Node(t, fun, 64, 96), header(t), state(val){
}
};
class Layout{
class Slot{
private:
int pos_;
int size_;
public:
Node* node;
friend Layout;
};
private:
const int maxwidth_;
int allocated_;
int allocate_(Node*, int);
int expand_(int );
int pack_(int);
public:
static const int maxslots = 10;
Layout(int max) : maxwidth_(max){
}
int apply(Node*);
Slot slots[maxslots];
void render();
};
#endif

View file

@ -3,9 +3,32 @@
#define _EXAMPLE_H_ #define _EXAMPLE_H_
#include "gfx/canvas.h" #include "gfx/canvas.h"
#include "node.h"
void draw_foo(int offset, int size, Node* ptr, canvas& c); class menu : public node {
public:
template<typename ... Args>
menu(Args ... ns) : node(ns...) {
}
void draw_blub(int offset, int size, Node* ptr, canvas& c); void render(canvas &c);
};
class fooValue : public value {
public:
fooValue(const char* str, int val) : value(str, val, 64, 96) {
}
void render(canvas &c);
};
class blubValue : public value{
public:
template<typename ... Args>
blubValue(Args ... ns) : value(ns..., 64, 96) {
}
void render(canvas& c);
};
#endif #endif

View file

@ -5,7 +5,7 @@
#ifndef MGL_DMXMENU_CANVAS_H #ifndef MGL_DMXMENU_CANVAS_H
#define MGL_DMXMENU_CANVAS_H #define MGL_DMXMENU_CANVAS_H
#include "hal.h" #include "hal/hal.h"
#include "gfx/font.h" #include "gfx/font.h"
#include "fonts/basic_5x4.h" #include "fonts/basic_5x4.h"
#include "gfx/buffer_1.h" #include "gfx/buffer_1.h"
@ -32,6 +32,10 @@ public:
void print(int x, int y, char *str, font & = basic_5x4); void print(int x, int y, char *str, font & = basic_5x4);
int getWidth();
int getHeight();
friend screen; friend screen;
}; };

49
inc/gfx/layout.h Normal file
View file

@ -0,0 +1,49 @@
//
// Created by jedi on 11/5/18.
//
#ifndef MGL_DMXMENU_LAYOUT_H
#define MGL_DMXMENU_LAYOUT_H
#include "gfx/canvas.h"
#include "gfx/screen.h"
class layout {
class Slot {
private:
int pos_;
int size_;
canvas *canvas_ = 0;
public:
node *content;
friend layout;
};
private:
const int maxwidth_;
int allocated_;
screen screen_;
int allocate_(node *, int);
int expand_(int);
int pack_(int);
public:
static const int maxslots = 10;
Slot slots[maxslots];
layout(int width) : maxwidth_(width), screen_(width, 32) {
}
int apply(node *);
void render();
};
#endif //MGL_DMXMENU_LAYOUT_H

View file

@ -5,7 +5,8 @@
#ifndef MGL_DMXMENU_SCREEN_H #ifndef MGL_DMXMENU_SCREEN_H
#define MGL_DMXMENU_SCREEN_H #define MGL_DMXMENU_SCREEN_H
#include "hal.h" #include "hal/hal.h"
#include "gfx/canvas.h"
class screen { class screen {
private: private:
@ -13,22 +14,26 @@ private:
const int height_; const int height_;
public: public:
screen(int w, int h): width_(w), height_(h){ screen(int w, int h) : width_(w), height_(h) {
} }
void draw(int x, int y){ void draw(int x, int y) {
hal_draw(x,y); hal_draw(x, y);
} }
void draw(int x, int y, canvas& c){ void draw(int x, int y, canvas &c) {
for (int x_ = 0; x_ < 128; x_++) for (int x_ = 0; x_ < 128; x_++)
for (int y_ = 0; y_ < 32; y_++) for (int y_ = 0; y_ < 32; y_++)
if(c.buffer_.get(x_*32+y_)) if (c.buffer_.get(x_ * 32 + y_))
hal_draw(x+x_,y+y_); hal_draw(x + x_, y + y_);
} }
void print(int x, int y, char* str){ void print(int x, int y, const char *str, font & = basic_5x4){
}
void print(int x, int y, char *str, font & = basic_5x4){
} }

View file

@ -2,9 +2,6 @@
#ifndef _HAL_H_ #ifndef _HAL_H_
#define _HAL_H_ #define _HAL_H_
class Layout;
#include "col.h"
#define I_LEFT 0 #define I_LEFT 0
#define I_RIGHT 0 #define I_RIGHT 0
#define I_UP 0 #define I_UP 0
@ -15,8 +12,8 @@ void debug(const char* str);
void hal_init(); void hal_init();
void hal_draw(int x, int y); void hal_draw(int x, int y);
void hal_print(int x, int y, const char* str); void hal_print(int x, int y, const char* str);
void render(Layout&);
void hal_render(); void hal_render();
void render();
void quit(); void quit();
extern bool input[4]; extern bool input[4];

64
inc/node.h Normal file
View file

@ -0,0 +1,64 @@
//
// Created by jedi on 11/5/18.
//
#ifndef MGL_DMXMENU_NODE_H
#define MGL_DMXMENU_NODE_H
#include "gfx/canvas.h"
class node {
private:
node *parent_ = 0;
node *child_ = 0;
node *next_ = 0;
void addNodes(bool b) {
}
template<class ...Us>
void addNodes(bool b, node *n, Us ... ns) {
next_ = n;
next_->parent_ = parent_;
next_->addNodes(!b, ns...);
}
public:
const char *title_ = 0;
const int minsize;
const int maxsize;
virtual void render(canvas &c) = 0;
node(int min = 24, int max = 48) : next_(nullptr), child_(nullptr), parent_(nullptr), minsize(min), maxsize(max) {
}
node(const char *t, int min = 24, int max = 48) : title_(t), minsize(min), maxsize(max), next_(nullptr),
child_(nullptr),
parent_(nullptr) {
}
template<typename ... Args>
node(const char *t, node *n, Args ... ns) : title_(t), minsize(24), maxsize(48) {
child_ = n;
n->parent_ = this;
child_->addNodes(true, ns...);
}
node *getChild() {
return child_;
}
};
class value: public node {
public:
const char *header;
int state;
value(const char *t, int val, int min = 64, int max = 96) : node(t, min, max), header(t), state(val) {
}
};
#endif //MGL_DMXMENU_NODE_H

View file

@ -1,63 +0,0 @@
#include <gfx/screen.h>
#include "hal.h"
#include "col.h"
void Layout::render() {
for (int j = 0; j < allocated_; j++) {
Node *node;
if ((node = slots[j].node) && node->view.render) {
screen s(128, 32);
canvas c(slots[j].size_, 32);
node->view.render(slots[j].pos_, slots[j].size_, node, c);
s.draw(slots[j].pos_, 0, c);
}
}
}
int Layout::apply(Node* node) {
int rootslot = allocate_(node, maxslots);
int minsize = pack_(rootslot);
expand_(minsize);
return 0;
}
int Layout::pack_(int usedslots) {
int minsum = 0;
int i;
for (i = 0; i < usedslots; i++) {
if (!slots[i].node) break;
if (minsum + slots[i].node->view.minsize > maxwidth_) break;
minsum += slots[i].node->view.minsize;
}
allocated_ = i;
return minsum;
}
int Layout::expand_(int packedsize) {
int diff = maxwidth_ - packedsize;
int pos = 0;
for (int i = allocated_ - 1; i >= 0; i--) {
slots[i].pos_ = pos;
int size = slots[i].node->view.minsize + (diff * slots[i].node->view.minsize) / packedsize;
if (size > slots[i].node->view.maxsize)
size = slots[i].node->view.maxsize;
slots[i].size_ = size;
pos += size;
}
return 0;
}
int Layout::allocate_(Node* node, int depth) {
if (Node *next = node->getChild()) {
int i = allocate_(next, depth) + 1;
slots[i].node = node;
return i;
} else {
slots[0].node = node;
return 0;
}
}

View file

@ -1,23 +1,22 @@
#include "col.h"
#include "example.h" #include "example.h"
#include "node.h"
#include "gfx/canvas.h" #include "gfx/canvas.h"
char peng[] = {'_','_','_','_','_',0}; char peng[] = {'_','_','_','_','_',0};
int j = 0; int j = 0;
void draw_foo(int offset, int size, Node* ptr, canvas& c){ void fooValue::render(canvas& c){
Value* val = (Value*) ptr;
for(int i = 0; i<32; i++) for(int i = 0; i<32; i++)
c.draw(0,i); c.draw(0,i);
for(int i = 0; i<size; i++) for(int i = 0; i<c.getWidth(); i++)
c.draw(i,31-i%32); c.draw(i,31-i%32);
for(int i = 0; i<size; i++) for(int i = 0; i<c.getWidth(); i++)
c.draw(i,31-(i+16)%32); c.draw(i,31-(i+16)%32);
c.print(5 , 10, ptr->title_); c.print(5 , 10, title_);
peng[0]=input[0]?' ':'#'; peng[0]=input[0]?' ':'#';
peng[1]=input[1]?' ':'#'; peng[1]=input[1]?' ':'#';
peng[2]=input[2]?' ':'#'; peng[2]=input[2]?' ':'#';
@ -28,18 +27,17 @@ void draw_foo(int offset, int size, Node* ptr, canvas& c){
c.print(5 , 20, peng); c.print(5 , 20, peng);
} }
void draw_blub(int offset, int size, Node* ptr, canvas& c){ void blubValue::render(canvas& c){
Value* val = (Value*) ptr;
for(int i = 0; i<32;i++) for(int i = 0; i<32;i++)
c.draw(0,i); c.draw(0,i);
c.print(5 , 10, ptr->title_); c.print(5 , 10, title_);
} }
void draw_menu(int offset, int size, Node* val, canvas& c){ void menu::render(canvas& c){
for(int i = 0; i<32;i++) for(int i = 0; i<32;i++)
c.draw(0,i); c.draw(0,i);
for(int i = 0; i<32 && i<size;i++) for(int i = 0; i<32 && i<c.getWidth();i++)
c.draw((i%size),i); c.draw((i%c.getWidth()),i);
} }

View file

@ -24,4 +24,12 @@ void canvas::print(int x, int y, const char *str, font& f) {
void canvas::print(int x, int y, char *str, font& f) { void canvas::print(int x, int y, char *str, font& f) {
f.print(x, y, str, *this); f.print(x, y, str, *this);
}
int canvas::getWidth(){
return width_;
}
int canvas::getHeight(){
return height_;
} }

63
src/gfx/layout.cpp Normal file
View file

@ -0,0 +1,63 @@
//
// Created by jedi on 11/5/18.
//
#include "node.h"
#include "gfx/layout.h"
void layout::render() {
for (int j = 0; j < allocated_; j++) {
node *node_ptr;
if (node_ptr = slots[j].content) {
node_ptr->render(*slots[j].canvas_);
screen_.draw(slots[j].pos_, 0, *slots[j].canvas_);
}
}
}
int layout::apply(node* node) {
int rootslot = allocate_(node, maxslots);
int minsize = pack_(rootslot);
expand_(minsize);
return 0;
}
int layout::pack_(int usedslots) {
int minsum = 0;
int i;
for (i = 0; i < usedslots; i++) {
if (!slots[i].content) break;
if (minsum + slots[i].content->minsize > maxwidth_) break;
minsum += slots[i].content->minsize;
}
allocated_ = i;
return minsum;
}
int layout::expand_(int packedsize) {
int diff = maxwidth_ - packedsize;
int pos = 0;
for (int i = allocated_ - 1; i >= 0; i--) {
slots[i].pos_ = pos;
int size = slots[i].content->minsize + (diff * slots[i].content->minsize) / packedsize;
if (size > slots[i].content->maxsize)
size = slots[i].content->maxsize;
slots[i].size_ = size;
slots[i].canvas_ = new canvas(size, 32);
pos += size;
}
return 0;
}
int layout::allocate_(node* current, int depth) {
if (node* next = current->getChild()) {
int i = allocate_(next, depth) + 1;
slots[i].content = current;
return i;
} else {
slots[0].content = current;
return 0;
}
}

View file

@ -1,4 +1,4 @@
#include "hal.h" #include "hal/hal.h"
const int SCREEN_WIDTH = 128; const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 32; const int SCREEN_HEIGHT = 32;

View file

@ -12,8 +12,6 @@ NanoCanvas canvas(SCREEN_WIDTH, SCREEN_HEIGHT, canvasData);
void hal_init(){ void hal_init(){
ssd1306_128x32_i2c_init(); ssd1306_128x32_i2c_init();
ssd1306_fillScreen( 0x00 ); ssd1306_fillScreen( 0x00 );
//ssd1306_setFixedFont(ssd1306xled_font6x8);
ssd1306_setFixedFont(ssd1306xled_font5x7);
canvas.clear(); canvas.clear();
} }
@ -21,12 +19,7 @@ void hal_draw(int x, int y){
canvas.putPixel(x, y); canvas.putPixel(x, y);
} }
void hal_print(int x, int y, const char* str){ void hal_render() {
canvas.printFixed(x, y, str, STYLE_NORMAL);
//TODO delete
}
void render(Layout& layout) {
DDRD = 0; DDRD = 0;
PORTD |= (1 << PC2)|(1 << PC3)|(1 << PC4)|(1 << PC5); PORTD |= (1 << PC2)|(1 << PC3)|(1 << PC4)|(1 << PC5);
@ -40,15 +33,11 @@ void render(Layout& layout) {
input[3]=in & (1<<PC5); input[3]=in & (1<<PC5);
canvas.clear(); canvas.clear();
layout.render(); render();
canvas.blt(0, 0); canvas.blt(0, 0);
} }
} }
void hal_render(){
canvas.blt(0, 0);
}
void * operator new(unsigned int size) void * operator new(unsigned int size)
{ {
return malloc(size); return malloc(size);

View file

@ -19,13 +19,13 @@ void hal_init(){
} }
//Create window /* //Create window
window = SDL_CreateWindow( "SDL Tutorial", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS ); window = SDL_CreateWindow( "SDL Tutorial", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS );
if( window == NULL ) { if( window == NULL ) {
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
exit(1); exit(1);
} }
*/
int i; int i;
SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer); SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer);
@ -45,7 +45,7 @@ void hal_print(int x, int y, const char* str){
//TODO delete //TODO delete
} }
void render(Layout& layout) { void hal_render() {
bool quit = false; bool quit = false;
@ -93,7 +93,7 @@ void render(Layout& layout) {
} }
} }
layout.render(); render();
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }

View file

@ -1,6 +1,7 @@
#include "hal.h" #include "hal/hal.h"
#include "col.h" #include "node.h"
#include "gfx/layout.h"
#include "example.h" #include "example.h"
@ -19,24 +20,24 @@ extern SDL_Renderer *renderer;
#endif #endif
layout* lp;
int main() { int main() {
//build tree //build tree
Node root(nullptr, menu root(nullptr,
new Node("foo1", new menu("foo1",
new Node("foo2", new menu("foo2",
new Node("foo3", new Value("FOO4",5,draw_foo)) new menu("foo3", new fooValue("FOO4",5))
), ),
new Node("foo", new Value("BAR",5,draw_foo)), new menu("foo", new fooValue("BAR",5)),
new Node("foo", new Value("BAZ",5,draw_foo)) new menu("foo", new fooValue("BAZ",5))
), ),
new Node("blub", new Value("BLUB",5,draw_blub)), new menu("blub", new blubValue("BLUB",5)),
new Node("blub", new Value("BLUB",5,draw_blub)), new menu("blub", new blubValue("BLUB",5)),
new Node("blub", new Value("BLUB",5,draw_blub)) new menu("blub", new blubValue("BLUB",5))
); );
///root.p(0); ///root.p(0);
@ -47,46 +48,19 @@ int main() {
// 128 x 32 // 128 x 32
screen s(128, 32);//<128, 32> s; screen s(128, 32);//<128, 32> s;
Layout l(128); layout l(128);
l.apply(&root); l.apply(&root);
lp=&l;
//render //render
hal_init(); hal_init();
render(l); hal_render();
/*
canvas c(64,32);
char foo[] = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
c.print(1,1,"HELLO");
c.print(1,7,"Foo Bar");
c.print(1,13,foo);
*/
//s.draw(0,0,c);
#ifdef LINUX
bool quit = false;
SDL_RenderPresent(renderer);
while (!quit) {
if (SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
}
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
#else
hal_render();
#endif
return 0; return 0;
} }
void render(){
lp->render();
}

5
src/node.cpp Normal file
View file

@ -0,0 +1,5 @@
//
// Created by jedi on 11/5/18.
//
#include "node.h"