-improved WASM compilation scripts

-added external bindbc.sdl import for WASM
-working on demos (WIP, working simple demo with ECS and SDL2)
-small change in ecs.std
This commit is contained in:
Mergul 2019-11-06 20:38:46 +01:00
parent a8c74d5045
commit 73f2aa6861
60 changed files with 9015 additions and 67 deletions

216
demos/simple/source/app.d Normal file
View file

@ -0,0 +1,216 @@
module source.app;
import ecs.std;
import ecs.core;
import ecs.manager;
import ecs.attributes;
import ecs.entity;
import bindbc.sdl;
import utils.utils;
import utils.texture;
import ecs_utils.math.vector;
version (WebAssembly)
{
extern (C) void _start()
{
}
}
SDL_Renderer *renderer;
SDL_Window* window;
Texture texture;
EntityManager* manager;
struct CLocation
{
mixin ECS.Component;
alias location this;
vec2 location;
}
struct CTexture
{
mixin ECS.Component;
Texture tex;
}
struct DrawSystem
{
mixin ECS.System;
struct EntitiesData
{
uint length;
@readonly CTexture[] textures;
@readonly CLocation[] locations;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1));
}
}
}
struct MoveSystem
{
mixin ECS.System;
struct EntitiesData
{
uint length;
CLocation[] locations;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
data.locations[i].location.y = data.locations[i].location.y + 1;
if(data.locations[i].location.y > 400)data.locations[i].location.y = 0;
}
}
}
extern (C) int main(int argc, char** argv)
{
fps = 0;
time = 0;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s", SDL_GetError());
return -1;
}
SDL_version sdl_version;
SDL_GetVersion(&sdl_version);
printf("SDL version: %u.%u.%u\n",cast(uint)sdl_version.major,cast(uint)sdl_version.minor,cast(uint)sdl_version.patch);
SDL_Window* window = SDL_CreateWindow("Simple", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
//SDL_CreateWindowAndRenderer(600, 400, SDL_RENDERER_ACCELERATED, &window, &renderer);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(renderer, 1, 255, 255, 255);
texture.create();
texture.load(renderer, "assets/textures/buckler.png");
EntityManager.initialize(8);
manager = EntityManager.instance;
manager.beginRegister();
manager.registerComponent!CLocation;
manager.registerComponent!CTexture;
manager.registerSystem!MoveSystem(64);
manager.registerSystem!DrawSystem(64);
manager.endRegister();
ushort[2] components = [CLocation.component_id, CTexture.component_id];
EntityTemplate* tmpl = manager.allocateTemplate(components);
CTexture* tex_comp = tmpl.getComponent!CTexture;
tex_comp.tex = texture;
CLocation* loc_comp = tmpl.getComponent!CLocation;
// loc_comp.location = vec2(64,64);
foreach(i; 0..10)
foreach(j; 0..10)
{
loc_comp.location = vec2(i*32+64,j*32+64);
manager.addEntity(tmpl);
}
manager.freeTemplate(tmpl);
/*image = IMG_Load("assets/owl.png");
tex = SDL_CreateTextureFromSurface(renderer, image);*/
version(WebAssembly)
{
emscripten_set_main_loop_arg(&loop, null, -1, 1);
}
bool arg = true;
while(arg == true)
{
loop(&arg);
}
if (window !is null) {
SDL_DestroyWindow(window);
}
end();
return 0;
}
long time;
uint fps;
extern(C) void loop(void *arg = null)
{
static float fps_time = 0;
float delta_time = cast(float)(Time.getUSecTime() - time);
if(delta_time > 1000_000)delta_time = 1000;
time = Time.getUSecTime();
if(fps_time < 0)fps_time = 0;
fps++;
fps_time += delta_time;
if(fps_time > 1000_000)
{
printf("FPS: %u\n",fps);
fps = 0;
fps_time -= 1000_000;
}
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT) {
version(WebAssembly)emscripten_cancel_main_loop();
*cast(bool*)arg = false;
//return false;
}
if (event.type == SDL_KEYDOWN) {
version(WebAssembly)emscripten_cancel_main_loop();
*cast(bool*)arg = false;
//return false;
}
}
SDL_RenderClear(renderer);
manager.begin();
manager.update();
manager.end();
draw(renderer,texture,vec2(32,32),vec2(32,32),vec4(0,0,1,1));
//SDL_RenderCopy(ctx->renderer, ctx->owl_tex, NULL, &ctx->dest);
SDL_RenderPresent(renderer);
//return true;
}
void end()
{
EntityManager.destroy();
SDL_Quit();
}