Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <title>Phaser - Making your first game, part 1</title>
- <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
- <script type="text/javascript" src="js/phaser.js"></script>
- <style type="text/css">
- body
- {
- margin: 0;
- }
- h2, p
- {
- text-align: center;
- }
- #game-canvas
- {
- width: 100%;
- height: 100%;
- }
- #game-container
- {
- margin: 0 auto;
- }
- #outer-container
- {
- margin: 0 auto;
- margin-top: 20px;
- display: block;
- width: 100%;
- height: 100%;
- /*width: 640px;
- height: 480px;*/
- }
- </style>
- </head>
- <body>
- <script type="text/javascript">
- function resizeGame()
- {
- var gameArea = document.getElementById('game-container');
- var widthToHeight = 4 / 3;
- var newWidth = $("#outer-container").innerWidth();
- var newHeight = $("#outer-container").innerHeight();
- var newWidthToHeight = newWidth / newHeight;
- console.log("Inner Width: " + newWidth + " height: " + newHeight);
- console.log("Normal Width: " +
- $("#outer-container").width() +
- " height: " +
- $("#outer-container").height());
- if (newWidthToHeight > widthToHeight) {
- newWidth = newHeight * widthToHeight;
- gameArea.style.height = newHeight + 'px';
- gameArea.style.width = newWidth + 'px';
- } else {
- newHeight = newWidth / widthToHeight;
- gameArea.style.width = newWidth + 'px';
- gameArea.style.height = newHeight + 'px';
- }
- var gameCanvas = document.getElementById('game-canvas');
- gameCanvas.width = newWidth;
- gameCanvas.height = newHeight;
- }
- function presentRender()
- {
- var fromCanvas = $('#game canvas')[0];
- var toCanvas = $('#game-canvas')[0];
- var toWidth = $('#game-canvas').width();
- var toHeight = $('#game-canvas').height();
- var context = toCanvas.getContext('2d');
- context.drawImage(fromCanvas, 0, 0, toWidth, toHeight);
- }
- $(function()
- {
- var gameCanvas = document.getElementById('game-canvas');
- var context = gameCanvas.getContext('2d');
- context.imageSmoothingEnabled = false;
- resizeGame();
- window.addEventListener('resize', resizeGame, false);
- window.addEventListener('orientationchange', resizeGame, false);
- var gameMem = {};
- gameMem.bmd = null;
- var game = new Phaser.Game(160, 144, Phaser.CANVAS, 'game',
- {
- preload: preload,
- create: create,
- update: update,
- render: render
- }, false, false);
- function preload()
- {
- game.stage.smoothed = false;
- game.load.image('test', 'assets/testImage4.jpg');
- }
- function create()
- {
- // We're going to be using physics, so enable the Arcade Physics system
- game.physics.startSystem(Phaser.Physics.ARCADE);
- game.physics.arcade.gravity.y = 100;
- var img = game.add.sprite(0, 0, "test");
- /*game.physics.enable([img], Phaser.Physics.ARCADE);
- img.body.collideWorldBounds = false;*/
- /*gameMem.physicsGroup = game.add.group();
- gameMem.physicsGroup.enableBody = true;
- gameMem.bmd = game.make.bitmapData();
- gameMem.bmd.load('test');
- //gameMem.bmd.processPixelRGB(forEachPixel, this);
- gameMem.bmd.addToWorld(game.world.centerX, game.world.centerY, 0.5, 0.5);*/
- }
- function update() {}
- function render()
- {
- var context = game.context;
- gameMem.bmd2 = game.make.bitmapData();
- gameMem.bmd2.load($("#game canvas")[0]);
- var canvas = $("#game canvas")[0];
- var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
- for(var i = 0; i < imgData.data.length; i += 4)
- {
- var pixel =
- {
- r: imgData.data[i],
- g: imgData.data[i+1],
- b: imgData.data[i+2],
- a: imgData.data[i+3]
- };
- forEachPixel(pixel);
- imgData.data[i] = pixel.r;
- imgData.data[i+1] = pixel.g;
- imgData.data[i+2] = pixel.b;
- imgData.data[i+3] = pixel.a;
- }
- context.putImageData(imgData, 0, 0);
- presentRender();
- }
- function forEachPixel(pixel)
- {
- //The incoming pixel values
- var r = pixel.r;
- var g = pixel.g;
- var b = pixel.b;
- // Average the pixels together
- // to produce a single value
- var avg = (r + g + b) / 3;
- // Change all the pixel colors to that value
- // Producing a monochromatic color
- r = avg;
- g = avg;
- b = avg;
- // Now load in the gameboy pallettes
- // These are realistic shades to the gameboy
- // The gameboy had 4 shades from darkest to lightest
- var shade1 = {r: 15, g: 56, b: 15};
- var shade2 = {r: 48, g: 98, b: 48};
- var shade3 = {r: 140, g: 173, b: 15};
- var shade4 = {r: 156, g: 189, b: 15};
- // Determine which shade it gets
- // There are 256 possible colors so divide enevly by 4
- var divider = 256 / 4; // (64)
- // This keeps it clamped in case for some weird
- // reason it goes above or below the 0-255 range
- var activeShade = shade1;
- if(avg <= divider * 1) activeShade = shade1;
- else if(avg <= divider * 2) activeShade = shade2;
- else if(avg <= divider * 3) activeShade = shade3;
- else if(avg <= divider * 4) activeShade = shade4;
- else activeShade = shade4;
- // Load in new pixel values
- pixel.r = activeShade.r;
- pixel.g = activeShade.g;
- pixel.b = activeShade.b;
- // and return it
- return pixel;
- }
- });
- </script>
- <h2>Global Filter and Proportional Stretching Test</h2>
- <p></p>
- <div id="game" style="display: none;"></div>
- <div id="outer-container">
- <div id="game-container">
- <canvas id="game-canvas"></canvas>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment