Guest User

Untitled

a guest
Apr 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1. /***************************
  2. * Brian Capps
  3. * 04/27/2008
  4. * Description
  5. ****************************/
  6. import javax.media.opengl.GL;
  7. import javax.media.opengl.GLAutoDrawable;
  8. import javax.media.opengl.GLEventListener;
  9. import javax.media.opengl.glu.GLU;
  10. import javax.media.opengl.glu.*;
  11. import com.sun.opengl.util.j2d.TextRenderer;
  12. import java.awt.Font;
  13. import java.awt.geom.*;
  14. import java.awt.event.*;
  15. import java.io.*;
  16. import java.util.*;
  17. import com.sun.opengl.util.texture.*;
  18.  
  19. public class Board implements GLEventListener
  20. {
  21. private String mp3File, artist, album;
  22. private long time;
  23. private ArrayList<Line> lines;
  24. private Music song;
  25. private boolean songIsPlaying;
  26. private int lowestNoteToProcess, lowestNoteToRender;
  27. private int score;
  28. private boolean[] key;
  29. private long dt_timer;
  30.  
  31. private int fretDuration; //number of milliseconds on the fret
  32. private int noteErrorDuration; //number of milliseconds to still accept a note. bidirectional.
  33. private float targetPos; //location of buttons to press
  34. private float length;
  35. float[][] colors;
  36. Note noteToDraw;
  37.  
  38. private GLU glu = new GLU();
  39. private TextRenderer renderer;
  40.  
  41. //test values///////////////////////
  42. //private float y =0.8624921f , z=-2.3839877f;
  43. //1.9000014 0.35450974
  44.  
  45. private float y =1.9000014f, z=0.35450974f;
  46.  
  47. public static Input1 input = new Input1();
  48. private File menuBG = new File (System.getProperty("user.dir")+"/menuBG.png");
  49. private Texture texture;
  50. private float zTest;
  51. private Note redNote= new Note(255f, 0f, 0f), yellowNote= new Note(255f, 255f, 0f),
  52. blueNote = new Note(0f, 0f, 255f), greenNote = new Note(0f, 255f, 0f),
  53. orangeNote = new Note();
  54. ////////////////////////////////////
  55.  
  56. public Board()
  57. {
  58. time = (long) 0;
  59. lines = new ArrayList<Line>();
  60. zTest = -10f;
  61. song = null;
  62. songIsPlaying = false;
  63. lowestNoteToProcess=0;
  64. lowestNoteToRender=0;
  65.  
  66. fretDuration = 4000;
  67. noteErrorDuration = 100;
  68. targetPos = -2.5f;
  69. length = 10f;
  70. dt_timer = 0;
  71.  
  72. colors= new float[][]
  73. {
  74. {255f,0f,0f},
  75. {255f,255f,0f},
  76. {0f,0f,255f},
  77. {0f,255f,0f},
  78. {255f,255f,0f}//filler
  79. };
  80. key = new boolean[21];
  81. }
  82. public void loadData(String file)
  83. {
  84. try
  85. {
  86. Scanner txt = new Scanner(new File(file));
  87. mp3File = txt.nextLine();
  88. artist = txt.nextLine();
  89. album = txt.nextLine();
  90. while(txt.hasNext())
  91. {
  92. long noteTime = txt.nextLong();
  93. int lengthThrowaway = txt.nextInt();
  94. String states = txt.next().trim();
  95. lines.add(new Line(noteTime, states));
  96. }
  97. song = new Music(mp3File);
  98. song.load();
  99. }
  100. catch(Exception e) {
  101. System.out.println(e);
  102. }
  103. }//end loadData
  104. public void playSong()
  105. {
  106. if(song != null && !songIsPlaying && song.play())
  107. {
  108. songIsPlaying = true;
  109. }
  110. }
  111. public void pauseSong()
  112. {
  113. if(song !=null && !songIsPlaying)
  114. {
  115. song.pause();
  116. songIsPlaying = false;
  117. }
  118. }
  119. public void display(GLAutoDrawable gLDrawable)
  120. {
  121. key = Input.keysPressed();
  122.  
  123. double dt = (System.currentTimeMillis() - dt_timer )/1000.0;
  124. dt_timer = System.currentTimeMillis();
  125.  
  126. //Start 3d Rendering
  127. GL gl = gLDrawable.getGL();
  128.  
  129. gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
  130.  
  131. gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  132.  
  133. gl.glLoadIdentity();
  134.  
  135. GLU glu = new GLU();
  136.  
  137. //Code to adjust camera
  138. if(input.getKey(KeyEvent.VK_UP))
  139. z -= .5*dt;
  140. if(input.getKey(KeyEvent.VK_DOWN))
  141. z += .5*dt;
  142.  
  143. if(input.getKey(KeyEvent.VK_LEFT))
  144. y -= .5*dt;
  145. if(input.getKey(KeyEvent.VK_RIGHT))
  146. y += .5*dt;
  147. if(input.getKey(KeyEvent.VK_SPACE))
  148. System.out.println(y+" "+z);
  149.  
  150.  
  151.  
  152. glu.gluLookAt(0,y, z, 0,0,-3, 0,1,0);
  153.  
  154. //orangeNote.drawBar(gLDrawable, zTest);
  155. //redNote.draw(gLDrawable, -3f, -4f, zTest);
  156. //yellowNote.draw(gLDrawable, -1.5f, -4f, zTest);
  157. //blueNote.draw(gLDrawable, 0f, -4f, zTest);
  158. //greenNote.draw(gLDrawable, 1.5f, -4f, zTest);
  159. zTest+= 0.005f;
  160. if (zTest >-2f)
  161. zTest=-10f;
  162.  
  163. gl.glPushMatrix();
  164. gl.glEnable(GL.GL_BLEND);
  165. //gl.glRotatef(70,1,-2,1);
  166.  
  167. gl.glBegin(GL.GL_QUADS);
  168. //Draw the Board
  169. //x goes basically from -1 to 1(camera changed tho)
  170. //y stays same
  171. //board length is -z
  172. gl.glColor4f(40/256f,100/256f,150/256f,1f);//R,G,B,A
  173. gl.glVertex3f(-3f,-4f, 0f);//x,y,z
  174.  
  175. gl.glColor4f(40/256f,100/256f,150/256f,1f);
  176. gl.glVertex3f(3f,-4f, 0f);
  177.  
  178. gl.glColor4f(60/256f,150/256f,200/256f,0f);
  179. gl.glVertex3f(3f,-4f, -10f);
  180.  
  181. gl.glColor4f(60/256f,150/256f,200/256f,0f);
  182. gl.glVertex3f(-3f,-4f, -10f);
  183.  
  184. //All y values on top of the Board must have at least
  185. //0.0001f added for some reason
  186. //Bottom bar - Orange
  187. gl.glColor4f(255/256f,165/256f,0/256f, 1f);
  188. gl.glVertex3f(-3f,-4f+.0001f, -2.15f);//close left
  189. gl.glVertex3f(3f,-4f+.0001f, -2.15f);//close right
  190. gl.glVertex3f(3f,-4f+.0001f, -2.85f);//far right
  191. gl.glVertex3f(-3f,-4f+.0001f, -2.85f);//far left
  192. //RedNote
  193. gl.glColor4f(1f,0f,0f, 1f);
  194. gl.glVertex3f(-3f,-4f+.001f, -2.25f);
  195. gl.glVertex3f(-1.5f,-4f+.001f, -2.25f);
  196. gl.glVertex3f(-1.5f,-4f+.001f, -2.75f);
  197. gl.glVertex3f(-3f,-4f+.001f, -2.75f);
  198. //YellowNote
  199. gl.glColor4f(1f,1f,0f, 1f);
  200. gl.glVertex3f(-1.5f,-4f+.001f, -2.25f);
  201. gl.glVertex3f(0f,-4f+.001f, -2.25f);
  202. gl.glVertex3f(0f,-4f+.001f, -2.75f);
  203. gl.glVertex3f(-1.5f,-4f+.001f, -2.75f);
  204. //BlueNote
  205. gl.glColor4f(0f,0f,1f, 1f);
  206. gl.glVertex3f(0f,-4f+.001f, -2.25f);
  207. gl.glVertex3f(1.5f,-4f+.001f, -2.25f);
  208. gl.glVertex3f(1.5f,-4f+.001f, -2.75f);
  209. gl.glVertex3f(0f,-4f+.001f, -2.75f);
  210. //GreenNote
  211. gl.glColor4f(0f,1f,0f, 1f);
  212. gl.glVertex3f(1.5f,-4f+.001f, -2.25f);
  213. gl.glVertex3f(3f,-4f+.001f, -2.25f);
  214. gl.glVertex3f(3f,-4f+.001f, -2.75f);
  215. gl.glVertex3f(1.5f,-4f+.001f, -2.75f);
  216. //End Bottom Bar
  217.  
  218. this.renderNotes(gLDrawable, dt);
  219.  
  220. /////////////////////////////////////
  221. gl.glEnd();
  222.  
  223. gl.glDisable(GL.GL_BLEND);
  224. gl.glPopMatrix();
  225.  
  226. try { Thread.sleep(1); } catch (Exception e) {}
  227. }
  228. public void renderNotes(GLAutoDrawable gLDrawable, double dt)
  229. {
  230. //RENDER NOTES////////////////////////
  231. if (songIsPlaying)
  232. {
  233. time = song.getTime()*(long)dt;
  234. for(int i=lowestNoteToProcess; i<lines.size(); i++)
  235. {
  236. Line line = lines.get(i);
  237. if(line.getTime() - noteErrorDuration > time)
  238. break;
  239. if(line.getState()==0) //not pressed
  240. {
  241. if(time > line.getTime() + noteErrorDuration) //missed line
  242. {
  243. System.out.println("missed line");
  244. line.setState(3);
  245. score -= 1;
  246. lowestNoteToProcess++;
  247. }
  248. } //code below takes care of this
  249.  
  250. }//end for
  251.  
  252. //find closest line in bounds to be pressed
  253. //if a line exists
  254. //see if correct key combo was pressed
  255. //do the thing
  256. //else
  257. //play a bad line sound
  258. //if it doesnt exist
  259. //play a bad line sound
  260. Line closest = null;
  261. long closestDistance = 1000000;
  262. for(int i=lowestNoteToProcess; i<lines.size(); i++)
  263. {
  264. Line n = lines.get(i);
  265. if(n.getTime() - noteErrorDuration > time)
  266. break;
  267. if(n.getState() == 1) //user is holding down this line, so it is the only one that can be processed
  268. {
  269. closest = n;
  270. break;
  271. }
  272. if(Math.abs(time - n.getTime()) <= closestDistance && time >= n.getTime() - noteErrorDuration && time <= n.getTime() + noteErrorDuration)
  273. {
  274. closest = n;
  275. closestDistance = (long)Math.abs(time - n.getTime());
  276. }
  277. }
  278. if(closest != null)
  279. {
  280. if(closest.getState() == 0) //not pressed
  281. {
  282. boolean seq = true;
  283. for(int x=0; x<5; x++)
  284. if(key[x] != closest.getNotes()[x])
  285. { seq = false; break; }
  286. if(seq)
  287. {
  288. if(closest.getLength() == 0)
  289. {
  290. System.out.println("pressed button");
  291. closest.setState(2); //pressed button
  292. lowestNoteToProcess++;
  293. }
  294. else
  295. {
  296. System.out.println("pressed and holding button");
  297. closest.setState(1); //holding down button
  298. }
  299.  
  300. score += 1;
  301. }
  302. else
  303. {
  304. //play bad line sound
  305. }
  306. }
  307. /*else if(closest.getState() == 1)
  308. { //holding and strummed, cant do that
  309. closest.getState() = 2;
  310. System.out.println("you interrupted the holding");
  311. lowestNoteToProcess++;
  312. //play bad line sound
  313. }*/
  314. }
  315. else //(if closest == null)
  316. {
  317. //play bad line sound
  318. }
  319.  
  320. //Part 2
  321. for(int i=lowestNoteToRender; i<lines.size(); i++)
  322. {
  323. Line line = lines.get(i);
  324. float posz = (line.getTime() + -targetPos/length*fretDuration - time) / fretDuration * length; //head
  325. if(posz > length) break; //not rendered yet
  326. float posz2 = (line.getTime()+line.getLength() + -targetPos/length*fretDuration - time) / fretDuration * length; //tail
  327. if(posz2 <= -2.5) //will never be rendered again
  328. {
  329. lowestNoteToRender++;
  330. continue;
  331. }
  332. if(posz <= length)
  333. for(int x=0; x<5; x++)
  334. {
  335. if(!line.getNotes()[x]) continue;
  336. if(line.getState() == 2) continue;//pressed
  337.  
  338. if(line.getState() == 3) //missed
  339. noteToDraw = new Note(127f, 127f, 127f);
  340. else
  341. noteToDraw = new Note(colors[x][0], colors[x][1], colors[x][2]);
  342. if (x<4)
  343. noteToDraw.draw(gLDrawable, -3+(1.5f*x), -4, -posz);
  344. else
  345. noteToDraw.drawBar(gLDrawable, -posz);
  346. }
  347. }
  348. }//end if songIsPlaying
  349. }
  350. public void init(GLAutoDrawable gLDrawable)
  351. {
  352. renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 36)); //creates textrenderer
  353. GL gl = gLDrawable.getGL();
  354. gl.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
  355. gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Black Background
  356. gl.glClearDepth(1.0f); // Depth Buffer Setup
  357. gl.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
  358. gl.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing To Do
  359. // Really Nice Perspective Calculations
  360. gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
  361. }
  362. public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width,
  363. int height)
  364. {
  365. GL gl = gLDrawable.getGL();
  366. float h = (float)width / (float)height;
  367. gl.glMatrixMode(GL.GL_PROJECTION);
  368. gl.glLoadIdentity();
  369. gl.glFrustum(-h, h, -1, 1, 1, 600);
  370. gl.glMatrixMode(GL.GL_MODELVIEW);
  371. gl.glLoadIdentity();
  372. gl.glTranslatef(0.0f, 0.0f, -6f);
  373. }
  374. public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
  375. }//end class
Add Comment
Please, Sign In to add comment