add sdl2 for debugging

This commit is contained in:
j3d1 2018-10-28 14:01:25 +01:00
parent 6da35ff25d
commit 8fcd668585
7 changed files with 263 additions and 43 deletions

View file

@ -1,9 +1,10 @@
#ifndef _COL_H_
#define _COL_H_
class Node; class Node;
typedef void (*drawfun_t)(int offset, int size, Node*); typedef void (*drawfun_t)(int offset, int size, Node*);
void draw_menu(int, int, Node*); void draw_menu(int, int, Node*);
class Drawable{ class Drawable{
@ -11,7 +12,7 @@ class Drawable{
const int minsize; const int minsize;
const int maxsize; const int maxsize;
drawfun_t render; drawfun_t render;
Drawable(drawfun_t fun, int min = 0, int max=~0): minsize(min), maxsize(max), render(fun){ Drawable(drawfun_t fun, int min = 0, int max=0): minsize(min), maxsize(max), render(fun){
} }
}; };
@ -31,18 +32,20 @@ class Node{
public: public:
Drawable view; Drawable view;
const char* title_ = 0; const char* title_ = 0;
Node(): next_(nullptr), child_(nullptr), parent_(nullptr), view(nullptr){ 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) : title_(t), view(fun), next_(nullptr), child_(nullptr), parent_(nullptr){ 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> template<typename ... Args>
Node(const char* t, Node* n, Args ... ns) : title_(t), view(nullptr){ Node(const char* t, Node* n, Args ... ns) : title_(t), view(draw_menu, 24, 48){
child_ = n; child_ = n;
n->parent_=this; n->parent_=this;
child_->addNodes(true,ns...); child_->addNodes(true,ns...);
} }
Node* getChild(){
return child_;
}
void p(int i); void p(int i);
}; };
@ -50,17 +53,33 @@ class Value: public Node{
public: public:
const char* header; const char* header;
int state; int state;
Value(const char* t, int val, drawfun_t fun): Node(t, fun), header(t), state(val){ Value(const char* t, int val, drawfun_t fun): Node(t, fun, 64, 96), header(t), state(val){
} }
}; };
class Layout{ class Layout{
class Slot{
private:
int pos_;
int size_;
public:
Node* node;
friend Layout;
};
private: private:
int menuthrs_; const int maxwidth_;
int allocated_;
int allocate_(Node*, int);
int expand_(int );
int pack_(int);
public: public:
Layout(int max) : menuthrs_(max){ static const int maxslots = 10;
Layout(int max) : maxwidth_(max){
} }
//slots int apply(Node*);
Slot slots[maxslots];
void p();
}; };
#endif

9
inc/example.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_
void draw_foo(int offset, int size, Node* ptr);
void draw_blub(int offset, int size, Node* ptr);
#endif

View file

@ -1,7 +1,14 @@
#ifndef _HAL_H_
#define _HAL_H_
void print(const char* str); void print(const char* str);
void print(char str);
void print(int str); void print(int str);
void setup();
void draw(int x, int y);
void render();
#ifndef LINUX #ifndef LINUX
@ -9,3 +16,8 @@ void * operator new(unsigned int size);
void operator delete(void * ptr); void operator delete(void * ptr);
#endif #endif
extern const int SCREEN_WIDTH;
extern const int SCREEN_HEIGHT;
#endif

View file

@ -5,10 +5,6 @@
void Node::p(int i){ void Node::p(int i){
for(int j = 0; j < i; j++){ print("__"); } for(int j = 0; j < i; j++){ print("__"); }
if(title_)
print(title_);
else
print("--");
if(view.render) if(view.render)
view.render(0,0,this); view.render(0,0,this);
@ -22,8 +18,72 @@ void Node::p(int i){
next_->p(i); next_->p(i);
} }
void Layout::p(){
for(int j = 0; j < allocated_; j++){
void draw_menu(int offset, int size, Node* val){ Node * node;
if((node = slots[j].node) && node->view.render){
print(node->view.minsize);
print("...");
print(node->view.maxsize);
print("\n");
print(slots[j].pos_);
print("...");
print(slots[j].size_);
print("\n");
node->view.render(slots[j].pos_,slots[j].size_,node);
}
print("\n");
}
}
int Layout::apply(Node* node){
int rootslot = allocate_(node, maxslots);
print(rootslot);
print("\n");
int minsize = pack_(rootslot);
print(minsize);
print("\n");
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;
print(i);
print("\n");
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;
}
print(pos);
print("\n");
}
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;
}
} }

46
src/example.cpp Normal file
View file

@ -0,0 +1,46 @@
#include "hal.h"
#include "col.h"
#include "example.h"
void draw_foo(int offset, int size, Node* ptr){
Value* val = (Value*) ptr;
print("|");
if(ptr)
print(ptr->title_);
//print(ptr->state);
print("|");
for(int i = 0; i<32; i++)
draw(offset,i);
for(int i = 0; i<size; i++)
draw(offset+i,31-i%32);
for(int i = 0; i<size; i++)
draw(offset+i,31-(i+16)%32);
}
void draw_blub(int offset, int size, Node* ptr){
Value* val = (Value*) ptr;
print("#");
if(ptr)
print(ptr->title_);
//print(ptr->state);
print("#");
for(int i = 0; i<32;i++)
draw(offset,i);
}
void draw_menu(int offset, int size, Node* val){
if(val&&val->title_){
print("_");
print(val->title_);
print("_");
//print(ptr->state);
}
for(int i = 0; i<32;i++)
draw(offset,i);
for(int i = 0; i<32 && i<size;i++)
draw(offset+(i%size),i);
}

View file

@ -1,11 +1,71 @@
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 32;
#ifdef LINUX #ifdef LINUX
#include <iostream> #include <iostream>
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
SDL_Window* window = NULL;
SDL_Event event;
SDL_Renderer *renderer;
void setup(){
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
exit(1);
}
//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);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
}
void draw(int x, int y){
SDL_RenderDrawPoint(renderer, x, y);
}
void render(){
SDL_RenderPresent(renderer);
while (1) {
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void print(const char* str){ void print(const char* str){
std::cout << str << std::flush; std::cout << str << std::flush;
} }
void print(char chr){
char arr[] = "_";
arr[0] = chr;
std::cout << arr << std::flush;
}
void print(int str){ void print(int str){
std::cout << str << std::flush; std::cout << str << std::flush;
} }
@ -14,6 +74,18 @@ void print(int str){
#include <stdlib.h> #include <stdlib.h>
void setup(){
//TODO implement
}
void draw(int x, int y){
//TODO implement
}
void render(){
//TODO implement
}
void * operator new(unsigned int size) void * operator new(unsigned int size)
{ {
return malloc(size); return malloc(size);
@ -25,11 +97,15 @@ void operator delete(void * ptr)
} }
void print(const char* str){ void print(const char* str){
//TODO implement
}
void print(char str){
//TODO implement
} }
void print(int str){ void print(int str){
//TODO implement
} }
#endif #endif

View file

@ -1,30 +1,23 @@
#include "hal.h" #include "hal.h"
#include "col.h" #include "col.h"
void draw_foo(int offset, int size, Node* ptr){ #include "example.h"
Value* val = (Value*) ptr;
print("\t|");
if(ptr)
print(ptr->title_);
//print(ptr->state);
print("|");
}
void draw_blub(int offset, int size, Node* val){
}
int main() { int main() {
setup();
//build tree
Node root(nullptr, Node root(nullptr,
new Node("foo", new Node("foo1",
new Node("foo", new Node("foo2",
new Node("foo", new Value("FOO",5,draw_foo)) new Node("foo3", new Value("FOO4",5,draw_foo))
), ),
new Node("foo", new Value("FOO",5,draw_foo)), new Node("foo", new Value("BAR",5,draw_foo)),
new Node("foo", new Value("FOO",5,draw_foo)) new Node("foo", new Value("BAZ",5,draw_foo))
), ),
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 Node("blub", new Value("BLUB",5,draw_blub)),
@ -35,11 +28,16 @@ int main() {
root.p(0); root.p(0);
//walk tree + allocate slots
//position slots
// 128 x 32 // 128 x 32
Layout l(64); Layout l(128);
l.apply(&root);
//build tree
//walk tree + allocate slots
//position slots
//render //render
l.p();
render();
} }