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

@ -5,10 +5,6 @@
void Node::p(int i){
for(int j = 0; j < i; j++){ print("__"); }
if(title_)
print(title_);
else
print("--");
if(view.render)
view.render(0,0,this);
@ -22,8 +18,72 @@ void Node::p(int i){
next_->p(i);
}
void draw_menu(int offset, int size, Node* val){
void Layout::p(){
for(int j = 0; j < allocated_; j++){
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
#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){
std::cout << str << std::flush;
}
void print(char chr){
char arr[] = "_";
arr[0] = chr;
std::cout << arr << std::flush;
}
void print(int str){
std::cout << str << std::flush;
}
@ -14,6 +74,18 @@ void print(int str){
#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)
{
return malloc(size);
@ -25,11 +97,15 @@ void operator delete(void * ptr)
}
void print(const char* str){
//TODO implement
}
void print(char str){
//TODO implement
}
void print(int str){
//TODO implement
}
#endif

View file

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