Skip to content

Commit

Permalink
direct rendering flag
Browse files Browse the repository at this point in the history
  • Loading branch information
yekm committed Jun 11, 2023
1 parent 57526f1 commit 36b6629
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 31 deletions.
33 changes: 27 additions & 6 deletions art.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


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


Expand All @@ -27,14 +28,25 @@ class Art {
bool gui() {
if (pb)
ScrollableSliderUInt("Max pixels", &pixel_buffer_maximum, 1, pixel_buffer_maximum_max, "%d", pixel_buffer_maximum/16);
return render_gui();
bool up = render_gui();

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

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

return up;
}
void draw(uint32_t *p) {
bool direct = render(p);
if (pb) {
pb->trunc(pixel_buffer_maximum);
render_pixel_buffer(p);
} else if (!direct) {
std::copy(pixels.begin(), pixels.end(), p);
}
render(p);
}
virtual void load(std::string json) {};
virtual std::string save() { return ""; };
Expand Down Expand Up @@ -62,10 +74,8 @@ class Art {

virtual ~Art() = default;

unsigned pixels_drawn = 0;
unsigned pixels_discarded = 0;
unsigned frame_number = 0, clear_every = 0;
unsigned pixel_buffer_maximum = 1024*10, pixel_buffer_maximum_max = 1024*64;
unsigned pixel_buffer_maximum = 1024*10, pixel_buffer_maximum_max = 1024*1024;

void clear() {
fill0(pixels);
Expand All @@ -75,15 +85,26 @@ class Art {
private:
virtual void resize(int _w, int _h) {default_resize(_w, _h);};
virtual bool render_gui() = 0;
virtual void render(uint32_t *p) = 0;
virtual bool render(uint32_t *p) = 0;

void render_pixel_buffer(uint32_t *screen) {
std::fill_n(screen, w*h, 0);
#if 1
auto b = pb->buffer.begin();
auto pbm = pb->buffer.size() > pixel_buffer_maximum ? pixel_buffer_maximum : pb->buffer.size();
auto end = pb->buffer.begin() + pbm;
for (; b != end; ++b)
drawdot(screen, b->x, b->y, b->color);
#else
for (auto &p : pb->buffer)
drawdot(screen, p.x, p.y, p.color);
#endif
}


protected:
unsigned pixels_drawn = 0;
unsigned pixels_discarded = 0;
void default_resize(int _w, int _h) {
w = _w;
h = _h;
Expand Down
7 changes: 3 additions & 4 deletions cloudlife.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
* Dear ImGui port by Pavel Vasilyev <[email protected]>, Jan 2023
*/

Expand Down Expand Up @@ -243,7 +243,7 @@ unsigned int Cloudlife::do_tick()
return count;
}

void Cloudlife::render(uint32_t *p) {
bool Cloudlife::render(uint32_t *p) {
unsigned int count = 0;

for (int i=0; i<f->ticks_per_frame; ++i)
Expand All @@ -270,8 +270,7 @@ void Cloudlife::render(uint32_t *p) {
draw_field();
cycles++;

std::copy(pixels.begin(), pixels.end(), p);

return false;
}


2 changes: 1 addition & 1 deletion cloudlife.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Cloudlife : public Art {
private:
virtual bool render_gui() override;
virtual void resize(int _w, int _h) override;
virtual void render(uint32_t *p) override;
virtual bool render(uint32_t *p) override;
std::unique_ptr<struct field> f;

unsigned ncolors=512;
Expand Down
20 changes: 12 additions & 8 deletions discrete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static enum ftypes bias[BIASES] =

void Discrete::init_discrete()
{
pb.reset();
discrete = discretestruct{};
double range;
discretestruct *hp = &discrete;
Expand Down Expand Up @@ -172,7 +173,8 @@ void Discrete::init_discrete()
break;
}
case THORNBIRD:
hp->b = 0.1;
pb = std::make_unique<PixelBuffer>();
hp->b = 0.1;
hp->i = hp->j = 0.1;

/* select frequencies for parameter variation */
Expand Down Expand Up @@ -323,21 +325,22 @@ void Discrete::draw_discrete_1()
break;
}
if (hp->op == THORNBIRD) {
x = (short) (hp->maxx / 2 * (1
+ sint*hp->j + cost*cosp*hp->i - cost*sinp*hp->b));
y = (short) (hp->maxy / 2 * (1
- cost*hp->j + sint*cosp*hp->i - sint*sinp*hp->b));
x = (short) (hp->maxx / 2 * (1
+ sint*hp->j + cost*cosp*hp->i - cost*sinp*hp->b));
y = (short) (hp->maxy / 2 * (1
- cost*hp->j + sint*cosp*hp->i - sint*sinp*hp->b));
pb->append({x, y, pal.get_color(count - k)});
}
else {
x = hp->maxx / 2 + (int) ((hp->i - hp->ic) * hp->is);
y = hp->maxy / 2 - (int) ((hp->j - hp->jc) * hp->js);
drawdot(x, y, pal.get_color(count - k));
}
drawdot(x, y, pal.get_color(k));
}

}

void Discrete::render(uint32_t *p)
bool Discrete::render(uint32_t *p)
{
discretestruct *hp = &discrete;
int i;
Expand All @@ -347,13 +350,14 @@ void Discrete::render(uint32_t *p)
hp->count++;
}

std::copy(pixels.begin(), pixels.end(), p);
//if (hp->op == THORNBIRD)
// clear();

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

return false;
}


Expand Down
4 changes: 2 additions & 2 deletions ifs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ IFS::iterate(int count, int p)
/* Come on and iterate, iterate, iterate and sing... *
* Yeah, this function just calls iterate, mutate, *
* and then draws everything. */
void IFS::render(uint32_t *p)
bool IFS::render(uint32_t *p)
{
int i;
int partcolor, x, y;
Expand Down Expand Up @@ -285,7 +285,7 @@ void IFS::render(uint32_t *p)
mutate(&lenses[i]);
}

std::copy(pixels.begin(), pixels.end(), p);
return false;
}


Expand Down
2 changes: 1 addition & 1 deletion ifs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class IFS : public Art {
private:
virtual bool render_gui() override;
virtual void resize(int _w, int _h) override;
virtual void render(uint32_t *p) override;
virtual bool render(uint32_t *p) override;

int ncolours = 1024;
int ccolour;
Expand Down
3 changes: 0 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,6 @@ int main(int argc, char *argv[])

bool reinit = art->gui();

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

ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);
Expand Down
4 changes: 2 additions & 2 deletions mtron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void Minskytron::dt(uint32_t *p, int x, int y, double o, uint32_t c) {
p[ y*W + x ] = c | ((unsigned)(0xff*o)<<24);
}

void Minskytron::render(uint32_t *p) {
bool Minskytron::render(uint32_t *p) {
//clear();
//std::fill(p, p+TEXTURE_SIZE, 0);
memset(p, 0, TEXTURE_SIZE);
Expand Down Expand Up @@ -74,7 +74,7 @@ void Minskytron::render(uint32_t *p) {
++i;
}

//std::copy(pixels.begin(), pixels.end(), p);
return true;
}

bool Minskytron::render_gui() {
Expand Down
2 changes: 1 addition & 1 deletion mtron.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Minskytron : public Art {
private:
virtual bool render_gui() override;
//virtual void resize(int _w, int _h) override;
virtual void render(uint32_t *p) override;
virtual bool render(uint32_t *p) override;

void reinit();
void dt(uint32_t *p, int x, int y, double o, uint32_t c);
Expand Down
4 changes: 2 additions & 2 deletions vermiculate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ bool Vermiculate::move (unsigned char thr)
return (!LP->dead);
}

void Vermiculate::render (uint32_t *p)
bool Vermiculate::render (uint32_t *p)
{
int had_instring = (instring != 0);
int tick = 0;
Expand Down Expand Up @@ -695,7 +695,7 @@ void Vermiculate::render (uint32_t *p)
if (loop++ < cycles)
goto AGAIN;

std::copy(pixels.begin(), pixels.end(), p);
return false;
}

bool Vermiculate::render_gui ()
Expand Down
2 changes: 1 addition & 1 deletion vermiculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Vermiculate : public Art {
private:
virtual bool render_gui() override;
virtual void resize(int _w, int _h) override;
virtual void render(uint32_t *p) override;
virtual bool render(uint32_t *p) override;

int speed = 1;
bool erasing, cleared, autopal;
Expand Down

0 comments on commit 36b6629

Please sign in to comment.