Demos #10
6 changed files with 98 additions and 16 deletions
40
demos/external/sources/mmutils/thread_pool.d
vendored
40
demos/external/sources/mmutils/thread_pool.d
vendored
|
|
@ -810,6 +810,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;
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ struct Launcher
|
|||
uint style = 3;
|
||||
uint entities_count;
|
||||
bool multithreading;
|
||||
int threads;
|
||||
int threads = 1;
|
||||
ulong timer_freq;
|
||||
double delta_time;
|
||||
uint fps;
|
||||
|
|
@ -103,6 +103,14 @@ struct Launcher
|
|||
manager.update("clean");
|
||||
manager.end();
|
||||
|
||||
foreach(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;
|
||||
|
|
@ -316,11 +324,19 @@ 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, 12, null))//"Multithreading", null, launcher.multithreading, true))
|
||||
if(igSliderInt("##Threads",&launcher.threads, 1, 32, null))//"Multithreading", null, launcher.multithreading, true))
|
||||
{
|
||||
launcher.job_updater.pool.setThreadsNum(launcher.threads);
|
||||
//launcher.threads = !launcher.multithreading;
|
||||
|
|
@ -547,13 +563,13 @@ void mainLoop(void* arg)
|
|||
launcher.renderer.clear();
|
||||
|
||||
double loop_time = launcher.getTime();
|
||||
launcher.job_updater.pool.tryWaitCount = 5000;
|
||||
launcher.job_updater.pool.tryWaitCount = 10000;
|
||||
if(launcher.loop && !launcher.loop())
|
||||
{
|
||||
quit();
|
||||
*cast(bool*)arg = false;
|
||||
}
|
||||
launcher.job_updater.pool.tryWaitCount = 10;
|
||||
launcher.job_updater.pool.tryWaitCount = 0;
|
||||
|
||||
loop_time = launcher.getTime() - loop_time;
|
||||
|
||||
|
|
@ -697,10 +713,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);
|
||||
launcher.manager = EntityManager.instance;
|
||||
|
||||
//launcher.manager.m_thread_id_func = &launcher.job_updater.getThreadID;
|
||||
|
|
@ -720,6 +736,10 @@ 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();
|
||||
|
||||
{
|
||||
|
|
@ -752,6 +772,8 @@ int main(int argc, char** argv)
|
|||
}
|
||||
}
|
||||
|
||||
EntityManager.destroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -90,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;
|
||||
|
|
@ -805,9 +815,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();
|
||||
|
|
|
|||
|
|
@ -48,6 +48,10 @@ struct SpaceInvaders
|
|||
~this() @nogc nothrow
|
||||
{
|
||||
if(shoot_grid)Mallocator.dispose(shoot_grid);
|
||||
launcher.manager.freeTemplate(enemy_tmpl);
|
||||
launcher.manager.freeTemplate(ship_tmpl);
|
||||
launcher.manager.freeTemplate(laser_tmpl);
|
||||
texture.destory();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -337,7 +341,7 @@ struct ShootGridCleaner
|
|||
|
||||
struct ShootGridManager
|
||||
{
|
||||
mixin ECS.System!32;
|
||||
mixin ECS.System!128;
|
||||
|
||||
mixin ECS.WritableDependencies!(ShootGridDependency);
|
||||
|
||||
|
|
@ -793,7 +797,7 @@ struct MovementSystem
|
|||
}
|
||||
}
|
||||
|
||||
import core.stdc.math;
|
||||
extern(C) float sqrtf(float x) @nogc nothrow @system;
|
||||
|
||||
/**
|
||||
*System is responsible for movement of objects with CInput component.
|
||||
|
|
@ -893,6 +897,8 @@ void spaceInvadersStart()
|
|||
|
||||
launcher.manager.beginRegister();
|
||||
|
||||
launcher.manager.registerDependency(ShootGridDependency);
|
||||
|
||||
launcher.manager.registerComponent!CLocation;
|
||||
launcher.manager.registerComponent!CTexture;
|
||||
launcher.manager.registerComponent!CInput;
|
||||
|
|
@ -1009,10 +1015,10 @@ void spaceInvadersStart()
|
|||
enemy_tmpl = launcher.manager.allocateTemplate(enemy_id);
|
||||
grouped_tmpl = launcher.manager.allocateTemplate(grouped_id);
|
||||
|
||||
launcher.gui_manager.addTemplate(space_invaders.ship_tmpl,"Ship");
|
||||
launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(space_invaders.ship_tmpl),"Ship");
|
||||
launcher.gui_manager.addTemplate(enemy_tmpl,"Enemy");
|
||||
launcher.gui_manager.addTemplate(grouped_tmpl,"Grouped enemy");
|
||||
launcher.gui_manager.addTemplate(space_invaders.laser_tmpl,"Laser");
|
||||
launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(space_invaders.laser_tmpl),"Laser");
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1023,8 +1029,9 @@ void spaceInvadersEnd()
|
|||
launcher.manager.getSystem(LaserShootingSystem.system_id).disable();
|
||||
launcher.manager.getSystem(MovementSystem.system_id).disable();
|
||||
launcher.manager.getSystem(ClampPositionSystem.system_id).disable();
|
||||
launcher.manager.getSystem(ShootGridCleaner.system_id).disable();
|
||||
|
||||
launcher.manager.freeTemplate(space_invaders.enemy_tmpl);
|
||||
//launcher.manager.freeTemplate(space_invaders.enemy_tmpl);
|
||||
Mallocator.dispose(space_invaders);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -230,7 +230,7 @@ struct Renderer
|
|||
block_stack_mutex.initialize();
|
||||
|
||||
|
||||
threads = Mallocator.makeArray!Thread(12);
|
||||
threads = Mallocator.makeArray!Thread(32);
|
||||
foreach(ref Thread thread;threads)
|
||||
{
|
||||
thread.block = getBlock();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue