Skip to content

Commit

Permalink
refactoring wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yekm committed Nov 16, 2023
1 parent cbb4612 commit a681eac
Show file tree
Hide file tree
Showing 23 changed files with 180 additions and 168 deletions.
2 changes: 1 addition & 1 deletion acidwarp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void AcidWarp::resize(int _w, int _h) {
initPalArray(MainPalArray, pal_num);
initPalArray(TargetPalArray, pal_num);

buf_graf.resize(tex_w*tex_h);
buf_graf.resize(easel->w * easel->h);
fill0(buf_graf);

//default_resize(width, height);
Expand Down
12 changes: 6 additions & 6 deletions acidworm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,18 @@ bool AcidWorm::render_gui() {
void AcidWorm::resize(int _w, int _h) {
//worm_t *wrm;

default_resize(_w, _h);
clear();

last = (_w - 1);
bottom = (_h - 1);
max_x = tex_w;
max_y = tex_h;
last = (easel->w - 1);
bottom = (easel->h - 1);
max_x = easel->w;
max_y = easel->h;

worm.resize(number);
//mpp.resize(1024);
_ip.resize(max_x * max_y); ip = _ip.data();
ref.resize(max_y);
#warning "fill0(ref);"
fill0(ref);

for (int n = 0; n < max_y; ++n) {
ref[n] = ip;
Expand Down
73 changes: 44 additions & 29 deletions art.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
#include <assert.h>

#include "art.hpp"

#include "imgui.h"
#include "imgui_elements.h"

#include "easelplane.h"
#include "easelvertex.h"

Art::Art(std::string _name)
: m_name(_name)
{
if (!easel)
usePlane();
}


const char* Art::name() {
return m_name.c_str();
}

void Art::resized(int _w, int _h) {
tex_w = _w, tex_h = _h;
frame_number = 0;
easel->set_window_size(_w, _h);
easel->set_texture_size(_w, _h);
resize(_w, _h);
}

Expand All @@ -19,44 +32,46 @@ bool Art::gui() {

bool resize_pbo = render_gui();

//ImGui::Text("pixels drawn %d, discarded %d",
// pixels_drawn, pixels_discarded);

return resize_pbo;
}

void Art::draw(uint32_t* p) {
bool direct = render(p);
void Art::draw() {
// uint32_t* p = easel->begin();
bool direct = render(0);
easel->render();

++frame_number;
}

void Art::drawdot(float x, float y) {
evertex()->dab(x,y);
}


void Art::clear() {
easel->clear();
}
/*
void Art::render_pixel_buffer(uint32_t* screen) {
std::fill_n(screen, w*h, 0);
#if 1
auto pbm = std::min<uint64_t>(pb->buffer.size(), pixel_buffer_maximum);// ? pixel_buffer_maximum : pb->buffer.size();
std::for_each_n(pb->buffer.begin(), pbm, [&](PixelBuffer::Pixel & p) {
really_drawdot(screen, p.x, p.y, p.color);
});
#else
for (auto &p : pb->buffer)
drawdot(screen, p.x, p.y, p.color);
#endif
}
*/

void Art::default_resize(int _w, int _h) {
w = _w;
h = _h;
//data = (uint8_t *)xrealloc(data, w*h*sizeof(uint32_t));
//m_pixels.resize(w*h);
clear();
/* TODO: fill square in drawdot() if pscale > 1
pscale = 1;
if (xgwa.width > 2560 || xgwa.height > 2560)
pscale = 3; // Retina displays
*/
}

void Art::usePlane() {
easel = std::make_unique<EaselPlane>();
ep = dynamic_cast<EaselPlane*>(easel.get());
}
void Art::useVertex() {
easel = std::make_unique<EaselVertex>();
ev = dynamic_cast<EaselVertex*>(easel.get());
}

EaselPlane* Art::eplane() const {
assert(ep);
return ep;
//return dynamic_cast<EaselPlane*>(easel.get());
}
EaselVertex* Art::evertex() const {
assert(ev);
return ev;
//return dynamic_cast<EaselVertex*>(easel.get());
}
38 changes: 13 additions & 25 deletions art.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include <algorithm>


#include "easelplane.h"
#include "easelvertex.h"
#include "easel.h"
//#include "easelplane.h"
//#include "easelvertex.h"


template <int N, typename T>
Expand All @@ -26,12 +27,7 @@ fill0(T &container) {

class Art {
public:
Art(std::string _name)
: m_name(_name)
{
if (!easel)
easel = std::make_unique<EaselPlane>();
}
Art(std::string _name);
const char * name();

/* called when main window is resized and if reinit needed.
Expand All @@ -42,7 +38,7 @@ class Art {
bool gui();

/* draws a picture into tex_w x tex_h uint32_t RGBA memory buffer */
void draw(uint32_t *p);
void draw();

/* TODO: load store of current gui configns */
virtual void load(std::string json) {};
Expand All @@ -62,41 +58,33 @@ class Art {
easel->drawdot(x,y,c);
}

/* deprecated? */
virtual void reinit() { resize(w, h); }
void drawdot(float x, float y);

virtual ~Art() = default;


unsigned frame_number = 0, clear_every = 0, max_kframes = 0;
unsigned pixel_buffer_maximum = 1024*10, pixel_buffer_maximum_max = 1024*1024;



/* clears m_pixels */
void clear();

/* in order to use custom texture size this variables must be overwritten
* on each resize(). make_pbo() in main() uses this variables */
int tex_w, tex_h;
private:
virtual void resize(int _w, int _h) { default_resize(_w, _h); };
virtual bool render_gui() {return false;}
virtual bool render(uint32_t *p) = 0;

void render_pixel_buffer(uint32_t *screen);

//uint32_t *data() { return m_pixels.data(); }

protected:
void default_resize(int _w, int _h);

std::unique_ptr<Easel> easel;
int w, h;
std::string m_name;
EaselPlane * ep = nullptr;
EaselVertex * ev = nullptr;
void usePlane();
void useVertex();
EaselPlane* eplane() const;
EaselVertex* evertex() const;

EaselPlane* plane() const {
return dynamic_cast<EaselPlane*>(easel.get());
}
std::string m_name;
};

20 changes: 12 additions & 8 deletions attractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@

void Attractor::init()
{
/*
int centerx, centery;
centerx = w / 2;
centery = h / 2;
centerx = easel->w / 2;
centery = easel->h / 2;
double range = sqrt((double) centerx * centerx +
(double) centery * centery) / (1.0 + LRAND() / MAXRAND);
*/
ai = aj = 0.0;
/*
inc = (int) ((LRAND() / MAXRAND) * 200) - 100;
Expand All @@ -45,18 +47,20 @@ bool Attractor::render(uint32_t *p)

auto ftarget = easel->frame_vertex_target();
auto vbmax = easel->vertex_buffer_maximum();
for (int i=0; i < ftarget && count < vbmax; ++i, ++count) {
for (int i=0; i < ftarget /*&& count < vbmax*/; ++i, ++count) {
oldj = aj;
oldi = ai + inc;
aj = a - ai;
ai = oldj + (ai < 0
? sqrt(fabs(b * oldi - c))
: -sqrt(fabs(b * oldi - c))
);
x = w/2 + (int) (ai + aj);
y = h/2 - (int) (ai - aj);
//x = easel->w/2 + (int) (ai + aj);
//y = easel->h/2 - (int) (ai - aj);

drawdot(x, y, 0);
drawdot((ai + aj)/easel->w, (ai - aj)/easel->h);
//drawdot((float)x/w-.5, (float)y/h-.5);
//drawdot(x, y, 0);
//drawdot(x, y, pal.get_color((vertex_buffer_maximum() - count)>>2));
//pb->adot((float)x/w-.5, (float)y/h-.5);
}
Expand Down Expand Up @@ -90,14 +94,14 @@ bool Attractor::render_gui ()


if (up) {
resize(w, h);
resize(easel->w, easel->h);
}

return false;
}

void Attractor::resize(int _w, int _h) {
default_resize(_w, _h);
clear();

pal.rescale(easel->vertex_buffer_maximum() >> 2);
count = 0;
Expand Down
6 changes: 2 additions & 4 deletions attractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
#include <vector>

#include "settings.hpp"

#include "easelvertex.h"


class Attractor : public Art {
public:
Attractor()
: Art("strange attractors") {
easel = std::make_unique<EaselVertex>();
//use_pixel_buffer = true;
//pixel_buffer_maximum = 1024*1024;
useVertex();
}

private:
Expand Down
13 changes: 6 additions & 7 deletions cloudlife.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,18 @@ void Cloudlife::populate_field(unsigned int p)
}

void Cloudlife::resize(int _w, int _h) {
default_resize(_w, _h);

if (f->height != h / (1 << f->cell_size) + 2 ||
f->width != w / (1 << f->cell_size) + 2) {
clear();
if (f->height != easel->h / (1 << f->cell_size) + 2 ||
f->width != easel->w / (1 << f->cell_size) + 2) {
refield();
}
}

void Cloudlife::refield() {
resize_field(w / (1 << f->cell_size) + 2,
h / (1 << f->cell_size) + 2);
resize_field(easel->w / (1 << f->cell_size) + 2,
easel->h / (1 << f->cell_size) + 2);
populate_field(density);
default_resize(w, h); // clear pixels
clear();
}

bool Cloudlife::render_gui() {
Expand Down
10 changes: 5 additions & 5 deletions discrete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void Discrete::init_discrete()
double range;
discretestruct *hp = &discrete;

hp->maxx = w;
hp->maxy = h;
hp->maxx = easel->w;
hp->maxy = easel->h;
hp->op = (ftypes)bias;
switch (hp->op) {
case HSHOE:
Expand Down Expand Up @@ -286,7 +286,7 @@ bool Discrete::render(uint32_t *p)
}

if (hp->count > cycles) {
resize(w, h);
resize(easel->w, easel->h);
}

return false;
Expand All @@ -308,14 +308,14 @@ bool Discrete::render_gui ()


if (up) {
resize(w, h);
resize(easel->w, easel->h);
}

return up;
}

void Discrete::resize(int _w, int _h) {
default_resize(_w, _h);
clear();

init_discrete();
}
2 changes: 1 addition & 1 deletion discrete.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Discrete : public Art {
public:
Discrete()
: Art("discrete --- chaotic mappings") {
pixel_buffer_maximum = 1024*128;
useVertex();
}
private:
virtual bool render_gui() override;
Expand Down
Loading

0 comments on commit a681eac

Please sign in to comment.