summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZoRo <dos21h@gmail.com>2020-11-24 20:01:30 +0000
committerZoRo <dos21h@gmail.com>2020-11-24 20:01:30 +0000
commit1c5bd982ab37b161410be55eaa401e4686d16351 (patch)
tree4c90edbc0f3953999a7594b83a6876d811937704
parente41ad7adc31c26dfa8a9c31b6b16bda46f9b85a7 (diff)
downloadwasm_fractal-1c5bd982ab37b161410be55eaa401e4686d16351.tar.gz
wasm_fractal-1c5bd982ab37b161410be55eaa401e4686d16351.zip
Updated version that looks like works. EMCC loop works ok now
-rw-r--r--Makefile16
-rw-r--r--fractal.c199
-rw-r--r--index.html19
-rw-r--r--koh.h1
4 files changed, 215 insertions, 20 deletions
diff --git a/Makefile b/Makefile
index 6f3e457..3711d67 100644
--- a/Makefile
+++ b/Makefile
@@ -7,15 +7,23 @@ LDFLAGS=-lSDL2 -lSDL2_ttf -lGLESv2 -lGL
EM_LDFALGS=-s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' -s USE_SDL_TTF=2 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s LLD_REPORT_UNDEFINED -s USE_GLFW=3
make:
- $(CC) -c koh.c -g3 -pg
- $(CC) -c fractal.c -g3 -pg
+ $(CC) -c koh.c -g3
+ $(CC) -c fractal.c -g3
#$(CC) -c fractal2.c
- $(CC) koh.o fractal.o -o fractal $(LDFLAGS) -g3 -pg
+ $(CC) koh.o fractal.o -o fractal $(LDFLAGS) -g3
#$(CC) koh.o fractal2.o -o fractal2 $(LDFLAGS)
emcc:
$(EMCC) fractal.c koh.c -s WASM=1 -O3 -o index.js $(EM_LDFALGS) -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]'
#$(EMCC) fractal2.c koh.c -s WASM=1 -O3 -o index.js $(EM_LDFALGS) -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]'
+deploy:
+ #scp index.html segfault:~/public_html/draw.html
+ #scp index.js segfault:~/public_html/index.js
+ #scp index.wasm segfault:~/public_html/index.wasm
+ scp index.html fam@do2:/srv/http/wasm.main.lv/fractal
+ scp index.js fam@do2:/srv/http/wasm.main.lv/fractal
+ scp index.wasm fam@do2:/srv/http/wasm.main.lv/fractal
+
web:
- darkhttpd ./ --port 12345 \ No newline at end of file
+ darkhttpd ./ --port 12345
diff --git a/fractal.c b/fractal.c
index e5c0841..86ca9f4 100644
--- a/fractal.c
+++ b/fractal.c
@@ -27,7 +27,7 @@
#define SCREEN_WIDTH2 (SCREEN_WIDTH/2)
#define SCREEN_HEIGHT 320
#define SCREEN_HEIGHT2 (SCREEN_HEIGHT/2)
-#define MAX_GENERATION 5
+//#define MAX_GENERATION 5
static int quit=0;
static SDL_Window *window = NULL;
@@ -133,6 +133,67 @@ rgba32_t color_gen_mainlv(rgba32_t original, int gen, int max_gen)
return c;
}
+rgba32_t color_gen_redish(rgba32_t original, int gen, int max_gen)
+{
+ double color_r = 20.0/255;
+ double color_g = 10.0/255;
+ double color_b = 14.0/255;
+ rgba32_t c = {color_r, color_g, color_b,0};
+
+ /*
+ c.r = 1.0*180/255 + (gen-max_gen)*color_r;
+ c.g = 1.0*100/255 + (gen-max_gen)*color_g;
+ c.b = 1.0*50/255 + (gen-max_gen)*color_b;
+ */
+ c.r = 1.0*180/255 - (max_gen-gen)*color_r;
+ c.g = 1.0*30/255 - (max_gen-gen)*color_g;
+ c.b = 1.0*10/255 - (max_gen-gen)*color_b;
+ c.a = original.a;
+
+ return c;
+}
+
+rgba32_t color_gen_yellowish(rgba32_t original, int gen, int max_gen)
+{
+ double color_r = 10.0/255;
+ double color_g = 20.0/255;
+ double color_b = 14.0/255;
+ rgba32_t c = {color_r, color_g, color_b,0};
+
+ /*
+ c.r = 1.0*180/255 + (gen-max_gen)*color_r;
+ c.g = 1.0*100/255 + (gen-max_gen)*color_g;
+ c.b = 1.0*50/255 + (gen-max_gen)*color_b;
+ */
+ c.r = 1.0*180/255 - (max_gen-gen)*color_r;
+ c.g = 1.0*160/255 - (max_gen-gen)*color_g;
+ c.b = 1.0*40/255 - (max_gen-gen)*color_b;
+ c.a = original.a;
+
+ return c;
+}
+
+rgba32_t color_switch(int pattern_id, rgba32_t original, int gen, int max_gen)
+{
+ switch(pattern_id)
+ {
+ case 0:
+ return color_gen_shade(original, gen, max_gen);
+ case 1:
+ return color_gen_shade_inv(original, gen, max_gen);
+ case 2:
+ return color_gen_mainlv(original, gen, max_gen);
+ case 3:
+ return color_gen_yellowish(original, gen, max_gen);
+ case 4:
+ return color_gen_redish(original, gen, max_gen);
+ default:
+ return color_gen_mainlv(original, gen, max_gen);
+ }
+
+ return color_gen_mainlv(original, gen, max_gen);
+}
+
void generate_koh(int gener, double size, int posx, int posy, double k)
{
int i;
@@ -314,6 +375,27 @@ void RectPXcolor( int x, int y,int w, int h, rgba32_t c )
glDrawArrays ( GL_TRIANGLE_FAN, 0, 4 );
+ glDeleteBuffers( 1, &vertexPosObject );
+ glDeleteBuffers( 1, &colorBuffer );
+
+}
+
+void koh_generation_inc()
+{
+ koh_set_config.max_generations += 1;
+ if (koh_set_config.max_generations >= MAX_LEVEL)
+ {
+ koh_set_config.max_generations = MAX_LEVEL-1;
+ }
+}
+
+void koh_generation_dec()
+{
+ koh_set_config.max_generations -= 1;
+ if (koh_set_config.max_generations < 0)
+ {
+ koh_set_config.max_generations = 0;
+ }
}
void koh_pos_x_add(int x)
@@ -336,6 +418,36 @@ void koh_pos_y_sub(int y)
koh_set_config.y -= 10;
}
+void koh_pattern_next()
+{
+ koh_set_config.color_pattern += 1;
+ if (koh_set_config.color_pattern>4)
+ {
+ koh_set_config.color_pattern=2;
+ }
+}
+
+void koh_pattern_prev()
+{
+ koh_set_config.color_pattern -= 1;
+ if (koh_set_config.color_pattern<0)
+ koh_set_config.color_pattern=0;
+}
+
+void koh_size_dec()
+{
+ koh_set_config.size -= 25;
+ if (koh_set_config.size<50)
+ koh_set_config.size = 50;
+}
+
+void koh_size_inc()
+{
+ koh_set_config.size += 25;
+ if (koh_set_config.size>400)
+ koh_set_config.size = 400;
+}
+
rgba32_t start_col = {0.0,1.0,0.0,0.0};
#if __EMSCRIPTEN__
@@ -361,14 +473,16 @@ int main_tick() {
{
switch (event.key.keysym.sym)
{
-
+ case 81:
case SDLK_DOWN:
update_fractal = 1;
- koh_pos_y_add(10);
+ koh_pos_y_sub(10);
break;
+
+ case 82:
case SDLK_UP:
update_fractal = 1;
- koh_pos_y_sub(10);
+ koh_pos_y_add(10);
break;
case 80:
case SDLK_LEFT:
@@ -382,6 +496,54 @@ int main_tick() {
printf("add 10\n");
koh_pos_x_add(10);
break;
+
+ case 61:
+ case 46:
+ case SDLK_KP_PLUS:
+ case SDLK_PLUS:
+ update_fractal=1;
+ koh_generation_inc();
+ printf("plus\n");
+ break;
+
+ case SDLK_MINUS:
+ update_fractal = 1;
+ koh_generation_dec();
+ break;
+
+ case 21:
+ case SDLK_r:
+ update_fractal = 1;
+ koh_set_config.size = 100.0;
+ koh_set_config.x = SCREEN_WIDTH2-100/2;
+ koh_set_config.y = SCREEN_WIDTH2-100/2;
+ koh_set_config.k = 0.5f;
+ koh_set_config.max_generations = MAX_LEVEL-2;
+ koh_set_config.color_pattern = 0;
+ break;
+
+ case 20:
+ case SDLK_q:
+ koh_pattern_prev();
+ break;
+
+ case 26:
+ case SDLK_w:
+ koh_pattern_next();
+ break;
+
+ case 4:
+ case SDLK_a:
+ update_fractal = 1;
+ koh_size_dec();
+ break;
+
+ case 22:
+ case SDLK_s:
+ update_fractal = 1;
+ koh_size_inc();
+ break;
+
default:
printf("Unknown key %d\n",event.key.keysym.sym);
update_fractal = 1;
@@ -389,12 +551,13 @@ int main_tick() {
break;
}
}
- printf("Poll event\n");
+ //printf("Poll event\n");
}
if (update_fractal == 1)
{
queue_init();
+ printf("k=%f",koh_set_config.k);
generate_koh(
koh_set_config.max_generations,
koh_set_config.size,
@@ -410,10 +573,11 @@ int main_tick() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgram);
- for (i=0;i<=MAX_GENERATION;i++)
+ for (i=0;i<=koh_set_config.max_generations;i++)
{
//rgba32_t c = color_gen_shade(start_col, i, MAX_GENERATION);
- rgba32_t c = color_gen_mainlv(start_col, i, MAX_GENERATION);
+ //rgba32_t c = color_gen_mainlv(start_col, i, MAX_LEVEL);
+ rgba32_t c = color_switch(koh_set_config.color_pattern, start_col, i, MAX_LEVEL);
//static int once=0
//glColor(1.0f,0.0,0.0,0.0);
for (j=0;j<queue_index;j++)
@@ -425,18 +589,22 @@ int main_tick() {
koh_node kn = queue_get(j);
//printf("j:%d %d %d %d\n",j,kn.x, kn.y, kn.size);
//RectPX(kn.x, kn.y, kn.size, kn.size);
- //if (update_fractal == 1)
+ #ifndef __EMSCRIPTEN__
+ if (update_fractal == 1)
+ #endif
RectPXcolor(kn.x, kn.y, kn.size, kn.size, c);
}
}
}
- //if (update_fractal == 1)
+ #ifndef __EMSCRIPTEN__
+ if (update_fractal == 1)
+ #endif
{
- printf("SWAP\n");
+ //printf("SWAP\n");
SDL_GL_SwapWindow( window );
}
//SDL_UpdateWindowSurface(window);
- SDL_Delay( 20 );
+ //SDL_Delay( 20 );
update_fractal=0;
#if !__EMSCRIPTEN__
@@ -448,14 +616,15 @@ void main_loop()
{
#if __EMSCRIPTEN__
- emscripten_set_main_loop(main_tick, -1, 1);
- //emscripten_set_main_loop(main_tick, 10, 1);
+ //emscripten_set_main_loop(main_tick, -1, 1);
+ //emscripten_set_main_loop(main_tick, 100, 1);
+ emscripten_set_main_loop(main_tick, 25, 1);
#else
while (0 == quit)
{
main_tick();
- printf("One tick\n");
+ //printf("One tick\n");
}
#endif
}
@@ -508,7 +677,7 @@ int main()
koh_set_config.x = SCREEN_WIDTH2-frac_size/2;
koh_set_config.y = SCREEN_WIDTH2-frac_size/2;
koh_set_config.k = 0.5;
- koh_set_config.max_generations = MAX_GENERATION;
+ koh_set_config.max_generations = MAX_LEVEL-2;
generate_koh(
koh_set_config.max_generations,
diff --git a/index.html b/index.html
index 94606af..5499fd9 100644
--- a/index.html
+++ b/index.html
@@ -4,6 +4,9 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
+ <pre>
+ Koh fractal. Version: 0.1
+ </pre>
<script type='text/javascript'>
var Module = {};
fetch('index.wasm')
@@ -20,7 +23,21 @@
document.body.appendChild(script);
});
</script>
- <canvas id="canvas"></canvas>
+ <div align="center">
+ <canvas id="canvas" ></canvas>
+ </div>
+
+ <pre>
+ Supported keys:
+ left,right,up,down - change position by 10 pixels
+ r - reset the configuration
+ '-' - decrease amount of levels
+ '+' - increase amount of levels
+ q - previous color pattern
+ w - next color pattern
+ a - decrease size
+ s - increase size
+ </pre>
</body>
</html> \ No newline at end of file
diff --git a/koh.h b/koh.h
index b197c90..b003b0b 100644
--- a/koh.h
+++ b/koh.h
@@ -16,6 +16,7 @@ typedef struct
int max_generations;
int x,y;
double size;
+ int color_pattern;
} koh_config;
typedef struct