Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  'ResourceManager' lwjgl [Criticism Accepted Warmly!]  (Read 1167 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Wizard


Medals: 65
Projects: 4
Exp: 3 years


Radirius Software Developer


« Posted 2013-09-19 03:13:03 »

Please give constructive criticism, since I did all of this with minimal knowledge of openAL, and it is my first try at a 'resource manager.' This file is just so that I can load resources, and not have to make a new Object for every single entity I have. Like say I had an entity: I do not want to initialize the same texture/sound for every single entity! I want to load it once, and then reference it later!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
package com.wessles.MERCury;

import java.io.*;
import java.util.Vector;

import org.newdawn.slick.openal.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import static org.lwjgl.openal.AL10.*;

/**
 * @from MERCury
 * @author wessles
 * @website www.wessles.com
 */


public class ResourceManager {
   private Vector<Texture> textures = new Vector<Texture>();

   /**
    * Used to generate an {@code int src} corresponding to your .wav (more sound types coming soon!) file (location specified in {@code String location}) that you can later reference in use of {@code playSound(int src), pauseSound(int src), stopSound(int src),} and {@code rewindSound(int src)}.
    *
    * @param location
    *           The location of your .wav file (more sound types coming soon!).
    * @return An id that corresponds to the sound that you gave. You can get that sound back by using this id in {@code playSound(int src);}
    * @throws FileNotFoundException
    */

   public int loadSound(String location) throws FileNotFoundException {
      WaveData data = WaveData.create(new BufferedInputStream(new FileInputStream(location)));
      int buffer = alGenBuffers();
      alBufferData(buffer, data.format, data.data, data.samplerate);
      data.dispose();
      int source = alGenSources();
      alSourcei(source, AL_BUFFER, buffer);
      return source;
   }

   /**
    * Plays a sound corresponding to the {@code src} that you pass in.
    *
    * @param src
    *           Reference to the sound's source.
    */

   public void playSound(int src) {
      alSourcePlay(src);
   }

   /**
    * Pauses a sound corresponding to the {@code src} that you pass in.
    *
    * @param src
    *           Reference to the sound's source.
    */

   public void pauseSound(int src) {
      alSourcePause(src);
   }

   /**
    * Stops a sound corresponding to the {@code src} that you pass in.
    *
    * @param src
    *           Reference to the sound's source.
    */

   public void stopSound(int src) {
      alSourceStop(src);
   }

   /**
    * Rewinds a sound corresponding to the {@code src} that you pass in.
    *
    * @param src
    *           Reference to the sound's source.
    */

   public void rewindSound(int src) {
      alSourceRewind(src);
   }

   /**
    * Generates an {@code int src} corresponding to your texture that may be used in {@code getTexture(int src);}.
    *
    * @param format
    *           The format of the image you wish to load. I.E. "PNG" "JPG"
    * @param location
    *           The location of the image you wish to load. I.E. "res/genericuseforexpamples.png"
    * @return An {@code int src} corresponding to your texture that may be used in {@code getTexture(int src);}.
    * @throws IOException
    */

   public int loadTexture(String format, String location) throws IOException {
      textures.add(TextureLoader.getTexture(format, this.getClass().getResourceAsStream(location)));
      return textures.size()-1;
   }
   
   /**
    * Gives you a texture based off of a source id, given at {@code loadTexture();}.
    * @param src The source id corresponding to the texture you wish to load, given at {@code loadTexture();}.
    * @return The texture corresponding to the source id you got with {@code loadTexture();}.
    */

   public Texture getTexture(int src) {
      return textures.get(src);
   }
}


And yes, I am not doing all this from scratch   Smiley I am using slick-util, and wrapping it. Dont judge!

Offline wessles

JGO Wizard


Medals: 65
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #1 - Posted 2013-09-19 04:00:03 »

Updated: Now I have added in textures!

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2013-09-19 04:03:12 »

I don't understand. Why use slick util when you can creates your own texture loader? This code isn't honestly too useful unless you provide a different way to load the textures. Anyone can make this, what's hard and useful to share is a texture processor.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Wizard


Medals: 65
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #3 - Posted 2013-09-19 04:06:55 »

This isn't supposed to be a new loader or anything. It is a manager of resources, so that you have a way to initialize things once. I will make a new loader soon, but right now I am making a framework. I'll focus on that soon though, now that you say it!

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2013-09-19 04:10:20 »

I'm sorry, I just don't like the fact that people post half finished, already showed off a million other times, pieces of code Tongue If you have some new amazing algorithm, yeah post it, but a resource loader isn't a new idea, its been around forever so I just don't see the need to post it here!

Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #5 - Posted 2013-09-19 05:49:05 »

It doesn't neccessarily have to be new, just uncommon enough for people to learn something.

I made a post a long while ago about an extremely simple and 2x faster AABB collision detection. I by no means was the first to discover it (although I came up with it myself), but it was unknown enough that I posted it, and people have linked to it a few times.

So, @OP: Don't be discouraged to post code like this. Just 1) Make sure it's complete; 2) Make sure it's uncommonly used, or even completely new; and 3) Make sure it is better than the current 'favoured' approach in some way.

Offline SHC
« Reply #6 - Posted 2013-09-19 06:03:19 »

I don't like Slick-Util as well. You can instead code your loading mechanism in less than a 100 lines. I have it in my LWJGL Tutorial Series at here Textures

Offline Danny02
« Reply #7 - Posted 2013-09-19 10:25:32 »

letting aside the other criticism in this thread.

Some basics about your code, which hopefully helps you to code better in the future:
  • Use a real class for your sounds, this int id is some implementation detail of OpenAL. You can then also put the play,pause ... commands In this SoundCLip class.
  • Do you know and need what makes Vector special? If not use an ArrayList (the Oracle docs about the collection classes are a must read).
  • An more Advanced step would be to try to generalize things. One generic resource cache for one resource type. Then in your app you would have one sound cache, one texture cache and so on.

ps: You can read about my very advanced resource managment system in this old thread
Offline Troncoso

JGO Coder


Medals: 20



« Reply #8 - Posted 2013-09-19 13:29:36 »

Why are you using a Vector? If you're just holding a list of stuff....use a List..
Online Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2013-09-19 13:41:55 »

1  
2  
3  
4  
5  
6  
7  
8  
   public int loadTexture(String format, String location) throws IOException {
      textures.add(TextureLoader.getTexture(format, this.getClass().getResourceAsStream(location)));
     return textures.size();
   }

   public Texture getTexture(int src) {
     return textures.get(src);
   }

This proves you haven't even run the code once. Clueless

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Wizard


Medals: 65
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #10 - Posted 2013-09-19 21:58:13 »

Yup. I haven't run it, nor did I say that I did, thus the '[Criticism Accepted Warmly!]'
I'll fix that right away!

Offline wessles

JGO Wizard


Medals: 65
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #11 - Posted 2013-09-19 22:23:29 »

Why are you using a Vector? If you're just holding a list of stuff....use a List..
Vectors are faster, according to this.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2013-09-19 22:25:22 »

2001

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Danny02
« Reply #13 - Posted 2013-09-19 22:31:58 »

ok let me write you this in more words as riven uses.

Vectors are outdated and slow. Slow because all operations are synchronized.
The general recommendation is to use ArrayLists.

Also, using Java benchmark before Java 1.4 have no value whatsoever.
Offline wessles

JGO Wizard


Medals: 65
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #14 - Posted 2013-09-19 22:35:27 »

Ok. ArrayLists it is!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!