simplify camera

This commit is contained in:
John Alanbrook 2024-03-20 16:48:03 -05:00
parent d3db5ca61e
commit 5a52afc2fd
9 changed files with 35 additions and 67 deletions

View file

@ -8,16 +8,4 @@ this.right = function() { return this.pos.x + (window.rendersize.x/2); }
this.left = function() { return this.pos.x - (window.rendersize.x/2); } this.left = function() { return this.pos.x - (window.rendersize.x/2); }
this.mixin({ this.zoom = 1;
get zoom() {
// var z = game.native.y / window.dimensions.y;
return render.get_zoom(); // /z
},
set zoom(x) {
x = Math.clamp(x,0.1,10);
// var z = game.native.y / window.dimensions.y;
// z *= x;
render.add_zoom(x);
},
});

View file

@ -31,8 +31,6 @@ var editor = {
get_this() { return this.edit_level; }, get_this() { return this.edit_level; },
get_that() { return this.selectlist.length === 1 ? this.selectlist[0] : this.get_this(); },
try_select() { /* nullify true if it should set selected to null if it doesn't find an object */ try_select() { /* nullify true if it should set selected to null if it doesn't find an object */
var go = physics.pos_query(Mouse.worldpos()); var go = physics.pos_query(Mouse.worldpos());
return this.do_select(go); return this.do_select(go);
@ -237,11 +235,7 @@ var editor = {
this.selectlist = []; this.selectlist = [];
editor.camera = world.spawn("scripts/camera2d.jso"); editor.camera = world.spawn("scripts/camera2d.jso");
editor.camera._ed.selectable = false; editor.camera._ed.selectable = false;
game.view_camera(editor.camera); game.camera = editor.camera;
},
end_debug() {
}, },
openpanel(panel) { openpanel(panel) {

View file

@ -188,7 +188,10 @@ function process()
prosperon.physupdate(phys_step*timescale); prosperon.physupdate(phys_step*timescale);
} }
prosperon.window_render(); if (!game.camera)
prosperon.window_render(world, 1);
else
prosperon.window_render(game.camera, game.camera.zoom);
render.sprites(); render.sprites();
render.models(); render.models();
render.emitters(); render.emitters();
@ -221,7 +224,6 @@ game.doc.stop = "Stop game simulation. This does the same thing as 'pause', and
game.doc.play = "Resume or start game simulation."; game.doc.play = "Resume or start game simulation.";
game.doc.editor_mode = "Set to true for the game to only update on input; otherwise the game updates every frame."; game.doc.editor_mode = "Set to true for the game to only update on input; otherwise the game updates every frame.";
game.doc.dt = "Current frame dt."; game.doc.dt = "Current frame dt.";
game.doc.view_camera = "Set the camera for the current view.";
game.doc.camera = "Current camera."; game.doc.camera = "Current camera.";
prosperon.semver = {}; prosperon.semver = {};
@ -433,17 +435,11 @@ function world_start() {
world.ur = "world"; world.ur = "world";
world.kill = function() { this.clear(); }; world.kill = function() { this.clear(); };
world.phys = 2; world.phys = 2;
var cam = world.spawn("scripts/camera2d.jso"); world.zoom = 1;
game.view_camera(cam); game.cam = world;
} }
global.mixin("scripts/physics.js"); global.mixin("scripts/physics.js");
game.view_camera = function(cam)
{
game.camera = cam;
render.cam_body(game.camera);
}
window.title = `Prosperon v${prosperon.version}`; window.title = `Prosperon v${prosperon.version}`;
window.size = [500,500]; window.size = [500,500];

View file

@ -1,7 +1,7 @@
/* On collisions, entities are sent a 'hit' object, which looks like this: /* On collisions, entities are sent a 'hit' object, which looks like this:
var HIT = { var HIT = {
normal: "The normal of the collision point.", normal: "The normal of the collision point.",
hit: "The gameobject of the object that collided.", obj: "The gameobject of the object that collided.",
sensor: "Boolean for if the colliding object was a sensor.", sensor: "Boolean for if the colliding object was a sensor.",
velocity: "Velocity of the contact.", velocity: "Velocity of the contact.",
pos: "Position in world space of the contact.", pos: "Position in world space of the contact.",

View file

@ -546,8 +546,8 @@ void draw_box(HMM_Vec2 c, HMM_Vec2 wh, struct rgba color)
void draw_grid(float width, float span, struct rgba color) void draw_grid(float width, float span, struct rgba color)
{ {
HMM_Vec2 offset = (HMM_Vec2)cam_pos(); HMM_Vec2 offset = campos;
offset = HMM_MulV2F(offset, 1/cam_zoom()); offset = HMM_MulV2F(offset, 1/camzoom);
float ubo[4]; float ubo[4];
ubo[0] = offset.x; ubo[0] = offset.x;
@ -562,7 +562,7 @@ void draw_grid(float width, float span, struct rgba color)
fs_params_t pt; fs_params_t pt;
pt.thickness = (float)width; pt.thickness = (float)width;
pt.span = span/cam_zoom(); pt.span = span/camzoom;
memcpy(&pt.color, col, sizeof(float)*4); memcpy(&pt.color, col, sizeof(float)*4);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(ubo)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(ubo));
sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(pt)); sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(pt));

View file

@ -844,16 +844,10 @@ JSC_CCALL(render_commit, sg_commit(); debug_newframe();)
JSC_SCALL(render_text_size, ret = bb2js(text_bb(str, js2number(argv[1]), js2number(argv[2]), 1))) JSC_SCALL(render_text_size, ret = bb2js(text_bb(str, js2number(argv[1]), js2number(argv[2]), 1)))
JSC_SCALL(render_gif_times, ret = ints2js(gif_delays(str))) JSC_SCALL(render_gif_times, ret = ints2js(gif_delays(str)))
JSC_SCALL(render_gif_frames, ret = number2js(gif_nframes(str))) JSC_SCALL(render_gif_frames, ret = number2js(gif_nframes(str)))
JSC_CCALL(render_cam_body, set_cam_body(js2gameobject(argv[0])->body))
JSC_CCALL(render_add_zoom, add_zoom(js2number(argv[0])))
JSC_CCALL(render_get_zoom, return number2js(cam_zoom()))
JSC_CCALL(render_world2screen, return vec22js(world2screen(js2vec2(argv[0])))) JSC_CCALL(render_world2screen, return vec22js(world2screen(js2vec2(argv[0]))))
JSC_CCALL(render_screen2world, return vec22js(screen2world(js2vec2(argv[0])))) JSC_CCALL(render_screen2world, return vec22js(screen2world(js2vec2(argv[0]))))
static const JSCFunctionListEntry js_render_funcs[] = { static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render,cam_body, 1),
MIST_FUNC_DEF(render,add_zoom,1),
MIST_FUNC_DEF(render,get_zoom,0),
MIST_FUNC_DEF(render,world2screen,1), MIST_FUNC_DEF(render,world2screen,1),
MIST_FUNC_DEF(render,screen2world,1), MIST_FUNC_DEF(render,screen2world,1),
MIST_FUNC_DEF(render,gif_frames,1), MIST_FUNC_DEF(render,gif_frames,1),
@ -982,7 +976,7 @@ static const JSCFunctionListEntry js_input_funcs[] = {
JSC_CCALL(prosperon_emitters_step, emitters_step(js2number(argv[0]))) JSC_CCALL(prosperon_emitters_step, emitters_step(js2number(argv[0])))
JSC_CCALL(prosperon_phys2d_step, phys2d_update(js2number(argv[0]))) JSC_CCALL(prosperon_phys2d_step, phys2d_update(js2number(argv[0])))
JSC_CCALL(prosperon_window_render, openglRender(&mainwin)) JSC_CCALL(prosperon_window_render, openglRender(&mainwin, js2gameobject(argv[0]), js2number(argv[1])))
static const JSCFunctionListEntry js_prosperon_funcs[] = { static const JSCFunctionListEntry js_prosperon_funcs[] = {
MIST_FUNC_DEF(prosperon, emitters_step, 1), MIST_FUNC_DEF(prosperon, emitters_step, 1),

View file

@ -26,6 +26,9 @@
#include "msf_gif.h" #include "msf_gif.h"
HMM_Vec2 campos = {0,0};
float camzoom = 1;
static struct { static struct {
sg_swapchain swap; sg_swapchain swap;
sg_pipeline pipe; sg_pipeline pipe;
@ -356,18 +359,10 @@ void render_init() {
} }
static cpBody *camera = NULL;
void set_cam_body(cpBody *body) { camera = body; }
cpVect cam_pos() { return camera ? cpBodyGetPosition(camera) : cpvzero; }
static float zoom = 1.f;
float cam_zoom() { return zoom; }
void add_zoom(float val) { zoom = val; }
HMM_Vec2 world2screen(HMM_Vec2 pos) HMM_Vec2 world2screen(HMM_Vec2 pos)
{ {
pos = HMM_SubV2(pos, HMM_V2(cam_pos().x, cam_pos().y)); pos = HMM_SubV2(pos, campos);
pos = HMM_ScaleV2(pos, 1.0/zoom); pos = HMM_ScaleV2(pos, 1.0/camzoom);
pos = HMM_AddV2(pos, HMM_ScaleV2(mainwin.size,0.5)); pos = HMM_AddV2(pos, HMM_ScaleV2(mainwin.size,0.5));
return pos; return pos;
} }
@ -376,8 +371,8 @@ HMM_Vec2 screen2world(HMM_Vec2 pos)
{ {
pos = HMM_ScaleV2(pos, 1/mainwin.dpi); pos = HMM_ScaleV2(pos, 1/mainwin.dpi);
pos = HMM_SubV2(pos, HMM_ScaleV2(mainwin.size, 0.5)); pos = HMM_SubV2(pos, HMM_ScaleV2(mainwin.size, 0.5));
pos = HMM_ScaleV2(pos, zoom); pos = HMM_ScaleV2(pos, camzoom);
pos = HMM_AddV2(pos, HMM_V2(cam_pos().x, cam_pos().y)); pos = HMM_AddV2(pos, campos);
return pos; return pos;
} }
@ -415,7 +410,7 @@ void full_3d_pass(struct window *window)
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(subo)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(subo));
} }
void openglRender(struct window *window) { void openglRender(struct window *window, gameobject *cam, float zoom) {
sg_swapchain sch = sglue_swapchain(); sg_swapchain sch = sglue_swapchain();
sg_begin_pass(&(sg_pass){ sg_begin_pass(&(sg_pass){
.action = pass_action, .action = pass_action,
@ -450,13 +445,14 @@ void openglRender(struct window *window) {
} }
// 2D projection // 2D projection
cpVect pos = cam_pos(); campos = go_pos(cam);
camzoom = zoom;
projection = HMM_Orthographic_LH_NO( projection = HMM_Orthographic_LH_NO(
pos.x - zoom * usesize.x / 2, campos.x - camzoom * usesize.x / 2,
pos.x + zoom * usesize.x / 2, campos.x + camzoom * usesize.x / 2,
pos.y - zoom * usesize.y / 2, campos.y - camzoom * usesize.y / 2,
pos.y + zoom * usesize.y / 2, -10000.f, 10000.f); campos.y + camzoom * usesize.y / 2, -10000.f, 10000.f);
hudproj = HMM_Orthographic_LH_ZO(0, usesize.x, 0, usesize.y, -1.f, 1.f); hudproj = HMM_Orthographic_LH_ZO(0, usesize.x, 0, usesize.y, -1.f, 1.f);

View file

@ -14,6 +14,7 @@
#include "sokol/sokol_gfx.h" #include "sokol/sokol_gfx.h"
#include "HandmadeMath.h" #include "HandmadeMath.h"
#include "gameobject.h"
#define RGBA_MAX 255 #define RGBA_MAX 255
@ -52,17 +53,16 @@ enum RenderMode {
}; };
void render_init(); void render_init();
void openglRender(struct window *window); extern HMM_Vec2 campos;
extern float camzoom;
void openglRender(struct window *window, gameobject *cam, float zoom);
void opengl_rendermode(enum RenderMode r); void opengl_rendermode(enum RenderMode r);
void openglInit3d(struct window *window); void openglInit3d(struct window *window);
void openglRender3d(struct window *window, camera3d *camera); void openglRender3d(struct window *window, camera3d *camera);
void capture_screen(int x, int y, int w, int h, const char *path); void capture_screen(int x, int y, int w, int h, const char *path);
void set_cam_body(cpBody *body);
cpVect cam_pos();
float cam_zoom();
void add_zoom(float val);
HMM_Vec2 world2screen(HMM_Vec2 pos); HMM_Vec2 world2screen(HMM_Vec2 pos);
HMM_Vec2 screen2world(HMM_Vec2 pos); HMM_Vec2 screen2world(HMM_Vec2 pos);

View file

@ -207,8 +207,8 @@ void sprite_draw(struct sprite *sprite) {
if (!sprite->go) t = t2d_unit; if (!sprite->go) t = t2d_unit;
else t = go2t(sprite->go); else t = go2t(sprite->go);
t.pos.x += (cam_pos().x - (cam_pos().x/sprite->parallax)); t.pos.x += (campos.x - (campos.x/sprite->parallax));
t.pos.y += (cam_pos().y - (cam_pos().y/sprite->parallax)); t.pos.y += (campos.y - (campos.y/sprite->parallax));
HMM_Mat3 m = transform2d2mat(t); HMM_Mat3 m = transform2d2mat(t);
HMM_Mat3 sm = transform2d2mat(sprite2t(sprite)); HMM_Mat3 sm = transform2d2mat(sprite2t(sprite));
tex_draw(sprite->tex, HMM_MulM3(m,sm), sprite->frame, sprite->color, sprite->drawmode, (HMM_Vec2){0,0}, sprite->scale, sprite->emissive, sprite->parallax); tex_draw(sprite->tex, HMM_MulM3(m,sm), sprite->frame, sprite->color, sprite->drawmode, (HMM_Vec2){0,0}, sprite->scale, sprite->emissive, sprite->parallax);