Skip to content

Commit 11b1e68

Browse files
committed
wip pen
1 parent 6af3426 commit 11b1e68

File tree

8 files changed

+92
-2
lines changed

8 files changed

+92
-2
lines changed

include/ba_runtime.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ typedef struct ba_thread_stack ba_thread_stack_t;
7474
typedef struct ba_thread_wait ba_thread_wait_t;
7575
typedef union ba_runtime_union ba_runtime_union_t;
7676
typedef struct ba_audio_stream ba_audio_stream_t;
77+
typedef struct ba_pen ba_pen_t;
7778

7879
typedef unsigned char* (*ba_load_file_t)(ba_runtime_t* rt, const char* path, int* size);
7980
typedef void (*ba_swap_buffer_t)(ba_runtime_t* rt);
@@ -175,12 +176,19 @@ union ba_runtime_union {
175176
ba_zip_t* zip;
176177
};
177178

179+
struct ba_pen {
180+
double color[3];
181+
182+
double* coords;
183+
};
184+
178185
struct ba_runtime {
179186
ba_cJSON* json;
180187

181188
ba_target_t** targets;
182189
ba_thread_t** threads;
183190
ba_sprite_t** sprites;
191+
ba_pen_t** pens;
184192

185193
ba_audio_t* audio;
186194

@@ -299,6 +307,9 @@ struct ba_inputkv {
299307

300308
struct ba_sprite {
301309
ba_target_t* target;
310+
double pen_color[3];
311+
312+
ba_pen_t* pen;
302313

303314
int costume;
304315

@@ -338,6 +349,7 @@ BADECL ba_sprite_t* ba_runtime_get_stage_sprite(ba_runtime_t* rt);
338349
BADECL ba_bool ba_runtime_load_path(ba_runtime_t* rt, const char* path);
339350
BADECL void ba_runtime_block_handler(ba_runtime_t* rt, const char* name, ba_thread_block_handler_t handler);
340351
BADECL void ba_runtime_shadow_handler(ba_runtime_t* rt, const char* name, ba_thread_shadow_handler_t handler);
352+
BADECL ba_pen_t* ba_runtime_pen(ba_runtime_t* rt);
341353

342354
/* audio.c */
343355
BADECL ba_audio_t* ba_audio_open(ba_runtime_t* rt);
@@ -415,6 +427,7 @@ BADECL void ba_block_sound(ba_runtime_t* rt);
415427
BADECL void ba_block_event(ba_runtime_t* rt);
416428
BADECL void ba_block_control(ba_runtime_t* rt);
417429
BADECL void ba_block_text2speech(ba_runtime_t* rt);
430+
BADECL void ba_block_pen(ba_runtime_t* rt);
418431

419432
/* shadows */
420433
BADECL void ba_shadow_sound(ba_runtime_t* rt);

lib/audio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ static void data_callback(ma_device* device, void* output, const void* input, ma
6262
if(buffer[i] < -1) buffer[i] = -1;
6363
if(buffer[i] > 1) buffer[i] = 1;
6464

65-
out[i] = buffer[i] * 32767;
65+
out[i] = 0;
66+
buffer[i] * 32767;
6667
}
6768

6869
for(i = 0; i < arrlen(streams); i++) ba_audio_stream_free(streams[i]);

lib/block/motion.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ static int motion_movesteps(ba_thread_t* thread) {
1010
thread->sprite->x += c * atof(str);
1111
thread->sprite->y += s * atof(str);
1212

13+
if(thread->sprite->pen != NULL) {
14+
arrput(thread->sprite->pen->coords, thread->sprite->x);
15+
arrput(thread->sprite->pen->coords, thread->sprite->y);
16+
}
17+
1318
free(str);
1419
}
1520

@@ -58,6 +63,11 @@ static int motion_gotoxy(ba_thread_t* thread) {
5863
if((str1 = ba_thread_input(thread, "X")) != NULL && (str2 = ba_thread_input(thread, "Y")) != NULL) {
5964
thread->sprite->x = atof(str1);
6065
thread->sprite->y = atof(str2);
66+
67+
if(thread->sprite->pen != NULL) {
68+
arrput(thread->sprite->pen->coords, thread->sprite->x);
69+
arrput(thread->sprite->pen->coords, thread->sprite->y);
70+
}
6171
}
6272

6373
if(str1 != NULL) free(str1);

lib/block/pen.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <ba_runtime.h>
2+
3+
static int pen_penDown(ba_thread_t* thread) {
4+
if(thread->sprite->pen != NULL) return ba_status_next;
5+
6+
thread->sprite->pen = ba_runtime_pen(thread->runtime);
7+
8+
memcpy(thread->sprite->pen->color, thread->sprite->pen_color, sizeof(double) * 3);
9+
arrput(thread->sprite->pen->coords, thread->sprite->x);
10+
arrput(thread->sprite->pen->coords, thread->sprite->y);
11+
12+
return ba_status_next;
13+
}
14+
15+
static int pen_penUp(ba_thread_t* thread) {
16+
thread->sprite->pen = NULL;
17+
18+
return ba_status_next;
19+
}
20+
21+
void ba_block_pen(ba_runtime_t* rt) {
22+
ba_runtime_block_handler(rt, "pen_penDown", pen_penDown);
23+
ba_runtime_block_handler(rt, "pen_penUp", pen_penUp);
24+
}

lib/render.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,36 @@ void ba_render(ba_runtime_t* rt) {
4040
glClear(GL_COLOR_BUFFER_BIT);
4141

4242
glEnable(GL_TEXTURE_2D);
43+
/* draw stage */
4344
for(i = 0; i < arrlen(rt->sprites); i++) {
4445
if(rt->sprites[i]->target->stage) draw_sprite(rt, rt->sprites[i]);
4546
}
47+
glDisable(GL_TEXTURE_2D);
48+
49+
for(i = 0; i < arrlen(rt->pens); i++) {
50+
glColor3f(rt->pens[i]->color[0], rt->pens[i]->color[1], rt->pens[i]->color[2]);
51+
52+
if(arrlen(rt->pens[i]->coords) == 2) {
53+
glBegin(GL_POINT);
54+
glVertex2f(rt->pens[i]->coords[0], rt->pens[i]->coords[1]);
55+
glEnd();
56+
} else if(arrlen(rt->pens[i]->coords) > 2) {
57+
GLfloat* c = malloc(sizeof(*c) * arrlen(rt->pens[i]->coords));
58+
int j;
59+
60+
for(j = 0; j < arrlen(rt->pens[i]->coords); j++) c[j] = rt->pens[i]->coords[j];
61+
62+
glEnableClientState(GL_VERTEX_ARRAY);
63+
glVertexPointer(2, GL_FLOAT, 0, c);
64+
glDrawArrays(GL_LINE_STRIP, 0, arrlen(rt->pens[i]->coords) / 2);
65+
glDisableClientState(GL_VERTEX_ARRAY);
66+
67+
free(c);
68+
}
69+
}
70+
71+
glEnable(GL_TEXTURE_2D);
72+
/* draw sprites. */
4673
for(i = 0; i < arrlen(rt->sprites); i++) {
4774
if(!rt->sprites[i]->target->stage) draw_sprite(rt, rt->sprites[i]);
4875
}

lib/runtime.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ba_bool ba_runtime_init(ba_runtime_t* rt) {
2222
ba_block_control(rt);
2323

2424
ba_block_text2speech(rt);
25+
ba_block_pen(rt);
2526

2627
ba_shadow_sound(rt);
2728
ba_shadow_operator(rt);
@@ -343,3 +344,13 @@ void ba_runtime_block_handler(ba_runtime_t* rt, const char* name, ba_thread_bloc
343344
void ba_runtime_shadow_handler(ba_runtime_t* rt, const char* name, ba_thread_shadow_handler_t handler) {
344345
shput(rt->shadow_handlers, name, handler);
345346
}
347+
348+
ba_pen_t* ba_runtime_pen(ba_runtime_t* rt) {
349+
ba_pen_t* pen = malloc(sizeof(*pen));
350+
351+
memset(pen, 0, sizeof(*pen));
352+
353+
arrput(rt->pens, pen);
354+
355+
return pen;
356+
}

lib/sprite.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ ba_sprite_t* ba_sprite_start(ba_runtime_t* rt, ba_target_t* target) {
1212
sprite->y = 0;
1313
sprite->angle = 0;
1414

15+
sprite->pen_color[0] = 0;
16+
sprite->pen_color[1] = 0;
17+
sprite->pen_color[2] = 1;
18+
1519
arrput(rt->sprites, sprite);
1620

1721
sh_new_strdup(sprite->variables);

src/glfw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static void swap_interval(ba_runtime_t* rt, int interval) {
2020
int main(int argc, char** argv) {
2121
ba_runtime_t ba;
2222
int i;
23-
double scale = 1;
23+
double scale = 1;
2424

2525
ba.param.turbo = ba_false;
2626
ba.param.fps = 30;

0 commit comments

Comments
 (0)