Demos #10

Merged
Mergul merged 39 commits from Demos into master 2020-05-28 18:48:45 +02:00
7 changed files with 61 additions and 40 deletions
Showing only changes of commit 8381ac166b - Show all commits

View file

@ -92,7 +92,7 @@ compile(['source'], 'demo.bc')
if clean or os.path.exists('../ecs.bc') == 0 or os.path.isfile('../ecs.bc') == 0: if clean or os.path.exists('../ecs.bc') == 0 or os.path.isfile('../ecs.bc') == 0:
compile(['../source'], '../ecs.bc') compile(['../source'], '../ecs.bc')
emcc_cmd = 'emcc -v ' + shared_flags + emc_flags + '-s ERROR_ON_UNDEFINED_SYMBOLS=0 -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1 -s ALLOW_MEMORY_GROWTH=1 -s MINIFY_HTML=0 -s WASM_MEM_MAX=2048MB -s MALLOC=dlmalloc -s WASM=1 -o ecs_demo.html ' emcc_cmd = 'emcc -v ' + shared_flags + emc_flags + '-s ERROR_ON_UNDEFINED_SYMBOLS=0 -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1 -s ALLOW_MEMORY_GROWTH=1 -s MINIFY_HTML=0 -s WASM_MEM_MAX=2048MB -s MALLOC=dlmalloc -s WASM=1 -o ecs_demo.html --shell-file emscripten_shell.html '
#-s ALLOW_MEMORY_GROWTH=1 -s PROXY_TO_PTHREAD=1 -Wl,--no-check-features -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s TOTAL_MEMORY=512MB #-s ALLOW_MEMORY_GROWTH=1 -s PROXY_TO_PTHREAD=1 -Wl,--no-check-features -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s TOTAL_MEMORY=512MB
emcc_cmd += '../ecs.bc ' emcc_cmd += '../ecs.bc '

View file

@ -51,6 +51,7 @@ struct Launcher
void function() end; void function() end;
void function(SDL_Event*) event; void function(SDL_Event*) event;
void function(vec2, Tool, int) tool; void function(vec2, Tool, int) tool;
float scalling;
ivec2 window_size = ivec2(1024,768); ivec2 window_size = ivec2(1024,768);
Renderer renderer; Renderer renderer;
ubyte[] keys; ubyte[] keys;
@ -60,6 +61,7 @@ struct Launcher
ulong timer_freq; ulong timer_freq;
double delta_time; double delta_time;
uint fps; uint fps;
vec2 render_position;
Tool used_tool; Tool used_tool;
int tool_size = 0; int tool_size = 0;
@ -229,7 +231,7 @@ void mainLoop(void* arg)
} }
if(launcher.tool && event.button.button == SDL_BUTTON_LEFT && launcher.tool_repeat == 0 && !igIsWindowHovered(ImGuiHoveredFlags_AnyWindow)) if(launcher.tool && event.button.button == SDL_BUTTON_LEFT && launcher.tool_repeat == 0 && !igIsWindowHovered(ImGuiHoveredFlags_AnyWindow))
{ {
launcher.tool(vec2(event.button.x, launcher.window_size.y - event.button.y), launcher.used_tool, launcher.tool_size); launcher.tool(vec2(event.button.x, launcher.window_size.y - event.button.y) * launcher.scalling - launcher.render_position, launcher.used_tool, launcher.tool_size);
} }
} }
else if(event.type == SDL_MOUSEBUTTONUP) else if(event.type == SDL_MOUSEBUTTONUP)
@ -255,7 +257,7 @@ void mainLoop(void* arg)
while(launcher.repeat_time > range) while(launcher.repeat_time > range)
{ {
launcher.repeat_time -= range; launcher.repeat_time -= range;
launcher.tool(launcher.mouse.position, launcher.used_tool, launcher.tool_size); launcher.tool((launcher.mouse.position*launcher.scalling)-launcher.render_position, launcher.used_tool, launcher.tool_size);
} }
} }
@ -525,7 +527,14 @@ void mainLoop(void* arg)
} }
launcher.renderer.resize(launcher.window_size); launcher.renderer.resize(launcher.window_size);
launcher.renderer.view(vec2(0,0),vec2(launcher.window_size.x,launcher.window_size.y)); //launcher.renderer.view(vec2(0,0),vec2(launcher.window_size.x,launcher.window_size.y));
//if(384, 768, 1152, 1536)
//576 960 1344 1728
//float scalling;
if(launcher.window_size.y < 360)launcher.scalling = 1;
else launcher.scalling = 1.0 / ((launcher.window_size.y+120)/360);
launcher.renderer.view(launcher.render_position,vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling);
//launcher.renderer.view(vec2(0,0),vec2(1024*launcher.window_size.x/launcher.window_size.y,768));
//glClear(GL_COLOR_BUFFER_BIT); //glClear(GL_COLOR_BUFFER_BIT);
launcher.renderer.clear(); launcher.renderer.clear();

View file

@ -60,7 +60,7 @@ struct DrawSystem
{ {
foreach(i; 0..data.length) foreach(i; 0..data.length)
{ {
launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(32,32), 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), 0, 0 , 0);
//draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1)); //draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1));
} }
} }
@ -81,7 +81,7 @@ struct MoveSystem
foreach(i; 0..data.length) foreach(i; 0..data.length)
{ {
data.locations[i].location.y = data.locations[i].location.y + 1; data.locations[i].location.y = data.locations[i].location.y + 1;
if(data.locations[i].location.y > 400)data.locations[i].location.y = 0; if(data.locations[i].location.y > 300)data.locations[i].location.y = 0;
} }
} }
} }
@ -119,7 +119,7 @@ void simpleStart()
foreach(i; 0..10) foreach(i; 0..10)
foreach(j; 0..10) foreach(j; 0..10)
{ {
loc_comp.location = vec2(i*32+64,j*32+64); loc_comp.location = vec2(i*16+64,j*16+64);
launcher.manager.addEntity(simple.tmpl); launcher.manager.addEntity(simple.tmpl);
} }
} }
@ -147,6 +147,10 @@ void simpleTool(vec2 position, Tool tool, int size)
{ {
position.x += (randomf - 0.5) * size; position.x += (randomf - 0.5) * size;
position.y += (randomf - 0.5) * size; position.y += (randomf - 0.5) * size;
if(position.x > 400)position.x -= 400;
else if(position.x < 0)position.x += 400;
if(position.y > 300)position.y -= 300;
else if(position.y < 0)position.y += 300;
*location = position; *location = position;
} }
launcher.manager.addEntity(tmpl); launcher.manager.addEntity(tmpl);
@ -169,17 +173,18 @@ void simpleEvent(SDL_Event* event)
void spawnEntity() void spawnEntity()
{ {
CLocation* loc_comp = simple.tmpl.getComponent!CLocation; CLocation* loc_comp = simple.tmpl.getComponent!CLocation;
loc_comp.location = vec2(randomf() * 600,0); loc_comp.location = vec2(randomf() * 400,0);
launcher.manager.addEntity(simple.tmpl); launcher.manager.addEntity(simple.tmpl);
} }
bool simpleLoop() bool simpleLoop()
{ {
launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(400,300)) * 0.5;
if(launcher.getKeyState(SDL_SCANCODE_SPACE)) if(launcher.getKeyState(SDL_SCANCODE_SPACE))
{ {
foreach(i;0..1)spawnEntity(); foreach(i;0..1)spawnEntity();
} }
launcher.manager.begin(); launcher.manager.begin();
if(launcher.multithreading) if(launcher.multithreading)

View file

@ -90,9 +90,9 @@ struct Snake
{ {
switch(element(ivec2(x,y)).type) switch(element(ivec2(x,y)).type)
{ {
case MapElement.Type.apple:launcher.renderer.draw(texture, vec2(x*32,y*32), vec2(32,32), vec4(0,32*px,16*px,16*px), 0, 0 , 0);break; 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:launcher.renderer.draw(texture, vec2(x*32,y*32), vec2(32,32), vec4(0,48*px,16*px,16*px), 0, 0 , 0);break; case MapElement.Type.snake:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,48*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.wall:launcher.renderer.draw(texture, vec2(x*32,y*32), vec2(32,32), vec4(0,0,1,1), 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; default:break;
} }
} }
@ -520,8 +520,8 @@ void snakeTool(vec2 position, Tool tool, int size)
position.x += (randomf - 0.5) * size; position.x += (randomf - 0.5) * size;
position.y += (randomf - 0.5) * size; position.y += (randomf - 0.5) * size;
ivec2 ipos; ivec2 ipos;
ipos.x = cast(int)(position.x / 32); ipos.x = cast(int)(position.x / 16);
ipos.y = cast(int)(position.y / 32); ipos.y = cast(int)(position.y / 16);
*ilocation = ipos; *ilocation = ipos;
if(snake.element(ipos).type != MapElement.Type.empty)return; if(snake.element(ipos).type != MapElement.Type.empty)return;
} }
@ -540,6 +540,8 @@ void snakeEvent(SDL_Event* event)
bool snakeLoop() bool snakeLoop()
{ {
launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(288,288)) * 0.5;
/*if(launcher.show_demo_wnd) /*if(launcher.show_demo_wnd)
{ {
igSetNextWindowPos(ImVec2(800 - 260, 30), ImGuiCond_Once, ImVec2(0,0)); igSetNextWindowPos(ImVec2(800 - 260, 30), ImGuiCond_Once, ImVec2(0,0));

View file

@ -34,7 +34,7 @@ struct SpaceInvaders
bool move_system = true; bool move_system = true;
bool draw_system = true; bool draw_system = true;
const vec2 map_size = vec2(600,600); const vec2 map_size = vec2(400,300);
const float cell_size = 60; const float cell_size = 60;
} }
@ -101,7 +101,7 @@ struct CScale
///use component as it value ///use component as it value
alias value this; alias value this;
vec2 value = vec2(32,32); vec2 value = vec2(16,16);
} }
struct CTexture struct CTexture
@ -564,8 +564,8 @@ struct MovementSystem
{ {
foreach(i;0..data.length) foreach(i;0..data.length)
{ {
data.locations[i].x += data.velocity[i].x * launcher.delta_time; data.locations[i].x += data.velocity[i].x * launcher.delta_time * 0.5;
data.locations[i].y += data.velocity[i].y * launcher.delta_time; data.locations[i].y += data.velocity[i].y * launcher.delta_time * 0.5;
} }
} }
} }
@ -630,8 +630,8 @@ struct InputMovementSystem
//move every entity using movement vector //move every entity using movement vector
foreach(i; 0..data.length) foreach(i; 0..data.length)
{ {
data.locations[i].x += move_vector.x * launcher.delta_time * 0.5; data.locations[i].x += move_vector.x * launcher.delta_time * 0.25;
data.locations[i].y += move_vector.y * launcher.delta_time * 0.5; data.locations[i].y += move_vector.y * launcher.delta_time * 0.25;
} }
} }
} }
@ -709,7 +709,7 @@ void spaceInvadersStart()
tex_comp.tex = space_invaders.texture;//laser_tex; tex_comp.tex = space_invaders.texture;//laser_tex;
tex_comp.coords = vec4(0*px,48*px,16*px,16*px); tex_comp.coords = vec4(0*px,48*px,16*px,16*px);
CScale* scale_comp = space_invaders.laser_tmpl.getComponent!CScale; CScale* scale_comp = space_invaders.laser_tmpl.getComponent!CScale;
scale_comp.value = vec2(4,16); scale_comp.value = vec2(2,8);
CVelocity* vel_comp = space_invaders.laser_tmpl.getComponent!CVelocity; CVelocity* vel_comp = space_invaders.laser_tmpl.getComponent!CVelocity;
vel_comp.value = vec2(0,1); vel_comp.value = vec2(0,1);
} }
@ -727,7 +727,7 @@ void spaceInvadersStart()
tex_comp.tex = space_invaders.texture;//ship_tex; tex_comp.tex = space_invaders.texture;//ship_tex;
tex_comp.coords = vec4(32*px,32*px,16*px,16*px); tex_comp.coords = vec4(32*px,32*px,16*px,16*px);
CLocation* loc_comp = space_invaders.enemy_tmpl.getComponent!CLocation; CLocation* loc_comp = space_invaders.enemy_tmpl.getComponent!CLocation;
loc_comp.value = vec2(64,space_invaders.map_size.y - 64); loc_comp.value = vec2(64,space_invaders.map_size.y - 16);
CShootDirection* shoot_dir_comp = space_invaders.enemy_tmpl.getComponent!CShootDirection; CShootDirection* shoot_dir_comp = space_invaders.enemy_tmpl.getComponent!CShootDirection;
shoot_dir_comp.direction = Direction.down; shoot_dir_comp.direction = Direction.down;
CVelocity* vel_comp = space_invaders.enemy_tmpl.getComponent!CVelocity; CVelocity* vel_comp = space_invaders.enemy_tmpl.getComponent!CVelocity;
@ -738,17 +738,17 @@ void spaceInvadersStart()
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl); current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
launcher.manager.addComponents(current_entity.id,CSideMove(0)); launcher.manager.addComponents(current_entity.id,CSideMove(0));
loc_comp.value = vec2(128,space_invaders.map_size.y - 64); loc_comp.value = vec2(128,space_invaders.map_size.y - 16);
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl); current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
launcher.manager.addComponents(current_entity.id,CSideMove(-1)); launcher.manager.addComponents(current_entity.id,CSideMove(-1));
enemy_id = current_entity.id; enemy_id = current_entity.id;
//enemy_tmpl = launcher.manager.allocateTemplate(current_entity.id); //enemy_tmpl = launcher.manager.allocateTemplate(current_entity.id);
loc_comp.value = vec2(256,space_invaders.map_size.y - 64); loc_comp.value = vec2(256,space_invaders.map_size.y - 16);
launcher.manager.addEntity(space_invaders.enemy_tmpl); launcher.manager.addEntity(space_invaders.enemy_tmpl);
loc_comp.value = vec2(0,space_invaders.map_size.y - 64); loc_comp.value = vec2(0,space_invaders.map_size.y - 16);
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl); current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
launcher.manager.addComponents(current_entity.id,CSideMove(0)); launcher.manager.addComponents(current_entity.id,CSideMove(0));
@ -809,6 +809,7 @@ void spaceInvadersEvent(SDL_Event* event)
bool spaceInvadersLoop() bool spaceInvadersLoop()
{ {
launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(400,300)) * 0.5;
/*if(launcher.show_demo_wnd) /*if(launcher.show_demo_wnd)
{ {

View file

@ -55,10 +55,10 @@ struct Buffer
glBufferStorage(GL_ARRAY_BUFFER,size*count,data, flags); glBufferStorage(GL_ARRAY_BUFFER,size*count,data, flags);
}*/ }*/
void bufferSubData(uint size, uint offset, void* data) nothrow void bufferSubData(BindTarget target, uint size, uint offset, void* data) nothrow
{ {
bind(BindTarget.array); bind(target);
glBufferSubData(GL_ARRAY_BUFFER,offset,size,data); glBufferSubData(target,offset,size,data);
} }
void map(BindTarget target) nothrow void map(BindTarget target) nothrow

View file

@ -98,7 +98,7 @@ struct Renderer
alias Technique = RenderTechnique; alias Technique = RenderTechnique;
__gshared Technique technique = Technique.simple; __gshared Technique technique = Technique.vbo_batch;
void* data_ptr; void* data_ptr;
//import ecs_utils.core : RenderTechnique; //import ecs_utils.core : RenderTechnique;
@ -339,6 +339,7 @@ struct Renderer
//import core.stdc.string; //import core.stdc.string;
with(this_) with(this_)
{ {
if(item_id >= MaxObjects)return;
//pos += view_pos; //pos += view_pos;
size.x *= view_size.x; size.x *= view_size.x;
size.y *= view_size.y; size.y *= view_size.y;
@ -470,8 +471,8 @@ struct Renderer
break; break;
case Technique.vbo_batch: case Technique.vbo_batch:
//if(data_index){ //if(data_index){
batch_vbo[0].bufferSubData(item_id*4*16,0,batch_vertices.ptr); batch_vbo[0].bufferSubData(Buffer.BindTarget.array,item_id*4*16,0,batch_vertices.ptr);
batch_ibo[0].bufferSubData(item_id*6*2,0,batch_indices.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_vbo[0].bind(Buffer.BindTarget.array);
batch_ibo[0].bind(Buffer.BindTarget.element_array); batch_ibo[0].bind(Buffer.BindTarget.element_array);
@ -480,8 +481,8 @@ struct Renderer
glVertexAttribPointer(1,2,GL_FLOAT,false,16,cast(void*)8);//} glVertexAttribPointer(1,2,GL_FLOAT,false,16,cast(void*)8);//}
break; break;
case Technique.instanced_attrib_divisor: case Technique.instanced_attrib_divisor:
ubos[0].bufferSubData(data_index,0,uniform_block.ptr); ubos[0].bufferSubData(Buffer.BindTarget.uniform,data_index,0,uniform_block.ptr);
ubos[0].bind(Buffer.BindTarget.array); ubos[0].bind(Buffer.BindTarget.uniform);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);
@ -496,7 +497,7 @@ struct Renderer
break; break;
case Technique.uniform_buffer: case Technique.uniform_buffer:
//ubos[0].bufferData(1,64*MaxObjects,BufferUsage,null); //ubos[0].bufferData(1,64*MaxObjects,BufferUsage,null);
/*if(data_index)*/ubos[0].bufferSubData(data_index,0,uniform_block.ptr); /*if(data_index)*/ubos[0].bufferSubData(Buffer.BindTarget.uniform,data_index,0,uniform_block.ptr);
break; break;
case Technique.uniform_buffer_indexed: case Technique.uniform_buffer_indexed:
ubos[0].bindRange(Buffer.BindTarget.uniform,0,0,block_max_size); ubos[0].bindRange(Buffer.BindTarget.uniform,0,0,block_max_size);
@ -581,6 +582,8 @@ struct Renderer
{ {
material_id = render_list[i].material_id; material_id = render_list[i].material_id;
GfxConfig.materials[material_id].bind(); GfxConfig.materials[material_id].bind();
float[3*4] data = [1,0,0,1,0,0,0,0,0,0,1,1];
GfxConfig.materials[material_id].pushUniforms(data.ptr);
} }
if(texture.data != render_list[i].texture.data) if(texture.data != render_list[i].texture.data)
{ {
@ -589,17 +592,17 @@ struct Renderer
} }
uint instance_count = 16_384; uint instance_count = 16_384;
if(i*16_384 > item_id) if((i+1)*16_384 > item_id)
{ {
instance_count = i*16_384 - item_id; instance_count = item_id%16_384;
} }
/*glVertexAttribPointer(0,2,GL_FLOAT,false,16,cast(void*)(i*16_384*4*16)); 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(1,2,GL_FLOAT,false,16,cast(void*)(i*16_384*4*16+8));
glDrawElements(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2));*/ glDrawElements(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2));
glDrawElementsBaseVertex(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2),i*16_384*4); //glDrawElementsBaseVertex(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2),i*16_384*4);
} }
} }
else if(technique == Technique.ssbo_instanced || technique == Technique.instanced_attrib_divisor) else if(technique == Technique.ssbo_instanced || technique == Technique.instanced_attrib_divisor)
@ -794,9 +797,10 @@ struct Renderer
void view(vec2 pos, vec2 size) void view(vec2 pos, vec2 size)
{ {
view_pos = pos * size - 1; //view_pos = pos * size - 1;
view_size = vec2(2/size.x,2/size.y); view_size = vec2(2/size.x,2/size.y);
sdl_transform = vec4(0,0,1.0/size.x,1.0/size.y); sdl_transform = vec4(0,0,1.0/size.x,1.0/size.y);
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, float angle, uint material_id, uint mesh_id) __draw;