Skip to content

Commit

Permalink
Proper screen/sprite update on resize
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed May 24, 2012
1 parent d6d721a commit a38a765
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
1 change: 0 additions & 1 deletion public/assets/js/main.js
Expand Up @@ -41,7 +41,6 @@ require({
//INVADERS = [];
SPRITE = ( new Sprite() ).init();
SCREEN = ( new Screen() ).init()


$(document).ready(function(){

Expand Down
6 changes: 2 additions & 4 deletions public/assets/js/misc/screen.js
Expand Up @@ -16,10 +16,8 @@ Screen = function(){
return this;
},
update : function(){
this.width = window.innerWidth;
this.height = window.innerHeight;

SPRITE.update();
SCREEN["width"] = window.innerWidth;
SCREEN["height"] = window.innerHeight;
}
});

Expand Down
19 changes: 10 additions & 9 deletions public/assets/js/objects/sprite.js
Expand Up @@ -19,6 +19,7 @@ Sprite = function(){
return this;
},
update: function(){
var self = (SPRITE) ? SPRITE : this;
// game has a definite 16x12 grid (4/3 aspect ratio)
var cell_width = window.innerWidth / CONFIG["screen"]["grid"].x;
var cell_height = window.innerHeight / CONFIG["screen"]["grid"].y;
Expand All @@ -27,19 +28,19 @@ Sprite = function(){
// (replace with SCREEN.width...)
var ratio = cell_width / cell_height;

if( ratio > this.ratio ){
if( ratio > self.ratio ){
// widescreen...
// - x padding required
this.height = cell_height;
this.width = this.height * this.ratio;
this.padding.x = (cell_width - this.width) / 2;
this.padding.y = 0;
self.height = cell_height;
self.width = self.height * self.ratio;
self.padding.x = (cell_width - self.width) / 2;
self.padding.y = 0;
} else {
// - y padding required
this.width = cell_width;
this.height = this.width * (1/this.ratio);
this.padding.x = 0;
this.padding.y = (cell_height - this.height) / 2;
self.width = cell_width;
self.height = self.width * (1/self.ratio);
self.padding.x = 0;
self.padding.y = (cell_height - self.height) / 2;
}

}
Expand Down

0 comments on commit a38a765

Please sign in to comment.