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; public static void init() throws SlickException { loadedImages = new HashMap<>(); try { URL url = Art.class.getResource("../res"); 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); } } 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"));
} 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)
|
|
|
|
|
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 { private static Map<String,Image> loadedImages; private static AngelCodeFont font; public static void init() throws SlickException { 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"));
} 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.
|
|
|
|
|
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.phpEDIT: It seems you don't have them aligned, so this wouldn't really work. Shame.
|
|
|
|
|
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.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|