Compare commits
2 Commits
29d163216c
...
5a83381ae1
Author | SHA1 | Date | |
---|---|---|---|
|
5a83381ae1 | ||
|
793bd850f6 |
@ -179,6 +179,7 @@ static void draw_terrain(SceneIngame *scn) {
|
|||||||
draw_billboard("/assets/grasses/10.png",
|
draw_billboard("/assets/grasses/10.png",
|
||||||
(Vec3){ (float)x, d0 + 0.15f, (float)y },
|
(Vec3){ (float)x, d0 + 0.15f, (float)y },
|
||||||
(Vec2){0.3f, 0.3f},
|
(Vec2){0.3f, 0.3f},
|
||||||
|
NULL,
|
||||||
(Color){255, 255, 255, 255}, true);
|
(Color){255, 255, 255, 255}, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
apps/twnlua/.gitignore
vendored
1
apps/twnlua/.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
luabind.c
|
luabind.c
|
||||||
|
data/scripts/twnapi.lua
|
||||||
|
@ -151,15 +151,12 @@ for typename, typedesc in used_converters.items():
|
|||||||
|
|
||||||
|
|
||||||
print('\n'.join(storages))
|
print('\n'.join(storages))
|
||||||
print("extern void bindgen_init(void);\n")
|
|
||||||
print("void bindgen_init(void) {\n" + '\n'.join(initializers) + "\n}\n")
|
|
||||||
print('\n'.join(converters))
|
print('\n'.join(converters))
|
||||||
print('\n'.join(bindings))
|
print('\n'.join(bindings))
|
||||||
|
|
||||||
|
|
||||||
loader = "extern void bindgen_load_%s(lua_State *L);\n" % api["name"]
|
loader = "extern void bindgen_load_%s(lua_State *L);\n" % api["name"]
|
||||||
loader += "void bindgen_load_%s(lua_State *L) {\n" % api["name"]
|
loader += "void bindgen_load_%s(lua_State *L) {\n" % api["name"]
|
||||||
loader += " bindgen_init();\n"
|
|
||||||
for procedure, procedure_desc in api["procedures"].items():
|
for procedure, procedure_desc in api["procedures"].items():
|
||||||
loader += " lua_pushcfunction(L, binding_%s);\n" % procedure
|
loader += " lua_pushcfunction(L, binding_%s);\n" % procedure
|
||||||
loader += " lua_setglobal(L, \"%s\");\n" % procedure
|
loader += " lua_setglobal(L, \"%s\");\n" % procedure
|
||||||
|
@ -4,16 +4,39 @@ offset = { x = 0, y = 0 }
|
|||||||
angle = 0
|
angle = 0
|
||||||
|
|
||||||
function game_tick()
|
function game_tick()
|
||||||
|
if ctx.udata == nil then
|
||||||
|
ctx.udata = {
|
||||||
|
frame_count = 0,
|
||||||
|
nest = {
|
||||||
|
frame_count = 0,
|
||||||
|
},
|
||||||
|
arr = { [0] = 0 },
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
draw_text {
|
||||||
|
string = tostring(ctx.udata.frame_count),
|
||||||
|
position = { x = 0, y = 0 },
|
||||||
|
font = "/fonts/kenney-pixel.ttf",
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_text {
|
||||||
|
string = tostring(ctx.udata.nest.frame_count),
|
||||||
|
position = { x = 0, y = 14 },
|
||||||
|
font = "/fonts/kenney-pixel.ttf",
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_text {
|
||||||
|
string = tostring(ctx.udata.arr[0]),
|
||||||
|
position = { x = 0, y = 28 },
|
||||||
|
font = "/fonts/kenney-pixel.ttf",
|
||||||
|
}
|
||||||
|
|
||||||
input_action {
|
input_action {
|
||||||
name = "press",
|
name = "press",
|
||||||
control = "A"
|
control = "A"
|
||||||
}
|
}
|
||||||
|
|
||||||
draw_rectangle {
|
|
||||||
rect = { x = 0, y = 0, w = 640, h = 360 },
|
|
||||||
color = { r = 127, g = 0, b = 127, a = 255 },
|
|
||||||
}
|
|
||||||
|
|
||||||
draw_sprite {
|
draw_sprite {
|
||||||
texture = "/assets/title.png",
|
texture = "/assets/title.png",
|
||||||
rect = {
|
rect = {
|
||||||
@ -32,6 +55,10 @@ function game_tick()
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ctx.udata.frame_count = ctx.udata.frame_count + 1
|
||||||
|
ctx.udata.nest.frame_count = ctx.udata.nest.frame_count + 1
|
||||||
|
ctx.udata.arr[0] = ctx.udata.arr[0] + 1
|
||||||
|
|
||||||
offset.x = ORIGIN.x + (math.cos(angle) * RADIUS)
|
offset.x = ORIGIN.x + (math.cos(angle) * RADIUS)
|
||||||
offset.y = ORIGIN.y + (math.sin(angle) * RADIUS)
|
offset.y = ORIGIN.y + (math.sin(angle) * RADIUS)
|
||||||
angle = angle + 0.1
|
angle = angle + 0.1
|
||||||
|
@ -6,5 +6,6 @@ dev_id = "somebody"
|
|||||||
|
|
||||||
[game]
|
[game]
|
||||||
resolution = [ 640, 360 ]
|
resolution = [ 640, 360 ]
|
||||||
|
background_color = [ 127, 0, 127, 255 ]
|
||||||
|
|
||||||
[engine]
|
[engine]
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "twn_game_api.h"
|
#include "twn_game_api.h"
|
||||||
|
|
||||||
|
/* TODO: actually move it back it its own file, it doesn't give any compilation benefits */
|
||||||
#define LUA_IMPL
|
#define LUA_IMPL
|
||||||
#include "minilua.h"
|
#include "minilua.h"
|
||||||
|
|
||||||
@ -8,6 +9,8 @@
|
|||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#define UDATA_NESTING_LIMIT 128
|
||||||
|
|
||||||
|
|
||||||
/* generated by bindgen.py */
|
/* generated by bindgen.py */
|
||||||
void bindgen_load_twn(lua_State *L);
|
void bindgen_load_twn(lua_State *L);
|
||||||
@ -100,6 +103,47 @@ static void *custom_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void exchange_lua_states(lua_State *from, lua_State *to, int level, int index) {
|
||||||
|
if (level >= UDATA_NESTING_LIMIT) {
|
||||||
|
log_critical("ctx.udata nesting limit is reached (%u)", UDATA_NESTING_LIMIT);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: use arrays for optimized paths */
|
||||||
|
/* TODO: preallocate table records */
|
||||||
|
switch (lua_type(from, index)) {
|
||||||
|
case LUA_TTABLE:
|
||||||
|
lua_newtable(to);
|
||||||
|
lua_pushnil(from); /* first key */
|
||||||
|
while (lua_next(from, index - 1) != 0) {
|
||||||
|
/* 'key' at index -2 and 'value' at index -1 */
|
||||||
|
exchange_lua_states(from, to, level + 1, -2);
|
||||||
|
exchange_lua_states(from, to, level + 1, -1);
|
||||||
|
lua_settable(to, index - 2);
|
||||||
|
/* removes 'value'; keeps 'key' for next iteration */
|
||||||
|
lua_pop(from, 1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LUA_TNUMBER:
|
||||||
|
lua_pushnumber(to, lua_tonumber(from, index));
|
||||||
|
break;
|
||||||
|
case LUA_TBOOLEAN:
|
||||||
|
lua_pushboolean(to, lua_toboolean(from, index));
|
||||||
|
break;
|
||||||
|
case LUA_TSTRING:
|
||||||
|
lua_pushstring(to, lua_tostring(from, index));
|
||||||
|
break;
|
||||||
|
case LUA_TNIL:
|
||||||
|
lua_pushnil(to);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* TODO: provide a path and type of it for better diagnostic */
|
||||||
|
log_warn("Unserializable udata found and is ignored");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void game_tick(void) {
|
void game_tick(void) {
|
||||||
if (ctx.initialization_needed) {
|
if (ctx.initialization_needed) {
|
||||||
if (!ctx.udata)
|
if (!ctx.udata)
|
||||||
@ -108,13 +152,29 @@ void game_tick(void) {
|
|||||||
State *state = ctx.udata;
|
State *state = ctx.udata;
|
||||||
|
|
||||||
/* let's init lua */
|
/* let's init lua */
|
||||||
/* state existed already */
|
lua_State *new_state = luaL_newstate();
|
||||||
|
lua_setallocf(new_state, custom_alloc, NULL);
|
||||||
|
|
||||||
|
/* state existed already, copy its udata over */
|
||||||
if (state->L != NULL) {
|
if (state->L != NULL) {
|
||||||
|
lua_getglobal(state->L, "ctx");
|
||||||
|
lua_getfield(state->L, -1, "udata");
|
||||||
|
SDL_assert(!lua_isnoneornil(state->L, -1));
|
||||||
|
SDL_assert(!lua_isnoneornil(state->L, -2));
|
||||||
|
// SDL_TriggerBreakpoint();
|
||||||
|
if (!lua_isnoneornil(state->L, -1)) {
|
||||||
|
log_info("Exchanging lua states...");
|
||||||
|
lua_newtable(new_state);
|
||||||
|
exchange_lua_states(state->L, new_state, 0, -1);
|
||||||
|
lua_setfield(new_state, -2, "udata");
|
||||||
|
lua_setglobal(new_state, "ctx");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* bye :) */
|
||||||
lua_close(state->L);
|
lua_close(state->L);
|
||||||
}
|
}
|
||||||
state->L = luaL_newstate();
|
|
||||||
|
|
||||||
lua_setallocf(state->L, custom_alloc, NULL);
|
state->L = new_state;
|
||||||
|
|
||||||
/* fakey version of luaL_openlibs() that excludes file i/o and os stuff */
|
/* fakey version of luaL_openlibs() that excludes file i/o and os stuff */
|
||||||
{
|
{
|
||||||
|
@ -383,7 +383,8 @@ extern "C" {
|
|||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
#define LUA_API extern
|
/* TWN: Don't export anything, there's no need. */
|
||||||
|
#define LUA_API LUAI_FUNC
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
@ -66,7 +66,6 @@ TWN_API void draw_triangle(char const *texture,
|
|||||||
Color c1, /* optional, default: all 255 */
|
Color c1, /* optional, default: all 255 */
|
||||||
Color c2); /* optional, default: all 255 */
|
Color c2); /* optional, default: all 255 */
|
||||||
|
|
||||||
/* TODO: double sided option */
|
|
||||||
TWN_API void draw_quad(char const *texture,
|
TWN_API void draw_quad(char const *texture,
|
||||||
Vec3 v0, /* upper-left */
|
Vec3 v0, /* upper-left */
|
||||||
Vec3 v1, /* bottom-left */
|
Vec3 v1, /* bottom-left */
|
||||||
@ -75,11 +74,12 @@ TWN_API void draw_quad(char const *texture,
|
|||||||
Rect texture_region,
|
Rect texture_region,
|
||||||
Color color); /* optional, default: all 255 */
|
Color color); /* optional, default: all 255 */
|
||||||
|
|
||||||
TWN_API void draw_billboard(const char *texture,
|
TWN_API void draw_billboard(char const *texture,
|
||||||
Vec3 position,
|
Vec3 position,
|
||||||
Vec2 size,
|
Vec2 size,
|
||||||
Color color, /* optional, default: all 255 */
|
Rect const *texture_region, /* optional, default: NULL */
|
||||||
bool cylindrical); /* optional, default: false */
|
Color color, /* optional, default: all 255 */
|
||||||
|
bool cylindrical); /* optional, default: false */
|
||||||
|
|
||||||
TWN_API void draw_camera_2d(Vec2 position, /* optional, default: (0, 0) */
|
TWN_API void draw_camera_2d(Vec2 position, /* optional, default: (0, 0) */
|
||||||
float rotation, /* optional, default: 0 */
|
float rotation, /* optional, default: 0 */
|
||||||
|
@ -180,6 +180,7 @@
|
|||||||
{ "name": "texture", "type": "char *" },
|
{ "name": "texture", "type": "char *" },
|
||||||
{ "name": "position", "type": "Vec3" },
|
{ "name": "position", "type": "Vec3" },
|
||||||
{ "name": "size", "type": "Vec2" },
|
{ "name": "size", "type": "Vec2" },
|
||||||
|
{ "name": "texture_region", "type": "Rect *", "default": {} },
|
||||||
{ "name": "color", "type": "Color", "default": { "r": 255, "g": 255, "b": 255, "a": 255 } },
|
{ "name": "color", "type": "Color", "default": { "r": 255, "g": 255, "b": 255, "a": 255 } },
|
||||||
{ "name": "cylindrical", "type": "bool", "default": false }
|
{ "name": "cylindrical", "type": "bool", "default": false }
|
||||||
]
|
]
|
||||||
|
@ -9,9 +9,10 @@
|
|||||||
#include <stb_ds.h>
|
#include <stb_ds.h>
|
||||||
|
|
||||||
|
|
||||||
void draw_billboard(const char *texture,
|
void draw_billboard(char const *texture,
|
||||||
Vec3 position,
|
Vec3 position,
|
||||||
Vec2 size,
|
Vec2 size,
|
||||||
|
Rect const *texture_region,
|
||||||
Color color,
|
Color color,
|
||||||
bool cylindrical)
|
bool cylindrical)
|
||||||
{
|
{
|
||||||
@ -30,8 +31,12 @@ void draw_billboard(const char *texture,
|
|||||||
.cylindrical = cylindrical,
|
.cylindrical = cylindrical,
|
||||||
.position = position,
|
.position = position,
|
||||||
.size = size,
|
.size = size,
|
||||||
|
.texture_region_opt_set = texture_region != NULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (texture_region)
|
||||||
|
billboard.texture_region_opt = *texture_region;
|
||||||
|
|
||||||
struct SpaceBillboard *billboards = (struct SpaceBillboard *)(void *)batch_p->value.primitives;
|
struct SpaceBillboard *billboards = (struct SpaceBillboard *)(void *)batch_p->value.primitives;
|
||||||
|
|
||||||
arrpush(billboards, billboard);
|
arrpush(billboards, billboard);
|
||||||
@ -81,10 +86,10 @@ void finally_draw_billboard_batch(struct MeshBatch const *batch,
|
|||||||
const float xr = srcrect.x / dims.w;
|
const float xr = srcrect.x / dims.w;
|
||||||
const float yr = srcrect.y / dims.h;
|
const float yr = srcrect.y / dims.h;
|
||||||
|
|
||||||
const Vec2 uv0 = { xr, yr };
|
const Vec2 uv0c = { xr, yr };
|
||||||
const Vec2 uv1 = { xr, yr + hr };
|
const Vec2 uv1c = { xr, yr + hr };
|
||||||
const Vec2 uv2 = { xr + wr, yr + hr };
|
const Vec2 uv2c = { xr + wr, yr + hr };
|
||||||
const Vec2 uv3 = { xr + wr, yr };
|
const Vec2 uv3c = { xr + wr, yr };
|
||||||
|
|
||||||
for (size_t batch_n = 0; batch_n <= (primitives_len - 1) / QUAD_ELEMENT_BUFFER_LENGTH; batch_n++) {
|
for (size_t batch_n = 0; batch_n <= (primitives_len - 1) / QUAD_ELEMENT_BUFFER_LENGTH; batch_n++) {
|
||||||
|
|
||||||
@ -107,6 +112,20 @@ void finally_draw_billboard_batch(struct MeshBatch const *batch,
|
|||||||
b = vec3_mul(right_minus_up, ((Vec3){billboard.size.x, billboard.size.y, billboard.size.x }));
|
b = vec3_mul(right_minus_up, ((Vec3){billboard.size.x, billboard.size.y, billboard.size.x }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vec2 uv0, uv1, uv2, uv3;
|
||||||
|
if (billboard.texture_region_opt_set) {
|
||||||
|
uv0 = (Vec2){ (srcrect.x + billboard.texture_region_opt.x) / dims.w,
|
||||||
|
(srcrect.y + billboard.texture_region_opt.y) / dims.h };
|
||||||
|
uv1 = (Vec2){ (srcrect.x + billboard.texture_region_opt.x) / dims.w,
|
||||||
|
(srcrect.y + billboard.texture_region_opt.y + billboard.texture_region_opt.h) / dims.h };
|
||||||
|
uv2 = (Vec2){ (srcrect.x + billboard.texture_region_opt.x + billboard.texture_region_opt.w) / dims.w,
|
||||||
|
(srcrect.y + billboard.texture_region_opt.y + billboard.texture_region_opt.h) / dims.h };
|
||||||
|
uv3 = (Vec2){ (srcrect.x + billboard.texture_region_opt.x + billboard.texture_region_opt.w) / dims.w,
|
||||||
|
(srcrect.y + billboard.texture_region_opt.y) / dims.h };
|
||||||
|
} else {
|
||||||
|
uv0 = uv0c; uv1 = uv1c; uv2 = uv2c; uv3 = uv3c;
|
||||||
|
}
|
||||||
|
|
||||||
struct ElementIndexedBillboard const payload = {
|
struct ElementIndexedBillboard const payload = {
|
||||||
/* flat shading is assumed, so we can skip setting the duplicates */
|
/* flat shading is assumed, so we can skip setting the duplicates */
|
||||||
.c0 = billboard.color,
|
.c0 = billboard.color,
|
||||||
|
@ -121,6 +121,8 @@ typedef struct SpaceBillboard {
|
|||||||
Vec3 position;
|
Vec3 position;
|
||||||
Vec2 size;
|
Vec2 size;
|
||||||
Color color;
|
Color color;
|
||||||
|
m_option_list(
|
||||||
|
Rect, texture_region )
|
||||||
// TextureKey texture; /* is assumed from other places */
|
// TextureKey texture; /* is assumed from other places */
|
||||||
bool cylindrical;
|
bool cylindrical;
|
||||||
} SpaceBillboard;
|
} SpaceBillboard;
|
||||||
|
Loading…
Reference in New Issue
Block a user