-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:
parent
a8c74d5045
commit
73f2aa6861
60 changed files with 9015 additions and 67 deletions
216
demos/simple/source/app.d
Normal file
216
demos/simple/source/app.d
Normal 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();
|
||||
}
|
||||
49
demos/simple/source/utils/texture.d
Normal file
49
demos/simple/source/utils/texture.d
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
module utils.texture;
|
||||
|
||||
import ecs.std;
|
||||
|
||||
import bindbc.sdl;
|
||||
import bindbc.sdl.image;
|
||||
|
||||
import ecs_utils.math.vector;
|
||||
|
||||
struct Texture
|
||||
{
|
||||
|
||||
void create()
|
||||
{
|
||||
data = Mallocator.make!Data;
|
||||
}
|
||||
|
||||
bool load(SDL_Renderer* renderer, const char[] path)
|
||||
{
|
||||
char[] cpath = (cast(char*)alloca(path.length+1))[0..path.length+1];
|
||||
//cpath[0..$-1] = path[0..$];
|
||||
memcpy(cpath.ptr, path.ptr, path.length);
|
||||
cpath[$-1] = 0;
|
||||
|
||||
SDL_Surface* surf = IMG_Load(cpath.ptr);
|
||||
if(!surf)return false;
|
||||
|
||||
data.size = ivec2(surf.w,surf.h);
|
||||
|
||||
data.texture = SDL_CreateTextureFromSurface(renderer,surf);
|
||||
SDL_FreeSurface(surf);
|
||||
|
||||
if(!data.texture)return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct Data
|
||||
{
|
||||
ubyte[] data;
|
||||
|
||||
ivec2 size;
|
||||
uint bpp;
|
||||
|
||||
SDL_Texture* texture;
|
||||
}
|
||||
|
||||
Data* data;
|
||||
}
|
||||
110
demos/simple/source/utils/utils.d
Normal file
110
demos/simple/source/utils/utils.d
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
module utils.utils;
|
||||
|
||||
extern(C) int printf(scope const char* format, ...) @nogc nothrow @system;
|
||||
|
||||
version(WebAssembly)
|
||||
{
|
||||
|
||||
extern (C) alias em_callback_func = void function();
|
||||
extern (C) alias em_arg_callback_func = void function(void*);
|
||||
extern (C) void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop);
|
||||
extern (C) void emscripten_set_main_loop_arg(em_arg_callback_func func, void *arg, int fps, int simulate_infinite_loop);
|
||||
extern (C) int emscripten_set_main_loop_timing(int mode, int value);
|
||||
extern (C) void emscripten_cancel_main_loop();
|
||||
|
||||
alias int time_t;
|
||||
alias int clockid_t;
|
||||
enum CLOCK_REALTIME = 0;
|
||||
|
||||
struct timespec
|
||||
{
|
||||
time_t tv_sec;
|
||||
int tv_nsec;
|
||||
}
|
||||
|
||||
extern(C) int clock_gettime(clockid_t, timespec*) @nogc nothrow @system;
|
||||
|
||||
struct Time
|
||||
{
|
||||
|
||||
|
||||
static long getUSecTime()
|
||||
{
|
||||
time_t time;
|
||||
timespec spec;
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &spec);
|
||||
|
||||
//time = spec.tv_sec;
|
||||
return spec.tv_sec * 1000_000 + spec.tv_nsec / 1000;//time / 1000_000;
|
||||
|
||||
/*LARGE_INTEGER time, freq;
|
||||
QueryPerformanceFrequency(&freq);
|
||||
QueryPerformanceCounter(&time);
|
||||
return time.QuadPart / (freq.QuadPart / 1000_000);*/
|
||||
}
|
||||
}
|
||||
}
|
||||
else version(Windows)
|
||||
{
|
||||
import core.stdc.stdio : printf;
|
||||
import core.sys.windows.windows;
|
||||
struct Time
|
||||
{
|
||||
static long getUSecTime()
|
||||
{
|
||||
LARGE_INTEGER time, freq;
|
||||
QueryPerformanceFrequency(&freq);
|
||||
QueryPerformanceCounter(&time);
|
||||
return time.QuadPart / (freq.QuadPart / 1000_000);
|
||||
}
|
||||
}
|
||||
}
|
||||
else version(Posix)
|
||||
{
|
||||
import core.stdc.stdio : printf;
|
||||
import core.sys.posix.time;
|
||||
struct Time
|
||||
{
|
||||
static long getUSecTime()
|
||||
{
|
||||
time_t time;
|
||||
timespec spec;
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &spec);
|
||||
|
||||
//time = spec.tv_sec;
|
||||
return spec.tv_sec * 1000_000 + spec.tv_nsec / 1000;//time / 1000_000;
|
||||
|
||||
/*LARGE_INTEGER time, freq;
|
||||
QueryPerformanceFrequency(&freq);
|
||||
QueryPerformanceCounter(&time);
|
||||
return time.QuadPart / (freq.QuadPart / 1000_000);*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
import bindbc.sdl;
|
||||
import utils.texture;
|
||||
import ecs_utils.math.vector;
|
||||
|
||||
ivec2 resolution = ivec2(600, 400);
|
||||
|
||||
void draw(SDL_Renderer* renderer, Texture tex, vec2 pos, vec2 size, vec4 coords, float angle = 0)
|
||||
{
|
||||
|
||||
SDL_Rect rect = SDL_Rect(cast(int)(coords.x*tex.data.size.x),cast(int)(coords.y*tex.data.size.y),cast(int)(coords.z*tex.data.size.x),cast(int)(coords.w*tex.data.size.y));
|
||||
SDL_Rect rect2 = SDL_Rect(cast(int)((pos.x-size.x*0.5)),
|
||||
cast(int)(resolution.y - pos.y - size.y*0.5),
|
||||
cast(int)(size.x),
|
||||
cast(int)(size.y));
|
||||
|
||||
SDL_RenderCopyEx(renderer,
|
||||
tex.data.texture,
|
||||
&rect,
|
||||
&rect2,
|
||||
angle*360,
|
||||
null,
|
||||
SDL_FLIP_NONE);
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue