-added external bindbc.sdl import for WASM -working on demos (WIP, working simple demo with ECS and SDL2) -small change in ecs.std
110 lines
No EOL
2.8 KiB
D
110 lines
No EOL
2.8 KiB
D
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);
|
|
|
|
} |