texture scrolling

This commit is contained in:
2023-06-06 20:49:55 +00:00
parent b394bcce40
commit 6a1a06be76
10 changed files with 170 additions and 56 deletions

View File

@@ -268,13 +268,11 @@ JSValue duk_cursor_text(JSContext *js, JSValueConst this, int argc, JSValueConst
JSValue duk_gui_img(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
const char *img = JS_ToCString(js, argv[0]);
cpVect pos = js2vec2(argv[1]);
gui_draw_img(img, js2hmmv2(argv[1]), js2number(argv[2]), js2number(argv[3]));
gui_draw_img(img, js2hmmv2(argv[1]), js2hmmv2(argv[2]), js2number(argv[3]), js2bool(argv[4]), js2hmmv2(argv[5]), 1.0, js2color(argv[6]));
JS_FreeCString(js, img);
return JS_NULL;
}
struct nk_rect js2nk_rect(JSValue v) {
struct nk_rect rect;
rect.x = js2number(js_getpropstr(v, "x"));
@@ -1620,7 +1618,7 @@ void ffi_load() {
DUK_FUNC(gui_text, 6)
DUK_FUNC(ui_text, 5)
DUK_FUNC(cursor_text, 5)
DUK_FUNC(gui_img, 2)
DUK_FUNC(gui_img, 10)
DUK_FUNC(inflate_cpv, 3)

View File

@@ -191,6 +191,21 @@ void script_callee(struct callee c, int argc, JSValue *argv) {
js_callee_exec(&c, argc, argv);
}
void send_signal(const char *signal, int argc, JSValue *argv)
{
JSValue globalThis = JS_GetGlobalObject(js);
JSValue sig = JS_GetPropertyStr(js, globalThis, "Signal");
JSValue fn = JS_GetPropertyStr(js, sig, "call");
JSValue args[argc+1];
args[0] = str2js(signal);
for (int i = 0; i < argc; i++)
args[1+i] = argv[i];
JS_Call(js, fn, sig, argc+1, args);
}
static struct callee update_callee;
void register_update(struct callee c) {
update_callee = c;

View File

@@ -49,6 +49,8 @@ void call_gui();
void call_nk_gui();
void unregister_obj(JSValue obj);
void send_signal(const char *signal, int argc, JSValue *argv);
void register_physics(struct callee c);
void call_physics(double dt);

View File

@@ -25,7 +25,7 @@ static sg_bindings bind_sprite;
struct sprite_vert {
HMM_Vec2 pos;
struct uv_n uv;
HMM_Vec2 uv;
struct rgba color;
};
@@ -159,7 +159,7 @@ void sprite_initialize() {
.layout = {
.attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2,
[1].format = SG_VERTEXFORMAT_USHORT2N,
[1].format = SG_VERTEXFORMAT_FLOAT2,
[2].format = SG_VERTEXFORMAT_UBYTE4N}},
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.label = "sprite pipeline",
@@ -213,7 +213,7 @@ void sprite_initialize() {
}
/* offset given in texture offset, so -0.5,-0.5 results in it being centered */
void tex_draw(struct Texture *tex, HMM_Vec2 pos, float angle, HMM_Vec2 size, HMM_Vec2 offset, struct glrect r, struct rgba color) {
void tex_draw(struct Texture *tex, HMM_Vec2 pos, float angle, HMM_Vec2 size, HMM_Vec2 offset, struct glrect r, struct rgba color, int wrap, HMM_Vec2 wrapoffset, float wrapscale) {
struct sprite_vert verts[4];
HMM_Vec2 sposes[4] = {
@@ -238,15 +238,24 @@ void tex_draw(struct Texture *tex, HMM_Vec2 pos, float angle, HMM_Vec2 size, HMM
verts[i].pos = sposes[i];
verts[i].color = color;
}
if (!wrap) {
verts[0].uv.X = r.s0;
verts[0].uv.Y = r.t1;
verts[1].uv.X = r.s1;
verts[1].uv.Y = r.t1;
verts[2].uv.X = r.s0;
verts[2].uv.Y = r.t0;
verts[3].uv.X = r.s1;
verts[3].uv.Y = r.t0;
} else {
verts[0].uv = HMM_MulV2((HMM_Vec2){0,0}, size);
verts[1].uv = HMM_MulV2((HMM_Vec2){1,0}, size);
verts[2].uv = HMM_MulV2((HMM_Vec2){0,1}, size);
verts[3].uv = HMM_MulV2((HMM_Vec2){1,1}, size);
verts[0].uv.u = r.s0 * USHRT_MAX;
verts[0].uv.v = r.t1 * USHRT_MAX;
verts[1].uv.u = r.s1 * USHRT_MAX;
verts[1].uv.v = r.t1 * USHRT_MAX;
verts[2].uv.u = r.s0 * USHRT_MAX;
verts[2].uv.v = r.t0 * USHRT_MAX;
verts[3].uv.u = r.s1 * USHRT_MAX;
verts[3].uv.v = r.t0 * USHRT_MAX;
for (int i = 0; i < 4; i++)
verts[i].uv = HMM_AddV2(verts[i].uv, wrapoffset);
}
bind_sprite.fs_images[0] = tex->id;
sg_append_buffer(bind_sprite.vertex_buffers[0], SG_RANGE_REF(verts));
@@ -263,25 +272,22 @@ void sprite_draw(struct sprite *sprite) {
cpVect cpos = cpBodyGetPosition(go->body);
HMM_Vec2 pos = {cpos.x, cpos.y};
HMM_Vec2 size = {sprite->size.X * go->scale * go->flipx, sprite->size.Y * go->scale * go->flipy};
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, sprite->frame, sprite->color);
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, sprite->frame, sprite->color, 0, pos, 0);
}
}
void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame) {
if (!sprite) return;
sprite->tex = anim->tex;
sprite->frame = anim->st_frames[frame];
}
void gui_draw_img(const char *img, HMM_Vec2 pos, float scale, float angle) {
void gui_draw_img(const char *img, HMM_Vec2 pos, HMM_Vec2 scale, float angle, int wrap, HMM_Vec2 wrapoffset, float wrapscale, struct rgba color) {
sg_apply_pipeline(pip_sprite);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj));
struct Texture *tex = texture_loadfromfile(img);
HMM_Vec2 size = {scale, scale};
HMM_Vec2 offset = {0.f, 0.f};
tex_draw(tex, pos, 0.f, size, offset, tex_get_rect(tex), color_white);
tex_draw(tex, pos, angle, scale, offset, tex_get_rect(tex), color, wrap, wrapoffset, wrapscale);
}
void slice9_draw(const char *img, HMM_Vec2 pos, HMM_Vec2 dimensions, struct rgba color)

View File

@@ -41,7 +41,6 @@ void sprite_draw_all();
unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite);
void sprite_flush();
void gui_draw_img(const char *img, HMM_Vec2 pos, float scale, float angle);
void gui_draw_img(const char *img, HMM_Vec2 pos, HMM_Vec2 scale, float angle, int wrap, HMM_Vec2 wrapoffset, float wrapscale, struct rgba color);
#endif

View File

@@ -77,6 +77,8 @@ struct Texture *texture_pullfromfile(const char *path) {
tex->opts.sprite = 1;
tex->opts.mips = 0;
tex->opts.gamma = 0;
tex->opts.wrapx = 1;
tex->opts.wrapy = 1;
int n;
unsigned char *data = stbi_load(path, &tex->width, &tex->height, &n, 4);
@@ -133,6 +135,8 @@ struct Texture *texture_pullfromfile(const char *path) {
.min_filter = SG_FILTER_NEAREST_MIPMAP_NEAREST,
.mag_filter = SG_FILTER_NEAREST,
.num_mipmaps = mips,
.wrap_u = SG_WRAP_REPEAT,
.wrap_v = SG_WRAP_REPEAT,
.data = sg_img_data
});

View File

@@ -56,6 +56,8 @@ struct TextureOptions {
int mips;
unsigned int gamma:1;
int animation;
int wrapx;
int wrapy;
};
/* Represents an actual texture on the GPU */

View File

@@ -7,6 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ffi.h"
#include "openglrender.h"
@@ -51,11 +52,14 @@ void window_maximize_callback(GLFWwindow *w, int maximized) {
}
void window_framebuffer_size_cb(GLFWwindow *w, int width, int height) {
struct window *win = winfind(w);
struct window *win = mainwin;
win->width = width;
win->height = height;
window_makecurrent(win);
win->render = 1;
JSValue vals[2] = { int2js(width), int2js(height) };
send_signal("window_resize", 2, vals);
}
void window_close_callback(GLFWwindow *w) {
@@ -66,9 +70,8 @@ struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t f
if (arrcap(windows) == 0)
arrsetcap(windows, 5);
GLFWwindow *sharewin = mainwin == NULL ? NULL : mainwin->window;
if (sharewin) return sharewin;
if (mainwin != NULL)
return mainwin;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
@@ -80,19 +83,17 @@ struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t f
.width = width,
.height = height,
.id = arrlen(windows),
.window = glfwCreateWindow(width, height, name, NULL, sharewin)};
.window = glfwCreateWindow(width, height, name, NULL, mainwin)};
if (!w.window) {
YughError("Couldn't make GLFW window\n", 1);
return NULL;
}
mainwin = malloc(sizeof(struct window));
*mainwin = w;
glfwMakeContextCurrent(w.window);
// int version = gladLoadGL(glfwGetProcAddress);
// if (!version) {
// YughError("Failed to initialize OpenGL context.");
// exit(1);
// }
YughInfo("Loaded OpenGL %d.%d", 3, 3);
glfwSwapInterval(1);
@@ -108,6 +109,8 @@ struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t f
if (arrlen(windows) == 1)
mainwin = &windows[0];
window_framebuffer_size_cb(mainwin, width, height);
return &arrlast(windows);
}