Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Slick2D automatically load images? on: 2012-06-11 18:01:41
Well, got an answer from StackExchange. Figured i'd post the working code here for future reference:

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  
103  
104  
105  
106  
107  
package com.kirc.drilldescent.res;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.ResourceLoader;

public class Art
{
   
   private static Map<String,Image> loadedImages;
   private static AngelCodeFont font;
   /**
    * Loads all .png images located in source folders.
    * @throws SlickException
    */

   public static void init() throws SlickException {
     
      loadedImages = new HashMap<>();
      try
      {
         URL url = Art.class.getResource("../res");
         //use filesystem loading
        if(url != null)
         {
            File resDir = new File(new URI(url.toString()));
            File[] files = resDir.listFiles(new FilenameFilter(){
               @Override
               public boolean accept(File dir, String name)
               {
                  if(name.endsWith(".png"))
                     return true;
                  return false;
               }
               
            });
           
   
            for(File f:files)
            {
               FileInputStream fis = new FileInputStream(f);
               Image image = new Image(fis, f.getName(), false);
               loadedImages.put(f.getName(), image);
            }
         }
         //use jar loading
        else
         {
           
            URLConnection uc = ClassLoader.getSystemResource("com/kirc/drilldescent/res").openConnection();
            if(uc instanceof JarURLConnection)
            {
               JarURLConnection juc = (JarURLConnection) uc;
               JarFile jarFile = juc.getJarFile();
               Enumeration<JarEntry> jarFileEntries = jarFile.entries();
               while (jarFileEntries.hasMoreElements()) {
                  JarEntry jarEntry = jarFileEntries.nextElement();
                  String jarEntryName = jarEntry.getName();
                  if(jarEntryName.startsWith("com/kirc/drilldescent/res") && jarEntryName.endsWith(".png"))
                  {
                     Image image = new Image(jarEntryName);
                     loadedImages.put(jarEntryName.replace("com/kirc/drilldescent/res/",""),image);
                  }
                 
               }
            }
           
         }
      } catch (URISyntaxException | IOException e)
      {
         e.printStackTrace();
      }
     
      font = new AngelCodeFont("com/kirc/drilldescent/res/bitmapfont.fnt",Art.get("bitmapfont.png"));

   }
   /**
    *
    * @param filename The filename of the image in the /res folder
    * @return The loaded image of that filename
    */

   public static Image get(String filename)
   {
      return loadedImages.get(filename);
   }
   
   public static AngelCodeFont getFont()
   {
      return font;
   }
}


EDIT: Well, it works before I try to package the natives with jarsplice. After that, ClassLoader.getSystemResource fails to return anything. What does jarsplice do to prevent this from working? (I haven't actually got a clear idea what getSystemResource does either)
2  Game Development / Newbie & Debugging Questions / Re: Slick2D automatically load images? on: 2012-06-11 14:04:47
Okay, but how do I get "pathToZip" to point to itself without having to keep the .jar strictly named? (If you use the .jar's name then you can't rename it or it won't work).
3  Game Development / Newbie & Debugging Questions / Slick2D automatically load images? on: 2012-06-11 04:56:10
So, while I was working on a game I was making, I made myself a little Art class. Here it is:

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  
package com.kirc.drilldescent;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.ResourceLoader;

public class Art
{
   
//   public static Image dirtTile;
  private static Map<String,Image> loadedImages;
   private static AngelCodeFont font;
   /**
    * Loads all .png images located in the /res folder.
    * @throws SlickException
    */

   public static void init() throws SlickException {
     
//      dirtTile = new Image("res/dirtTile.png");
     loadedImages = new HashMap<>();
      try
      {
         URI uri = new URI(ResourceLoader.getResource("res").toString());
         File[] files = new File(uri).listFiles(new FilenameFilter(){

            @Override
            public boolean accept(File dir, String name)
            {
               if(name.endsWith(".png"))
                  return true;
               return false;
            }
           
         });
         
         for(File f:files)
         {
            FileInputStream fis = new FileInputStream(f);
            Image image = new Image(fis, "res/"+f.getName(), false);
            loadedImages.put(f.getName(), image);
         }
      } catch (URISyntaxException | FileNotFoundException e)
      {
         System.err.println("UNABLE TO LOAD IMAGES FROM RES FOLDER!");
         e.printStackTrace();
      }
     
      font = new AngelCodeFont("res/bitmapfont.fnt",Art.get("bitmapfont.png"));

   }
   /**
    *
    * @param filename The filename of the image in the /res folder
    * @return The loaded image of that filename
    */

   public static Image get(String filename)
   {
      return loadedImages.get(filename);
   }
   
   public static AngelCodeFont getFont()
   {
      return font;
   }
}


Unfortunately, while this will automatically load all images in my res/ directory (and let me load them once and only once), it won't work if I decide to pack everything into a .jar. I was wondering if anyone used a nicer pattern to keep all their game art organized and easy to access in their code. I took the lazy way because I didn't want to add a line for every single one of my images. (Also, if you know how to adapt this to a .jar that'd be nice to share.)
4  Games Center / WIP games, tools & toy projects / Re: Pong^2 on: 2012-03-06 03:10:09
How to do the AI:
Well when I think about this kind of stuff I imagine what the player has to do. Generally the player will attempt to hit the ball closest to their goal. Correct?
So we get the ball that is closest in x-terms to the AI's paddle, and navigate the paddle to that ball.
Yeah, I guess that could be a thing for a medium difficulty, instead of factoring speed as well.
5  Games Center / WIP games, tools & toy projects / Re: Pong^2 on: 2012-03-05 05:33:37
Okay, updated it. Now it has pausing, smoother controls, and a harder AI that gets the ball that will arrive first. I have yet to beat it. Can you?
6  Game Development / Game Play & Game Design / Re: Sprites, Animations and Gifs on: 2012-02-29 03:30:34
Not sure if this is what you're looking for, but once I was spriting and thought, "Hey, I wish I could see these animated!" But I didn't have internet at the time so I wrote a tool myself. It's mostly for checking the animation, but it also can save as an animated .gif. The images have to be evenly spaced, however (you can specify sprite height and width).

http://thisissilly.x10.mx/spriteviewer.php

EDIT: It seems you don't have them aligned, so this wouldn't really work. Shame.
7  Games Center / WIP games, tools & toy projects / Re: Pong^2 on: 2012-02-29 03:13:49
Well that's not the problem. It's all the other balls coming in right after it.
8  Games Center / WIP games, tools & toy projects / Re: Pong^2 on: 2012-02-29 03:07:42
Yeah it's really dumb. It follows what is supposed to be the oldest ball. What would you suggest the AI do in a situation like this? Even human intelligence panics when confronted with the situation.
9  Games Center / WIP games, tools & toy projects / Pong^2 on: 2012-02-29 02:51:58
So yeah it's Pong, but the ball splits every time it hits a Paddle. I made it with libGDX. This is my first post here, and my first game worth releasing. I wish it was a bit cooler than this, but meh.

http://thisissilly.x10.mx/pong.php



Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

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

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

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

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

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
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!