Animations in; working poorly

This commit is contained in:
John Alanbrook 2023-01-16 08:16:39 +00:00
parent 5da19ec407
commit 01256542b6
10 changed files with 150 additions and 65 deletions

View file

@ -18,6 +18,24 @@
cpSpace *space = NULL;
float phys2d_gravity = -50.f;
static float dbg_color[3] = {0.836f, 1.f, 0.45f};
static float trigger_color[3] = {0.278f, 0.953f, 1.f};
static struct color static_color = {56, 69, 255};
void set_dbg_color(struct color color)
{
dbg_color[0] = (float)color.r/255;
dbg_color[1] = (float)color.b/255;
dbg_color[2] = (float)color.g/255;
}
void set_trigger_color(struct color color)
{
trigger_color[0] = (float)color.r/255;
trigger_color[1] = (float)color.b/255;
trigger_color[2] = (float)color.g/255;
}
void phys2d_init()
{
space = cpSpaceNew();
@ -55,6 +73,8 @@ void phys2d_shape_del(struct phys2d_shape *shape)
cpSpaceRemoveShape(space, shape->shape);
}
/***************** CIRCLE2D *****************/
struct phys2d_circle *Make2DCircle(int go)
{
struct phys2d_circle *new = malloc(sizeof(struct phys2d_circle));
@ -90,19 +110,14 @@ void phys2d_dbgdrawcpcirc(cpCircleShape *c)
float radius = cpCircleShapeGetRadius(c);
float d = sqrt(pow(offset.x, 2.f) + pow(offset.y, 2.f));
float a = atan2(offset.y, offset.x) + cpBodyGetAngle(cpShapeGetBody(c));
draw_circle(pos.x + (d * cos(a)), pos.y + (d*sin(a)), radius, 1);
draw_circle(pos.x + (d * cos(a)), pos.y + (d*sin(a)), radius, 2, cpShapeGetSensor(c) ? trigger_color : dbg_color, 1);
}
void phys2d_dbgdrawcircle(struct phys2d_circle *circle)
{
phys2d_dbgdrawcpcirc((cpCircleShape *)circle->shape.shape);
cpVect p = cpBodyGetPosition(cpShapeGetBody(circle->shape.shape));
cpVect o = cpCircleShapeGetOffset(circle->shape.shape);
float d = sqrt(pow(o.x, 2.f) + pow(o.y, 2.f));
float a = atan2(o.y, o.x) + cpBodyGetAngle(cpShapeGetBody(circle->shape.shape));
draw_circle(p.x + (d * cos(a)), p.y + (d * sin(a)), cpCircleShapeGetRadius(circle->shape.shape), 1);
}
@ -375,7 +390,7 @@ void phys2d_dbgdrawseg(struct phys2d_segment *seg)
float bd = sqrt(pow(b.x, 2.f) + pow(b.y, 2.f));
float aa = atan2(a.y, a.x) + angle;
float ba = atan2(b.y, b.x) + angle;
draw_line(ad * cos(aa) + p.x, ad * sin(aa) + p.y, bd * cos(ba) + p.x, bd * sin(ba) + p.y);
draw_line(ad * cos(aa) + p.x, ad * sin(aa) + p.y, bd * cos(ba) + p.x, bd * sin(ba) + p.y, cpShapeGetSensor(seg->shape.shape) ? trigger_color : dbg_color);
}
void phys2d_dbgdrawbox(struct phys2d_box *box)
@ -393,11 +408,13 @@ void phys2d_dbgdrawbox(struct phys2d_box *box)
points[i * 2 + 1] = d * sin(a) + b.y;
}
draw_poly(points, n);
draw_poly(points, n, cpShapeGetSensor(box->shape.shape) ? trigger_color : dbg_color);
}
void phys2d_dbgdrawpoly(struct phys2d_poly *poly)
{
float *color = cpShapeGetSensor(poly->shape.shape) ? trigger_color : dbg_color;
cpVect b = cpBodyGetPosition(cpShapeGetBody(poly->shape.shape));
float angle = cpBodyGetAngle(cpShapeGetBody(poly->shape.shape));
@ -405,7 +422,7 @@ void phys2d_dbgdrawpoly(struct phys2d_poly *poly)
for (int i = 0; i < poly->n; i++) {
float d = sqrt(pow(poly->points[i * 2] * s, 2.f) + pow(poly->points[i * 2 + 1] * s, 2.f));
float a = atan2(poly->points[i * 2 + 1], poly->points[i * 2]) + angle;
draw_point(b.x + d * cos(a), b.y + d * sin(a), 3);
draw_point(b.x + d * cos(a), b.y + d * sin(a), 3, color);
}
if (poly->n >= 3) {
@ -420,13 +437,15 @@ void phys2d_dbgdrawpoly(struct phys2d_poly *poly)
points[i * 2 + 1] = d * sin(a) + b.y;
}
draw_poly(points, n);
draw_poly(points, n, color);
}
}
void phys2d_dbgdrawedge(struct phys2d_edge *edge)
{
float *color = cpShapeGetSensor(edge->shape.shape) ? trigger_color : dbg_color;
cpVect p = cpBodyGetPosition(cpShapeGetBody(edge->shape.shape));
float s = id2go(edge->shape.go)->scale;
float angle = cpBodyGetAngle(cpShapeGetBody(edge->shape.shape));
@ -434,7 +453,7 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge)
for (int i = 0; i < edge->n; i++) {
float d = sqrt(pow(edge->points[i * 2] * s, 2.f) + pow(edge->points[i * 2 + 1] * s, 2.f));
float a = atan2(edge->points[i * 2 + 1], edge->points[i * 2]) + angle;
draw_point(p.x + d * cos(a), p.y + d * sin(a), 3);
draw_point(p.x + d * cos(a), p.y + d * sin(a), 3, color);
}
for (int i = 0; i < edge->n - 1; i++) {
@ -444,7 +463,7 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge)
float bd = sqrt(pow(b.x, 2.f) + pow(b.y, 2.f));
float aa = atan2(a.y, a.x) + angle;
float ba = atan2(b.y, b.x) + angle;
draw_line(ad * cos(aa) + p.x, ad * sin(aa) + p.y, bd * cos(ba) + p.x, bd * sin(ba) + p.y);
draw_line(ad * cos(aa) + p.x, ad * sin(aa) + p.y, bd * cos(ba) + p.x, bd * sin(ba) + p.y, color);
}
}

View file

@ -97,6 +97,15 @@ void phys2d_add_handler_type(int cmd, int go, struct callee c);
void register_collide(void *sym);
void phys2d_set_gravity(cpVect v);
struct color {
unsigned char r;
unsigned char g;
unsigned char b;
};
void set_dbg_color(struct color);
void set_trigger_color(struct color);
void shape_gui(struct phys2d_shape *shape);
void phys2d_reindex_body(cpBody *body);

View file

@ -48,7 +48,7 @@ void debugdraw_init()
glGenVertexArrays(1, &rectVAO);
}
void draw_line(int x1, int y1, int x2, int y2)
void draw_line(int x1, int y1, int x2, int y2, float *color)
{
shader_use(rectShader);
float verts[] = {
@ -56,15 +56,15 @@ void draw_line(int x1, int y1, int x2, int y2)
x2, y2
};
draw_poly(verts, 2);
draw_poly(verts, 2, color);
}
void draw_edge(float *points, int n)
void draw_edge(float *points, int n, float *color)
{
draw_poly(points, n);
draw_poly(points, n, color);
}
void draw_circle(int x, int y, float radius, int pixels)
void draw_circle(int x, int y, float radius, int pixels, float *color, int fill)
{
shader_use(circleShader);
@ -80,6 +80,8 @@ void draw_circle(int x, int y, float radius, int pixels)
shader_setfloat(circleShader, "radius", radius);
shader_setint(circleShader, "thickness", pixels);
shader_setvec3(circleShader, "dbgColor", color);
shader_setbool(circleShader, "fill", fill);
glBindVertexArray(circleVAO);
glEnableVertexAttribArray(0);
@ -87,7 +89,7 @@ void draw_circle(int x, int y, float radius, int pixels)
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void draw_rect(int x, int y, int w, int h)
void draw_rect(int x, int y, int w, int h, float *color)
{
float hw = w / 2.f;
float hh = h / 2.f;
@ -99,7 +101,7 @@ void draw_rect(int x, int y, int w, int h)
x - hw, y + hh
};
draw_poly(verts, 4);
draw_poly(verts, 4, color);
}
void draw_grid(int width, int span)
@ -113,19 +115,26 @@ void draw_grid(int width, int span)
}
void draw_point(int x, int y, float r)
void draw_point(int x, int y, float r, float *color)
{
draw_circle(x, y, r, r);
draw_circle(x, y, r, r, color, 0);
}
void draw_poly(float *points, int n)
void draw_poly(float *points, int n, float *color)
{
shader_use(rectShader);
shader_setvec3(rectShader, "linecolor", color);
glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * n * 2, points, GL_DYNAMIC_DRAW);
glBindVertexArray(rectVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
shader_setfloat(rectShader, "alpha", 0.1f);
glDrawArrays(GL_POLYGON, 0, n);
shader_setfloat(rectShader, "alpha", 1.f);
glDrawArrays(GL_LINE_LOOP, 0, n);
}

View file

@ -2,13 +2,13 @@
#define DEBUGDRAW_H
void debugdraw_init();
void draw_line(int x1, int y1, int x2, int y2);
void draw_edge(float *points, int n);
void draw_circle(int x, int y, float radius, int pixels);
void draw_line(int x1, int y1, int x2, int y2, float *color);
void draw_edge(float *points, int n, float *color);
void draw_circle(int x, int y, float radius, int pixels, float *color, int fill);
void draw_grid(int width, int span);
void draw_rect(int x, int y, int w, int h);
void draw_point(int x, int y, float r);
void draw_poly(float *points, int n);
void draw_rect(int x, int y, int w, int h, float *color);
void draw_point(int x, int y, float r, float *color);
void draw_poly(float *points, int n, float *color);
void debugdraw_flush(); /* This is called once per frame to draw all queued elements */

View file

@ -18,6 +18,20 @@
#include "sound.h"
#include "font.h"
struct color duk2color(duk_context *duk, int p)
{
struct color color;
duk_get_prop_index(duk, p, 0);
color.r = duk_to_number(duk, -1);
duk_get_prop_index(duk, p, 0);
color.b = duk_to_number(duk, -1);
duk_get_prop_index(duk, p, 0);
color.g = duk_to_number(duk, -1);
return color;
}
cpVect duk2vec2(duk_context *duk, int p) {
cpVect pos;
duk_get_prop_index(duk, p, 0);
@ -128,11 +142,11 @@ duk_ret_t duk_cmd(duk_context *duk) {
break;
case 16:
set_dbg_color(duk2color(duk, 1));
break;
case 17:
set_trigger_color(duk2color(duk, 1));
break;
}
@ -367,9 +381,17 @@ duk_ret_t duk_make_anim2d(duk_context *duk) {
int frames = duk_to_int(duk, 2);
int fps = duk_to_int(duk, 3);
int sprite = make_sprite(go);
struct sprite *sp = id2sprite(sprite);
sp->pos[0] = -0.5f;
sp->pos[1] = -0.5f;
anim_load(&sp->anim, path);
sp->tex = sp->anim.anim->tex;
YughInfo("Made an animation.");
return 0;
duk_push_int(duk, sprite);
return 1;
}
duk_ret_t duk_make_box2d(duk_context *duk) {

View file

@ -166,15 +166,10 @@ void sprite_draw(struct sprite *sprite)
if (sprite->tex) {
cpVect cpos = cpBodyGetPosition(go->body);
float pos[2] = {cpos.x, cpos.y};
float size[2] = { sprite->size[0] * go->scale * go->flipx, sprite->size[1] * go->scale * go->flipy };
if (sprite->tex->opts.animation) {
float size[2];
//size[0] = sprite->tex->anim.dimensions[0] * go->scale;
//size[1] = sprite->tex->anim.dimensions[1] * go->scale;
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, anim_get_rect(&sprite->anim));
} else {
float size[2] = { sprite->size[0] * go->scale * go->flipx, sprite->size[1] * go->scale * go->flipy };
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, tex_get_rect(sprite->tex));
}
}

View file

@ -32,7 +32,12 @@ struct Texture *texture_pullfromfile(const char *path)
json_value_free(rv);
tex->opts.gamma = 0;
tex->anim.ms = 1;
tex->anim.ms = 2;
tex->anim.tex = tex;
texanim_fromframes(&tex->anim, 2);
tex->anim.loop = 1;
int n;
@ -134,24 +139,26 @@ void tex_gpu_reload(struct Texture *tex)
//tex_gpu_load(tex);
}
void tex_incr_anim(struct anim2d *tex_anim)
void anim_calc(struct anim2d *anim)
{
anim_incr(tex_anim);
if (!tex_anim->anim->loop && tex_anim->frame == arrlen(tex_anim->anim->st_frames))
anim_pause(tex_anim);
anim->size[0] = anim->anim->tex->width * st_s_w(anim->anim->st_frames[anim->frame]);
anim->size[1] = anim->anim->tex->height * st_s_h(anim->anim->st_frames[anim->frame]);
}
void anim_incr(struct anim2d *anim)
{
anim->frame = (anim->frame + 1) % arrlen(anim->anim->st_frames);
//tex_anim_calc_uv(anim);
if (!anim->anim->loop && anim->frame == arrlen(anim->anim->st_frames))
anim_pause(anim);
anim_calc(anim);
}
void anim_decr(struct anim2d *anim)
{
anim->frame = (anim->frame + arrlen(anim->anim->st_frames) - 1) % arrlen(anim->anim->st_frames);
//tex_anim_calc_uv(anim);
anim_calc(anim);
}
struct glrect anim_get_rect(struct anim2d *anim)
@ -162,22 +169,25 @@ struct glrect anim_get_rect(struct anim2d *anim)
void anim_setframe(struct anim2d *anim, int frame)
{
anim->frame = frame;
//tex_anim_calc_uv(anim);
anim_calc(anim);
}
void tex_anim_set(struct anim2d *anim)
void texanim_fromframes(struct TexAnim *anim, int frames)
{
if (anim->playing) {
timer_remove(anim->timer);
anim->timer = timer_make(1.f / anim->anim->ms, tex_incr_anim, anim);
if (anim->st_frames) free(anim->st_frames);
anim->st_frames = calloc(frames, sizeof(*anim->st_frames));
float width = (float)1/frames;
for (int i = 0; i < frames; i++) {
anim->st_frames[i].s0 = width*i;
anim->st_frames[i].s1 = width*(i+1);
anim->st_frames[i].t0 = 0.f;
anim->st_frames[i].t1 = 1.f;
}
//tex_anim_calc_uv(anim);
}
void tex_gpu_free(struct Texture *tex)
{
if (tex->id != 0) {
@ -203,6 +213,16 @@ void tex_bind(struct Texture *tex)
glBindTexture(GL_TEXTURE_2D_ARRAY, tex->id);
}
/********************** ANIM2D ****************/
void anim_load(struct anim2d *anim, const char *path)
{
anim->anim = &texture_pullfromfile(path)->anim;
anim->anim->tex->opts.animation = 1;
anim_stop(anim);
anim_play(anim);
}
void anim_play(struct anim2d *anim)
{
if (anim->playing)
@ -214,7 +234,7 @@ void anim_play(struct anim2d *anim)
anim->playing = 1;
if (anim->timer == NULL)
anim->timer = timer_make(1.f / anim->anim->ms, tex_incr_anim, anim);
anim->timer = timer_make(1.f / anim->anim->ms, anim_incr, anim);
else
timerr_settime(anim->timer, 1.f/anim->anim->ms);
@ -230,7 +250,6 @@ void anim_stop(struct anim2d *anim)
anim->frame = 0;
anim->pausetime = 0;
timer_stop(anim->timer);
//tex_anim_calc_uv(anim);
}
void anim_pause(struct anim2d *anim)

View file

@ -30,13 +30,14 @@ struct uvrect {
};
/* Tracks a playing animation */
/* Objects should keep this, and simple change what TexAnim they are pointing to */
struct anim2d {
int frame;
int playing;
int pausetime;
struct timer *timer;
struct TexAnim *anim;
float size[2]; /* Current size of animation in pixels*/
};
/* Describes an animation on a particular texture */
@ -71,6 +72,9 @@ void tex_bind(struct Texture *tex); // Bind to gl context
char * tex_get_path(struct Texture *tex); // Get image path for texture
void anim_load(struct anim2d *anim, const char *path); /* Load and start new animation */
void texanim_fromframes(struct TexAnim *anim, int frames);
void anim_calc(struct anim2d *anim);
void anim_play(struct anim2d *anim);
void anim_setframe(struct anim2d *anim, int frame);
void anim_stop(struct anim2d *anim);
@ -80,9 +84,6 @@ void anim_bkwd(struct anim2d *anim);
void anim_incr(struct anim2d *anim);
void anim_decr(struct anim2d *anim);
void tex_incr_anim(struct anim2d *tex_anim);
void tex_anim_set(struct anim2d *anim);
struct glrect tex_get_rect(struct Texture *tex);
struct glrect anim_get_rect(struct anim2d *anim);

View file

@ -5,6 +5,8 @@ out vec4 color;
uniform float radius;
uniform int thickness;
uniform vec3 dbgColor;
uniform bool fill;
void main()
{
@ -13,14 +15,20 @@ void main()
float R1 = 1.f;
float R2 = 1.f - (thickness / radius);
float dist = sqrt(dot(coords, coords));
if (dist <= R2 || dist >= R1)
if (dist >= R2 && dist <= R1)
color = vec4(dbgColor, 1.f);
else if (dist < R2)
color = vec4(dbgColor, 0.1f);
else
discard;
/*
float smoother = 0.01f - (radius * 0.00003f);
float sm = smoothstep(R1, R1-smoother, dist);
float sm2 = smoothstep(R2, R2+smoother, dist);
float alpha = sm*sm2;
*/
color = vec4(0.4f, 0.5f, 0.6f, 1.f);
}

View file

@ -1,7 +1,10 @@
#version 330
out vec4 color;
uniform vec3 linecolor;
uniform float alpha;
void main()
{
color = vec4(0.2f, 0.1f, 0.8f, 1.f);
color = vec4(linecolor, alpha);
}