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,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 "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; i<size; i++)
for(int i = 0; i<c.getWidth(); i++)
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.print(5 , 10, ptr->title_);
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 && i<size;i++)
c.draw((i%size),i);
for(int i = 0; i<32 && i<c.getWidth();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) {
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_HEIGHT = 32;

View file

@ -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<<PC5);
canvas.clear();
layout.render();
render();
canvas.blt(0, 0);
}
}
void hal_render(){
canvas.blt(0, 0);
}
void * operator new(unsigned int 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 );
if( window == NULL ) {
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
exit(1);
}
*/
int i;
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
}
void render(Layout& layout) {
void hal_render() {
bool quit = false;
@ -93,7 +93,7 @@ void render(Layout& layout) {
}
}
layout.render();
render();
SDL_RenderPresent(renderer);
}

View file

@ -1,6 +1,7 @@
#include "hal.h"
#include "col.h"
#include "hal/hal.h"
#include "node.h"
#include "gfx/layout.h"
#include "example.h"
@ -19,24 +20,24 @@ extern SDL_Renderer *renderer;
#endif
layout* lp;
int main() {
//build tree
Node root(nullptr,
new Node("foo1",
new Node("foo2",
new Node("foo3", new Value("FOO4",5,draw_foo))
menu root(nullptr,
new menu("foo1",
new menu("foo2",
new menu("foo3", new fooValue("FOO4",5))
),
new Node("foo", new Value("BAR",5,draw_foo)),
new Node("foo", new Value("BAZ",5,draw_foo))
new menu("foo", new fooValue("BAR",5)),
new menu("foo", new fooValue("BAZ",5))
),
new Node("blub", new Value("BLUB",5,draw_blub)),
new Node("blub", new Value("BLUB",5,draw_blub)),
new Node("blub", new Value("BLUB",5,draw_blub))
new menu("blub", new blubValue("BLUB",5)),
new menu("blub", new blubValue("BLUB",5)),
new menu("blub", new blubValue("BLUB",5))
);
///root.p(0);
@ -47,46 +48,19 @@ int main() {
// 128 x 32
screen s(128, 32);//<128, 32> 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();
}

5
src/node.cpp Normal file
View file

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