Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
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  
  loading textures in jogl  (Read 3071 times)
0 Members and 1 Guest are viewing this topic.
Offline tortoise

Junior Devvie

<3 Shmups

« Posted 2003-07-13 16:16:48 »

I'm really fighting with this, and there's gotta be a better way. I'm a complete newb and am probably just being stupid Smiley

My game can run in several distinct modes, and it's possible the user can play an entire game and only ever enter one mode. So classes for modes are only loaded as needed. This is causing a problem with texture loading.

As I understand it textures should only really be loaded in init(), but loading them in display() should work too since I've got the GL thread's full attention. Obviously loading textures in display() is not ideal, but it's also not bad because the first calls to display() are usually trivial things like displaying menus and such. By the time the game is really running everything is loaded and fine.

So what I've done is in init() I've requested 100 textures from glGenTextures(). Then I keep track of which ones of those I've handed out.

Whenever someone needs a texture loaded, they give their InputStream and the tga is loaded from disk into a TGAImage object, a texture name (int) is assigned to it, and all of this is shoved into a simple Texture object which goes into a java.util.List.

If on display() the renderer notices the list isn't empty, it goes and loads those textures. This is already getting ugly, but it gets worst when the two threads go after that list at the same time, getting concurrent modification exceptions. The thought of synchronizing the whole process is not appealing, so I'm thinking there's gotta be a better way. Restructuring my game to load everything at the beginning would be a ton of work.
Offline gregorypierce

Senior Devvie

I come upon thee like the blue screen of death....

« Reply #1 - Posted 2003-07-13 20:13:07 »

Okay I have to ask, why are you loading textures in a seperate thread than your rendering thread?

Also a couple of issues that you might want to consider from an architectural perspective. You're storing the textures you've loaded in a List. We if you need to store them at all you're probably going to want to get to them again at which time you'll be doing a linear search through the list to find a texture. Meanwhile you already have a key for the texture (the int that game back when you created the texture) and it is perfectly suited to being coaxed into being the key for a HashMap or similar allowing direct lookups when you need to get to those textures again.

In any event you will need to synchronize access to the list or use a synchronized data structure to stop two threads from modifying the list while its being accessed. You can solve this by using a Vector for the datastructure (it is synchronized) or synchronizing your access methods (a better move, but if you're a true newbie - just use the Vector).

She builds, she builds oh man
When she links, she links I go crazy
Cause she looks like good code but she's really a hack
I think I'll run upstairs and grab a snack!
Offline tortoise

Junior Devvie

<3 Shmups

« Reply #2 - Posted 2003-07-13 21:00:52 »

I'm not loading the textures in a seperate thread, I'm queuing them up to be loaded the next time my rendering thread gets a chance. Since init() is only called once at the beginning and most of my textures won't be loaded till well after that, I need some kind of system to get them in there. By asking GL for a bunch of texture ids ahead of time, I can go ahead and prepare everything before hand.

The list is just textures that need to be loaded (loaded as in glTexImage2D() not loaded up from the filesystem, they're sitting around in TGAImage objects at that point). I have a simple Texture class that's like

class Texture
    int index;
    TGAImage tga;

Once the Textures from this list are loaded into GL then that list is emptied. I don't think I need to get the texture again once GL has it. Everyone who needs to know about a given texture has its int key.

I've gone ahead and synchronized everything. And it is working, I'm just not convinced it's the best way to do it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gregorypierce

Senior Devvie

I come upon thee like the blue screen of death....

« Reply #3 - Posted 2003-07-14 01:04:54 »

Sounds reasonable.

Be careful about the synchronization of 'everything' though. Just synchronize access to the List datastructure. Since you're not a heavily multithreaded application the chances of seeing lots of monitor locking issues isn't high, but just for good practice - synchronize only what needs synchronization.

She builds, she builds oh man
When she links, she links I go crazy
Cause she looks like good code but she's really a hack
I think I'll run upstairs and grab a snack!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

bilznatch (24 views)
2015-08-04 11:03:17

SHC (40 views)
2015-08-01 03:58:20

Jesse (24 views)
2015-07-29 04:35:27

Riven (46 views)
2015-07-27 16:38:00

Riven (25 views)
2015-07-27 15:35:20

Riven (27 views)
2015-07-27 12:26:13

Riven (18 views)
2015-07-27 12:23:39

BurntPizza (41 views)
2015-07-25 00:14:37

BurntPizza (54 views)
2015-07-24 22:06:39

BurntPizza (34 views)
2015-07-24 06:06:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!