Skip to content

Commit

Permalink
Point array buffer to the correct location on colorShaderDrawRectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
elfedy committed Dec 2, 2022
1 parent 180e9d7 commit 3f7ec81
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 47 deletions.
32 changes: 17 additions & 15 deletions dev/watch.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
const fsevents = require('fsevents');
const chokidar = require('chokidar');
const { exec } = require('child_process');

console.log("Started watching /src directory...");

const stop = fsevents.watch('./src', (path, flags, id) => {
const info = fsevents.getInfo(path, flags, id);
if(info.event === 'moved') {
console.log(`Detected change in ${info.path}.`);
exec('./build.sh dev', (error, stdout, stderr) => {
if(error) {
console.log(`error: ${error.message}`);
}
if(stderr) {
console.log(`srderr: ${stderr}`);
}
const command = './bash.rc';

console.log(stdout);
})
}
chokidar.watch('./src', (path) => {
console.log(`Detected change in ${path}.`);
try {
exec(command, (error, stdout, stderr) => {
console.log(stdout);
if(error) {
console.log(`error: ${error.message}`);
}
if(stderr) {
console.log(`stderr: ${stderr}`);
}
})
} catch(e) {
console.log(`FAILED: ${e}`);
}
})
187 changes: 180 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"url": "https://github.com/elfedy/svg-draw/issues"
},
"homepage": "https://github.com/elfedy/svg-draw#readme",
"dependencies": {},
"devDependencies": {
"fsevents": "^2.1.2"
"dependencies": {
"chokidar": "^3.5.3"
}
}
26 changes: 7 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function run(sprite) {
[ 'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'],
[ 'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'],
[ 'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'],
]
];


if(gl === null) {
Expand Down Expand Up @@ -256,7 +256,7 @@ function run(sprite) {
},

// Enemies
enemySpawnCountdown: 3 * time.seconds,
enemySpawnCountdown: 1 * time.seconds,
enemyColor: {
r: 0.8,
g: 0.2,
Expand Down Expand Up @@ -562,17 +562,6 @@ function run(sprite) {
// ColorShader
// Create and bind buffer to the position attribute
gl.useProgram(colorShaderProgram);
gl.bindBuffer(gl.ARRAY_BUFFER, colorShader.buffers.aPosition);
gl.vertexAttribPointer(
colorShader.locations.aPosition,
2, // size: components per iteration
gl.FLOAT, // data type
false, // normalize
0, // stride: bytes between beggining of consecutive vetex attributes in buffer
0 // offset: where to start reading data from the buffer
);
// Enable vertex attribute
gl.enableVertexAttribArray(colorShader.locations.aPosition);

// TODO(fede): optimize tile drawing:
// * Just do a single pass through all the tiles.
Expand All @@ -593,9 +582,9 @@ function run(sprite) {
// Draw Tiles and store ground tiles to render
// after bullets
let grassTiles = []
for(var row = 0; row < Game.tileRows; row++) {
for(var col = 0; col < Game.tileCols; col++) {
var tile = Game.tiles[row][col];
for(let row = 0; row < Game.tileRows; row++) {
for(let col = 0; col < Game.tileCols; col++) {
let tile = Game.tiles[row][col];
if(tile == 'x') continue;
if(tile !== 'g') {
let tileName
Expand Down Expand Up @@ -624,7 +613,6 @@ function run(sprite) {
}
}
}


gl.useProgram(colorShaderProgram);
// DrawBullets
Expand Down Expand Up @@ -851,8 +839,8 @@ function bulletCreate(tank: Tank) {
tank.bullet = {
vx: vx,
vy: vy,
width: 10,
height: 10,
width: 5,
height: 5,
position: {
x: tank.position.x,
y: tank.position.y,
Expand Down
15 changes: 12 additions & 3 deletions src/shaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,19 @@ function squareVertices(width: number, height: number, x: number, y: number): nu
}

// COLOR SHADER
function colorShaderDrawRectangle(gl, colorShader, color: Color, width, height, x, y)
function colorShaderDrawRectangle(gl, colorShader, color: Color, width, height, x, y, debugMe =false)
{
var glLocations = colorShader.locations;
let glLocations = colorShader.locations;
gl.bindBuffer(gl.ARRAY_BUFFER, colorShader.buffers.aPosition);
gl.vertexAttribPointer(
glLocations.aPosition,
2, // size: components per iteration
gl.FLOAT, // data type
false, // normalize
0, // stride: bytes between beggining of consecutive vetex attributes in buffer
0 // offset: where to start reading data from the buffer
);
gl.enableVertexAttribArray(glLocations.aPosition);
// Set matices
var matrixProjection = mat3.projection(gl.canvas.width, gl.canvas.height);
var matrixChangeOrigin = mat3.translation(-width / 2, -height / 2);
Expand Down Expand Up @@ -141,7 +151,6 @@ var SPRITE_META = {
width: 20,
height: 20,
},
// TODO: The rest I added
};

function textureShaderDrawTank(gl, textureShader, textureName, tank) {
Expand Down

0 comments on commit 3f7ec81

Please sign in to comment.