Fix grid draw, circle draw, most editor drawing

This commit is contained in:
John Alanbrook 2023-08-23 22:18:34 +00:00
parent 09765f5336
commit f94c62c5c0
10 changed files with 53 additions and 57 deletions

View file

@ -85,15 +85,16 @@ struct circle_vertex {
static struct circle_vertex circle_b[v_amt]; static struct circle_vertex circle_b[v_amt];
/* Writes debug data to buffers, and draws */
void debug_flush(HMM_Mat4 *view) void debug_flush(HMM_Mat4 *view)
{ {
if (poly_c != 0) { if (poly_c != 0) {
sg_apply_pipeline(poly_pipe); sg_apply_pipeline(poly_pipe);
sg_apply_bindings(&poly_bind); sg_apply_bindings(&poly_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(*view)); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(*view));
sg_append_buffer(poly_bind.vertex_buffers[0], &(sg_range){ int b = sg_append_buffer(poly_bind.vertex_buffers[0], &(sg_range){
.ptr = poly_b, .size = sizeof(struct poly_vertex)*poly_v}); .ptr = poly_b, .size = sizeof(struct poly_vertex)*poly_v});
sg_append_buffer(poly_bind.index_buffer, &(sg_range){ int bi = sg_append_buffer(poly_bind.index_buffer, &(sg_range){
.ptr = poly_bi, .size = sizeof(uint32_t)*poly_c}); .ptr = poly_bi, .size = sizeof(uint32_t)*poly_c});
sg_draw(poly_sc,poly_c,1); sg_draw(poly_sc,poly_c,1);
} }
@ -106,6 +107,7 @@ void debug_flush(HMM_Mat4 *view)
.ptr = point_b, .ptr = point_b,
.size = sizeof(struct point_vertex)*point_c}); .size = sizeof(struct point_vertex)*point_c});
sg_draw(point_sc,point_c,1); sg_draw(point_sc,point_c,1);
YughWarn("DREW %d POINTS", point_c);
} }
if (line_c != 0) { if (line_c != 0) {
@ -163,6 +165,7 @@ void debug_newframe()
point_c = 0; point_c = 0;
circle_sc = circle_count = line_sv = line_v = line_sc = line_c = poly_sc = poly_c = 0; circle_sc = circle_count = line_sv = line_v = line_sc = line_c = poly_sc = poly_c = 0;
poly_sv = poly_v = 0; poly_sv = poly_v = 0;
} }
static sg_shader_uniform_block_desc projection_ubo = { static sg_shader_uniform_block_desc projection_ubo = {
@ -569,16 +572,18 @@ void draw_arrow(struct cpVect start, struct cpVect end, struct rgba color, int c
void draw_grid(int width, int span, struct rgba color) void draw_grid(int width, int span, struct rgba color)
{ {
cpVect offset = cam_pos(); cpVect offset = cam_pos();
offset = cpvmult(offset, 1/cam_zoom()); offset = cpvmult(offset, 1/cam_zoom());
offset.x -= mainwin->width/2; offset.x -= mainwin->width/2;
offset.y -= mainwin->height/2; offset.y -= mainwin->height/2;
// offset.x += span/2;
offset.y += span/2;
sg_apply_pipeline(grid_pipe); sg_apply_pipeline(grid_pipe);
sg_apply_bindings(&grid_bind); sg_apply_bindings(&grid_bind);
float col[4] = { color.r/255.0 ,color.g/255.0 ,color.b/255.0 ,color.a/255.0 }; float col[4] = { color.r/255.0 ,color.g/255.0 ,color.b/255.0 ,color.a/255.0 };
float fubo[6]; float fubo[6];
fubo[0] = 1; fubo[0] = 1;
fubo[1] = span; fubo[1] = span;
@ -596,7 +601,7 @@ void draw_cppoint(struct cpVect point, float r, struct rgba color)
.color = color, .color = color,
.radius = r .radius = r
}; };
memcpy(point_b+point_c, &p, sizeof(struct point_vertex)); memcpy(point_b+point_c, &p, sizeof(struct point_vertex));
point_c++; point_c++;
} }
@ -604,7 +609,7 @@ void draw_cppoint(struct cpVect point, float r, struct rgba color)
void draw_points(struct cpVect *points, int n, float size, struct rgba color) void draw_points(struct cpVect *points, int n, float size, struct rgba color)
{ {
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
draw_cppoint(points[i], size, color); draw_cppoint(points[i], size, color);
} }
void draw_poly(cpVect *points, int n, struct rgba color) void draw_poly(cpVect *points, int n, struct rgba color)
@ -613,7 +618,7 @@ void draw_poly(cpVect *points, int n, struct rgba color)
int tric = n - 2; int tric = n - 2;
if (tric < 1) return; if (tric < 1) return;
uint32_t tridxs[tric*3]; uint32_t tridxs[tric*3];
for (int i = 2, ti = 0; i < n; i++, ti+=3) { for (int i = 2, ti = 0; i < n; i++, ti+=3) {
@ -625,11 +630,6 @@ void draw_poly(cpVect *points, int n, struct rgba color)
for (int i = 0; i < tric*3; i++) for (int i = 0; i < tric*3; i++)
tridxs[i] += poly_v+poly_sv; tridxs[i] += poly_v+poly_sv;
sg_range trip = {
.ptr = tridxs,
.size = sizeof(uint32_t)*3*tric
};
struct poly_vertex polyverts[n]; struct poly_vertex polyverts[n];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@ -639,11 +639,6 @@ void draw_poly(cpVect *points, int n, struct rgba color)
polyverts[i].color = color; polyverts[i].color = color;
} }
sg_range ppp = {
.ptr = polyverts,
.size = sizeof(struct poly_vertex)*n
};
memcpy(poly_b+poly_v, polyverts, sizeof(struct poly_vertex)*n); memcpy(poly_b+poly_v, polyverts, sizeof(struct poly_vertex)*n);
memcpy(poly_bi+poly_c, tridxs, sizeof(uint32_t)*3*tric); memcpy(poly_bi+poly_c, tridxs, sizeof(uint32_t)*3*tric);

View file

@ -22,6 +22,7 @@ void draw_grid(int width, int span, struct rgba color);
void debug_flush(HMM_Mat4 *view); void debug_flush(HMM_Mat4 *view);
void debug_newframe(); void debug_newframe();
void debug_nextpass();
cpVect inflatepoint(cpVect a, cpVect b, cpVect c, float d); cpVect inflatepoint(cpVect a, cpVect b, cpVect c, float d);
void inflatepoints(cpVect *r, cpVect *p, float d, int n); void inflatepoints(cpVect *r, cpVect *p, float d, int n);

View file

@ -266,6 +266,7 @@ void render_winsize()
static cpBody *camera = NULL; static cpBody *camera = NULL;
void set_cam_body(cpBody *body) { void set_cam_body(cpBody *body) {
YughWarn("Camera body set to %p", body);
camera = body; camera = body;
} }
@ -317,7 +318,6 @@ void openglRender(struct window *window) {
draw_model(duck,model, lsm); draw_model(duck,model, lsm);
*/ */
// sg_begin_default_pass(&pass_action, window->width, window->height);
sg_begin_pass(crt_post.pass, &pass_action); sg_begin_pass(crt_post.pass, &pass_action);
//////////// 2D projection //////////// 2D projection
@ -338,24 +338,19 @@ void openglRender(struct window *window) {
//// DEBUG //// DEBUG
if (debugDrawPhysics) { if (debugDrawPhysics) {
gameobject_draw_debugs(); gameobject_draw_debugs();
call_debugs(); call_debugs();
} }
debug_flush(&projection); debug_flush(&projection);
// text_flush(&projection); text_flush(&projection);
////// TEXT && GUI ////// TEXT && GUI
debug_nextpass(); debug_nextpass();
nuke_start();
nuke_start(); call_gui();
call_gui();
debug_flush(&hudproj); debug_flush(&hudproj);
text_flush(&hudproj); text_flush(&hudproj);
// nuke_start();
call_nk_gui(); call_nk_gui();
nuke_end(); nuke_end();

View file

@ -246,4 +246,5 @@ void call_debugs() { call_callee(&debug_callee); }
static struct callee draw_callee; static struct callee draw_callee;
void register_draw(struct callee c) { draw_callee = c; } void register_draw(struct callee c) { draw_callee = c; }
void call_draw() { call_callee(&draw_callee); } void call_draw() { call_callee(&draw_callee); }

View file

@ -232,10 +232,12 @@ int main(int argc, char **args) {
lastTick = glfwGetTime(); lastTick = glfwGetTime();
//double wait = fmax(0, renderMS - elapsed); //double wait = fmax(0, renderMS - elapsed);
nuke_input_begin(); nuke_input_begin();
if (sim_playing()) if (sim_playing())
input_poll(fmax(0, renderMS-elapsed)); input_poll(fmax(0, renderMS-elapsed));
else else
input_poll(1000); input_poll(1000);
window_all_handle_events(); window_all_handle_events();
nuke_input_end(); nuke_input_end();
framems[framei++] = elapsed; framems[framei++] = elapsed;
@ -259,10 +261,10 @@ int main(int argc, char **args) {
renderlag += elapsed; renderlag += elapsed;
if (renderlag >= renderMS) { // if (renderlag >= renderMS) {
renderlag -= renderMS; // renderlag -= renderMS;
window_renderall(); window_renderall();
} // }
gameobjects_cleanup(); gameobjects_cleanup();
} }

View file

@ -20,7 +20,8 @@ var Debug = {
point(pos, size, color) { point(pos, size, color) {
color = color ? color : Color.blue; color = color ? color : Color.blue;
cmd(51, pos, size,color); Shape.circle(pos, size, color);
// cmd(51, pos, size,color);
}, },
arrow(start, end, color, capsize) { arrow(start, end, color, capsize) {

View file

@ -12,7 +12,8 @@ required_files.forEach(x => {
var editor_level = Level.create(); var editor_level = Level.create();
var editor_camera = editor_level.spawn(camera2d); var editor_camera = editor_level.spawn(camera2d);
editor_camera.save = false; editor_camera.save = false;
set_cam(editor_camera.body);
Yugine.view_camera(editor_camera);
var editor_config = { var editor_config = {
grid_size: 100, grid_size: 100,
@ -1251,12 +1252,12 @@ var editor = {
/* Clean out killed objects */ /* Clean out killed objects */
this.selectlist = this.selectlist.filter(function(x) { return x.alive; }); this.selectlist = this.selectlist.filter(function(x) { return x.alive; });
GUI.text("WORKING LAYER: " + this.working_layer, [0,520], 1); GUI.text("WORKING LAYER: " + this.working_layer, [0,520]);
GUI.text("MODE: " + this.edit_mode, [0,500],1); GUI.text("MODE: " + this.edit_mode, [0,500]);
Debug.point(world2screen(this.edit_level.pos), 5, Color.yellow); Debug.point(world2screen(this.edit_level.pos), 5, Color.yellow);
if (this.cursor) { if (this.cursor) {
Debug.point(world2screen(this.cursor), 5, Color.green); Debug.point(World2screen(this.cursor), 5, Color.green);
this.selectlist.forEach(function(x) { this.selectlist.forEach(function(x) {
var p = []; var p = [];
@ -1276,7 +1277,8 @@ var editor = {
GUI.text(this.sel_comp.help, [100,700],1); GUI.text(this.sel_comp.help, [100,700],1);
} }
gui_text("0,0", world2screen([0,0]), 1); GUI.text("0,0", world2screen([0,0]));
Debug.point([0,0],3);
var clvl = this.edit_level; var clvl = this.edit_level;
var ypos = 200; var ypos = 200;
@ -1330,7 +1332,7 @@ var editor = {
for (var key in this.selectlist[0].components) { for (var key in this.selectlist[0].components) {
var selected = this.sel_comp === this.selectlist[0].components[key]; var selected = this.sel_comp === this.selectlist[0].components[key];
var str = (selected ? ">" : " ") + key + " [" + this.selectlist[0].components[key].name + "]"; var str = (selected ? ">" : " ") + key + " [" + this.selectlist[0].components[key].name + "]";
gui_text(str, world2screen(this.selectlist[0].pos).add([0,-16*(i++)]), 1); GUI.text(str, world2screen(this.selectlist[0].pos).add([0,-16*(i++)]));
} }
if (this.sel_comp) { if (this.sel_comp) {
@ -1348,12 +1350,12 @@ var editor = {
var endgrid = screen2world([Window.width, 0]); var endgrid = screen2world([Window.width, 0]);
while(startgrid[0] <= endgrid[0]) { while(startgrid[0] <= endgrid[0]) {
gui_text(startgrid[0], [world2screen([startgrid[0], 0])[0], 1], 1); GUI.text(startgrid[0], [world2screen([startgrid[0], 0])[0],0]);
startgrid[0] += editor_config.grid_size; startgrid[0] += editor_config.grid_size;
} }
while(startgrid[1] <= endgrid[1]) { while(startgrid[1] <= endgrid[1]) {
gui_text(startgrid[1], [0, world2screen([0, startgrid[1]])[1]], 1); GUI.text(startgrid[1], [0, world2screen([0, startgrid[1]])[1]]);
startgrid[1] += editor_config.grid_size; startgrid[1] += editor_config.grid_size;
} }
@ -2523,7 +2525,7 @@ var limited_editor = {
Level.kill(); Level.kill();
Level.clear_all(); Level.clear_all();
editor.load_json(editor.stash); editor.load_json(editor.stash);
set_cam(editor_camera.body); Yugine.view_camera(editor_camera);
} }
}, },
input_f8_pressed() { sim_step(); }, input_f8_pressed() { sim_step(); },

View file

@ -163,11 +163,6 @@ function quit() {
Game.quit(); Game.quit();
}; };
function set_cam(id) {
cmd(61, id);
};
var Color = { var Color = {
white: [255,255,255,255], white: [255,255,255,255],
blue: [84,110,255,255], blue: [84,110,255,255],
@ -192,7 +187,7 @@ var GUI = {
var bb = cmd(118, str, size, wrap); var bb = cmd(118, str, size, wrap);
var opos = [bb.r, bb.t]; var opos = [bb.r, bb.t];
var h = ui_text(str, pos.sub(opos), size, color, wrap); var h = ui_text(str, pos, size, color, wrap);
return bb; return bb;
}, },
@ -1406,8 +1401,6 @@ var Level = {
var savereturn = JSON.stringify(this.objects, replacer_empty_nil, 1); var savereturn = JSON.stringify(this.objects, replacer_empty_nil, 1);
Log.warn(JSON.stringify(this));
if (this.flipx) { if (this.flipx) {
this.objects.forEach(function(obj) { this.objects.forEach(function(obj) {
this.mirror_x_obj(obj); this.mirror_x_obj(obj);
@ -2329,8 +2322,13 @@ var camera2d = gameobject.clone("camera2d", {
}, },
}); });
Yugine.camera = World.spawn(camera2d); Yugine.view_camera = function(cam)
cmd(61, Yugine.camera.id); {
Yugine.camera = cam;
cmd(61, Yugine.camera.body);
}
Yugine.view_camera(World.spawn(camera2d));
win_make(Game.title, Game.resolution[0], Game.resolution[1]); win_make(Game.title, Game.resolution[0], Game.resolution[1]);
//win_icon("icon.png"); //win_icon("icon.png");

View file

@ -1,7 +1,7 @@
#version 330 #version 330
out vec4 frag_color; out vec4 frag_color;
in vec2 apos; in vec2 apos; /* Drawing coordinates of the grid */
uniform float thickness; /* thickness in pixels */ uniform float thickness; /* thickness in pixels */
uniform float span; uniform float span;

View file

@ -13,6 +13,7 @@ void main()
{ {
// vec4 ipos = inverse(projection) * vec4(pos, 0.f, 1.f); // vec4 ipos = inverse(projection) * vec4(pos, 0.f, 1.f);
apos = pos * vec2(600, 360); apos = pos * vec2(600, 360);
apos += offset;
// apos = pos + offset; // apos = pos + offset;