Common update:
-added multiple new function to allocate template and add entity -updated README.md (complete initial version) -empty components now don't take memory -fixedd small bug with TestRunner -added many new tests (HashMap, Vector, EntityMeta, ...) -added default hashing function to HashMap -fixed critical bug with adding entities -fixed small bug with adding entity with remplacement components -added asserts into code to better bug detection -small performance improvement for events -added ComponentRef structure which contain data pointer and componentID -remove EntityID from Event structure -now events are handled before removing entiteis -fixed GDC compilation -fixed rendering of rotated sprites -added weapons as separate entities to space ship and others -added Tower enemy to SpaceInvaders demo -added Boss to SpaceInvaders demo (boss has four tower attached to it) -Boss towers shoot multiple bullets upon death -fixed critical bug with demos switching -fixed critical bug related to adding/removing entities form inside onAdd/onRemove entity callback -added animation support -added particles sypport and particles for firing and explostions, and more -multithreaded rendering now has same rendering order as singlethreaded -application automaticallu detect host CPU threads count -added upgrades to SPaceInvaders demo -fixed texture memory freeing -improved documentation -improved multithreaded performance -improve shader code -fixed registration issue -some additional performance improvements -added depth and colors to rendering parameters -jobs now has names corresponding to their systems -change execute() -> willExecute() -added EntityMeta structure to speedup getting fetching components form entity -improved multithreading rendering -added possibility tio change number of threads runtime -added bullets collision detection in SpaceInvaders demo -some CI changes -added VBO batch rendering (current default, no render mode switch yet) -fixed camera positioning calculation -fixed buffer issue with WebGL -added viewport scalling (at least 300 pixels height). Pixels are scalled if screen is bigger. -center demos gameplay area -added fullpage html template for Emscripten build -added many new sprites to atlas -fixed critical bug with CPU usage in multithreaded mode -snake render tile coresponding to body part -snake is destroyed after collision and emit some particles -added some functionality to vectors -fixed documentation issue in Manager.d -more minor code changes and cleanup
This commit is contained in:
parent
2ddb97e9ce
commit
024356df9b
62 changed files with 5918 additions and 1673 deletions
|
|
@ -3,6 +3,31 @@ precision mediump float;
|
|||
precision lowp sampler2D;
|
||||
precision lowp samplerCube;
|
||||
|
||||
|
||||
#ifdef GLES
|
||||
#define TEX(x,y) texture2D(x,y)
|
||||
#if __VERSION__ >290
|
||||
#define M_IN in mediump
|
||||
#define L_IN in lowp
|
||||
#else
|
||||
#define M_IN varying mediump
|
||||
#define L_IN varying lowp
|
||||
#endif
|
||||
#else
|
||||
#define TEX(x,y) texture(x,y)
|
||||
#if __VERSION__ > 320
|
||||
#define M_IN in
|
||||
#define L_IN in
|
||||
#else
|
||||
#define M_IN varying
|
||||
#define L_IN varying
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
M_IN vec2 uv;
|
||||
M_IN vec4 color;
|
||||
/*
|
||||
#ifdef GLES
|
||||
#if __VERSION__ >290
|
||||
in mediump vec2 uv;
|
||||
|
|
@ -15,7 +40,7 @@ precision lowp samplerCube;
|
|||
#else
|
||||
varying vec2 uv;
|
||||
#endif
|
||||
#endif
|
||||
#endif*/
|
||||
|
||||
//layout(binding = 0)uniform sampler2D tex;
|
||||
|
||||
|
|
@ -23,20 +48,8 @@ uniform sampler2D tex;
|
|||
|
||||
//layout(location = 0) out vec4 outColor;
|
||||
|
||||
void main() {
|
||||
|
||||
#ifdef GLES
|
||||
#if __VERSION__ >290
|
||||
gl_FragColor = texture(tex,uv);
|
||||
#else
|
||||
gl_FragColor = texture2D(tex,uv);
|
||||
#endif
|
||||
#else
|
||||
#if __VERSION__ > 320
|
||||
gl_FragColor = texture(tex,uv);
|
||||
#else
|
||||
gl_FragColor = texture2D(tex,uv);
|
||||
#endif
|
||||
#endif
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = TEX(tex,uv) * color;
|
||||
if(gl_FragColor.a < 0.01)discard;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,37 @@ precision highp float;
|
|||
precision highp int;
|
||||
precision lowp sampler2D;
|
||||
precision lowp samplerCube;
|
||||
|
||||
#ifdef GLES
|
||||
#if __VERSION__ >290
|
||||
layout(location = 0) uniform vec4 matrix_1;
|
||||
layout(location = 1) uniform vec4 matrix_2;
|
||||
layout(location = 2) uniform vec4 uv_transform;
|
||||
#define LOC(x) layout(location = x)
|
||||
#define ATT in
|
||||
#define M_OUT out mediump
|
||||
#define L_OUT out lowp
|
||||
#else
|
||||
#define LOC(x)
|
||||
#define ATT attribute
|
||||
#define M_OUT varying mediump
|
||||
#define L_OUT varying lowp
|
||||
#endif
|
||||
#else
|
||||
#if __VERSION__ > 320
|
||||
#define LOC(x) layout(location = x)
|
||||
#define ATT in
|
||||
#define M_OUT out
|
||||
#define L_OUT out
|
||||
#else
|
||||
#define LOC(x)
|
||||
#define ATT attribute
|
||||
#define M_OUT varying
|
||||
#define L_OUT varying
|
||||
#endif
|
||||
#endif
|
||||
/*
|
||||
#ifdef GLES
|
||||
#if __VERSION__ >290
|
||||
uniform vec4 matrix_1;
|
||||
uniform vec4 matrix_2;
|
||||
uniform vec4 uv_transform;
|
||||
|
||||
layout(location = 0) in vec2 positions;
|
||||
layout(location = 1) in vec2 tex_coords;
|
||||
|
|
@ -43,13 +68,39 @@ precision lowp samplerCube;
|
|||
|
||||
varying vec2 uv;
|
||||
#endif
|
||||
#endif*/
|
||||
|
||||
#define VBO_BATCH 1
|
||||
|
||||
M_OUT vec2 uv;
|
||||
L_OUT vec4 color;
|
||||
|
||||
LOC(0) ATT vec2 positions;
|
||||
LOC(1) ATT vec2 tex_coords;
|
||||
|
||||
#ifdef VBO_BATCH
|
||||
LOC(2) ATT float depth;
|
||||
LOC(3) ATT vec4 vcolor;
|
||||
#else
|
||||
uniform vec4 matrix_1;
|
||||
uniform vec4 matrix_2;
|
||||
uniform vec4 uv_transform;
|
||||
uniform vec4 vcolor;
|
||||
|
||||
float depth = matrix_2.z;
|
||||
#endif
|
||||
|
||||
void main() {
|
||||
#ifdef VBO_BATCH
|
||||
vec3 position = vec3(positions*4.0,1.0);
|
||||
uv = tex_coords;
|
||||
#else
|
||||
vec3 position = mat3(matrix_1.x,matrix_1.y,0,matrix_1.z,matrix_1.w,0,matrix_2.xy,1.0) * vec3(positions,1.0);
|
||||
uv = tex_coords * uv_transform.zw + uv_transform.xy;
|
||||
#endif
|
||||
|
||||
vec3 position = mat3(matrix_1.x,matrix_1.y,0,matrix_1.z,matrix_1.w,0,matrix_2.xy,1) * vec3(positions,1.0);
|
||||
uv = tex_coords * uv_transform.zw + uv_transform.xy;
|
||||
|
||||
gl_Position = vec4(position.xy,0,1.0);
|
||||
color = vcolor * 2.0;
|
||||
|
||||
gl_Position = vec4(position.xy,depth,1.0);
|
||||
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 41 KiB |
|
|
@ -18,6 +18,9 @@
|
|||
"libs-windows-x86_64": ["libs/windows/x64/SDL2","libs/windows/x64/SDL2_Image","libs/windows/x64/cimgui"],
|
||||
"libs-linux-x86_64": ["cimgui","SDL2","SDL2_image"],
|
||||
"lflags-linux-x86_64": ["-rpath=libs/linux/x64/","-Llibs/linux/x64/"],
|
||||
"dflags-ldc" : [
|
||||
"--ffast-math"
|
||||
],
|
||||
"configurations" : [
|
||||
{
|
||||
"name" : "default",
|
||||
|
|
|
|||
77
demos/external/sources/mmutils/thread_pool.d
vendored
77
demos/external/sources/mmutils/thread_pool.d
vendored
|
|
@ -1,6 +1,6 @@
|
|||
module mmutils.thread_pool;
|
||||
|
||||
import ecs.atomic;
|
||||
import bubel.ecs.atomic;
|
||||
|
||||
//import core.stdc.stdio;
|
||||
//import core.stdc.stdlib : free, malloc, realloc;
|
||||
|
|
@ -16,6 +16,7 @@ version (Posix)version = MM_USE_POSIX_THREADS;
|
|||
|
||||
version (WebAssembly)
|
||||
{
|
||||
version = MM_NO_LOGS;
|
||||
extern(C) struct FILE
|
||||
{
|
||||
|
||||
|
|
@ -32,7 +33,7 @@ else
|
|||
|
||||
version (D_BetterC)
|
||||
{
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
extern (C) __gshared int _d_eh_personality(int, int, size_t, void*, void*)
|
||||
{
|
||||
return 0;
|
||||
|
|
@ -181,6 +182,12 @@ void instructionPause()
|
|||
|
||||
__builtin_ia32_pause();
|
||||
}
|
||||
else version(GNU)
|
||||
{
|
||||
import gcc.builtins;
|
||||
|
||||
__builtin_ia32_pause();
|
||||
}
|
||||
else version (DigitalMars)
|
||||
{
|
||||
asm
|
||||
|
|
@ -188,7 +195,6 @@ void instructionPause()
|
|||
rep;
|
||||
nop;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -799,6 +805,7 @@ struct ThreadPool
|
|||
alias FlushLogsDelegaste = void delegate(ThreadData* threadData, JobLog[] logs); /// Type of delegate to flush logs
|
||||
FlushLogsDelegaste onFlushLogs; /// User custom delegate to flush logs, if overriden defaultFlushLogs will be used. Can be sset after initialize() call
|
||||
int logsCacheNum; /// Number of log cache entries. Should be set before setThreadsNum is called
|
||||
int tryWaitCount = 2000; ///Number of times which tryWait are called before timedWait call. Higher value sets better response but takes CPU time even if there are no jobs.
|
||||
private:
|
||||
ThreadData*[gMaxThreadsNum] threadsData; /// Data for threads
|
||||
align(64) shared int threadsNum; /// Number of threads currentlu accepting jobs
|
||||
|
|
@ -808,6 +815,46 @@ private:
|
|||
JobData[4] resumeJobs; /// Dummu jobs to resume some thread
|
||||
|
||||
public:
|
||||
|
||||
static int getCPUCoresCount()
|
||||
{
|
||||
version(Windows)
|
||||
{
|
||||
import core.sys.windows.winbase : SYSTEM_INFO, GetSystemInfo;
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
return sysinfo.dwNumberOfProcessors;
|
||||
}
|
||||
else version (linux)
|
||||
{
|
||||
version(D_BetterC)
|
||||
{
|
||||
import core.sys.posix.unistd : _SC_NPROCESSORS_ONLN, sysconf;
|
||||
return cast(int)sysconf(_SC_NPROCESSORS_ONLN);
|
||||
}
|
||||
else
|
||||
{
|
||||
import core.sys.linux.sched : CPU_COUNT, cpu_set_t, sched_getaffinity;
|
||||
import core.sys.posix.unistd : _SC_NPROCESSORS_ONLN, sysconf;
|
||||
|
||||
cpu_set_t set = void;
|
||||
if (sched_getaffinity(0, cpu_set_t.sizeof, &set) == 0)
|
||||
{
|
||||
int count = CPU_COUNT(&set);
|
||||
if (count > 0)
|
||||
return cast(uint) count;
|
||||
}
|
||||
return cast(int)sysconf(_SC_NPROCESSORS_ONLN);
|
||||
}
|
||||
}
|
||||
else version(Posix)
|
||||
{
|
||||
import core.sys.posix.unistd;
|
||||
return cast(int)sysconf(_SC_NPROCESSORS_ONLN);
|
||||
}
|
||||
else return -1;
|
||||
}
|
||||
|
||||
int jobsDoneCount()
|
||||
{
|
||||
int sum;
|
||||
|
|
@ -1141,17 +1188,19 @@ public:
|
|||
|
||||
foreach (ref log; logs)
|
||||
{
|
||||
size += log.name.length; // size of name
|
||||
size += log.name.length + 1; // size of name
|
||||
}
|
||||
|
||||
char* buffer = cast(char*) malloc(size);
|
||||
|
||||
foreach (ref log; logs)
|
||||
{
|
||||
|
||||
char[100] name_buffer;
|
||||
name_buffer[0 .. log.name.length] = log.name;
|
||||
name_buffer[log.name.length] = 0;
|
||||
size_t charWritten = snprintf(buffer + used, size - used,
|
||||
`{"name":"%s", "pid":1, "tid":%lld, "ph":"X", "ts":%lld, "dur":%lld }, %s`,
|
||||
log.name.ptr, threadData.threadId + 1, log.time, log.duration, "\n".ptr);
|
||||
name_buffer.ptr, threadData.threadId + 1, log.time, log.duration, "\n".ptr);
|
||||
used += charWritten;
|
||||
}
|
||||
|
||||
|
|
@ -1447,7 +1496,21 @@ private void threadFunc(ThreadData* threadData)
|
|||
if (data is null)
|
||||
{
|
||||
// Thread does not have own job and can not steal it, so wait for a job
|
||||
bool ok = threadData.semaphore.timedWait(1_000 + !acceptJobs * 10_000);
|
||||
int tryWait = 0;
|
||||
//bool ok = threadData.semaphore.timedWait(1_000 + !acceptJobs * 10_000);
|
||||
bool ok = true;
|
||||
while(!threadData.semaphore.tryWait())
|
||||
{
|
||||
tryWait++;
|
||||
if(tryWait>threadPool.tryWaitCount)
|
||||
{
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
static foreach(i;0..10)instructionPause();
|
||||
}
|
||||
if(!ok)ok = threadData.semaphore.timedWait(1_000 + !acceptJobs * 10_000);
|
||||
|
||||
if (ok)
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ import cimgui.cimgui;
|
|||
|
||||
import game_core.job_updater;
|
||||
|
||||
import ecs.manager;
|
||||
import ecs.core;
|
||||
import ecs.std;
|
||||
import bubel.ecs.manager;
|
||||
import bubel.ecs.core;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import ecs_utils.gfx.renderer;
|
||||
import ecs_utils.imgui_bind;
|
||||
|
|
@ -58,6 +58,7 @@ struct Launcher
|
|||
uint style = 3;
|
||||
uint entities_count;
|
||||
bool multithreading;
|
||||
int threads = 1;
|
||||
ulong timer_freq;
|
||||
double delta_time;
|
||||
uint fps;
|
||||
|
|
@ -95,6 +96,7 @@ struct Launcher
|
|||
void switchDemo(void function() start, bool function() loop, void function() end, void function(SDL_Event*) event, void function(vec2, Tool, int) tool, const (char)* tips)
|
||||
{
|
||||
gui_manager.clear();
|
||||
//launcher.ent
|
||||
|
||||
if(this.end)this.end();
|
||||
|
||||
|
|
@ -102,6 +104,14 @@ struct Launcher
|
|||
manager.update("clean");
|
||||
manager.end();
|
||||
|
||||
foreach(ref system; manager.systems)
|
||||
{
|
||||
if(system.id != CountSystem.system_id && system.id != CleanSystem.system_id)system.disable();
|
||||
}
|
||||
|
||||
/*launcher.manager.getSystem(CountSystem.system_id).enable();
|
||||
launcher.manager.getSystem(CleanSystem.system_id).enable();//*/
|
||||
|
||||
if(start)start();
|
||||
this.loop = loop;
|
||||
this.end = end;
|
||||
|
|
@ -259,7 +269,6 @@ void mainLoop(void* arg)
|
|||
launcher.repeat_time -= range;
|
||||
launcher.tool((launcher.mouse.position*launcher.scalling)-launcher.render_position, launcher.used_tool, launcher.tool_size);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
version(WebAssembly)
|
||||
|
|
@ -316,6 +325,22 @@ void mainLoop(void* arg)
|
|||
if(igMenuItemBool("Multithreading", null, launcher.multithreading, true))
|
||||
{
|
||||
launcher.multithreading = !launcher.multithreading;
|
||||
if(launcher.multithreading)
|
||||
{
|
||||
launcher.job_updater.pool.setThreadsNum(launcher.threads);
|
||||
}
|
||||
else
|
||||
{
|
||||
launcher.job_updater.pool.setThreadsNum(1);
|
||||
}
|
||||
}
|
||||
igSetNextItemWidth(0);
|
||||
igLabelText("Threads:",null);
|
||||
igSameLine(0,4);
|
||||
if(igSliderInt("##Threads",&launcher.threads, 1, 32, null))//"Multithreading", null, launcher.multithreading, true))
|
||||
{
|
||||
launcher.job_updater.pool.setThreadsNum(launcher.threads);
|
||||
//launcher.threads = !launcher.multithreading;
|
||||
}
|
||||
if(igBeginMenu("Show",true))
|
||||
{
|
||||
|
|
@ -439,7 +464,7 @@ void mainLoop(void* arg)
|
|||
if(launcher.show_demo_wnd)
|
||||
{
|
||||
igSetNextWindowPos(ImVec2(launcher.window_size.x - 260, 30), ImGuiCond_Once, ImVec2(0,0));
|
||||
igSetNextWindowSize(ImVec2(250, 250), ImGuiCond_Once);
|
||||
igSetNextWindowSize(ImVec2(250, 500), ImGuiCond_Once);
|
||||
if(igBegin("Demo",&launcher.show_demo_wnd,0))
|
||||
{
|
||||
ImDrawList* draw_list = igGetWindowDrawList();
|
||||
|
|
@ -539,11 +564,14 @@ void mainLoop(void* arg)
|
|||
launcher.renderer.clear();
|
||||
|
||||
double loop_time = launcher.getTime();
|
||||
launcher.job_updater.pool.tryWaitCount = 10000;
|
||||
if(launcher.loop && !launcher.loop())
|
||||
{
|
||||
quit();
|
||||
*cast(bool*)arg = false;
|
||||
}
|
||||
launcher.job_updater.pool.tryWaitCount = 0;
|
||||
|
||||
loop_time = launcher.getTime() - loop_time;
|
||||
|
||||
double draw_time = launcher.getTime();
|
||||
|
|
@ -686,10 +714,10 @@ int main(int argc, char** argv)
|
|||
|
||||
setStyle(3);
|
||||
|
||||
launcher.job_updater = Mallocator.make!ECSJobUpdater(12);
|
||||
launcher.job_updater = Mallocator.make!ECSJobUpdater(1);
|
||||
//launcher.job_updater.onCreate();
|
||||
|
||||
EntityManager.initialize(12);
|
||||
EntityManager.initialize(32, 1<<16);
|
||||
launcher.manager = EntityManager.instance;
|
||||
|
||||
//launcher.manager.m_thread_id_func = &launcher.job_updater.getThreadID;
|
||||
|
|
@ -709,10 +737,16 @@ int main(int argc, char** argv)
|
|||
|
||||
launcher.renderer.initialize();
|
||||
|
||||
import mmutils.thread_pool : ThreadPool;
|
||||
launcher.threads = ThreadPool.getCPUCoresCount();
|
||||
if(launcher.threads < 2)launcher.threads = 2;
|
||||
|
||||
launcher.gui_manager = Mallocator.make!GUIManager();
|
||||
|
||||
{
|
||||
import demos.simple;
|
||||
import demos.space_invaders;
|
||||
// launcher.switchDemo(&spaceInvadersStart,&spaceInvadersLoop,&spaceInvadersEnd,&spaceInvadersEvent,&spaceInvadersTool,SpaceInvaders.tips);
|
||||
launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,&simpleTool,Simple.tips);
|
||||
}
|
||||
|
||||
|
|
@ -741,6 +775,8 @@ int main(int argc, char** argv)
|
|||
}
|
||||
}
|
||||
|
||||
EntityManager.destroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -785,7 +821,15 @@ void loadGFX()
|
|||
GfxConfig.materials[0].compile();
|
||||
GfxConfig.materials[0].bindAttribLocation("positions",0);
|
||||
GfxConfig.materials[0].bindAttribLocation("tex_coords",1);
|
||||
GfxConfig.materials[0].bindAttribLocation("depth",2);
|
||||
GfxConfig.materials[0].bindAttribLocation("vcolor",3);
|
||||
GfxConfig.materials[0].link();
|
||||
|
||||
/* import std.stdio;
|
||||
writeln("positions ",glGetAttribLocation(GfxConfig.materials[0].data.modules[0].gl_handle,"positions"));
|
||||
writeln("tex_coords ",glGetAttribLocation(GfxConfig.materials[0].data.modules[0].gl_handle,"tex_coords"));
|
||||
writeln("depth ",glGetAttribLocation(GfxConfig.materials[0].data.modules[0].gl_handle,"depth"));
|
||||
writeln("vcolor ",glGetAttribLocation(GfxConfig.materials[0].data.modules[0].gl_handle,"vcolor"));*/
|
||||
|
||||
GfxConfig.materials[0].data.uniforms = Mallocator.makeArray!(Material.Uniform)(3);
|
||||
GfxConfig.materials[0].data.uniforms[0] = Material.Uniform(Material.Type.float4, GfxConfig.materials[0].getLocation("matrix_1"), 0);
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ import bindbc.sdl;
|
|||
|
||||
import cimgui.cimgui;
|
||||
|
||||
import ecs.attributes;
|
||||
import ecs.core;
|
||||
import ecs.entity;
|
||||
import ecs.manager;
|
||||
import ecs.std;
|
||||
import bubel.ecs.attributes;
|
||||
import bubel.ecs.core;
|
||||
import bubel.ecs.entity;
|
||||
import bubel.ecs.manager;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import ecs_utils.gfx.texture;
|
||||
import ecs_utils.math.vector;
|
||||
|
|
|
|||
|
|
@ -1,19 +0,0 @@
|
|||
module demos.chipmunk2d;
|
||||
|
||||
import app;
|
||||
|
||||
import bindbc.sdl;
|
||||
|
||||
import cimgui.cimgui;
|
||||
|
||||
import ecs.attributes;
|
||||
import ecs.core;
|
||||
import ecs.entity;
|
||||
import ecs.manager;
|
||||
import ecs.std;
|
||||
|
||||
import ecs_utils.gfx.texture;
|
||||
import ecs_utils.math.vector;
|
||||
import ecs_utils.utils;
|
||||
|
||||
extern(C):
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
module demos.events;
|
||||
|
||||
import app;
|
||||
|
||||
import bindbc.sdl;
|
||||
|
||||
import cimgui.cimgui;
|
||||
|
||||
import ecs.attributes;
|
||||
import ecs.core;
|
||||
import ecs.entity;
|
||||
import ecs.manager;
|
||||
import ecs.std;
|
||||
|
||||
import ecs_utils.gfx.texture;
|
||||
import ecs_utils.math.vector;
|
||||
import ecs_utils.utils;
|
||||
|
||||
extern(C):
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
module demos.flag_component;
|
||||
|
||||
import app;
|
||||
|
||||
import bindbc.sdl;
|
||||
|
||||
import cimgui.cimgui;
|
||||
|
||||
import ecs.attributes;
|
||||
import ecs.core;
|
||||
import ecs.entity;
|
||||
import ecs.manager;
|
||||
import ecs.std;
|
||||
|
||||
import ecs_utils.gfx.texture;
|
||||
import ecs_utils.math.vector;
|
||||
import ecs_utils.utils;
|
||||
|
||||
extern(C):
|
||||
|
|
@ -6,11 +6,11 @@ import bindbc.sdl;
|
|||
|
||||
import cimgui.cimgui;
|
||||
|
||||
import ecs.attributes;
|
||||
import ecs.core;
|
||||
import ecs.entity;
|
||||
import ecs.manager;
|
||||
import ecs.std;
|
||||
import bubel.ecs.attributes;
|
||||
import bubel.ecs.core;
|
||||
import bubel.ecs.entity;
|
||||
import bubel.ecs.manager;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import ecs_utils.gfx.texture;
|
||||
import ecs_utils.math.vector;
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ import bindbc.sdl;
|
|||
|
||||
import cimgui.cimgui;
|
||||
|
||||
import ecs.attributes;
|
||||
import ecs.core;
|
||||
import ecs.entity;
|
||||
import ecs.manager;
|
||||
import ecs.std;
|
||||
import bubel.ecs.attributes;
|
||||
import bubel.ecs.core;
|
||||
import bubel.ecs.entity;
|
||||
import bubel.ecs.manager;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import ecs_utils.gfx.texture;
|
||||
import ecs_utils.math.vector;
|
||||
|
|
@ -47,22 +47,27 @@ struct CTexture
|
|||
|
||||
struct DrawSystem
|
||||
{
|
||||
mixin ECS.System!1;
|
||||
mixin ECS.System!32;
|
||||
|
||||
struct EntitiesData
|
||||
{
|
||||
uint length;
|
||||
//uint thread_id;
|
||||
uint job_id;
|
||||
@readonly CTexture[] textures;
|
||||
@readonly CLocation[] locations;
|
||||
}
|
||||
|
||||
void onUpdate(EntitiesData data)
|
||||
{
|
||||
if(launcher.renderer.prepared_items >= launcher.renderer.MaxObjects)return;//simple leave loop if max visible objects count was reached
|
||||
foreach(i; 0..data.length)
|
||||
{
|
||||
launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(16,16), vec4(0,0,1,1), 0, 0 , 0);
|
||||
launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(16,16), vec4(0,0,1,1), cast(ushort)(data.locations[i].y), 0x80808080, 0, 0, 0, data.job_id);
|
||||
// launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(16,16), vec4(0,0,1,1), 0, 0x80808080, 0, 0, 0, data.job_id);
|
||||
//draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1));
|
||||
}
|
||||
//if(data.thread_id == 0)launcher.renderer.pushData();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,18 +6,18 @@ import bindbc.sdl;
|
|||
|
||||
import cimgui.cimgui;
|
||||
|
||||
import ecs.attributes;
|
||||
import ecs.core;
|
||||
import ecs.entity;
|
||||
import ecs.manager;
|
||||
import ecs.std;
|
||||
import ecs.vector;
|
||||
import bubel.ecs.attributes;
|
||||
import bubel.ecs.core;
|
||||
import bubel.ecs.entity;
|
||||
import bubel.ecs.manager;
|
||||
import bubel.ecs.std;
|
||||
import bubel.ecs.vector;
|
||||
|
||||
import ecs_utils.gfx.texture;
|
||||
import ecs_utils.math.vector;
|
||||
import ecs_utils.utils;
|
||||
|
||||
import std.array : staticArray;
|
||||
//import std.array : staticArray;
|
||||
|
||||
enum float px = 1.0/512.0;
|
||||
|
||||
|
|
@ -30,8 +30,9 @@ struct MapElement
|
|||
empty = 0,
|
||||
apple = 1,
|
||||
wall = 2,
|
||||
snake = 3,
|
||||
|
||||
snake_head_up = 5,
|
||||
/* snake_head_up = 5,
|
||||
snake_head_down = 6,
|
||||
snake_head_left = 7,
|
||||
snake_head_right = 8,
|
||||
|
|
@ -44,13 +45,31 @@ struct MapElement
|
|||
snake_turn_rd = 15,
|
||||
snake_turn_ru = 16,
|
||||
snake_vertical = 17,
|
||||
snake_horizontal = 18
|
||||
snake_horizontal = 18*/
|
||||
|
||||
}
|
||||
Type type;
|
||||
EntityID id;
|
||||
}
|
||||
|
||||
enum SnakePart : ubyte
|
||||
{
|
||||
head_up = 0,
|
||||
head_down = 1,
|
||||
head_left = 2,
|
||||
head_right = 3,
|
||||
tail_up = 4,
|
||||
tail_down = 5,
|
||||
tail_left = 6,
|
||||
tail_right = 7,
|
||||
turn_ld = 8,
|
||||
turn_lu = 9,
|
||||
turn_rd = 10,
|
||||
turn_ru = 11,
|
||||
vertical = 12,
|
||||
horizontal = 13
|
||||
}
|
||||
|
||||
struct Snake
|
||||
{
|
||||
__gshared const (char)* tips = "Use \"WASD\" keys to move.";
|
||||
|
|
@ -71,6 +90,16 @@ struct Snake
|
|||
|
||||
MapElement[map_size * map_size] map;
|
||||
|
||||
~this() @nogc nothrow
|
||||
{
|
||||
if(snake_destroy_particle_frames)Mallocator.dispose(snake_destroy_particle_frames);
|
||||
if(smoke_frames)Mallocator.dispose(smoke_frames);
|
||||
if(apple_tmpl)launcher.manager.freeTemplate(apple_tmpl);
|
||||
if(snake_tmpl)launcher.manager.freeTemplate(snake_tmpl);
|
||||
if(snake_destroy_particle)launcher.manager.freeTemplate(snake_destroy_particle);
|
||||
texture.destory();
|
||||
}
|
||||
|
||||
MapElement element(ivec2 pos)
|
||||
{
|
||||
uint index = pos.x + pos.y * map_size;
|
||||
|
|
@ -100,43 +129,11 @@ struct Snake
|
|||
}
|
||||
if(base_pos.x == random_pos.x && base_pos.y == random_pos.y)return;
|
||||
}
|
||||
CILocation* location = apple_tmpl.getComponent!CILocation;
|
||||
*location = random_pos;
|
||||
Entity* apple = launcher.manager.addEntity(apple_tmpl);
|
||||
//CILocation* location = apple_tmpl.getComponent!CILocation;
|
||||
//*location = random_pos;
|
||||
//Entity* apple =
|
||||
launcher.manager.addEntity(apple_tmpl,[CILocation(random_pos).ref_].staticArray);
|
||||
}
|
||||
|
||||
void drawMap()
|
||||
{
|
||||
foreach(x; 0 .. map_size)
|
||||
{
|
||||
foreach(y; 0 .. map_size)
|
||||
{
|
||||
switch(element(ivec2(x,y)).type)
|
||||
{
|
||||
case MapElement.Type.apple:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,32*px,16*px,16*px), 0, 0 , 0);break;
|
||||
|
||||
case MapElement.Type.snake_head_up:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(48*px,112*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_head_down:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(48*px,144*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_head_left:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,128*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_head_right:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(32*px,128*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_tail_up:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(16*px,112*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_tail_down:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,112*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_tail_left:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(32*px,112*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_tail_right:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,144*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_turn_ld:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(64*px,128*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_turn_lu:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(32*px,144*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_turn_rd:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(16*px,144*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_turn_ru:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(64*px,112*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_vertical:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(16*px,128*px,16*px,16*px), 0, 0 , 0);break;
|
||||
case MapElement.Type.snake_horizontal:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(48*px,128*px,16*px,16*px), 0, 0 , 0);break;
|
||||
|
||||
case MapElement.Type.wall:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,0,1,1), 0, 0 , 0);break;
|
||||
default:break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct Animation
|
||||
|
|
@ -184,6 +181,7 @@ struct CSnake
|
|||
|
||||
mixin ECS.Component;
|
||||
|
||||
|
||||
struct Parts
|
||||
{
|
||||
uint length = 0;
|
||||
|
|
@ -217,6 +215,7 @@ struct CSnake
|
|||
}
|
||||
|
||||
Parts parts;
|
||||
CMovement.Direction direction;
|
||||
}
|
||||
|
||||
struct CApple
|
||||
|
|
@ -287,7 +286,7 @@ struct ParticleSystem
|
|||
{
|
||||
uint length;
|
||||
@readonly Entity[] entities;
|
||||
@readonly CParticle[] particle;
|
||||
CParticle[] particle;
|
||||
}
|
||||
|
||||
void onUpdate(EntitiesData data)
|
||||
|
|
@ -358,7 +357,7 @@ struct AnimationRenderSystem
|
|||
{
|
||||
foreach(i;0..data.length)
|
||||
{
|
||||
launcher.renderer.draw(snake.texture, cast(vec2)cast(ivec2)data.location[i].location, vec2(16,16), data.animation[i].frames[cast(int)(data.animation[i].time)], 0, 0 , 0);
|
||||
launcher.renderer.draw(snake.texture, cast(vec2)cast(ivec2)data.location[i].location, vec2(16,16), data.animation[i].frames[cast(int)(data.animation[i].time)], -1, 0x80808080);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -368,8 +367,8 @@ struct MoveSystem
|
|||
mixin ECS.System!64;
|
||||
|
||||
EntityTemplate* destroy_template;
|
||||
CLocation* destroy_location;
|
||||
CParticleVector* destroy_vector;
|
||||
//CLocation* destroy_location;
|
||||
//CParticleVector* destroy_vector;
|
||||
|
||||
struct EntitiesData
|
||||
{
|
||||
|
|
@ -383,8 +382,8 @@ struct MoveSystem
|
|||
void setTemplates()
|
||||
{
|
||||
destroy_template = snake.snake_destroy_particle;
|
||||
destroy_location = destroy_template.getComponent!CLocation;
|
||||
destroy_vector = destroy_template.getComponent!CParticleVector;
|
||||
//destroy_location = destroy_template.getComponent!CLocation;
|
||||
//destroy_vector = destroy_template.getComponent!CParticleVector;
|
||||
}
|
||||
|
||||
void moveLocation(ref CILocation location, CMovement.Direction direction)
|
||||
|
|
@ -424,133 +423,56 @@ struct MoveSystem
|
|||
else .snake.element(MapElement(),location);
|
||||
}
|
||||
|
||||
static CMovement.Direction getDirection(ivec2 p1, ivec2 p2)
|
||||
{
|
||||
if(p1.x - p2.x == -1)return CMovement.direction.right;
|
||||
else if(p1.x - p2.x == 1)return CMovement.direction.left;
|
||||
else if(p1.y - p2.y == -1)return CMovement.direction.up;
|
||||
else if(p1.y - p2.y == 1)return CMovement.direction.down;
|
||||
else if(p1.x - p2.x > 1)return CMovement.direction.right;
|
||||
else if(p1.x - p2.x < -1)return CMovement.direction.left;
|
||||
else if(p1.y - p2.y > 1)return CMovement.direction.up;
|
||||
else return CMovement.direction.down;
|
||||
}
|
||||
|
||||
static MapElement.Type snakePart(ivec2 p1, ivec2 p2, ivec2 p3)
|
||||
{
|
||||
CMovement.Direction direction = getDirection(p1, p2);
|
||||
CMovement.Direction direction2 = getDirection(p1, p3);
|
||||
uint case_ = direction*4 + direction2;
|
||||
final switch(case_)
|
||||
{
|
||||
case 0:return MapElement.Type.snake_horizontal;
|
||||
case 1:return MapElement.Type.snake_horizontal;
|
||||
case 2:return MapElement.Type.snake_turn_lu;
|
||||
case 3:return MapElement.Type.snake_turn_ru;
|
||||
case 4:return MapElement.Type.snake_horizontal;
|
||||
case 5:return MapElement.Type.snake_horizontal;
|
||||
case 6:return MapElement.Type.snake_turn_ld;
|
||||
case 7:return MapElement.Type.snake_turn_rd;
|
||||
case 8:return MapElement.Type.snake_turn_lu;
|
||||
case 9:return MapElement.Type.snake_turn_ld;
|
||||
case 10:return MapElement.Type.snake_vertical;
|
||||
case 11:return MapElement.Type.snake_vertical;
|
||||
case 12:return MapElement.Type.snake_turn_ru;
|
||||
case 13:return MapElement.Type.snake_turn_rd;
|
||||
case 14:return MapElement.Type.snake_vertical;
|
||||
case 15:return MapElement.Type.snake_vertical;
|
||||
}
|
||||
}
|
||||
|
||||
static MapElement.Type snakeTail(ivec2 p1, ivec2 p2)
|
||||
{
|
||||
CMovement.Direction direction = getDirection(p1, p2);
|
||||
final switch(direction)
|
||||
{
|
||||
case CMovement.Direction.up:return MapElement.Type.snake_tail_up;
|
||||
case CMovement.Direction.down:return MapElement.Type.snake_tail_down;
|
||||
case CMovement.Direction.left:return MapElement.Type.snake_tail_left;
|
||||
case CMovement.Direction.right:return MapElement.Type.snake_tail_right;
|
||||
}
|
||||
}
|
||||
|
||||
void onUpdate(EntitiesData data)
|
||||
{
|
||||
if(data.snakes)
|
||||
{
|
||||
foreach(i; 0..data.length)
|
||||
{
|
||||
data.snakes[i].direction = data.movement[i].direction;
|
||||
ivec2 new_location = data.location[i];
|
||||
moveLocation(data.location[i], data.movement[i].direction);
|
||||
final switch(snake.element(data.location[i].location).type)
|
||||
{
|
||||
case MapElement.Type.snake_head_up:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_head_down:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_head_left:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_head_right:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_tail_up:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_tail_down:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_tail_left:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_tail_right:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_turn_ld:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_turn_lu:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_turn_rd:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_turn_ru:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_vertical:goto case(MapElement.Type.snake_horizontal);
|
||||
case MapElement.Type.snake_horizontal:
|
||||
foreach(ivec2 loc; data.snakes[i].parts)
|
||||
case MapElement.Type.snake:
|
||||
foreach(loc; data.snakes[i].parts)
|
||||
{
|
||||
destroy_location.x = loc.x * 16;
|
||||
destroy_location.y = loc.y * 16;
|
||||
//destroy_location.x = loc.x * 16;
|
||||
//destroy_location.y = loc.y * 16;
|
||||
snake.element(MapElement(MapElement.Type.empty, EntityID()),loc);
|
||||
launcher.manager.addEntity(snake.snake_destroy_particle);
|
||||
launcher.manager.addEntity(snake.snake_destroy_particle,[CLocation(cast(vec2)(loc * 16)).ref_].staticArray);
|
||||
|
||||
CLocation destroy_location;
|
||||
foreach(j;0..10)
|
||||
{
|
||||
destroy_location.x = loc.x * 16 + randomf() * 8 - 4;
|
||||
destroy_location.y = loc.y * 16 + randomf() * 8 - 4;
|
||||
destroy_vector.velocity = vec2(randomf(),randomf())*0.4-0.2;
|
||||
//destroy_vector.velocity = vec2(randomf(),randomf())*0.4-0.2;
|
||||
snake.element(MapElement(MapElement.Type.empty, EntityID()),loc);
|
||||
launcher.manager.addEntity(snake.snake_destroy_particle);
|
||||
launcher.manager.addEntity(snake.snake_destroy_particle, [destroy_location.ref_, CParticleVector(vec2(randomf(),randomf())*0.4-0.2).ref_].staticArray);
|
||||
}
|
||||
|
||||
}
|
||||
destroy_location.x = new_location.x * 16;
|
||||
destroy_location.y = new_location.y * 16;
|
||||
//destroy_location.x = new_location.x * 16;
|
||||
//destroy_location.y = new_location.y * 16;
|
||||
snake.element(MapElement(MapElement.Type.empty, EntityID()),new_location);
|
||||
launcher.manager.addEntity(snake.snake_destroy_particle);
|
||||
launcher.manager.addEntity(snake.snake_destroy_particle,[CLocation(cast(vec2)(new_location * 16)).ref_].staticArray);
|
||||
launcher.manager.removeEntity(data.entities[i].id);
|
||||
break;
|
||||
|
||||
case MapElement.Type.wall:break;
|
||||
//launcher.manager.removeEntity(data.entities[i].id);
|
||||
//break;
|
||||
|
||||
case MapElement.Type.empty:
|
||||
moveSnake(data.snakes[i], new_location);
|
||||
final switch(data.movement[i].direction)
|
||||
{
|
||||
case CMovement.Direction.up:
|
||||
snake.element(MapElement(MapElement.Type.snake_head_up, data.entities[i].id),data.location[i].location);
|
||||
break;
|
||||
case CMovement.Direction.right:
|
||||
snake.element(MapElement(MapElement.Type.snake_head_right, data.entities[i].id),data.location[i].location);
|
||||
break;
|
||||
case CMovement.Direction.down:
|
||||
snake.element(MapElement(MapElement.Type.snake_head_down, data.entities[i].id),data.location[i].location);
|
||||
break;
|
||||
case CMovement.Direction.left:
|
||||
snake.element(MapElement(MapElement.Type.snake_head_left, data.entities[i].id),data.location[i].location);
|
||||
break;
|
||||
}
|
||||
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].location);
|
||||
if(data.snakes[i].parts.length > 1)
|
||||
{
|
||||
MapElement.Type elem_type = snakePart(data.snakes[i].parts[$-1],data.location[i],data.snakes[i].parts[$-2]);
|
||||
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[$-1]);
|
||||
elem_type = snakeTail(data.snakes[i].parts[1], data.snakes[i].parts[0]);
|
||||
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[0]);
|
||||
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[$-1]);
|
||||
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[0]);
|
||||
}
|
||||
else if(data.snakes[i].parts.length == 1)
|
||||
{
|
||||
MapElement.Type elem_type = snakeTail(data.location[i], data.snakes[i].parts[0]);
|
||||
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[0]);
|
||||
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[0]);
|
||||
}
|
||||
break;
|
||||
case MapElement.Type.apple:
|
||||
|
|
@ -559,29 +481,13 @@ struct MoveSystem
|
|||
|
||||
if(data.snakes[i].parts.length > 1)
|
||||
{
|
||||
MapElement.Type elem_type = snakePart(data.snakes[i].parts[$-1],data.location[i],data.snakes[i].parts[$-2]);
|
||||
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[$-1]);
|
||||
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[$-1]);
|
||||
}
|
||||
else if(data.snakes[i].parts.length == 1)
|
||||
{
|
||||
MapElement.Type elem_type = snakeTail(data.location[i], new_location);
|
||||
snake.element(MapElement(elem_type, data.entities[i].id),new_location);
|
||||
}
|
||||
final switch(data.movement[i].direction)
|
||||
{
|
||||
case CMovement.Direction.up:
|
||||
snake.element(MapElement(MapElement.Type.snake_head_up, data.entities[i].id),data.location[i].location);
|
||||
break;
|
||||
case CMovement.Direction.right:
|
||||
snake.element(MapElement(MapElement.Type.snake_head_right, data.entities[i].id),data.location[i].location);
|
||||
break;
|
||||
case CMovement.Direction.down:
|
||||
snake.element(MapElement(MapElement.Type.snake_head_down, data.entities[i].id),data.location[i].location);
|
||||
break;
|
||||
case CMovement.Direction.left:
|
||||
snake.element(MapElement(MapElement.Type.snake_head_left, data.entities[i].id),data.location[i].location);
|
||||
break;
|
||||
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),new_location);
|
||||
}
|
||||
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].location);
|
||||
snake.addApple();
|
||||
break;
|
||||
}
|
||||
|
|
@ -593,10 +499,10 @@ struct MoveSystem
|
|||
{
|
||||
final switch(data.movement[i].direction)
|
||||
{
|
||||
case CMovement.Direction.down:data.location[i].location.y -= 1;break;
|
||||
case CMovement.Direction.up:data.location[i].location.y += 1;break;
|
||||
case CMovement.Direction.left:data.location[i].location.x -= 1;break;
|
||||
case CMovement.Direction.right:data.location[i].location.x += 1;break;
|
||||
case CMovement.Direction.down:data.location[i].y -= 1;break;
|
||||
case CMovement.Direction.up:data.location[i].y += 1;break;
|
||||
case CMovement.Direction.left:data.location[i].x -= 1;break;
|
||||
case CMovement.Direction.right:data.location[i].x += 1;break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -699,6 +605,132 @@ struct FixSnakeDirectionSystem
|
|||
}
|
||||
}
|
||||
|
||||
struct DrawAppleSystem
|
||||
{
|
||||
mixin ECS.System!1;
|
||||
|
||||
struct EntitiesData
|
||||
{
|
||||
uint length;
|
||||
@readonly CILocation[] location;
|
||||
const (CApple)[] apple;
|
||||
}
|
||||
|
||||
void onUpdate(EntitiesData data)
|
||||
{
|
||||
foreach(i; 0..data.location.length)
|
||||
{
|
||||
launcher.renderer.draw(snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,32*px,16*px,16*px), 0, 0x80808080, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DrawSnakeSystem
|
||||
{
|
||||
mixin ECS.System!1;
|
||||
|
||||
struct EntitiesData
|
||||
{
|
||||
uint length;
|
||||
@readonly CILocation[] location;
|
||||
const (CSnake)[] snake;
|
||||
}
|
||||
|
||||
static CMovement.Direction getDirection(ivec2 p1, ivec2 p2)
|
||||
{
|
||||
if(p1.x - p2.x == -1)return CMovement.direction.right;
|
||||
else if(p1.x - p2.x == 1)return CMovement.direction.left;
|
||||
else if(p1.y - p2.y == -1)return CMovement.direction.up;
|
||||
else if(p1.y - p2.y == 1)return CMovement.direction.down;
|
||||
else if(p1.x - p2.x > 1)return CMovement.direction.right;
|
||||
else if(p1.x - p2.x < -1)return CMovement.direction.left;
|
||||
else if(p1.y - p2.y > 1)return CMovement.direction.up;
|
||||
else return CMovement.direction.down;
|
||||
}
|
||||
|
||||
static SnakePart snakePart(ivec2 p1, ivec2 p2, ivec2 p3)
|
||||
{
|
||||
CMovement.Direction direction = getDirection(p1, p2);
|
||||
CMovement.Direction direction2 = getDirection(p1, p3);
|
||||
uint case_ = direction*4 + direction2;
|
||||
final switch(case_)
|
||||
{
|
||||
case 0:return SnakePart.horizontal;
|
||||
case 1:return SnakePart.horizontal;
|
||||
case 2:return SnakePart.turn_lu;
|
||||
case 3:return SnakePart.turn_ru;
|
||||
case 4:return SnakePart.horizontal;
|
||||
case 5:return SnakePart.horizontal;
|
||||
case 6:return SnakePart.turn_ld;
|
||||
case 7:return SnakePart.turn_rd;
|
||||
case 8:return SnakePart.turn_lu;
|
||||
case 9:return SnakePart.turn_ld;
|
||||
case 10:return SnakePart.vertical;
|
||||
case 11:return SnakePart.vertical;
|
||||
case 12:return SnakePart.turn_ru;
|
||||
case 13:return SnakePart.turn_rd;
|
||||
case 14:return SnakePart.vertical;
|
||||
case 15:return SnakePart.vertical;
|
||||
}
|
||||
}
|
||||
|
||||
static SnakePart snakeTail(ivec2 p1, ivec2 p2)
|
||||
{
|
||||
CMovement.Direction direction = getDirection(p1, p2);
|
||||
final switch(direction)
|
||||
{
|
||||
case CMovement.Direction.up:return SnakePart.tail_up;
|
||||
case CMovement.Direction.down:return SnakePart.tail_down;
|
||||
case CMovement.Direction.left:return SnakePart.tail_left;
|
||||
case CMovement.Direction.right:return SnakePart.tail_right;
|
||||
}
|
||||
}
|
||||
|
||||
static void drawElement(ivec2 loc, SnakePart part)
|
||||
{
|
||||
final switch(cast(ubyte)part)
|
||||
{
|
||||
case SnakePart.tail_up:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,112,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case SnakePart.tail_down:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,112,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case SnakePart.tail_left:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,112,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case SnakePart.tail_right:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,144,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case SnakePart.turn_ld:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,128,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case SnakePart.turn_lu:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,144,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case SnakePart.turn_rd:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,144,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case SnakePart.turn_ru:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,112,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case SnakePart.vertical:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,128,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case SnakePart.horizontal:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(48,128,16,16)*px, 0, 0x80808080, 0);break;
|
||||
}
|
||||
}
|
||||
|
||||
void onUpdate(EntitiesData data)
|
||||
{
|
||||
foreach(i; 0..data.length)
|
||||
{
|
||||
const (CSnake)* snake = &data.snake[i];
|
||||
scope vec2 loc = cast(vec2)(data.location[i].location * 16);
|
||||
final switch(snake.direction)
|
||||
{
|
||||
case CMovement.Direction.up:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,112,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case CMovement.Direction.down:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,144,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case CMovement.Direction.left:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,128,16,16)*px, 0, 0x80808080, 0);break;
|
||||
case CMovement.Direction.right:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(32,128,16,16)*px, 0, 0x80808080, 0);break;
|
||||
}
|
||||
if(snake.parts.length >1)
|
||||
{
|
||||
foreach(j;1..snake.parts.length - 1)drawElement(snake.parts[j]*16, snakePart(snake.parts[j], snake.parts[j+1], snake.parts[j-1]));
|
||||
drawElement(snake.parts[$-1]*16, snakePart(snake.parts[$-1], data.location[i], snake.parts[$-2]));
|
||||
drawElement(snake.parts[0]*16, snakeTail(snake.parts[1], snake.parts[0]));
|
||||
}
|
||||
else if(snake.parts.length == 1)
|
||||
{
|
||||
drawElement(snake.parts[0]*16, snakeTail(data.location[i], snake.parts[0]));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CleanSystem
|
||||
{
|
||||
mixin ECS.System!64;
|
||||
|
|
@ -749,6 +781,8 @@ void snakeStart()
|
|||
launcher.manager.registerSystem!AnimationSystem(-1);
|
||||
launcher.manager.registerSystem!ParticleSystem(-1);
|
||||
launcher.manager.registerSystem!ParticleMovementSystem(-1);
|
||||
launcher.manager.registerSystem!DrawAppleSystem(99);
|
||||
launcher.manager.registerSystem!DrawSnakeSystem(101);
|
||||
|
||||
launcher.manager.endRegister();
|
||||
|
||||
|
|
@ -765,9 +799,9 @@ void snakeStart()
|
|||
{
|
||||
ushort[4] components = [CILocation.component_id, CSnake.component_id, CMovement.component_id, CInput.component_id];
|
||||
snake.snake_tmpl = launcher.manager.allocateTemplate(components);
|
||||
CILocation* loc_comp = snake.snake_tmpl.getComponent!CILocation;
|
||||
loc_comp.location = ivec2(2,2);
|
||||
launcher.manager.addEntity(snake.snake_tmpl);
|
||||
//CILocation* loc_comp = snake.snake_tmpl.getComponent!CILocation;
|
||||
//*loc_comp = ivec2(2,2);
|
||||
launcher.manager.addEntity(snake.snake_tmpl,[CILocation(ivec2(2,2)).ref_].staticArray);
|
||||
}
|
||||
|
||||
{
|
||||
|
|
@ -784,9 +818,9 @@ void snakeStart()
|
|||
snake.addApple();
|
||||
}
|
||||
|
||||
launcher.gui_manager.addTemplate(snake.snake_tmpl, "Snake");
|
||||
launcher.gui_manager.addTemplate(snake.apple_tmpl, "Apple");
|
||||
launcher.gui_manager.addTemplate(snake.snake_destroy_particle, "Particle");
|
||||
launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(snake.snake_tmpl), "Snake");
|
||||
launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(snake.apple_tmpl), "Apple");
|
||||
launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(snake.snake_destroy_particle), "Particle");
|
||||
|
||||
MoveSystem* move_system = launcher.manager.getSystem!MoveSystem();
|
||||
move_system.setTemplates();
|
||||
|
|
@ -794,7 +828,7 @@ void snakeStart()
|
|||
/*foreach(i; 0..10)
|
||||
foreach(j; 0..10)
|
||||
{
|
||||
loc_comp.location = vec2(i*32+64,j*32+64);
|
||||
loc_compation = vec2(i*32+64,j*32+64);
|
||||
launcher.manager.addEntity(simple.tmpl);
|
||||
}*/
|
||||
}
|
||||
|
|
@ -815,15 +849,15 @@ void snakeTool(vec2 position, Tool tool, int size)
|
|||
CLocation* location = tmpl.getComponent!CLocation;
|
||||
if(location)
|
||||
{
|
||||
position.x += (randomf - 0.5) * size;
|
||||
position.y += (randomf - 0.5) * size;
|
||||
position.x += (randomf() - 0.5) * size;
|
||||
position.y += (randomf() - 0.5) * size;
|
||||
*location = position;
|
||||
}
|
||||
CILocation* ilocation = tmpl.getComponent!CILocation;
|
||||
if(ilocation)
|
||||
{
|
||||
position.x += (randomf - 0.5) * size;
|
||||
position.y += (randomf - 0.5) * size;
|
||||
position.x += (randomf() - 0.5) * size;
|
||||
position.y += (randomf() - 0.5) * size;
|
||||
ivec2 ipos;
|
||||
ipos.x = cast(int)(position.x / 16);
|
||||
ipos.y = cast(int)(position.y / 16);
|
||||
|
|
@ -878,7 +912,7 @@ bool snakeLoop()
|
|||
|
||||
launcher.manager.end();
|
||||
|
||||
snake.drawMap();
|
||||
//snake.drawMap();
|
||||
|
||||
return true;
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,13 +1,13 @@
|
|||
module game_core.job_updater;
|
||||
|
||||
import ecs.std;
|
||||
import ecs.vector;
|
||||
import ecs.atomic;
|
||||
import bubel.ecs.std;
|
||||
import bubel.ecs.vector;
|
||||
import bubel.ecs.atomic;
|
||||
|
||||
import ecs_utils.utils;
|
||||
|
||||
//import core.time;
|
||||
import ecs.manager;
|
||||
import bubel.ecs.manager;
|
||||
import mmutils.thread_pool;
|
||||
|
||||
version(LDC)
|
||||
|
|
@ -63,6 +63,7 @@ struct ECSJobUpdater
|
|||
JobData[1024] jobs;
|
||||
JobCaller[1024] callers;
|
||||
uint count = 0;
|
||||
string name;
|
||||
|
||||
void dependantOn(Group* dependency)
|
||||
{
|
||||
|
|
@ -89,7 +90,7 @@ struct ECSJobUpdater
|
|||
void add(JobCaller caller)
|
||||
{
|
||||
callers[count] = caller;
|
||||
jobs[count] = JobData(&callers[count].callJob,"hmm");
|
||||
jobs[count] = JobData(&callers[count].callJob,name);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
|
@ -216,6 +217,8 @@ struct ECSJobUpdater
|
|||
return;
|
||||
}
|
||||
|
||||
jobs[group.id].name = cast(string)group.caller.system.name;
|
||||
|
||||
foreach(ref job;group.jobs)
|
||||
{
|
||||
uint index = 0;
|
||||
|
|
@ -233,7 +236,7 @@ struct ECSJobUpdater
|
|||
|
||||
foreach(dep;group.dependencies)
|
||||
{
|
||||
if(jobs[dep.id].count && dep.caller.system.execute && dep.caller.system.enabled)jobs[group.id].dependantOn(&jobs[dep.id]);
|
||||
if(jobs[dep.id].count && dep.caller.system.willExecute && dep.caller.system.enabled)jobs[group.id].dependantOn(&jobs[dep.id]);
|
||||
else deps--;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ import app;
|
|||
|
||||
import cimgui.cimgui;
|
||||
|
||||
import ecs.std;
|
||||
import ecs.system;
|
||||
import ecs.vector;
|
||||
import ecs.entity;
|
||||
import bubel.ecs.std;
|
||||
import bubel.ecs.system;
|
||||
import bubel.ecs.vector;
|
||||
import bubel.ecs.entity;
|
||||
|
||||
import gui.system;
|
||||
import gui.template_;
|
||||
|
|
@ -30,6 +30,7 @@ struct GUIManager
|
|||
|
||||
systems.clear();
|
||||
templates.clear();
|
||||
selected_tempalte = 0;
|
||||
}
|
||||
|
||||
EntityTemplate* getSelectedTemplate()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
module gui.system;
|
||||
|
||||
import ecs.system;
|
||||
import bubel.ecs.system;
|
||||
|
||||
struct SystemGUI
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
module gui.template_;
|
||||
|
||||
import ecs.entity;
|
||||
import bubel.ecs.entity;
|
||||
|
||||
struct TemplateGUI
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
module ecs_utils.gfx.buffer;
|
||||
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import glad.gl.gl;
|
||||
import glad.gl.gles2;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ module ecs_utils.gfx.config;
|
|||
|
||||
import bindbc.sdl;
|
||||
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import ecs_utils.gfx.material;
|
||||
import ecs_utils.gfx.mesh;
|
||||
|
|
@ -18,7 +18,7 @@ enum LayerType
|
|||
sorted
|
||||
}
|
||||
|
||||
import ecs.vector;
|
||||
import bubel.ecs.vector;
|
||||
|
||||
static struct GfxConfig
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ module ecs_utils.gfx.material;
|
|||
|
||||
import bindbc.sdl;
|
||||
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import ecs_utils.gfx.shader;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ module ecs_utils.gfx.mesh;
|
|||
|
||||
import bindbc.sdl;
|
||||
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import ecs_utils.gfx.buffer;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,14 +2,17 @@ module ecs_utils.gfx.renderer;
|
|||
|
||||
import bindbc.sdl;
|
||||
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
|
||||
//import ecs_utils.core : Backend;
|
||||
import ecs_utils.gfx.buffer;
|
||||
import ecs_utils.gfx.texture;
|
||||
import ecs_utils.math.vector;
|
||||
|
||||
import glad.gl.gl;
|
||||
import bubel.ecs.block_allocator;
|
||||
import bubel.ecs.vector;
|
||||
version(WebAssembly)import glad.gl.gles2;
|
||||
else import glad.gl.gl;
|
||||
|
||||
version = ver1;
|
||||
/*version(ver5)version = vv2;
|
||||
|
|
@ -41,9 +44,10 @@ enum RenderTechnique
|
|||
struct Renderer
|
||||
{
|
||||
//static SDL_Renderer* main_sdl_renderer;
|
||||
BlockAllocator allocator;
|
||||
|
||||
enum MaxObjects = 1024 * 64 * 4;
|
||||
enum BufferUsage = GL_STATIC_DRAW;
|
||||
enum BufferUsage = GL_DYNAMIC_DRAW;
|
||||
|
||||
//SDL_Window* sdl_window;
|
||||
//SDL_Renderer* sdl_renderer;
|
||||
|
|
@ -53,8 +57,136 @@ struct Renderer
|
|||
vec2 view_pos = vec2(-1,-1);
|
||||
vec2 view_size = vec2(1,1);
|
||||
|
||||
enum block_size = 2^^16;
|
||||
enum batch_size = block_size/68;//963;//16_384;
|
||||
//uint[2] time_queries;
|
||||
|
||||
struct VertexBlock
|
||||
{
|
||||
enum max_items = batch_size;//963;
|
||||
byte[] batch_vertices;
|
||||
ushort[] batch_indices;
|
||||
void* memory;
|
||||
uint items = 0;
|
||||
}
|
||||
|
||||
Mutex* get_block_mutex;
|
||||
Mutex* block_stack_mutex;
|
||||
|
||||
VertexBlock getBlock()
|
||||
{
|
||||
VertexBlock block;
|
||||
get_block_mutex.lock();
|
||||
block.memory = allocator.getBlock();
|
||||
get_block_mutex.unlock();
|
||||
block.batch_vertices = (cast(byte*)block.memory)[0 .. VertexBlock.max_items * 4 * 14];
|
||||
block.batch_indices = (cast(ushort*)block.memory)[VertexBlock.max_items * 4 * 7 .. VertexBlock.max_items * (4 * 7 + 6)];
|
||||
return block;
|
||||
}
|
||||
|
||||
Vector!VertexBlock blocks;
|
||||
uint current_block = 0;
|
||||
uint render_blocks = 0;
|
||||
|
||||
void pushBlock(VertexBlock block)
|
||||
{
|
||||
block_stack_mutex.lock();
|
||||
prepared_items += block.items;
|
||||
blocks.add(block);
|
||||
render_blocks++;
|
||||
block_stack_mutex.unlock();
|
||||
}
|
||||
|
||||
bool isRemainingBlocks()
|
||||
{
|
||||
if(render_blocks <= current_block)return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
VertexBlock fetchBlock()
|
||||
{
|
||||
block_stack_mutex.lock();
|
||||
VertexBlock block = blocks[current_block];
|
||||
current_block++;
|
||||
block_stack_mutex.unlock();
|
||||
return block;
|
||||
}
|
||||
|
||||
void freeBlocks()
|
||||
{
|
||||
/*block_stack_mutex.lock();
|
||||
render_blocks = 0;
|
||||
current_block = 0;
|
||||
foreach(VertexBlock block; blocks)
|
||||
{
|
||||
allocator.freeBlock(block.memory);
|
||||
}
|
||||
blocks.clear;
|
||||
prepared_items=0;
|
||||
draw_list.clear();
|
||||
block_stack_mutex.unlock();*/
|
||||
foreach(ref Thread thread; threads)
|
||||
{
|
||||
foreach(VertexBlock block; thread.blocks)
|
||||
{
|
||||
allocator.freeBlock(block.memory);
|
||||
}
|
||||
thread.blocks.clear();
|
||||
}
|
||||
render_blocks = 0;
|
||||
current_block = 0;
|
||||
prepared_items = 0;
|
||||
draw_list.clear();
|
||||
}
|
||||
|
||||
void pushData()
|
||||
{
|
||||
foreach(ref Thread thread; threads)
|
||||
{
|
||||
foreach(VertexBlock block; thread.blocks)
|
||||
{
|
||||
uint items = block.items;
|
||||
if(items + item_id >= MaxObjects)items = MaxObjects - item_id;
|
||||
batch_vbo[0].bufferSubData(Buffer.BindTarget.array,items*4*14,item_id*4*14,block.batch_vertices.ptr);
|
||||
batch_ibo[0].bufferSubData(Buffer.BindTarget.element_array,items*2*6,item_id*2*6,block.batch_indices.ptr);
|
||||
draw_list.add(DrawCall(item_id,items));
|
||||
item_id += items;
|
||||
}
|
||||
//thread.blocks.clear();
|
||||
}
|
||||
//if(!isRemainingBlocks())return;
|
||||
/* while(isRemainingBlocks())
|
||||
{
|
||||
VertexBlock block = fetchBlock();
|
||||
uint items = block.items;
|
||||
if(items + item_id >= MaxObjects)items = MaxObjects - item_id;
|
||||
batch_vbo[0].bufferSubData(Buffer.BindTarget.array,items*4*14,item_id*4*14,block.batch_vertices.ptr);
|
||||
batch_ibo[0].bufferSubData(Buffer.BindTarget.element_array,items*2*6,item_id*2*6,block.batch_indices.ptr);
|
||||
draw_list.add(DrawCall(item_id,items));
|
||||
item_id += items;
|
||||
}*/
|
||||
}
|
||||
|
||||
void pushThreadsBlocks()
|
||||
{
|
||||
foreach(i, ref Thread thread; threads)
|
||||
{
|
||||
//pushBlock(thread.block);
|
||||
thread.blocks.add(thread.block);
|
||||
thread.block = getBlock();
|
||||
}
|
||||
}
|
||||
|
||||
struct Thread
|
||||
{
|
||||
//Vector!VertexBlock block;
|
||||
RenderData[] render_list;
|
||||
VertexBlock block;
|
||||
Vector!VertexBlock blocks;
|
||||
}
|
||||
Thread[] threads;
|
||||
|
||||
|
||||
Buffer[2] ubos;
|
||||
int block_alignment = 1;
|
||||
int block_max_size = 16384;
|
||||
|
|
@ -71,7 +203,7 @@ struct Renderer
|
|||
Buffer[2] batch_vbo;
|
||||
Buffer[2] batch_ibo;
|
||||
|
||||
float[] batch_vertices;
|
||||
ubyte[] batch_vertices;
|
||||
ushort[] batch_indices;
|
||||
|
||||
Buffer indirect_buffer;
|
||||
|
|
@ -90,8 +222,17 @@ struct Renderer
|
|||
uint mesh_id;
|
||||
}
|
||||
|
||||
struct DrawCall
|
||||
{
|
||||
uint start;
|
||||
uint count;
|
||||
}
|
||||
|
||||
Vector!DrawCall draw_list;
|
||||
|
||||
RenderData[] render_list;
|
||||
uint item_id;
|
||||
uint prepared_items;
|
||||
|
||||
uint[] multi_count;
|
||||
uint[] multi_offset;
|
||||
|
|
@ -109,6 +250,18 @@ struct Renderer
|
|||
{
|
||||
//this.technique = __ecs_used_technique;
|
||||
__initialize(this);
|
||||
|
||||
get_block_mutex = Mallocator.make!Mutex();
|
||||
block_stack_mutex = Mallocator.make!Mutex();
|
||||
get_block_mutex.initialize();
|
||||
block_stack_mutex.initialize();
|
||||
|
||||
|
||||
threads = Mallocator.makeArray!Thread(32);
|
||||
foreach(ref Thread thread;threads)
|
||||
{
|
||||
thread.block = getBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private static void __initialize_gl(ref Renderer this_)
|
||||
|
|
@ -141,16 +294,16 @@ struct Renderer
|
|||
case Technique.vbo_batch:
|
||||
batch_vbo[0].create();
|
||||
batch_ibo[0].create();
|
||||
batch_vbo[0].bufferData(Buffer.BindTarget.array,16,4*MaxObjects,BufferUsage,null);
|
||||
batch_vbo[0].bufferData(Buffer.BindTarget.array,14,4*MaxObjects,BufferUsage,null);
|
||||
batch_ibo[0].bufferData(Buffer.BindTarget.element_array,2,6*MaxObjects,BufferUsage,null);
|
||||
|
||||
batch_vbo[1].create();
|
||||
batch_ibo[1].create();
|
||||
batch_vbo[1].bufferData(Buffer.BindTarget.array,16,4*MaxObjects,BufferUsage,null);
|
||||
batch_vbo[1].bufferData(Buffer.BindTarget.array,14,4*MaxObjects,BufferUsage,null);
|
||||
batch_ibo[1].bufferData(Buffer.BindTarget.element_array,2,6*MaxObjects,BufferUsage,null);
|
||||
|
||||
batch_vertices = Mallocator.makeArray!float(16*MaxObjects);
|
||||
batch_indices = Mallocator.makeArray!ushort(6*MaxObjects);
|
||||
//batch_vertices = Mallocator.makeArray!ubyte(14*4*MaxObjects);
|
||||
//batch_indices = Mallocator.makeArray!ushort(6*MaxObjects);
|
||||
break;
|
||||
case Technique.instanced_attrib_divisor:
|
||||
goto case(Technique.uniform_buffer_indexed);
|
||||
|
|
@ -253,6 +406,8 @@ struct Renderer
|
|||
SDL_Log("Uniform block alignment: %u",block_alignment);
|
||||
SDL_Log("Uniform block max size: %u",block_max_size);
|
||||
SDL_Log("Data offset: %u",data_offset);
|
||||
|
||||
allocator = BlockAllocator(block_size, 32);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -261,12 +416,13 @@ struct Renderer
|
|||
|
||||
}
|
||||
|
||||
void draw(Texture tex, vec2 pos, vec2 size, vec4 coords, float angle = 0, uint material_id = 0, uint mesh_id = 0)
|
||||
void draw(Texture tex, vec2 pos, vec2 size, vec4 coords, short depth = 0, uint color = uint.max, float angle = 0, uint material_id = 0, uint mesh_id = 0, uint thread_id = 0)
|
||||
{
|
||||
__draw(this,tex,pos,size,coords,angle,material_id,mesh_id);
|
||||
if(prepared_items >= MaxObjects)return;
|
||||
__draw(this,tex,pos,size,coords,depth,color,angle,material_id,mesh_id,thread_id);
|
||||
}
|
||||
|
||||
private static void __draw_sdl(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, float angle, uint material_id, uint mesh_id)
|
||||
private static void __draw_sdl(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, short depth, uint color, float angle, uint material_id, uint mesh_id, uint thread_id)
|
||||
{
|
||||
/*with(this_)
|
||||
{
|
||||
|
|
@ -286,7 +442,7 @@ struct Renderer
|
|||
}*/
|
||||
}
|
||||
|
||||
private static void __draw_gl(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, float angle, uint material_id, uint mesh_id)
|
||||
private static void __draw_gl(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, short depth, uint color, float angle, uint material_id, uint mesh_id, uint thread_id)
|
||||
{
|
||||
//import core.stdc.string;
|
||||
with(this_)
|
||||
|
|
@ -330,19 +486,20 @@ struct Renderer
|
|||
|
||||
data_index += data_offset;
|
||||
item_id++;
|
||||
prepared_items++;
|
||||
}
|
||||
}
|
||||
|
||||
private static void __draw_gl_vbo_batch(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, float angle, uint material_id, uint mesh_id)
|
||||
private static void __draw_gl_vbo_batch(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, short depth, uint color, float angle, uint material_id, uint mesh_id, uint thread_id = 0)
|
||||
{
|
||||
import ecs_utils.gfx.config;
|
||||
short[3] mem = [depth, *cast(short*)&color, *(cast(short*)&color + 1)];
|
||||
//import core.stdc.string;
|
||||
with(this_)
|
||||
{
|
||||
if(item_id >= MaxObjects)return;
|
||||
//if(item_id >= MaxObjects)return;
|
||||
//pos += view_pos;
|
||||
size.x *= view_size.x;
|
||||
size.y *= view_size.y;
|
||||
|
||||
pos.x = pos.x * view_size.x + view_pos.x;
|
||||
pos.y = pos.y * view_size.y + view_pos.y;//*/
|
||||
|
||||
|
|
@ -355,67 +512,134 @@ struct Renderer
|
|||
memcpy(ptr+16,pos.data.ptr,8);
|
||||
memcpy(ptr+32,coords.data.ptr,16);*/
|
||||
|
||||
short[] verts = cast(short[])threads[thread_id].block.batch_vertices;
|
||||
uint item_id = threads[thread_id].block.items;
|
||||
|
||||
if(angle == 0)
|
||||
{
|
||||
batch_vertices[item_id*16] = GfxConfig.meshes[mesh_id].vertices[0] * size.x + pos.x;
|
||||
batch_vertices[item_id*16+1] = GfxConfig.meshes[mesh_id].vertices[1] * size.y + pos.y;
|
||||
batch_vertices[item_id*16+4] = GfxConfig.meshes[mesh_id].vertices[4] * size.x + pos.x;
|
||||
batch_vertices[item_id*16+5] = GfxConfig.meshes[mesh_id].vertices[5] * size.y + pos.y;
|
||||
batch_vertices[item_id*16+8] = GfxConfig.meshes[mesh_id].vertices[8] * size.x + pos.x;
|
||||
batch_vertices[item_id*16+9] = GfxConfig.meshes[mesh_id].vertices[9] * size.y + pos.y;
|
||||
batch_vertices[item_id*16+12] = GfxConfig.meshes[mesh_id].vertices[12] * size.x + pos.x;
|
||||
batch_vertices[item_id*16+13] = GfxConfig.meshes[mesh_id].vertices[13] * size.y + pos.y;
|
||||
size.x *= view_size.x;
|
||||
size.y *= view_size.y;
|
||||
|
||||
verts[item_id*28] = cast(short)((GfxConfig.meshes[mesh_id].vertices[0] * size.x + pos.x) * 8191);
|
||||
verts[item_id*28+1] = cast(short)((GfxConfig.meshes[mesh_id].vertices[1] * size.y + pos.y) * 8191);
|
||||
verts[item_id*28+2] = cast(short)((GfxConfig.meshes[mesh_id].vertices[2] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+3] = cast(short)((GfxConfig.meshes[mesh_id].vertices[3] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+4,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+7] = cast(short)((GfxConfig.meshes[mesh_id].vertices[4] * size.x + pos.x) * 8191);
|
||||
verts[item_id*28+8] = cast(short)((GfxConfig.meshes[mesh_id].vertices[5] * size.y + pos.y) * 8191);
|
||||
verts[item_id*28+9] = cast(short)((GfxConfig.meshes[mesh_id].vertices[6] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+10] = cast(short)((GfxConfig.meshes[mesh_id].vertices[7] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+11,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+14] = cast(short)((GfxConfig.meshes[mesh_id].vertices[8] * size.x + pos.x) * 8191);
|
||||
verts[item_id*28+15] = cast(short)((GfxConfig.meshes[mesh_id].vertices[9] * size.y + pos.y) * 8191);
|
||||
verts[item_id*28+16] = cast(short)((GfxConfig.meshes[mesh_id].vertices[10] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+17] = cast(short)((GfxConfig.meshes[mesh_id].vertices[11] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+18,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+21] = cast(short)((GfxConfig.meshes[mesh_id].vertices[12] * size.x + pos.x) * 8191);
|
||||
verts[item_id*28+22] = cast(short)((GfxConfig.meshes[mesh_id].vertices[13] * size.y + pos.y) * 8191);
|
||||
verts[item_id*28+23] = cast(short)((GfxConfig.meshes[mesh_id].vertices[14] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+24] = cast(short)((GfxConfig.meshes[mesh_id].vertices[15] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+25,mem.ptr,6);
|
||||
}
|
||||
else
|
||||
{
|
||||
//import core.stdc.math;
|
||||
float sinn = sinf(angle);
|
||||
float coss = cosf(angle);
|
||||
float sinx = sinf(angle) * size.x * view_size.y;
|
||||
float cosx = cosf(angle) * size.x * view_size.x;
|
||||
float siny = sinf(angle) * size.y * view_size.x;
|
||||
float cosy = cosf(angle) * size.y * view_size.y;
|
||||
|
||||
/*batch_vertices[item_id*16] = GfxConfig.meshes[mesh_id].vertices[0] * size.x;
|
||||
batch_vertices[item_id*16+1] = GfxConfig.meshes[mesh_id].vertices[1] * size.y;
|
||||
batch_vertices[item_id*16+4] = GfxConfig.meshes[mesh_id].vertices[4] * size.x;
|
||||
batch_vertices[item_id*16+5] = GfxConfig.meshes[mesh_id].vertices[5] * size.y;
|
||||
batch_vertices[item_id*16+8] = GfxConfig.meshes[mesh_id].vertices[8] * size.x;
|
||||
batch_vertices[item_id*16+9] = GfxConfig.meshes[mesh_id].vertices[9] * size.y;
|
||||
batch_vertices[item_id*16+12] = GfxConfig.meshes[mesh_id].vertices[12] * size.x;
|
||||
batch_vertices[item_id*16+13] = GfxConfig.meshes[mesh_id].vertices[13] * size.y;*/
|
||||
/*batch_vertices[item_id*28] = GfxConfig.meshes[mesh_id].vertices[0] * size.x;
|
||||
batch_vertices[item_id*28+1] = GfxConfig.meshes[mesh_id].vertices[1] * size.y;
|
||||
batch_vertices[item_id*28+4] = GfxConfig.meshes[mesh_id].vertices[4] * size.x;
|
||||
batch_vertices[item_id*28+5] = GfxConfig.meshes[mesh_id].vertices[5] * size.y;
|
||||
batch_vertices[item_id*28+8] = GfxConfig.meshes[mesh_id].vertices[8] * size.x;
|
||||
batch_vertices[item_id*28+9] = GfxConfig.meshes[mesh_id].vertices[9] * size.y;
|
||||
batch_vertices[item_id*28+12] = GfxConfig.meshes[mesh_id].vertices[12] * size.x;
|
||||
batch_vertices[item_id*28+13] = GfxConfig.meshes[mesh_id].vertices[13] * size.y;*/
|
||||
|
||||
batch_vertices[item_id*16] = (GfxConfig.meshes[mesh_id].vertices[0] * coss + GfxConfig.meshes[mesh_id].vertices[1] * sinn) * size.x + pos.x;
|
||||
batch_vertices[item_id*16+1] = (GfxConfig.meshes[mesh_id].vertices[1] * coss - GfxConfig.meshes[mesh_id].vertices[0] * sinn) * size.y + pos.y;
|
||||
batch_vertices[item_id*16+4] = (GfxConfig.meshes[mesh_id].vertices[4] * coss + GfxConfig.meshes[mesh_id].vertices[5] * sinn) * size.x + pos.x;
|
||||
batch_vertices[item_id*16+5] = (GfxConfig.meshes[mesh_id].vertices[5] * coss - GfxConfig.meshes[mesh_id].vertices[4] * sinn) * size.y + pos.y;
|
||||
batch_vertices[item_id*16+8] = (GfxConfig.meshes[mesh_id].vertices[8] * coss + GfxConfig.meshes[mesh_id].vertices[9] * sinn) * size.x + pos.x;
|
||||
batch_vertices[item_id*16+9] = (GfxConfig.meshes[mesh_id].vertices[9] * coss - GfxConfig.meshes[mesh_id].vertices[8] * sinn) * size.y + pos.y;
|
||||
batch_vertices[item_id*16+12] = (GfxConfig.meshes[mesh_id].vertices[12] * coss + GfxConfig.meshes[mesh_id].vertices[13] * sinn) * size.x + pos.x;
|
||||
batch_vertices[item_id*16+13] = (GfxConfig.meshes[mesh_id].vertices[13] * coss - GfxConfig.meshes[mesh_id].vertices[12] * sinn) * size.y + pos.y;
|
||||
verts[item_id*28] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[0] * cosx + GfxConfig.meshes[mesh_id].vertices[1] * siny) + pos.x) * 8191);
|
||||
verts[item_id*28+1] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[1] * cosy - GfxConfig.meshes[mesh_id].vertices[0] * sinx) + pos.y) * 8191);
|
||||
verts[item_id*28+2] = cast(short)((GfxConfig.meshes[mesh_id].vertices[2] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+3] = cast(short)((GfxConfig.meshes[mesh_id].vertices[3] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+4,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+7] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[4] * cosx + GfxConfig.meshes[mesh_id].vertices[5] * siny) + pos.x) * 8191);
|
||||
verts[item_id*28+8] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[5] * cosy - GfxConfig.meshes[mesh_id].vertices[4] * sinx) + pos.y) * 8191);
|
||||
verts[item_id*28+9] = cast(short)((GfxConfig.meshes[mesh_id].vertices[6] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+10] = cast(short)((GfxConfig.meshes[mesh_id].vertices[7] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+11,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+14] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[8] * cosx + GfxConfig.meshes[mesh_id].vertices[9] * siny) + pos.x) * 8191);
|
||||
verts[item_id*28+15] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[9] * cosy - GfxConfig.meshes[mesh_id].vertices[8] * sinx) + pos.y) * 8191);
|
||||
verts[item_id*28+16] = cast(short)((GfxConfig.meshes[mesh_id].vertices[10] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+17] = cast(short)((GfxConfig.meshes[mesh_id].vertices[11] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+18,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+21] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[12] * cosx + GfxConfig.meshes[mesh_id].vertices[13] * siny) + pos.x) * 8191);
|
||||
verts[item_id*28+22] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[13] * cosy - GfxConfig.meshes[mesh_id].vertices[12] * sinx) + pos.y) * 8191);
|
||||
verts[item_id*28+23] = cast(short)((GfxConfig.meshes[mesh_id].vertices[14] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+24] = cast(short)((GfxConfig.meshes[mesh_id].vertices[15] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+25,mem.ptr,6);
|
||||
}
|
||||
|
||||
batch_vertices[item_id*16+2] = GfxConfig.meshes[mesh_id].vertices[2] * coords.z + coords.x;
|
||||
batch_vertices[item_id*16+3] = GfxConfig.meshes[mesh_id].vertices[3] * coords.w + coords.y;
|
||||
batch_vertices[item_id*16+6] = GfxConfig.meshes[mesh_id].vertices[6] * coords.z + coords.x;
|
||||
batch_vertices[item_id*16+7] = GfxConfig.meshes[mesh_id].vertices[7] * coords.w + coords.y;
|
||||
batch_vertices[item_id*16+10] = GfxConfig.meshes[mesh_id].vertices[10] * coords.z + coords.x;
|
||||
batch_vertices[item_id*16+11] = GfxConfig.meshes[mesh_id].vertices[11] * coords.w + coords.y;
|
||||
batch_vertices[item_id*16+14] = GfxConfig.meshes[mesh_id].vertices[14] * coords.z + coords.x;
|
||||
batch_vertices[item_id*16+15] = GfxConfig.meshes[mesh_id].vertices[15] * coords.w + coords.y;
|
||||
/*verts[item_id*28+2] = cast(short)((GfxConfig.meshes[mesh_id].vertices[2] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+3] = cast(short)((GfxConfig.meshes[mesh_id].vertices[3] * coords.w + coords.y)*32767);
|
||||
verts[item_id*28+9] = cast(short)((GfxConfig.meshes[mesh_id].vertices[6] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+10] = cast(short)((GfxConfig.meshes[mesh_id].vertices[7] * coords.w + coords.y)*32767);
|
||||
verts[item_id*28+16] = cast(short)((GfxConfig.meshes[mesh_id].vertices[10] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+17] = cast(short)((GfxConfig.meshes[mesh_id].vertices[11] * coords.w + coords.y)*32767);
|
||||
verts[item_id*28+23] = cast(short)((GfxConfig.meshes[mesh_id].vertices[14] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+24] = cast(short)((GfxConfig.meshes[mesh_id].vertices[15] * coords.w + coords.y)*32767);*/
|
||||
|
||||
uint ind_id = item_id % 16_384;
|
||||
/*verts[item_id*28+4] = depth;
|
||||
verts[item_id*28+11] = depth;
|
||||
verts[item_id*28+18] = depth;
|
||||
verts[item_id*28+25] = depth;
|
||||
|
||||
batch_indices[item_id*6] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[0] + ind_id*4);
|
||||
batch_indices[item_id*6+1] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[1] + ind_id*4);
|
||||
batch_indices[item_id*6+2] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[2] + ind_id*4);
|
||||
batch_indices[item_id*6+3] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[3] + ind_id*4);
|
||||
batch_indices[item_id*6+4] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[4] + ind_id*4);
|
||||
batch_indices[item_id*6+5] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[5] + ind_id*4);
|
||||
*cast(uint*)&verts[item_id*28+5] = color;
|
||||
*cast(uint*)&verts[item_id*28+12] = color;
|
||||
*cast(uint*)&verts[item_id*28+19] = color;
|
||||
*cast(uint*)&verts[item_id*28+26] = color;
|
||||
|
||||
memcpy(verts.ptr+item_id*28+4,mem.ptr,6);
|
||||
memcpy(verts.ptr+item_id*28+11,mem.ptr,6);
|
||||
memcpy(verts.ptr+item_id*28+18,mem.ptr,6);
|
||||
memcpy(verts.ptr+item_id*28+25,mem.ptr,6);*/
|
||||
|
||||
uint ind_id = (item_id % batch_size)*4;
|
||||
|
||||
ushort[] indices = threads[thread_id].block.batch_indices;
|
||||
|
||||
indices[item_id*6] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[0] + ind_id);
|
||||
indices[item_id*6+1] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[1] + ind_id);
|
||||
indices[item_id*6+2] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[2] + ind_id);
|
||||
indices[item_id*6+3] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[3] + ind_id);
|
||||
indices[item_id*6+4] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[4] + ind_id);
|
||||
indices[item_id*6+5] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[5] + ind_id);
|
||||
|
||||
//render_list[item_id] = RenderData(tex,material_id,mesh_id);
|
||||
render_list[item_id].texture = tex;
|
||||
render_list[item_id].material_id = material_id;
|
||||
render_list[item_id].mesh_id = mesh_id;
|
||||
//render_list[item_id].texture = tex;
|
||||
//render_list[item_id].material_id = material_id;
|
||||
//render_list[item_id].mesh_id = mesh_id;
|
||||
|
||||
//data_index += 1;//data_offset;
|
||||
item_id++;
|
||||
threads[thread_id].block.items++;
|
||||
if(threads[thread_id].block.items >= VertexBlock.max_items)
|
||||
{
|
||||
//pushBlock(threads[thread_id].block);
|
||||
threads[thread_id].blocks.add(threads[thread_id].block);
|
||||
threads[thread_id].block = getBlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -433,9 +657,22 @@ struct Renderer
|
|||
{
|
||||
glClearColor(0,0,0,0);
|
||||
glViewport(0,0,this_.resolution.x,this_.resolution.y);
|
||||
glClear(GL_COLOR_BUFFER_BIT);// | GL_DEPTH_BUFFER_BIT);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
//glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDepthFunc(GL_LESS);
|
||||
|
||||
version(WebAssembly)
|
||||
{
|
||||
glDepthRangef(0,1);
|
||||
}
|
||||
else
|
||||
{
|
||||
glDepthRange(0,1);
|
||||
}
|
||||
//glDepthRange(0,1);
|
||||
//glClearDepth(1);
|
||||
}
|
||||
|
||||
void present()
|
||||
|
|
@ -450,6 +687,10 @@ struct Renderer
|
|||
|
||||
private static void __present_gl(ref Renderer this_)
|
||||
{
|
||||
|
||||
this_.pushThreadsBlocks();
|
||||
this_.pushData();
|
||||
|
||||
glViewport(0,0,this_.resolution.x,this_.resolution.y);
|
||||
//glEnable(GL_ALPHA_TEST);
|
||||
//glAlphaFunc(GL_GREATER, 0.01);
|
||||
|
|
@ -471,14 +712,17 @@ struct Renderer
|
|||
break;
|
||||
case Technique.vbo_batch:
|
||||
//if(data_index){
|
||||
batch_vbo[0].bufferSubData(Buffer.BindTarget.array,item_id*4*16,0,batch_vertices.ptr);
|
||||
batch_ibo[0].bufferSubData(Buffer.BindTarget.element_array,item_id*6*2,0,batch_indices.ptr);
|
||||
//batch_vbo[0].bufferSubData(Buffer.BindTarget.array,item_id*4*14,0,batch_vertices.ptr);
|
||||
//batch_ibo[0].bufferSubData(Buffer.BindTarget.element_array,item_id*6*2,0,batch_indices.ptr);
|
||||
|
||||
batch_vbo[0].bind(Buffer.BindTarget.array);
|
||||
batch_ibo[0].bind(Buffer.BindTarget.element_array);
|
||||
|
||||
glVertexAttribPointer(0,2,GL_FLOAT,false,16,null);
|
||||
glVertexAttribPointer(1,2,GL_FLOAT,false,16,cast(void*)8);//}
|
||||
//glVertexAttribPointer(0,2,GL_SHORT,true,14,null);
|
||||
//glVertexAttribPointer(1,2,GL_SHORT,true,14,cast(void*)4);//}
|
||||
glEnableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(3);
|
||||
//glVertexAttribPointer(2,1,GL_SHORT,true,14,cast(void*)6);//}
|
||||
break;
|
||||
case Technique.instanced_attrib_divisor:
|
||||
ubos[0].bufferSubData(Buffer.BindTarget.uniform,data_index,0,uniform_block.ptr);
|
||||
|
|
@ -575,8 +819,8 @@ struct Renderer
|
|||
//glBeginQuery(GL_TIME_ELAPSED, time_queries[0]);
|
||||
if(technique == Technique.vbo_batch)
|
||||
{
|
||||
uint items = item_id/16_384+1;
|
||||
foreach(i; 0..items)
|
||||
//uint items = item_id/batch_size+1;
|
||||
foreach(i; 0..draw_list.length)
|
||||
{
|
||||
if(material_id != render_list[i].material_id)
|
||||
{
|
||||
|
|
@ -591,16 +835,20 @@ struct Renderer
|
|||
render_list[i].texture.bind();
|
||||
}
|
||||
|
||||
uint instance_count = 16_384;
|
||||
if((i+1)*16_384 > item_id)
|
||||
/*uint instance_count = batch_size;
|
||||
if((i+1)*batch_size > item_id)
|
||||
{
|
||||
instance_count = item_id%16_384;
|
||||
}
|
||||
instance_count = item_id%batch_size;
|
||||
}*/
|
||||
|
||||
glVertexAttribPointer(0,2,GL_FLOAT,false,16,cast(void*)(i*16_384*4*16));
|
||||
glVertexAttribPointer(1,2,GL_FLOAT,false,16,cast(void*)(i*16_384*4*16+8));
|
||||
// glVertexAttribPointer(0,2,GL_FLOAT,false,16,cast(void*)(i*batch_size*4*16));
|
||||
// glVertexAttribPointer(1,2,GL_FLOAT,false,16,cast(void*)(i*batch_size*4*16+8));
|
||||
glVertexAttribPointer(0,2,GL_SHORT,true,14,cast(void*)(draw_list[i].start*4*14));
|
||||
glVertexAttribPointer(1,2,GL_SHORT,true,14,cast(void*)(draw_list[i].start*4*14+4));
|
||||
glVertexAttribPointer(2,1,GL_SHORT,true,14,cast(void*)(draw_list[i].start*4*14+8));
|
||||
glVertexAttribPointer(3,4,GL_UNSIGNED_BYTE,true,14,cast(void*)(draw_list[i].start*4*14+10));
|
||||
|
||||
glDrawElements(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2));
|
||||
glDrawElements(GL_TRIANGLES,draw_list[i].count*6,GL_UNSIGNED_SHORT,cast(void*)(draw_list[i].start*6*2));
|
||||
|
||||
//glDrawElementsBaseVertex(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2),i*16_384*4);
|
||||
}
|
||||
|
|
@ -783,6 +1031,9 @@ struct Renderer
|
|||
}
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glDisableVertexAttribArray(2);
|
||||
glDisableVertexAttribArray(3);
|
||||
this_.freeBlocks();
|
||||
/*glUseProgram(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);*/
|
||||
|
|
@ -803,7 +1054,7 @@ struct Renderer
|
|||
view_pos = (pos - size * 0.5) * view_size;
|
||||
}
|
||||
|
||||
__gshared void function(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, float angle, uint material_id, uint mesh_id) __draw;
|
||||
__gshared void function(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, short depth, uint color, float angle, uint material_id, uint mesh_id, uint thread_id) __draw;
|
||||
__gshared void function(ref Renderer this_) __present;
|
||||
__gshared void function(ref Renderer this_) __clear;
|
||||
__gshared void function(ref Renderer this_) __initialize;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ module ecs_utils.gfx.shader;
|
|||
|
||||
import bindbc.sdl;
|
||||
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import glad.gl.gl;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ module ecs_utils.gfx.texture;
|
|||
|
||||
import bindbc.sdl;
|
||||
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
|
||||
import ecs_utils.math.vector;
|
||||
|
||||
|
|
@ -54,6 +54,8 @@ struct Texture
|
|||
data.data = Mallocator.makeArray!ubyte(surf.w*surf.h*surf.format.BytesPerPixel);
|
||||
data.data[0..$] = (cast(ubyte*)surf.pixels)[0..data.data.length];
|
||||
|
||||
SDL_FreeSurface(surf);
|
||||
|
||||
glGenTextures(1, &data.gl_handle);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D,data.gl_handle);
|
||||
|
|
@ -78,11 +80,12 @@ struct Texture
|
|||
glBindTexture(GL_TEXTURE_2D, data.gl_handle);
|
||||
}
|
||||
|
||||
void destory()
|
||||
void destory() @nogc nothrow
|
||||
{
|
||||
if(data)
|
||||
{
|
||||
glDeleteTextures(1, &data.gl_handle);
|
||||
if(data.data)Mallocator.dispose(data.data);
|
||||
Mallocator.dispose(data);
|
||||
data = null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
module ecs_utils.gfx.vertex;
|
||||
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,6 +2,18 @@ module ecs_utils.math.vector;
|
|||
|
||||
struct vec2
|
||||
{
|
||||
this(float v) @nogc nothrow
|
||||
{
|
||||
x = v;
|
||||
y = v;
|
||||
}
|
||||
|
||||
this(float x, float y) @nogc nothrow
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
|
|
@ -75,6 +87,22 @@ struct vec4
|
|||
float[4] data;
|
||||
}
|
||||
|
||||
this(float v) @nogc nothrow
|
||||
{
|
||||
x = v;
|
||||
y = v;
|
||||
z = v;
|
||||
w = v;
|
||||
}
|
||||
|
||||
this(float x, float y, float z, float w) @nogc nothrow
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
|
||||
vec4 opBinary(string op)(float v)
|
||||
{
|
||||
static if (op == "+") return vec4(x + v, y + v, z + v, w + v);
|
||||
|
|
@ -96,6 +124,27 @@ struct ivec2
|
|||
}
|
||||
int[2] data;
|
||||
}
|
||||
|
||||
this(int v) @nogc nothrow
|
||||
{
|
||||
x = v;
|
||||
y = v;
|
||||
}
|
||||
|
||||
this(int x, int y) @nogc nothrow
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
ivec2 opBinary(string op, T)(T v)
|
||||
{
|
||||
static if (op == "+") return ivec2(x + v, y + v);
|
||||
else static if (op == "-") return ivec2(x - v, y - v);
|
||||
else static if (op == "*") return ivec2(x * v, y * v);
|
||||
else static if (op == "/") return ivec2(x / v, y / v);
|
||||
else static assert(0, "Operator "~op~" not implemented");
|
||||
}
|
||||
|
||||
vec2 opCast()
|
||||
{
|
||||
|
|
@ -116,4 +165,20 @@ struct ivec4
|
|||
}
|
||||
int[4] data;
|
||||
}
|
||||
|
||||
this(int v) @nogc nothrow
|
||||
{
|
||||
x = v;
|
||||
y = v;
|
||||
z = v;
|
||||
w = v;
|
||||
}
|
||||
|
||||
this(int x, int y, int z, int w) @nogc nothrow
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +21,19 @@ float randomRangef(float min, float max)
|
|||
return rand()%4096;
|
||||
}*/
|
||||
|
||||
extern(C) int printf(scope const char* format, ...) @nogc nothrow @system;
|
||||
version(GNU)
|
||||
{
|
||||
public import core.stdc.stdio : printf;
|
||||
pragma(inline, true) T[n] staticArray(T, size_t n)(auto ref T[n] a)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
extern(C) int printf(scope const char* format, ...) @nogc nothrow @system;
|
||||
public import std.array : staticArray;
|
||||
}
|
||||
extern(C) int rand();
|
||||
|
||||
version(D_BetterC)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue