001 /*
002 *
003 * Created: Jun 7 2006
004 *
005 * Copyright (C) 1999-2000 Fabien Sanglard
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020 */
021
022 package renderer;
023
024 import java.applet.Applet;
025 import java.awt.Color;
026 import java.awt.Graphics;
027 import java.awt.Image;
028 import java.awt.image.ColorModel;
029 import java.awt.image.DirectColorModel;
030 import java.awt.image.MemoryImageSource;
031
032 import renderer.modelers.*;
033
034
035 public class Renderer extends Applet implements Runnable
036 {
037
038 private static final long serialVersionUID = 1L;
039
040 private Modeler modeler = null;
041
042 Image vramBundle;
043 public MemoryImageSource source ;
044 public int[] offScreenRaster ;
045
046 public final static int renderedWidth = 640;
047 public final static int renderedHeight = 480;
048
049 public int drawWidth = 640;
050 public int drawHeight = 480;
051
052 public void start()
053 {
054 new Thread(this).start();
055 }
056
057
058
059
060 public void init()
061 {
062 this.setSize(drawWidth,drawHeight);
063
064 setBackground(Color.black);
065
066
067 ColorModel colorModel = new DirectColorModel(32, 0xff0000, 0x00ff00, 0x0000ff, 0);
068
069 offScreenRaster = new int[renderedWidth*renderedHeight];
070
071 source = new MemoryImageSource(renderedWidth, renderedHeight, colorModel , offScreenRaster, 0, renderedWidth);
072 source.setAnimated(true);
073 source.setFullBufferUpdates(true);
074 vramBundle = createImage(source);
075
076 this.modeler = new Tunnel(this);
077 }
078
079
080
081 long now = 0;
082 long before = 0;
083 //float framespd = 0;
084 int fps = 0 ;
085 public static long tick;
086
087 public void run()
088 {
089 before = (int)System.currentTimeMillis();
090
091 while(true)
092 {
093 repaint();
094 now = (int)System.currentTimeMillis();
095 tick = now - before;
096 if(now - before > 1000)
097 {
098 fps = frameCounter;
099 before = now;
100 frameCounter = 0;
101 }
102
103 try
104 {
105 Thread.sleep(20L);
106 }
107 catch(InterruptedException interruptedexception) { }
108
109 }
110 }
111
112 public void update(Graphics g)
113 {
114 paint(g);
115 }
116
117 int frameCounter;
118 boolean showFps = true;
119 public void paint(Graphics g)
120 {
121
122 frameCounter++;
123 this.modeler.drawOffScreen();
124 source.newPixels();
125 g.drawImage(vramBundle, 0, 0, getWidth(),getHeight(), this);
126
127 if(showFps)
128 {
129 g.setColor(Color.white);
130 g.drawString("fps : " + fps, 5, 15);
131 }
132
133 }
134
135 public static int getRenderedHeight() {
136 return renderedHeight;
137 }
138
139
140
141 public static int getRenderedWidth() {
142 return renderedWidth;
143 }
144
145 }
|