Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#Actualwarnexus

Posted 12 November 2013 - 10:43 PM


You would need to share a lot more details. If you are allowing the video card to do everything and you have a small number of sprites, the video card can easily handle a few hundred sprites, even a 4000-pixel wide background sprite is easy for the card. If instead you are running everything on the CPU then you could easily be trashing your CPU cache with your graphics data, or consuming all your memory bandwidth by moving images back and forth into memory, or having memory access patterns that are cache unfriendly.
 

 

I am happy to share code. Here is the code of my 2D image in my 2D Java game.

I have comments that explains the code.

 

I am doing my painting and I am drawing this background image to Java double buffered graphics canvas

 

Note: The game is not full screen and the code does not use the GraphicsConfiguration Object in Java.

 

I am not sure if the game is using the video card or the CPU because I never use any code that

tells the game to take over either of the two.

package com.nicholaskong.SpaceShoot.characterSprites;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.nicholaskong.SpaceShoot.Game;
import com.nicholaskong.SpaceShoot.Main;
import com.nicholaskong.SpaceShoot.Sprite;
import com.nicholaskong.SpaceShoot.Vector2D;

public class MountainStage extends Sprite implements KeyListener{

    private ArrayList<Image> stage;
    private BufferedImage image;
    private double speed = 5;
    
    public MountainStage(Vector2D position,Spiderman spiderman)
    {
        this(position.getX(),position.getY());
    }
    public MountainStage(double x, double y) {
        
        position = new Vector2D(x,y);
        velocity = new Vector2D();
        
        stage = new ArrayList<>();
        try {
            image = ImageIO.read(new File("src/stage/stage.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            stage.add(image);
            // mountain stage can now listen for key events
            Game.getInstance().addKeyListener(this);
            
        
    }
    public void draw(Graphics g)
    {
        // draw the mountain stage to Java graphics canvas
        g.drawImage(stage.get(0), getX(), getY(),getWidth(),getHeight(),null);
    }
    
    public void update(long milliseconds)
    {
        // mountain stage gets updated with a time-based motion
        position = position.add(velocity.multiply(milliseconds / 16.667));

    }
    
    public int getWidth()
    {
        return image.getWidth();
    }
    
    public int getHeight()
    {
        return image.getHeight();
    }
    public int getX()
    {
        return (int) position.getX();
    }
    
    public int getY()
    {
        return (int) position.getY();
    }
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        
        // while the character Spiderman is alive
        if(Spiderman.alive)
        {
            
            if(e.getKeyCode() == KeyEvent.VK_LEFT)
            {
                /*
                * use -5 instead of say 0 because
                * if I use 0, otherwise the image won;t
                * cover the default black screen of the canvas
                * when moving the mountain stage image
                */
                int lowXThreshold = -5;
                if(position.getX() > lowXThreshold)
                {
                    // lock the image at a particular point
                    position.setX(lowXThreshold);
                
                
                }
                
                // moves the mountain stage background
                position.setX(position.getX() + speed );
            
            }
            else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            {
               /* As the player moves farther right,
                * this makes the mountain stage background
                * image move farther left to achieve the
                * illusion that the player is actually
                * moving in this background that makes
                * up the world.
                *  
                * You will notice the top left corner that represents the
                * image coordinate x position will be at a negative x value
                * when the player moves farther right.
                *
                * subtract off the image width from the game screen's width
                * the difference of this calculation is the distance of the image
                * the player does not see in the game but is merely outside
                * the game window in view.
                *
                * NOTE: when the player sees the game, the player
                * only see a part of an actual image
                *
                * The user will see the rest as the player
                * moves father right in the game
                */
                
                /* the calculation logic to enable the player to scroll
                *  through the entire image.
                *  The logic scales well so it works with any image
                *  of any width size.
                */
                if(-position.getX() > image.getWidth() - Main.getWidth())
                {
                    
                    
                    // locks the image at a particular point
                    position.setX(-(image.getWidth() - Main.getWidth()));
                    
                    
                }
                
                // moves the mountain stage background
                position.setX(position.getX() - speed);
            }
        }
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
}
 

#3warnexus

Posted 12 November 2013 - 10:42 PM


You would need to share a lot more details. If you are allowing the video card to do everything and you have a small number of sprites, the video card can easily handle a few hundred sprites, even a 4000-pixel wide background sprite is easy for the card. If instead you are running everything on the CPU then you could easily be trashing your CPU cache with your graphics data, or consuming all your memory bandwidth by moving images back and forth into memory, or having memory access patterns that are cache unfriendly.
 

 

I am happy to share code. Here is the code of my 2D image in my 2D Java game.

I have comments that explains the code.

 

I am doing my painting and I am drawing this background image to Java double buffered graphics canvas

 

Note: The game is not full screen and the code does not use the GraphicsConfiguration Object in Java.

 

I am not sure if the game is using the video card or the CPU because I never use any code that

tells the game to take over either of the two.

package com.nicholaskong.SpaceShoot.characterSprites;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.nicholaskong.SpaceShoot.Game;
import com.nicholaskong.SpaceShoot.Main;
import com.nicholaskong.SpaceShoot.Sprite;
import com.nicholaskong.SpaceShoot.Vector2D;

public class MountainStage extends Sprite implements KeyListener{

    private ArrayList<Image> stage;
    private BufferedImage image;
    private double speed = 5;
    
    public MountainStage(Vector2D position,Spiderman spiderman)
    {
        this(position.getX(),position.getY());
    }
    public MountainStage(double x, double y) {
        
        position = new Vector2D(x,y);
        velocity = new Vector2D();
        
        stage = new ArrayList<>();
        try {
            image = ImageIO.read(new File("src/stage/stage.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            stage.add(image);
            // mountain stage can now listen for key events
            Game.getInstance().addKeyListener(this);
            
        
    }
    public void draw(Graphics g)
    {
        // draw the mountain stage to Java graphics canvas
        g.drawImage(stage.get(0), getX(), getY(),getWidth(),getHeight(),null);
    }
    
    public void update(long milliseconds)
    {
        // mountain stage gets updated with a time-based motion
        position = position.add(velocity.multiply(milliseconds / 16.667));

    }
    
    public int getWidth()
    {
        return image.getWidth();
    }
    
    public int getHeight()
    {
        return image.getHeight();
    }
    public int getX()
    {
        return (int) position.getX();
    }
    
    public int getY()
    {
        return (int) position.getY();
    }
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        
        // while the character Spiderman is alive
        if(Spiderman.alive)
        {
            
            if(e.getKeyCode() == KeyEvent.VK_LEFT)
            {
                /*
                * use -5 instead of say 0 because
                * if I use 0, otherwise the image won;t
                * cover the default black screen of the canvas
                * when moving the mountain stage image
                */
                int lowXThreshold = -5;
                if(position.getX() > lowXThreshold)
                {
                    // lock the image at a particular point
                    position.setX(lowXThreshold);
                
                
                }
                
                // moves the mountain stage background
                position.setX(position.getX() + speed );
            
            }
            else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            {
               /* As the player moves farther right,
                * this makes the mountain stage background
                * image would farther left to achieve the
                * illusion that the player is actually
                * moving in this background that makes
                * up the world.
                *  
                * You will notice the top left corner that represents the
                * image coordinate x position will be at a negative x value
                * when the player moves farther right.
                *
                * subtract off the image width from the game screen's width
                * the difference of this calculation is the distance of the image
                * the player does not see in the game but is merely outside
                * the game window in view.
                *
                * NOTE: when the player sees the game, the player
                * only see a part of an actual image
                *
                * The user will see the rest as the player
                * moves father right in the game
                */
                
                /* the calculation logic to enable the player to scroll
                *  through the entire image.
                *  The logic scales well so it works with any image
                *  of any width size.
                */
                if(-position.getX() > image.getWidth() - Main.getWidth())
                {
                    
                    
                    // locks the image at a particular point
                    position.setX(-(image.getWidth() - Main.getWidth()));
                    
                    
                }
                
                // moves the mountain stage background
                position.setX(position.getX() - speed);
            }
        }
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
}
 

#2warnexus

Posted 12 November 2013 - 08:47 PM


You would need to share a lot more details. If you are allowing the video card to do everything and you have a small number of sprites, the video card can easily handle a few hundred sprites, even a 4000-pixel wide background sprite is easy for the card. If instead you are running everything on the CPU then you could easily be trashing your CPU cache with your graphics data, or consuming all your memory bandwidth by moving images back and forth into memory, or having memory access patterns that are cache unfriendly.
 

 

I am happy to share code. Here is the code of my 2D image in my 2D Java game.

I have comments that explains the code.

 

I am doing my painting and I am drawing this background image to Java double buffered graphics canvas

 

Note: The game is not full screen and the code does not use the GraphicsConfiguration Object in Java.

 

I am not sure if the game is using the video card or the CPU because I never use any code that

tells the game to take over either of the two.

package com.nicholaskong.SpaceShoot.characterSprites;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.nicholaskong.SpaceShoot.Game;
import com.nicholaskong.SpaceShoot.Main;
import com.nicholaskong.SpaceShoot.Sprite;
import com.nicholaskong.SpaceShoot.Vector2D;

public class MountainStage extends Sprite implements KeyListener{

    private ArrayList<Image> stage;
    private BufferedImage image;
    private double speed = 5;
    
    public MountainStage(Vector2D position,Spiderman spiderman)
    {
        this(position.getX(),position.getY());
    }
    public MountainStage(double x, double y) {
        
        position = new Vector2D(x,y);
        velocity = new Vector2D();
        
        stage = new ArrayList<>();
        try {
            image = ImageIO.read(new File("src/stage/stage.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            stage.add(image);
            // mountain stage can now listen for key events
            Game.getInstance().addKeyListener(this);
            
        
    }
    public void draw(Graphics g)
    {
        // draw the mountain stage to Java graphics canvas
        g.drawImage(stage.get(0), getX(), getY(),getWidth(),getHeight(),null);
    }
    
    public void update(long milliseconds)
    {
        // mountain stage gets updated with a time-based motion
        position = position.add(velocity.multiply(milliseconds / 16.667));

    }
    
    public int getWidth()
    {
        return image.getWidth();
    }
    
    public int getHeight()
    {
        return image.getHeight();
    }
    public int getX()
    {
        return (int) position.getX();
    }
    
    public int getY()
    {
        return (int) position.getY();
    }
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        
        // while the character Spiderman is alive
        if(Spiderman.alive)
        {
            
            if(e.getKeyCode() == KeyEvent.VK_LEFT)
            {
                /*
                * since the mountain stage is added to the game
                * at the origin of the canvas it needs to
                * check for values below 0 which would
                * be a low negative number
                * it is -5 instead of say 0 because
                * i use 0 you will see a black screen
                * when moving the mountain stage image
                */
                int lowXThreshold = -5;
                if(position.getX() > lowXThreshold)
                {
                    // lock the image at a particular point
                    position.setX(lowXThreshold);
                
                
                }
                
                // moves the mountain stage background
                position.setX(position.getX() + speed );
            
            }
            else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            {
               /* As the player moves farther right,
                * this makes the mountain stage background
                * image would farther left to achieve the
                * illusion that the player is actually
                * moving in this background that makes
                * up the world.
                *  
                * You will notice the top left corner that represents the
                * image coordinate x position will be at a negative x value
                * when the player moves farther right.
                *
                * subtract off the image width from the game screen's width
                * the difference of this calculation is the distance of the image
                * the player does not see in the game but is merely outside
                * the game window in view.
                *
                * NOTE: when the player sees the game, the player
                * only see a part of an actual image
                *
                * The user will see the rest as the player
                * moves father right in the game
                */
                
                /* the calculation logic to enable the player to scroll
                *  through the entire image.
                *  The logic scales well so it works with any image
                *  of any width size.
                */
                if(-position.getX() > image.getWidth() - Main.getWidth())
                {
                    
                    
                    // locks the image at a particular point
                    position.setX(-(image.getWidth() - Main.getWidth()));
                    
                    
                }
                
                // moves the mountain stage background
                position.setX(position.getX() - speed);
            }
        }
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
}
 

#1warnexus

Posted 12 November 2013 - 08:41 PM


You would need to share a lot more details. If you are allowing the video card to do everything and you have a small number of sprites, the video card can easily handle a few hundred sprites, even a 4000-pixel wide background sprite is easy for the card. If instead you are running everything on the CPU then you could easily be trashing your CPU cache with your graphics data, or consuming all your memory bandwidth by moving images back and forth into memory, or having memory access patterns that are cache unfriendly.

 

I am happy to share code. Here is the code of my 2D image in my 2D Java game.

I have comments that explains the code.

 

I am doing my painting and I am drawing this background image to Java double buffered graphics canvas

 

package com.nicholaskong.SpaceShoot.characterSprites;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.nicholaskong.SpaceShoot.Game;
import com.nicholaskong.SpaceShoot.Main;
import com.nicholaskong.SpaceShoot.Sprite;
import com.nicholaskong.SpaceShoot.Vector2D;

public class MountainStage extends Sprite implements KeyListener{

    private ArrayList<Image> stage;
    private BufferedImage image;
    private double speed = 5;
    
    public MountainStage(Vector2D position,Spiderman spiderman)
    {
        this(position.getX(),position.getY());
    }
    public MountainStage(double x, double y) {
        
        position = new Vector2D(x,y);
        velocity = new Vector2D();
        
        stage = new ArrayList<>();
        try {
            image = ImageIO.read(new File("src/stage/stage.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            stage.add(image);
            // mountain stage can now listen for key events
            Game.getInstance().addKeyListener(this);
            
        
    }
    public void draw(Graphics g)
    {
        // draw the mountain stage to Java graphics canvas
        g.drawImage(stage.get(0), getX(), getY(),getWidth(),getHeight(),null);
    }
    
    public void update(long milliseconds)
    {
        // mountain stage gets updated with a time-based motion
        position = position.add(velocity.multiply(milliseconds / 16.667));

    }
    
    public int getWidth()
    {
        return image.getWidth();
    }
    
    public int getHeight()
    {
        return image.getHeight();
    }
    public int getX()
    {
        return (int) position.getX();
    }
    
    public int getY()
    {
        return (int) position.getY();
    }
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        
        // while the character Spiderman is alive
        if(Spiderman.alive)
        {
            
            if(e.getKeyCode() == KeyEvent.VK_LEFT)
            {
                /*
                * since the mountain stage is added to the game
                * at the origin of the canvas it needs to
                * check for values below 0 which would
                * be a low negative number
                * it is -5 instead of say 0 because
                * i use 0 you will see a black screen
                * when moving the mountain stage image
                */
                int lowXThreshold = -5;
                if(position.getX() > lowXThreshold)
                {
                    // lock the image at a particular point
                    position.setX(lowXThreshold);
                
                
                }
                
                // moves the mountain stage background
                position.setX(position.getX() + speed );
            
            }
            else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            {
               /* As the player moves farther right,
                * this makes the mountain stage background
                * image would farther left to achieve the
                * illusion that the player is actually
                * moving in this background that makes
                * up the world.
                *  
                * You will notice the top left corner that represents the
                * image coordinate x position will be at a negative x value
                * when the player moves farther right.
                *
                * subtract off the image width from the game screen's width
                * the difference of this calculation is the distance of the image
                * the player does not see in the game but is merely outside
                * the game window in view.
                *
                * NOTE: when the player sees the game, the player
                * only see a part of an actual image
                *
                * The user will see the rest as the player
                * moves father right in the game
                */
                
                /* the calculation logic to enable the player to scroll
                *  through the entire image.
                *  The logic scales well so it works with any image
                *  of any width size.
                */
                if(-position.getX() > image.getWidth() - Main.getWidth())
                {
                    
                    
                    // locks the image at a particular point
                    position.setX(-(image.getWidth() - Main.getWidth()));
                    
                    
                }
                
                // moves the mountain stage background
                position.setX(position.getX() - speed);
            }
        }
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
}
 
 

PARTNERS