Common update:

-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
This commit is contained in:
Mergul 2020-04-24 20:55:25 +02:00
parent 7c263d3ed4
commit 8381ac166b
7 changed files with 61 additions and 40 deletions

View file

@ -51,6 +51,7 @@ struct Launcher
void function() end;
void function(SDL_Event*) event;
void function(vec2, Tool, int) tool;
float scalling;
ivec2 window_size = ivec2(1024,768);
Renderer renderer;
ubyte[] keys;
@ -60,6 +61,7 @@ struct Launcher
ulong timer_freq;
double delta_time;
uint fps;
vec2 render_position;
Tool used_tool;
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))
{
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)
@ -255,7 +257,7 @@ void mainLoop(void* arg)
while(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.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);
launcher.renderer.clear();

View file

@ -60,7 +60,7 @@ struct DrawSystem
{
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));
}
}
@ -81,7 +81,7 @@ struct MoveSystem
foreach(i; 0..data.length)
{
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(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);
}
}
@ -147,6 +147,10 @@ void simpleTool(vec2 position, Tool tool, int size)
{
position.x += (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;
}
launcher.manager.addEntity(tmpl);
@ -169,17 +173,18 @@ void simpleEvent(SDL_Event* event)
void spawnEntity()
{
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);
}
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))
{
foreach(i;0..1)spawnEntity();
}
launcher.manager.begin();
if(launcher.multithreading)

View file

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

View file

@ -34,7 +34,7 @@ struct SpaceInvaders
bool move_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;
}
@ -101,7 +101,7 @@ struct CScale
///use component as it value
alias value this;
vec2 value = vec2(32,32);
vec2 value = vec2(16,16);
}
struct CTexture
@ -564,8 +564,8 @@ struct MovementSystem
{
foreach(i;0..data.length)
{
data.locations[i].x += data.velocity[i].x * launcher.delta_time;
data.locations[i].y += data.velocity[i].y * 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 * 0.5;
}
}
}
@ -630,8 +630,8 @@ struct InputMovementSystem
//move every entity using movement vector
foreach(i; 0..data.length)
{
data.locations[i].x += move_vector.x * launcher.delta_time * 0.5;
data.locations[i].y += move_vector.y * 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.25;
}
}
}
@ -709,7 +709,7 @@ void spaceInvadersStart()
tex_comp.tex = space_invaders.texture;//laser_tex;
tex_comp.coords = vec4(0*px,48*px,16*px,16*px);
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;
vel_comp.value = vec2(0,1);
}
@ -727,7 +727,7 @@ void spaceInvadersStart()
tex_comp.tex = space_invaders.texture;//ship_tex;
tex_comp.coords = vec4(32*px,32*px,16*px,16*px);
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;
shoot_dir_comp.direction = Direction.down;
CVelocity* vel_comp = space_invaders.enemy_tmpl.getComponent!CVelocity;
@ -738,17 +738,17 @@ void spaceInvadersStart()
current_entity = launcher.manager.addEntity(space_invaders.enemy_tmpl);
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);
launcher.manager.addComponents(current_entity.id,CSideMove(-1));
enemy_id = 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);
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);
launcher.manager.addComponents(current_entity.id,CSideMove(0));
@ -809,6 +809,7 @@ void spaceInvadersEvent(SDL_Event* event)
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)
{