From bfef6fdc1ec2396cda641cfdbbef67601e3a7941 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Mon, 5 Nov 2018 21:09:01 +0100 Subject: [PATCH] cleanup prototype code --- OBJECTS | 4 +-- inc/col.h | 87 --------------------------------------------- inc/example.h | 27 ++++++++++++-- inc/gfx/canvas.h | 6 +++- inc/gfx/layout.h | 49 +++++++++++++++++++++++++ inc/gfx/screen.h | 21 ++++++----- inc/{ => hal}/hal.h | 5 +-- inc/node.h | 64 +++++++++++++++++++++++++++++++++ src/col.cpp | 63 -------------------------------- src/example.cpp | 22 ++++++------ src/gfx/canvas.cpp | 8 +++++ src/gfx/layout.cpp | 63 ++++++++++++++++++++++++++++++++ src/hal.cpp | 2 +- src/hal/avr.cpp | 15 ++------ src/hal/linux.cpp | 8 ++--- src/main.cpp | 66 +++++++++++----------------------- src/node.cpp | 5 +++ 17 files changed, 272 insertions(+), 243 deletions(-) delete mode 100644 inc/col.h create mode 100644 inc/gfx/layout.h rename inc/{ => hal}/hal.h (89%) create mode 100644 inc/node.h delete mode 100644 src/col.cpp create mode 100644 src/gfx/layout.cpp create mode 100644 src/node.cpp diff --git a/OBJECTS b/OBJECTS index d34239e..e1d1e63 100644 --- a/OBJECTS +++ b/OBJECTS @@ -1,7 +1,7 @@ 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 diff --git a/inc/col.h b/inc/col.h deleted file mode 100644 index 9fe8675..0000000 --- a/inc/col.h +++ /dev/null @@ -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 - 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 - 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 diff --git a/inc/example.h b/inc/example.h index 213b6d5..27aa4bb 100644 --- a/inc/example.h +++ b/inc/example.h @@ -3,9 +3,32 @@ #define _EXAMPLE_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 + 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 + blubValue(Args ... ns) : value(ns..., 64, 96) { + } + void render(canvas& c); +}; #endif diff --git a/inc/gfx/canvas.h b/inc/gfx/canvas.h index 0ee451e..f12a206 100644 --- a/inc/gfx/canvas.h +++ b/inc/gfx/canvas.h @@ -5,7 +5,7 @@ #ifndef MGL_DMXMENU_CANVAS_H #define MGL_DMXMENU_CANVAS_H -#include "hal.h" +#include "hal/hal.h" #include "gfx/font.h" #include "fonts/basic_5x4.h" #include "gfx/buffer_1.h" @@ -32,6 +32,10 @@ public: void print(int x, int y, char *str, font & = basic_5x4); + int getWidth(); + + int getHeight(); + friend screen; }; diff --git a/inc/gfx/layout.h b/inc/gfx/layout.h new file mode 100644 index 0000000..a947dd4 --- /dev/null +++ b/inc/gfx/layout.h @@ -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 diff --git a/inc/gfx/screen.h b/inc/gfx/screen.h index ebdaec7..bb06ef8 100644 --- a/inc/gfx/screen.h +++ b/inc/gfx/screen.h @@ -5,7 +5,8 @@ #ifndef MGL_DMXMENU_SCREEN_H #define MGL_DMXMENU_SCREEN_H -#include "hal.h" +#include "hal/hal.h" +#include "gfx/canvas.h" class screen { private: @@ -13,22 +14,26 @@ private: const int height_; 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){ - hal_draw(x,y); + void draw(int x, int 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 y_ = 0; y_ < 32; y_++) - if(c.buffer_.get(x_*32+y_)) - hal_draw(x+x_,y+y_); + if (c.buffer_.get(x_ * 32 + 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){ } diff --git a/inc/hal.h b/inc/hal/hal.h similarity index 89% rename from inc/hal.h rename to inc/hal/hal.h index a692c64..2d84d77 100644 --- a/inc/hal.h +++ b/inc/hal/hal.h @@ -2,9 +2,6 @@ #ifndef _HAL_H_ #define _HAL_H_ -class Layout; -#include "col.h" - #define I_LEFT 0 #define I_RIGHT 0 #define I_UP 0 @@ -15,8 +12,8 @@ void debug(const char* str); void hal_init(); void hal_draw(int x, int y); void hal_print(int x, int y, const char* str); -void render(Layout&); void hal_render(); +void render(); void quit(); extern bool input[4]; diff --git a/inc/node.h b/inc/node.h new file mode 100644 index 0000000..a838572 --- /dev/null +++ b/inc/node.h @@ -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 + 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 + 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 diff --git a/src/col.cpp b/src/col.cpp deleted file mode 100644 index bdb036e..0000000 --- a/src/col.cpp +++ /dev/null @@ -1,63 +0,0 @@ - -#include -#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; - } -} diff --git a/src/example.cpp b/src/example.cpp index 566af6b..c97dcfa 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -1,23 +1,22 @@ -#include "col.h" #include "example.h" +#include "node.h" #include "gfx/canvas.h" char peng[] = {'_','_','_','_','_',0}; int j = 0; -void draw_foo(int offset, int size, Node* ptr, canvas& c){ - Value* val = (Value*) ptr; +void fooValue::render(canvas& c){ for(int i = 0; i<32; i++) c.draw(0,i); - for(int i = 0; ititle_); + c.print(5 , 10, title_); peng[0]=input[0]?' ':'#'; peng[1]=input[1]?' ':'#'; peng[2]=input[2]?' ':'#'; @@ -28,18 +27,17 @@ void draw_foo(int offset, int size, Node* ptr, canvas& c){ c.print(5 , 20, peng); } -void draw_blub(int offset, int size, Node* ptr, canvas& c){ - Value* val = (Value*) ptr; +void blubValue::render(canvas& c){ for(int i = 0; i<32;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++) c.draw(0,i); - for(int i = 0; i<32 && irender(*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; + } +} \ No newline at end of file diff --git a/src/hal.cpp b/src/hal.cpp index e7d605b..006655f 100644 --- a/src/hal.cpp +++ b/src/hal.cpp @@ -1,4 +1,4 @@ -#include "hal.h" +#include "hal/hal.h" const int SCREEN_WIDTH = 128; const int SCREEN_HEIGHT = 32; diff --git a/src/hal/avr.cpp b/src/hal/avr.cpp index e676ecf..31f471f 100644 --- a/src/hal/avr.cpp +++ b/src/hal/avr.cpp @@ -12,8 +12,6 @@ NanoCanvas canvas(SCREEN_WIDTH, SCREEN_HEIGHT, canvasData); void hal_init(){ ssd1306_128x32_i2c_init(); ssd1306_fillScreen( 0x00 ); - //ssd1306_setFixedFont(ssd1306xled_font6x8); - ssd1306_setFixedFont(ssd1306xled_font5x7); canvas.clear(); } @@ -21,12 +19,7 @@ void hal_draw(int x, int y){ canvas.putPixel(x, y); } -void hal_print(int x, int y, const char* str){ - canvas.printFixed(x, y, str, STYLE_NORMAL); - //TODO delete -} - -void render(Layout& layout) { +void hal_render() { DDRD = 0; PORTD |= (1 << PC2)|(1 << PC3)|(1 << PC4)|(1 << PC5); @@ -40,15 +33,11 @@ void render(Layout& layout) { input[3]=in & (1< s; - Layout l(128); + layout l(128); l.apply(&root); + lp=&l; //render hal_init(); - render(l); - - /* - 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; - } - } + hal_render(); - } - - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); - -#else - hal_render(); -#endif return 0; } + +void render(){ + lp->render(); +} \ No newline at end of file diff --git a/src/node.cpp b/src/node.cpp new file mode 100644 index 0000000..e05dbca --- /dev/null +++ b/src/node.cpp @@ -0,0 +1,5 @@ +// +// Created by jedi on 11/5/18. +// + +#include "node.h"