Huge demos update

-moved C stdlib function definitions to ecs_utils.utils
-added function to calculate mix(linear interpolation) and rsqrt(fast inverse sqrt)
-added some math to vec2 (length, normalize...)
-improved renderer with possibility to use multiple materials (one per block, not perfect solution for parallel compute, but works with some requirements)
-added blending support for material (opaque, additive, mixed)
-added Android support
-added gprahical representation for mouse tools (tool_circle.d)
-added initial support for editing template components variables
-better Component and Templates listing
-added possibility to add/removes components using mouse
-move CLocation to game_core.basic and reuse in every test
-moved tools code from demos to App (now code is fully separated from demos!)
-some improvement and fixes in Snake demo, with additional systems to handle adding and removing entities
-added new demo: Particles. By now demo has several particles to spawn and support for attractors and vortexes (calculation is made as every attractor with every entity)
-fixed bug with window hover and tools
-improved tool behaviour
-added new material
-now window is always opened as maximized windowed mode
-some minor fixes and improvements
This commit is contained in:
Mergul 2020-06-06 22:46:29 +02:00
parent 13e6ed8fd5
commit e76c5ccdb2
20 changed files with 1804 additions and 288 deletions

View file

@ -4,10 +4,11 @@ import bindbc.sdl;
import cimgui.cimgui;
import game_core.basic;
import game_core.job_updater;
import bubel.ecs.manager;
import bubel.ecs.core;
import bubel.ecs.manager;
import bubel.ecs.std;
import ecs_utils.gfx.renderer;
@ -21,6 +22,7 @@ import glad.gl.gles2;
import glad.gl.loader;
import gui.manager;
import gui.tool_circle;
extern (C) :
@ -50,7 +52,7 @@ struct Launcher
bool function() loop;
void function() end;
void function(SDL_Event*) event;
void function(vec2, Tool, int) tool;
//void function(vec2, Tool, int, bool) tool;
float scalling;
ivec2 window_size = ivec2(1024,768);
Renderer renderer;
@ -65,9 +67,12 @@ struct Launcher
vec2 render_position;
Tool used_tool;
int tool_size = 0;
int tool_size = 100;
float tool_repeat = 0;
float repeat_time = 0;
bool tool_show = true;
bool tool_mode = true;
ToolCircle* tool_circle;
bool swap_interval = true;
@ -93,7 +98,7 @@ struct Launcher
float draw_time = 0;
}
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)
void switchDemo(void function() start, bool function() loop, void function() end, void function(SDL_Event*) event, const (char)* tips)
{
gui_manager.clear();
//launcher.ent
@ -117,7 +122,97 @@ struct Launcher
this.end = end;
this.event = event;
this.tips = tips;
this.tool = tool;
//this.tool = tool;
}
void processTool(vec2 position, bool mode)
{
static struct Iterator
{
float size2;
vec2 position;
ComponentRef[] add_comps;
ushort[] rem_comps;
void removeEntity(IteratorSystem.EntitiesData data)
{
foreach(i;0..data.length)
{
vec2 rel_vec = data.location[i] - position;
float length = rel_vec.x * rel_vec.x + rel_vec.y * rel_vec.y;
if(length < size2)gEM.removeEntity(data.entity[i].id);
}
}
void addComponent(IteratorSystem.EntitiesData data)
{
foreach(i;0..data.length)
{
vec2 rel_vec = data.location[i] - position;
float length = rel_vec.x * rel_vec.x + rel_vec.y * rel_vec.y;
if(length < size2)gEM.addComponents(data.entity[i].id, add_comps);
}
}
void removeComponent(IteratorSystem.EntitiesData data)
{
foreach(i;0..data.length)
{
vec2 rel_vec = data.location[i] - position;
float length = rel_vec.x * rel_vec.x + rel_vec.y * rel_vec.y;
if(length < size2)gEM.removeComponents(data.entity[i].id, rem_comps);
}
}
}
float half_size = tool_size * 0.5;
float size2 = half_size * half_size;
Iterator iterator;
iterator.size2 = size2;
iterator.position = position;
switch(used_tool)
{
case Tool.entity_spawner:
if(mode)
{
if(gui_manager.templates.length == 0)return;
EntityTemplate* tmpl = gui_manager.getSelectedTemplate();
CLocation* location = tmpl.getComponent!CLocation;
if(location)
{
position += randomCircularSample() * half_size;
if(position.y < 16)position.y = 16;
else if(position.y > 299)position.y = 299;
*location = position;
}
manager.addEntity(tmpl);
}
else
{
manager.callEntitiesFunction!IteratorSystem(&iterator.removeEntity);
}
break;
case Tool.component_manipulator:
{
if(gui_manager.components.length == 0)return;
if(mode)
{
ComponentRef[1] comps = [gui_manager.getSelectedComponent()];
iterator.add_comps = comps;
manager.callEntitiesFunction!IteratorSystem(&iterator.addComponent);
}
else
{
ushort[1] comps = [gui_manager.getSelectedComponent().component_id];
iterator.rem_comps = comps;
manager.callEntitiesFunction!IteratorSystem(&iterator.removeComponent);
}
}
break;
default:
break;
}
}
bool getKeyState(SDL_Scancode key)
@ -184,6 +279,28 @@ struct CleanSystem
}
}
struct IteratorSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
const (Entity)[] entity;
CLocation[] location;
}
bool onBegin()
{
return false;
}
void onUpdate(EntitiesData)
{
}
}
void mainLoop(void* arg)
{
__gshared double time = 0;
@ -237,9 +354,12 @@ void mainLoop(void* arg)
case SDL_BUTTON_MIDDLE:launcher.mouse.middle = true;break;
default:break;
}
if(launcher.tool && event.button.button == SDL_BUTTON_LEFT && launcher.tool_repeat == 0 && !igIsWindowHovered(ImGuiHoveredFlags_AnyWindow))
if(!igIsAnyItemHovered())igSetWindowFocus();
if(!igIsWindowHovered(ImGuiHoveredFlags_AnyWindow) && !igIsWindowFocused(ImGuiFocusedFlags_AnyWindow))
{
launcher.tool(vec2(event.button.x, launcher.window_size.y - event.button.y) * launcher.scalling - launcher.render_position, launcher.used_tool, launcher.tool_size);
launcher.repeat_time = 0;
if(event.button.button == SDL_BUTTON_LEFT)launcher.processTool(vec2(event.button.x, launcher.window_size.y - event.button.y) * launcher.scalling - launcher.render_position,launcher.tool_mode);
else if(event.button.button == SDL_BUTTON_RIGHT)launcher.processTool(vec2(event.button.x, launcher.window_size.y - event.button.y) * launcher.scalling - launcher.render_position,!launcher.tool_mode);
}
}
else if(event.type == SDL_MOUSEBUTTONUP)
@ -255,17 +375,52 @@ void mainLoop(void* arg)
else if(event.type == SDL_MOUSEMOTION)
{
launcher.mouse.position = vec2(event.motion.x, launcher.window_size.y - event.motion.y);
}else if(event.type == SDL_MOUSEWHEEL)
{
if(!igIsAnyItemHovered() && !igIsWindowHovered(ImGuiHoveredFlags_AnyWindow))
{
if(SDL_GetModState() & KMOD_CTRL)
{
float sign = 1;
if(event.wheel.y < 0)sign = -1;
float val = sign * event.wheel.y * launcher.tool_repeat * 0.25;
if(val < 0.1)val = 0.1;
launcher.tool_repeat -= sign * val;
if(launcher.tool_repeat < 0)launcher.tool_repeat = 0;
else if(launcher.tool_repeat > 1000)launcher.tool_repeat = 1000;
}
else
{
int sign = 1;
if(event.wheel.y < 0)sign = -1;
int val = sign * event.wheel.y * launcher.tool_size / 4;
if(val < 1)val = 1;
launcher.tool_size -= sign * val;
if(launcher.tool_size < 1)launcher.tool_size = 1;
else if(launcher.tool_size > 256)launcher.tool_size = 256;
}
}
}
}
if(launcher.tool && launcher.tool_repeat != 0 && launcher.mouse.left && !igIsWindowHovered(ImGuiHoveredFlags_AnyWindow) && !igIsWindowFocused(ImGuiFocusedFlags_AnyWindow))
if(launcher.tool_repeat != 0 && (launcher.mouse.left || launcher.mouse.right) && !igIsWindowHovered(ImGuiHoveredFlags_AnyWindow) && !igIsWindowFocused(ImGuiFocusedFlags_AnyWindow))
{
bool mode = launcher.tool_mode;
if(launcher.mouse.right)mode = !mode;
float range = 500.0 / cast(float)launcher.tool_repeat;
launcher.repeat_time += launcher.delta_time;
while(launcher.repeat_time > range)
if(launcher.used_tool != Tool.entity_spawner || !mode)
{
launcher.repeat_time -= range;
launcher.tool((launcher.mouse.position*launcher.scalling)-launcher.render_position, launcher.used_tool, launcher.tool_size);
if(launcher.repeat_time > range)launcher.processTool((launcher.mouse.position*launcher.scalling)-launcher.render_position, mode);
while(launcher.repeat_time > range)launcher.repeat_time -= range;
}
else
{
while(launcher.repeat_time > range)
{
launcher.repeat_time -= range;
launcher.processTool((launcher.mouse.position*launcher.scalling)-launcher.render_position, mode);
}
}
}
@ -300,17 +455,22 @@ void mainLoop(void* arg)
if(igMenuItemBool("Simpe",null,false,true))
{
import demos.simple;
launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,&simpleTool,Simple.tips);
launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,Simple.tips);
}
if(igMenuItemBool("Snake",null,false,true))
{
import demos.snake;
launcher.switchDemo(&snakeStart,&snakeLoop,&snakeEnd,&snakeEvent,&snakeTool,Snake.tips);
launcher.switchDemo(&snakeStart,&snakeLoop,&snakeEnd,&snakeEvent,Snake.tips);
}
if(igMenuItemBool("Space invaders",null,false,true))
{
import demos.space_invaders;
launcher.switchDemo(&spaceInvadersStart,&spaceInvadersLoop,&spaceInvadersEnd,&spaceInvadersEvent,&spaceInvadersTool,SpaceInvaders.tips);
launcher.switchDemo(&spaceInvadersStart,&spaceInvadersLoop,&spaceInvadersEnd,&spaceInvadersEvent,SpaceInvaders.tips);
}
if(igMenuItemBool("Particles",null,false,true))
{
import demos.particles;
launcher.switchDemo(&particlesStart,&particlesLoop,&particlesEnd,&particlesEvent,ParticlesDemo.tips);
}
igEndMenu();
}
@ -463,20 +623,22 @@ 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, 500), ImGuiCond_Once);
igSetNextWindowSize(ImVec2(250, launcher.window_size.y - 60), ImGuiCond_Once);
if(igBegin("Demo",&launcher.show_demo_wnd,0))
{
ImDrawList* draw_list = igGetWindowDrawList();
//igBeginGroup();
igBeginGroup();
launcher.gui_manager.gui();
//igEndGroup();
igEndGroup();
ImDrawList_AddRect(draw_list, igGetItemRectMin(), ImVec2(igGetWindowPos().x+igGetWindowWidth()-2,igGetItemRectMax().y), igColorConvertFloat4ToU32(ImVec4(0.4,0.4,0.4,0.4)), 4, ImDrawCornerFlags_All, 1);
//ImDrawList_AddRect(draw_list, igGetItemRectMin(), igGetItemRectMax(), igColorConvertFloat4ToU32(ImVec4(0.4,0.4,0.4,0.4)), -1, 0, 1);
//igBeginChildFrame(1,ImVec2(0,-1),ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_ChildWindow);
//igBeginChild("Tool frame",ImVec2(-1,-1),true,0);
if(igCollapsingHeader("Tool##ToolHeader", ImGuiTreeNodeFlags_SpanAvailWidth))
igBeginGroup();
if(igCollapsingHeader("Tool##ToolHeader", ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_DefaultOpen))
{
igIndent(8);
igBeginGroup();
if(igBeginCombo("Tool",tool_strings[launcher.used_tool],0))
{
if(igSelectable("Entity spawner",false,0,ImVec2(0,0)))
@ -493,14 +655,20 @@ void mainLoop(void* arg)
}
igEndCombo();
}
igCheckbox("Show Tool", &launcher.tool_show);
//igSelectable("Selectabe",false,ImGuiSelectableFlags_None,ImVec2(0,0));
if(igRadioButtonBool("Add", launcher.tool_mode))launcher.tool_mode = true;
igSameLine(0,0);
if(igRadioButtonBool("Remove", !launcher.tool_mode))launcher.tool_mode = false;
igSliderInt("Tool size", &launcher.tool_size, 0, 256, null);
igSliderFloat("Tool repeat", &launcher.tool_repeat, 0, 1024, null, 4);
launcher.gui_manager.toolGui();
igEndGroup();
ImDrawList_AddRect(draw_list, igGetItemRectMin(), igGetItemRectMax(), igColorConvertFloat4ToU32(ImVec4(0.4,0.4,0.4,0.4)), 4, ImDrawCornerFlags_All, 1);
igUnindent(8);
}
igEndGroup();
ImDrawList_AddRect(draw_list, igGetItemRectMin(), ImVec2(igGetWindowPos().x+igGetWindowWidth()-2,igGetItemRectMax().y), igColorConvertFloat4ToU32(ImVec4(0.4,0.4,0.4,0.4)), 2, ImDrawCornerFlags_All, 1);
//igEndChild();
//igEndChildFrame();
@ -516,7 +684,8 @@ void mainLoop(void* arg)
if(launcher.show_profile_wnd)
{
igSetNextWindowPos(ImVec2(launcher.window_size.x - 260, launcher.window_size.y - 280), ImGuiCond_Once, ImVec2(0,0));
//igSetNextWindowPos(ImVec2(launcher.window_size.x - 260, launcher.window_size.y - 280), ImGuiCond_Once, ImVec2(0,0));
igSetNextWindowPos(ImVec2(8, launcher.window_size.y - 258), ImGuiCond_Once, ImVec2(0,0));
igSetNextWindowSize(ImVec2(250, 250), ImGuiCond_Once);
if(igBegin("Profile",&launcher.show_profile_wnd,0))
{
@ -577,6 +746,10 @@ void mainLoop(void* arg)
launcher.renderer.present();
draw_time = launcher.getTime() - draw_time;
//import std.stdio;
//printf("Scalling: %f",launcher.scalling);
if(launcher.tool_show)launcher.tool_circle.draw(&launcher.renderer, (launcher.mouse.position*launcher.scalling)-launcher.render_position, cast(float)launcher.tool_size, launcher.renderer.view_size.y*6*launcher.scalling);
__gshared float plot_time = 0;
__gshared uint plot_samples = 0;
plot_time += launcher.delta_time;
@ -703,6 +876,7 @@ int app_main(int argc, char** argv)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
launcher.window = SDL_CreateWindow("Simple", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, launcher.window_size.x, launcher.window_size.y, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_MaximizeWindow(launcher.window);
launcher.gl_context = SDL_GL_CreateContext(launcher.window);
launcher.context = igCreateContext(null);
@ -778,8 +952,11 @@ int app_main(int argc, char** argv)
launcher.manager.registerPass("clean");
launcher.manager.registerComponent!CLocation;
launcher.manager.registerSystem!CountSystem(10000);
launcher.manager.registerSystem!CleanSystem(0,"clean");
launcher.manager.registerSystem!IteratorSystem(0,"clean");
launcher.manager.endRegister();
@ -796,8 +973,10 @@ int app_main(int argc, char** argv)
{
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);
import demos.particles;
// launcher.switchDemo(&spaceInvadersStart,&spaceInvadersLoop,&spaceInvadersEnd,&spaceInvadersEvent,SpaceInvaders.tips);
// launcher.switchDemo(&particlesStart,&particlesLoop,&particlesEnd,&particlesEvent,ParticlesDemo.tips);
launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,Simple.tips);
}
int key_num;
@ -842,7 +1021,7 @@ void loadGFX()
Texture.__loadBackend();
Renderer.__loadBackend();
GfxConfig.materials = Mallocator.makeArray!Material(1);
GfxConfig.materials = Mallocator.makeArray!Material(3);
GfxConfig.meshes = Mallocator.makeArray!Mesh(1);
float[16] vertices = [-0.5,-0.5, 0,1, -0.5,0.5, 0,0, 0.5,-0.5, 1,1, 0.5,0.5, 1,0];
@ -889,7 +1068,84 @@ void loadGFX()
GfxConfig.materials[0].data.bindings[0] = GfxConfig.materials[0].getLocation("tex");
Shader vsh2;
vsh2.create();
vsh2.load("assets/shaders/circle.vp");
vsh2.compile();
Shader fsh2;
fsh2.create();
fsh2.load("assets/shaders/circle.fp");
fsh2.compile();
GfxConfig.materials[1].create();
GfxConfig.materials[1].data.blend_mode = Material.BlendMode.mixed;
GfxConfig.materials[1].data.mode = Material.TransformMode.position;
Material.ShaderModule[1] modules2 = [Material.ShaderModule(vsh2,fsh2)];
GfxConfig.materials[1].attachModules(modules2);
//GfxConfig.materials[0].
//GfxConfig.materials[0].load(load_data.materials[i].str);
GfxConfig.materials[1].compile();
GfxConfig.materials[1].bindAttribLocation("positions",0);
//GfxConfig.materials[1].bindAttribLocation("tex_coords",1);
//GfxConfig.materials[1].bindAttribLocation("depth",2);
//GfxConfig.materials[1].bindAttribLocation("vcolor",3);
GfxConfig.materials[1].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[1].data.uniforms = Mallocator.makeArray!(Material.Uniform)(2);
GfxConfig.materials[1].data.uniforms[0] = Material.Uniform(Material.Type.float4, GfxConfig.materials[1].getLocation("matrix_1"), 0);
GfxConfig.materials[1].data.uniforms[1] = Material.Uniform(Material.Type.float4, GfxConfig.materials[1].getLocation("matrix_2"), 16);
//GfxConfig.materials[1].data.uniforms[2] = Material.Uniform(Material.Type.float4, GfxConfig.materials[0].getLocation("uv_transform"), 32);
//GfxConfig.materials[1].data.bindings = Mallocator.makeArray!(int)(1);
//GfxConfig.materials[1].data.bindings[0] = GfxConfig.materials[0].getLocation("tex");
GfxConfig.materials[2].create();
GfxConfig.materials[2].data.blend_mode = Material.BlendMode.opaque;
GfxConfig.materials[2].data.mode = Material.TransformMode.position;
//Material.ShaderModule[1] modules = [Material.ShaderModule(vsh,fsh)];
GfxConfig.materials[2].attachModules(modules);
//GfxConfig.materials[0].
//GfxConfig.materials[0].load(load_data.materials[i].str);
GfxConfig.materials[2].compile();
GfxConfig.materials[2].bindAttribLocation("positions",0);
GfxConfig.materials[2].bindAttribLocation("tex_coords",1);
GfxConfig.materials[2].bindAttribLocation("depth",2);
GfxConfig.materials[2].bindAttribLocation("vcolor",3);
GfxConfig.materials[2].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[2].data.uniforms = Mallocator.makeArray!(Material.Uniform)(3);
GfxConfig.materials[2].data.uniforms[0] = Material.Uniform(Material.Type.float4, GfxConfig.materials[0].getLocation("matrix_1"), 0);
GfxConfig.materials[2].data.uniforms[1] = Material.Uniform(Material.Type.float4, GfxConfig.materials[0].getLocation("matrix_2"), 16);
GfxConfig.materials[2].data.uniforms[2] = Material.Uniform(Material.Type.float4, GfxConfig.materials[0].getLocation("uv_transform"), 32);
GfxConfig.materials[2].data.bindings = Mallocator.makeArray!(int)(1);
GfxConfig.materials[2].data.bindings[0] = GfxConfig.materials[0].getLocation("tex");
GfxConfig.materials[2].data.blend_mode = Material.BlendMode.additive;
/*glUseProgram(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);*/
launcher.tool_circle = Mallocator.make!ToolCircle;
}