Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package com.franklygames.main;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Canvas;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8. import java.awt.image.BufferStrategy;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.image.DataBufferInt;
  11.  
  12. import javax.swing.JFrame;
  13.  
  14. import com.franklygames.gfx.Screen;
  15. import com.franklygames.gfx.SpriteSheet;
  16.  
  17. public class Game extends Canvas implements Runnable{
  18.  
  19. private static final long serialVersionUID = 1L;
  20.  
  21. public static final int WIDTH = 160;
  22. public static final int Height = WIDTH/12*9;
  23. public static final int SCALE = 6;
  24. public static final String NAME = "Game";
  25.  
  26. private JFrame frame;
  27.  
  28. public boolean running = false;
  29. public int tickCount = 0;
  30.  
  31. private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  32. private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  33.  
  34. private Screen screen;
  35.  
  36. public Game(){
  37. setMinimumSize(new Dimension(WIDTH*SCALE, Height* SCALE));
  38. setMaximumSize(new Dimension(WIDTH*SCALE, Height* SCALE));
  39. setPreferredSize(new Dimension(WIDTH*SCALE, Height* SCALE));
  40.  
  41. frame = new JFrame(NAME);
  42.  
  43. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44. frame.setLayout(new BorderLayout());
  45.  
  46. frame.add(this, BorderLayout.CENTER);
  47. frame.pack();
  48.  
  49. frame.setResizable(false);
  50. frame.setLocationRelativeTo(null);
  51. frame.setVisible(true);
  52. }
  53.  
  54. public void init(){
  55. screen = new Screen(WIDTH,HEIGHT, new SpriteSheet("/sprite_sheet.png"));
  56. }
  57.  
  58. public synchronized void start() {
  59. running = true;
  60. new Thread(this).start();
  61.  
  62. }
  63.  
  64. public synchronized void stop() {
  65. running = false;
  66. }
  67.  
  68. public void run(){
  69. long lastTime = System.nanoTime();
  70. double nsPerTick = 1000000000D/60D;
  71.  
  72. int ticks = 0;
  73. int frames = 0;
  74.  
  75. long lastTimer = System.currentTimeMillis();
  76. double delta = 0;
  77.  
  78. init();
  79.  
  80. while(running){
  81. long now = System.nanoTime();
  82. delta +=(now - lastTime) / nsPerTick;
  83. lastTime = now;
  84. boolean shouldRender = true;
  85.  
  86. while(delta >= 1){
  87. ticks++;
  88. tick();
  89. delta -= 1;
  90. shouldRender = true;
  91. }
  92.  
  93. try {
  94. Thread.sleep(2);
  95. } catch (InterruptedException e) {
  96. e.printStackTrace();
  97. }
  98. if(shouldRender){
  99. frames++;
  100. render();
  101. }
  102.  
  103. if(System.currentTimeMillis() - lastTimer >= 1000){
  104. lastTimer += 1000;
  105. System.out.println(frames + "," + ticks);
  106. frames = 0;
  107. ticks = 0;
  108. }
  109. }
  110. }
  111.  
  112. public void tick(){
  113. tickCount++;
  114.  
  115. for(int i = 0; i< pixels.length; i++){
  116. pixels[i] = i + tickCount;
  117. }
  118.  
  119. }
  120.  
  121. public void render(){
  122. BufferStrategy bs = getBufferStrategy();
  123. if(bs == null){
  124. createBufferStrategy(3);
  125. return;
  126. }
  127.  
  128. //screen.render(pixels, 0, WIDTH);
  129.  
  130.  
  131. Graphics g = bs.getDrawGraphics();
  132. g.setColor(Color.BLACK);
  133. g.fillRect(0, 0, getWidth(), getHeight());
  134. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  135. g.dispose();
  136. bs.show();
  137.  
  138. }
  139.  
  140. public static void main(String[] args){
  141. new Game().start();
  142. }
  143.  
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement