Show Posts
|
Pages: [1] 2
|
1
|
Game Development / Newbie & Debugging Questions / Re: Saving a "map" into a single file
|
on: 2017-10-23 15:57:08
|
Why save all information about a level, even resources? If resources are static, you don't need to save images, or level structure, but only entities state (position, hp, etc...) I'm afraid I don't quite understand what you mean. If I don't save these resources (images, level structure, etc.) then how is the program meant to "remember" how it looks like after the program is shut down? If I have an image with a nice texture of a rock, which I use as an obstacle in the game, I need to save that as a file right? If I compile a JAR file with the game I'm making and I send it to a friend for testing, the game will need that image to render the map, right? What I see as a potential problem is if I have hundreds of texture images and I just put them inside folders, if some disappears the game will have problems. On the other hand, if all are in a single file then you either have a fully functional map, or you have nothing at all.
|
|
|
2
|
Game Development / Newbie & Debugging Questions / Saving a "map" into a single file
|
on: 2017-10-19 21:56:10
|
FINAL UPDATE: I managed to work it out. Am still interested in case anyone has a nice way of putting together a large number of objects into a single file in a way that is suitable for saving game map dataI am working on a Tile-based labyrinth game. I want to be able to save an entire Map (or level, you might also call it) into a single file. Currently I can save all the information about the Map structure as text and use that to recreate the Map later, but I have to save/load all the images I use to individual files which are loaded separately. If possible, I would like to be able to save all the images I use into the same file as the map itself. Maybe in a structure similar to how a folder looks like, only as a single file. This is to guarantee that if the Map exists, all of the resources are there as well. So, my question; is there a way to make a single file that can contain multiple files, similar to how a folder would be like? The file would start with a set of text (a number of lines) and it'd be fine if I have to put the information about the images that are stored there as text as well if I need to do that in order to load the images that I want to save after the text in the file UPDATE: I am trying to use Zip Files for this. This is my code so far (first part seems to work, see bottom of the post for my current problem; reading the saved image back)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
| public static void saveAllToFile(String path, String textData, String[] imageNames, BufferedImage[] images) throws Exception{ File saveFile = new File(path); if(!saveFile.exists()) saveFile.createNewFile(); FileOutputStream fos = new FileOutputStream(saveFile); ZipOutputStream zos = new ZipOutputStream(fos); addToZipFile("TEXT_DATA", textData.getBytes(), zos); int limit = Math.min(imageNames.length, images.length); ByteArrayOutputStream baos; for(int i=0; i<limit; i++){ baos = new ByteArrayOutputStream(); ImageIO.write( images[i], "jpg", baos ); baos.flush(); addToZipFile(imageNames[i], baos.toByteArray(), zos); baos.close(); } zos.close(); fos.close(); } private static void addToZipFile(String entryName, byte[] data, ZipOutputStream zos) throws Exception { if(DEBUG) System.out.println("Writing '" + entryName + "' to zip file"); ZipEntry zipEntry = new ZipEntry(entryName); zos.putNextEntry(zipEntry); zos.write(data, 0, data.length); zos.closeEntry(); } |
The issue I am facing now is how to properly read it back later. SECOND UPDATE: I managed to figure it out. All is well!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public static BufferedImage[] loadImagesFromZipFile(String path) throws Exception{ File file = new File(path); if(!file.exists()) return null;
ZipFile zFile = new ZipFile(file); ArrayList<BufferedImage> images = new ArrayList<BufferedImage>(); FileInputStream fis = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(fis); ZipEntry entry; BufferedImage img; while((entry = zis.getNextEntry()) != null){ if(entry.isDirectory()) continue; img = ImageIO.read(zFile.getInputStream(entry));
if(img != null) images.add(img); } return images.toArray(new String[images.size()]); } |
|
|
|
3
|
Game Development / Newbie & Debugging Questions / JButtons on top of an image
|
on: 2015-08-07 17:31:09
|
I have an image I use as a background in a JFrame (by putting it in a JLabel). I have a pair of JButtons I want to add on top of that image. I just can't find a way to make Components stack. Anyone have an idea? I tried checking out JLayeredPane but it was quite confusing and I couldn't find any easy-to-understand explanation
|
|
|
5
|
Game Development / Newbie & Debugging Questions / Re: Using enum, how does this work?
|
on: 2015-01-03 00:28:50
|
Um, I think I might have been misunderstood. I mean something like 1 2 3 4 5 6
| public enum FB_TYPE { GREEN, WRINKLED, SWEET, ALL; }
|
This is the code that has me confused. So, that ALL works for all the three previous or what? Does FB_TYPE.ALL == FB_TYPE.GREEN result in a true or a false? Ok so I head that you have to use instanceof, fine. So, does this become true or false? 1
| FB_TYPE.ALL instanceof FB_TYPE.GREEN |
|
|
|
6
|
Game Development / Newbie & Debugging Questions / Re: Using enum, how does this work?
|
on: 2015-01-03 00:25:54
|
Um, I think I might have been misunderstood. I mean something like 1 2 3 4 5 6
| public enum FB_TYPE { GREEN, WRINKLED, SWEET, ALL; }
|
This is the code that has me confused. So, that ALL works for all the three previous or what? Does FB_TYPE.ALL == FB_TYPE.GREEN result in a true or a false?
|
|
|
7
|
Game Development / Newbie & Debugging Questions / Using enum, how does this work?
|
on: 2015-01-02 23:54:13
|
I have heard about enums before but I have never used them. I thought they were kind of like structs in C but now I see a whole lot of different things that confuse me. I didn't have too much trouble with the idea of methods inside an enum but I have a question regarding a certain functionality I read about. Here's a piece of example code. 1 2 3 4
| public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } |
Now, this means I can declare a Day variable as any of these values. But I've read that you could also somehow declare a value that will "always be true". It will return true regardless of the comparison. Let's say we call that value DAY and we declare then we should get true from How exactly does this work? How do you declare that sort of value in an enum?
|
|
|
8
|
Game Development / Newbie & Debugging Questions / Re: Playing sounds for my game
|
on: 2014-05-11 14:44:41
|
Ok, now I've got it working. There is one thing that confuses me though. If I play a music track in the background and then play a short sound (like a gunshot) with another thread then the volume of the music goes down to about a third of the original volume even after the short sound has ended. Anyone have an idea about why this happens? I'll put my code for the "playShortSound" method below. The method that plays background music is the same but in a while loop to keep it repeating. 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
| private void playSound(String filename){ System.out.println("Going to play the sound " + filename); try { URL url = SoundPlayer.class.getResource("/resources/sound/"+filename); audioStream = AudioSystem.getAudioInputStream(url); } catch (Exception e){ e.printStackTrace(); }
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); try { sourceLine = (SourceDataLine) AudioSystem.getLine(info); sourceLine.open(audioFormat); } catch (LineUnavailableException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
sourceLine.start();
int nBytesRead = 0; byte[] abData = new byte[BUFFER_SIZE]; while (nBytesRead != -1) { try { nBytesRead = audioStream.read(abData, 0, abData.length); } catch (IOException e) { e.printStackTrace(); } if (nBytesRead >= 0) { @SuppressWarnings("unused") int nBytesWritten = sourceLine.write(abData, 0, nBytesRead); } }
sourceLine.drain(); sourceLine.close(); System.out.println("Done playing the sound " + filename); } |
|
|
|
9
|
Game Development / Newbie & Debugging Questions / Playing sounds for my game
|
on: 2014-05-11 11:56:03
|
I want to play some sounds for my game. In my "src" folder I have a "resource" folder which contains a folder "sound" contining WAV-files. I want to read and play those. Currently my playing mathod looks like this: 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
| public void playLoopedSound() { do{ try { musicStream = (AudioInputStream) SoundPlayer.class.getResourceAsStream("/resources/sound/"+path); } catch (Exception e){ e.printStackTrace(); }
musicFormat = musicStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, musicFormat); try { musicSourceLine = (SourceDataLine) AudioSystem.getLine(info); musicSourceLine.open(musicFormat); } catch (LineUnavailableException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
musicSourceLine.start();
int nBytesRead = 0; byte[] abData = new byte[BUFFER_SIZE]; while (running && nBytesRead!=-1) { try { nBytesRead = musicStream.read(abData, 0, abData.length); } catch (IOException e) { e.printStackTrace(); } if (nBytesRead >= 0) { @SuppressWarnings("unused") int nBytesWritten = musicSourceLine.write(abData, 0, nBytesRead); } }
musicSourceLine.drain(); musicSourceLine.close(); }while(running); } |
This code gives me the following Exception: 1 2 3 4 5 6
| java.lang.ClassCastException: java.io.BufferedInputStream cannot be cast to javax.sound.sampled.AudioInputStream at project.sound.SoundPlayer.playLoopedSound(SoundPlayer.java:72) at java.lang.Thread.run(Unknown Source) Exception in thread "Thread-1" java.lang.NullPointerException at project.sound.SoundPlayer.run(SoundPlayer.java:78) at java.lang.Thread.run(Unknown Source) |
I would really like to have some help. If anyone has a method that works for this I'd love to see it. If it's complicated to do this I wouldn't mind a method that plays WAV-files that lie in a folder next to the JAR-file instead of inside it. I don't know if this helps but here's the method I use to load images from a folder which lies next to the "sound" folder in the "resource" folder in the JAR-file. It works perfectly so maybe it can help you imagine how my folders are set up. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public static BufferedImage loadImage(String path){ URL myImageURL = FileHandler.class.getResource("/resources/images/" + path); try { BufferedImage myImage = ImageIO.read(myImageURL); return myImage; } catch (IOException e) { e.printStackTrace(); System.out.println("ERROR@FileHandler.loadImage(String path) - IOException with path: " + path); return null; } catch(IllegalArgumentException e){ e.printStackTrace(); System.out.println("ERROR@FileHandler.loadImage(String path) - IllegalArgumentException with path: " + path); return null; } } |
|
|
|
12
|
Game Development / Newbie & Debugging Questions / Loading Native files so that LWJGL can be used
|
on: 2014-04-30 09:03:15
|
I'm using the following method to load the native files from a folder "nat" next to the JAR file. 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
| public static void load() { String system = System.getProperty("os.name"); String path = null; path = new File("nat").getAbsolutePath();
if(path == null) { System.out.println("Couldn't load natives!"); return; }
path += "\\"; if (system.contains("Windows")) { System.setProperty("org.lwjgl.librarypath", path + "Windows"); } else if (system.contains("Mac")) { System.setProperty("org.lwjgl.librarypath", path + "Mac"); } else if (system.contains("Linux")) { System.setProperty("org.lwjgl.librarypath", path + "Linux"); } } |
If possible, I'd like to be able to put that folder inside the JAR and read it from there so that the program doesn't rely on that folder being copied along with the JAR file. I've been reading images and text files from within a JAR with the following methods but I can't seem to adapt that to reading natives. 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
| public BufferedImage loadImage(String path){ URL myImageURL = FileLoader.class.getResource("/res/" + path); try { BufferedImage myImage = ImageIO.read(myImageURL); return myImage; } catch (IOException e) { e.printStackTrace(); System.out.println("ERROR@FileHandler.loadImage(String path) - IOException with path: " + path); return null; } catch(IllegalArgumentException e){ e.printStackTrace(); System.out.println("ERROR@FileHandler.loadImage(String path) - IllegalArgumentException with path: " + path); return null; } } public String[] loadText(String path){ InputStream mySettingsStream = FileLoader.class.getResourceAsStream("/res/"+path); String mySettingsString = "null"; StringBuilder build = new StringBuilder(); int i = 0; try { while((i = mySettingsStream.read()) != -1) { build.append((char)i); } mySettingsString = build.toString(); return mySettingsString.split("\r\n"); } catch (IOException e) { e.printStackTrace(); return null; } } |
|
|
|
13
|
Game Development / Newbie & Debugging Questions / Re: Editing a text-file stored inside a JAR
|
on: 2014-04-29 15:05:08
|
Maybe that "read only" is the reason why this doesn't work even though I get no errors/exceptions with my code. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public void write(String path, String[] lines){ if(path==null || lines==null){return;} URL url = FileLoader.class.getResource("/res/" + path); System.out.println("The URL we got is " + url.toString()); try { File file = new File(url.toURI()); PrintWriter out = new PrintWriter(file); for(int i = 0; i < lines.length; i++){ System.out.println("Appending..."); out.append(lines[i]); out.append("\r\n"); } out.flush(); out.close(); } catch (IOException e) { System.err.println("Failed to write to file"); e.printStackTrace(); } catch (URISyntaxException e) { System.err.println("Failed to write to file"); e.printStackTrace(); } } |
In that case I just have one question. Storing save information outside of the JAR file is ok but I have a wish there. Instead of having a dozen files in the same folder as the JAR, I'd like to create a folder next to the JAR and save them in there. Is there a simple way to check if there is a folder with a certain name next to the JAR and, in case it doesn't exist, creating it?
|
|
|
14
|
Game Development / Newbie & Debugging Questions / Editing a text-file stored inside a JAR
|
on: 2014-04-29 14:22:47
|
Ok, I have a .txt file stored inside a folder "res" inside my JAR-file. I can read from that file with the following method. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public String[] loadText(String path){ InputStream mySettingsStream = FileLoader.class.getResourceAsStream("/res/"+path); String mySettingsString = "null"; StringBuilder build = new StringBuilder(); int i = 0; try { while((i = mySettingsStream.read()) != -1) { build.append((char)i); } mySettingsString = build.toString(); return mySettingsString.split("\r\n"); } catch (IOException e) { e.printStackTrace(); return null; } } |
However, I want to be able to store changes I've made to the data in that file and I can't figure out how to do that. I tried using an OutputStreamWriter with a URL (as you can see below) but it caused a "java.net.UnknownServiceException: protocol doesn't support output" so I clearly need something else. If possible I want to even be able to create a new .txt file at the location in case one with the name from path doesn't exist. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public void write(String path, String[] lines){ if(path==null || lines==null){return;} URL url = FileLoader.class.getResource("/res/" + path); try { URLConnection connection = url.openConnection(); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); for(int i = 0; i < lines.length; i++){ out.write(lines[i]); } out.close(); } catch (IOException e) { System.err.println("Failed to write to file"); e.printStackTrace(); } } |
Anyone have any ideas? I know that it might not be the best idea to mess around with changing and creating files inside a JAR file but it'd be great if I could do it safely. At the very least I'd like to have the ability to edit text files in there.
|
|
|
16
|
Game Development / Newbie & Debugging Questions / Re: Reading files within an Executeable JAR file
|
on: 2014-04-28 10:52:03
|
@orogamo THANK YOU!! It works like a charm. The only files I need to load in right now is text and image files so your solution covers everything. Later I'll need to be able to do the same with music files (WAV files) and hopefully I'll be able to adapt this to load those. I know how to play them if I just get a Stream open to them so if you have a particular way of opening a stream for music I'd be very happy.
|
|
|
17
|
Game Development / Newbie & Debugging Questions / Reading files within an Executeable JAR file
|
on: 2014-04-28 08:55:44
|
I have been working a lot to learn how to load files that lie within the JAR file, but I just can't get it working!! No guide I'm trying works and I'm getting desperate. I'll put an example of how the JAR file is set up below, could anyone tell me how to load the image "Star.jpg" please? 1 2 3 4 5 6 7 8
| folder Loader.jar package Loader.class <- The running code META-INF MANIFEST.MF resources Star.jpg <- the image I want to load |
If someone could give me a full method that can do this instead of saying "use this _____" I'd be very grateful
|
|
|
18
|
Game Development / Newbie & Debugging Questions / Re: Creating a simple AI for an enemy NPC
|
on: 2014-04-27 01:06:53
|
@Gibbo3771 Yes, it does make a lot of sense. I feel more confident with the "head towards the player when it's in a certain range" than I do with the "jump before the obstacle and keep moving forward" but I hope I'll be able to get it done. If you have more tips, or the time to go in depth about it, you're most welcome to comment again.
|
|
|
19
|
Game Development / Newbie & Debugging Questions / Re: Creating a simple AI for an enemy NPC
|
on: 2014-04-26 20:55:53
|
Well, I want them to at least be able to notice if there is an obstacle in their way and in that case I want them to try and jump over it. They will also use ranged attacks and I don't want them to just shoot blindly when they have no chance of hitting me. If you have a link to some webpage that gives tips to creating AI I'd be very grateful
|
|
|
20
|
Game Development / Newbie & Debugging Questions / Creating a simple AI for an enemy NPC
|
on: 2014-04-25 22:00:41
|
I'm doing a 2D platform game (think Super Mario) and I need to make an AI that can control my NPCs. It would be quite boring if nothing ever attacked you after all. However, I have absolutely no idea how to do something like that, I don't know first thing about AI. However what I need is guing to be quite simple so if I could just get some tips to get me started I think I can make it work.
|
|
|
21
|
Game Development / Newbie & Debugging Questions / Re: Using files in META-INF
|
on: 2014-04-24 15:01:01
|
Ok, I got it working. I can load an image like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public BufferedImage loadImage(String path){ java.net.URL imgURL = getClass().getResource(path); if(imgURL != null){ try { return ImageIO.read(imgURL); } catch (IOException e) { e.printStackTrace(); return null; } } else{ System.err.println("Couldn't find file: " + path); return null; } } |
|
|
|
23
|
Game Development / Newbie & Debugging Questions / Re: Using files in META-INF
|
on: 2014-04-24 13:01:18
|
@junkdog I'm sorry. First of all I don't quite understand that code you linked to and also I'm not interested in the Manifest file. I want to get, say, an image "ball.png" which lies in the META-INF map (or anywhere else in the JAR file) I use the following code to load Native files from a folder which lies parallel to the JAR file (they are in the same folder). Let's say I want to move that "nat"-folder into the JAR file so that I only have to keep track of one file 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
| public static void load() { String system = System.getProperty("os.name"); String path = null; path = new File("nat").getAbsolutePath();
if(path == null) { System.out.println("Couldn't load natives!"); return; }
path += "\\"; if (system.contains("Windows")) { System.setProperty("org.lwjgl.librarypath", path + "Windows"); } else if (system.contains("Mac")) { System.setProperty("org.lwjgl.librarypath", path + "Mac"); } else if (system.contains("Linux")) { System.setProperty("org.lwjgl.librarypath", path + "Linux"); } } |
|
|
|
24
|
Game Development / Newbie & Debugging Questions / Using files in META-INF
|
on: 2014-04-24 11:55:26
|
I can put a file into the META-INF folder easily (just change the type to ZIP and then move it there) but how should I do to load the file in the program? I'm fairly inexperienced with loading files and don't even know how to load something from a folder lying at the same place as the JAR file, is there a difference to this compared to loading something from the META-INF? If possible I'd like to be able to put folders in the META-INF folder and then load files from those folders (it'd make file handling a bit easier for me)
|
|
|
25
|
Game Development / Newbie & Debugging Questions / Problems with generating subclasses
|
on: 2014-04-23 14:18:22
|
I have an abstract class "Spell" and lets all spells extend that one. This is for the sake of being able to keep different spells in a single array (like, if your character knows 3 spells and he learns a new one I don't want to have to rewrite the code to make it possible for him to have that). Now, the subclasses of Spell has two "states" (I think you can call it that); Active & Inactive. In short, the player has one of every type of Spell he can use in his Array as an Inactive, but when he casts a spell I create a new instance of this spell, completely unrelated to the original Inactive one, which is then handled by the "Active Spells Handler". The problem I have is when it comes to creating a new spell, I don't know how to make it create the same type of class. If I cast FireBlast I want to create a new FireBlast Object but with the same row of code I want to be able to create an IceLance Object if that was the spell I tried to cast. I know how to check if an object of type Spell is an instance of FireBlast, but I'd rather not have to use a long set of if:s to first find the type of subclass it is and then make a direct creation of this subclass.
I hope you can understand what I'm trying to say
|
|
|
27
|
Game Development / Newbie & Debugging Questions / Re: Problem with getting LWJGL to work outside of Eclipse
|
on: 2014-04-17 11:15:11
|
@trollwarrior1 I did as you told me to and I did indeed get an error message. However I don't quite understand what it means. Could you help me? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| C:\Users\Admin>java -jar TestOpenGL.jar Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Mathod) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58) Caused by: java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at org.lwjgl.Sys$1.run(Sys.java:73) at java.security.AccessController.doPrivileged(Native Method) at org.lwjgl.Sys.doLoadLibrary(Sys.java:66) at org.lwjgl.Sys.loadLibrary(Sys.java:95) at org.lwjgl.Sys.<clinit>(Sys.java:112) at org.lwjgl.opengl.Display.<clinit>(Display.java:135) at OpenGL_Example.main(OpenGL_Example.java:26) ... 5 more |
|
|
|
28
|
Game Development / Newbie & Debugging Questions / Problem with getting LWJGL to work outside of Eclipse
|
on: 2014-04-17 09:35:25
|
I have the problem that when I work in Eclipse I can get a window to open as it should but for some reason I can't get it to start once I've exported the program to a JAR-file. I made a very simple example where I only draw a single image on the center of the screen and tried that. It worked in Eclipse and it didn't work once exported (in a way I'm glad that is so, it shows the problem is quite basic and not a huge error in my other program). I've added in the code here so that you can see. It works in Eclipse, but not when exported. 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 108 109 110 111
| import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.ARBDebugOutput.*;
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer;
import javax.imageio.ImageIO; import javax.swing.JOptionPane;
import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.ARBDebugOutputCallback; import org.lwjgl.opengl.ContextAttribs; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GLContext; import org.lwjgl.opengl.PixelFormat;
public class OpenGL_Example {
public static void main(String[] args) { try { Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode()); Display.create(new PixelFormat(), new ContextAttribs().withDebug(true)); if(GLContext.getCapabilities().GL_ARB_debug_output){ glDebugMessageCallbackARB(new ARBDebugOutputCallback()); } } catch (LWJGLException e) { e.printStackTrace(); } BufferedImage image = null; try { image = ImageIO.read(new File("BucketKitties.jpg")); } catch (IOException e) { e.printStackTrace(); } int width = image.getWidth(), height = image.getHeight(); int[] pixels = image.getRGB(0, 0, width, height, new int[width*height], 0, width); ByteBuffer data = BufferUtils.createByteBuffer(3*width*height); for(int i = 0; i < pixels.length; i++){ int pixel = pixels[i]; data.put((byte) ((pixel >> 16) & 0xFF)); data.put((byte) ((pixel >> 8) & 0xFF)); data.put((byte) (pixel & 0xFF)); } data.flip(); int texture = glGenTextures(); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glEnable(GL_TEXTURE_2D); while(!Display.isCloseRequested()){ glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(Display.getWidth()/2, Display.getHeight()/2, 0); glBegin(GL_QUADS); { glColor3f(1, 1, 1); glTexCoord2f(0, 0); glVertex2f(-250, -250); glTexCoord2f(1, 0); glVertex2f(250, -250); glTexCoord2f(1, 1); glVertex2f(250, 250); glTexCoord2f(0, 1); glVertex2f(-250, 250); } glEnd(); Display.sync(100); Display.update(); } Display.destroy(); JOptionPane.showMessageDialog(null, "Complete!"); }
} |
|
|
|
29
|
Game Development / Newbie & Debugging Questions / Re: Export from Eclipse causes trouble
|
on: 2014-04-17 08:46:28
|
@trollwarrior1 I'm terribly sorry but I'm afraid I don't under stand what you mean. I don't know if I load any files with .internal (I don't even know what that is). At least I have now been able to determine that the file is loaded as it should be and that the problem comes when I try to run the program.
|
|
|
|
|
nelsongames
(19 views)
2018-04-24 18:15:36
nelsongames
(17 views)
2018-04-24 18:14:32
ivj94
(607 views)
2018-03-24 14:47:39
ivj94
(51 views)
2018-03-24 14:46:31
ivj94
(400 views)
2018-03-24 14:43:53
Solater
(65 views)
2018-03-17 05:04:08
nelsongames
(110 views)
2018-03-05 17:56:34
Gornova
(175 views)
2018-03-02 22:15:33
buddyBro
(746 views)
2018-02-28 16:59:18
buddyBro
(93 views)
2018-02-28 16:45:17
|
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
|
|