Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yekm committed Jun 23, 2023
1 parent 335fe56 commit f00fb0a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 91 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ find_package(glfw3)
add_subdirectory(colormap)

add_library(clouds OBJECT
art.cpp
artfactory.cpp
settings.cpp
imgui_elements.cpp
Expand Down
117 changes: 26 additions & 91 deletions art.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@


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


template <typename T>
Expand All @@ -22,133 +20,70 @@ class Art {
public:
Art(std::string _name)
: m_name(_name) {}
const char * name() {return m_name.c_str();}
void resized(int _w, int _h) { // nb: old buffers -> forbidden to drawdot()
if (use_pixel_buffer && !pb)
pb = std::make_unique<PixelBuffer>();
tex_w = _w, tex_h = _h;
frame_number = 0;
resize(_w, _h);
}
bool gui() {
if (ImGui::Checkbox("Force use pixel buffer", &use_pixel_buffer)) {
if (use_pixel_buffer)
pb = std::make_unique<PixelBuffer>();
else
pb.reset();
}

if (pb)
ScrollableSliderUInt("Max pixels", &pixel_buffer_maximum, 1, pixel_buffer_maximum_max, "%d", pixel_buffer_maximum/16);
bool resize_pbo = render_gui();

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

if (pb)
ImGui::Text("pixel_buffer_size %ld",
pb->buffer.size());

return resize_pbo;
}
void draw(uint32_t *p) {
bool direct = render(p);
if (pb) {
pb->erase_old(pixel_buffer_maximum);
render_pixel_buffer(p);
} else if (!direct) {
std::copy(pixels.begin(), pixels.end(), p);
}
}
const char * name();

/* called when main window is resized and if reinit needed.
* after this call PBOs will be recreated */
void resized(int _w, int _h);

/* returns true if there is a need to recreate PBOs */
bool gui();

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

/* TODO: load store of current gui configns */
virtual void load(std::string json) {};
virtual std::string save() { return ""; };

void drawdot(uint32_t x, uint32_t y, double o, uint32_t c) {
drawdot(x, y, c | ((unsigned)(0xff*o)<<24));
}

void drawdot(uint32_t x, uint32_t y, uint32_t c) {
drawdot(data(), x, y, c);
}

void drawdot(uint32_t *screen, uint32_t x, uint32_t y, double o, uint32_t c) {
drawdot(screen, x, y, c | ((unsigned)(0xff*o)<<24));
}

void drawdot(uint32_t *screen, uint32_t x, uint32_t y, uint32_t c) {
if (x >= tex_w || y >= tex_h) {
++pixels_discarded;
return;
}

if (pb)
pb->append({x, y, c});
else
really_drawdot(screen, x, y, c);

++pixels_drawn;
}
/* don't really draws a dot if pixel buffer is used */
void drawdot(uint32_t *screen, uint32_t x, uint32_t y, uint32_t c);

void really_drawdot(uint32_t *screen, uint32_t x, uint32_t y, uint32_t c) {
screen[ y*tex_w + x ] = c;
}

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

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;

void clear() {
fill0(pixels);
pixels_drawn = 0;
pixels_discarded = 0;
}
/* 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 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) {
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 render_pixel_buffer(uint32_t *screen);
std::unique_ptr<PixelBuffer> pb;

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

protected:
bool use_pixel_buffer = false;
unsigned pixels_drawn = 0;
unsigned pixels_discarded = 0;
void default_resize(int _w, int _h) {
w = _w;
h = _h;
//data = (uint8_t *)xrealloc(data, w*h*sizeof(uint32_t));
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 default_resize(int _w, int _h);
int w, h;
//uint8_t *data() { return reinterpret_cast<uint8_t*>(pixels.data()); }
uint32_t *data() { return pixels.data(); }
std::vector<uint32_t> pixels;
std::vector<uint32_t> m_pixels;
std::string m_name;

};

2 changes: 2 additions & 0 deletions rdbomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "random.h"
#include "rdbomb.h"

#include "imgui_elements.h"

// random from yarandom.h returns unsigend int
#define random xoshiro256plus

Expand Down

0 comments on commit f00fb0a

Please sign in to comment.