Page 1 of 1

Game Programming in Linux for Windows Programmers - Part 4 Fun with JAVA in Linux.

#1 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 786
  • View blog
  • Posts: 7,618
  • Joined: 05-May 05

Posted 02 July 2009 - 12:34 AM

Introduction

Welcome back! It's been a while since I last wrote one of these. I'll cover anything in Linux that you, the Windows Game Programmer might find interesting. If you get lost on something, I probably have mentioned it before in a previous tutorial. You can find all the links on my Project Log on my blog.

Now... JAVA! Now before you make gagging noises at how slow it is compared to C or ASM, note that I think JAVA is worth mentioning and learning. Remember, there are cases when JAVA's features are more important than speed. You will obviously notice C programs tear up the road when compared with an equivalent JAVA program- both are fast but the lower language usually wins in speed. However, what if you wanted to write a game editor? Don't want to mess with GTK? Or a game server? MORPG server? Who knows!

JAVA is great in simplifying many, many tasks and being very object oriented. It's good for anything your actual gamers won't see. Your game engine might be some fast monster you wrote in C since my last tutorial, but it connects to a server, and the server can be written in anything you feel like. Or the game data could have been edited with a JAVA application. GTK isn't that messy, so don't always count it out.

Finally, I'm going to be messing with Makefiles and the actual JDK itself. You may or may not feel like using the terminal at this point, I already showed you how to build like you are probably used to. But if you want to practice it, that's fine too. Now, let's start!

Getting JDK for Linux

You need JAVA before you can work with it. Do you remember the synaptic package manager? If you didn't do anything, it should be here:
Posted Image

Now make sure you have the following packages installed:
Posted Image
Posted Image

You might see later versions, so get those if you want. If you type javac in the terminal you should get this:
sayuri@sayuri-lab:~$ javac
Usage: javac <options> <source files>
where possible options include:
  -g						 Generate all debugging info
  -g:none					Generate no debugging info
  -g:{lines,vars,source}	 Generate only some debugging info
  -nowarn					Generate no warnings
  -verbose				   Output messages about what the compiler is doing
  -deprecation			   Output source locations where deprecated APIs are used
  -classpath <path>		  Specify where to find user class files and annotation processors
  -cp <path>				 Specify where to find user class files and annotation processors
  -sourcepath <path>		 Specify where to find input source files
  -bootclasspath <path>	  Override location of bootstrap class files
  -extdirs <dirs>			Override location of installed extensions
  -endorseddirs <dirs>	   Override location of endorsed standards path
  -proc:{none,only}		  Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process
  -processorpath <path>	  Specify where to find annotation processors
  -d <directory>			 Specify where to place generated class files
  -s <directory>			 Specify where to place generated source files
  -implicit:{none,class}	 Specify whether or not to generate class files for implicitly referenced files 
  -encoding <encoding>	   Specify character encoding used by source files
  -source <release>		  Provide source compatibility with specified release
  -target <release>		  Generate class files for specific VM version
  -version				   Version information
  -help					  Print a synopsis of standard options
  -Akey[=value]			  Options to pass to annotation processors
  -X						 Print a synopsis of nonstandard options
  -J<flag>				   Pass <flag> directly to the runtime system

sayuri@sayuri-lab:~$ 



This means you've did something right. If something doesn't work, check your JAVA packages. There's also the OpenJDK, but I don't really like it.

Hello JAVA

Alright. In any folder you want, write the classic Hello World JAVA example shown below:
/**
	Hello JAVA Linux
	<p>
	A test program for your JAVA Linux installation.

	@author WolfCoder
*/
public class LinuxHello
{
	/**
		Program start.

		@param args Ignored command line arguments.
	*/
	public static void main(String[] args)
	{
		// Say hello
		System.out.println("Hello, JAVA!");
	}
}



Note the strange use of JavaDoc comments even though this is a hello world program. This is a good habit to get into when writing JAVA. I'll demonstrate why later. So you've written LinuxHello.java as JAVA filenames must match the outermost class. Create a file Makefile and fill it with this:

compile:
	javac *.java;
	-rm *~;



Then build it~ Now the command to run is simply "java LinuxHello". Here's what it looks like:
Posted Image

That's all there is to it.

JavaDoc

Here's how you make some JavaDocs out of your files. This is pretty basic, you'll find many options at Sun's website for using JavaDoc. It's easier for me to show you what it does than for me to explain it >.< Go ahead and, type "mkdir doc" or make a new folder called doc to keep things clean. Now type "javadoc -d doc *.java" and you should get this:
sayuri@sayuri-lab:/media/WOLF/My Programs/tutorial/tutorial-4$ javadoc -d doc *.java
Loading source file LinuxHello.java...
Constructing Javadoc information...
Standard Doclet version 1.6.0_13
Building tree for all the packages and classes...
Generating doc/LinuxHello.html...
Generating doc/package-frame.html...
Generating doc/package-summary.html...
Generating doc/package-tree.html...
Generating doc/constant-values.html...
Building index for all the packages and classes...
Generating doc/overview-tree.html...
Generating doc/index-all.html...
Generating doc/deprecated-list.html...
Building index for all classes...
Generating doc/allclasses-frame.html...
Generating doc/allclasses-noframe.html...
Generating doc/index.html...
Generating doc/help-doc.html...
Generating doc/stylesheet.css...
sayuri@sayuri-lab:/media/WOLF/My Programs/tutorial/tutorial-4$ 



Now go into doc and open the index.html file to see how cool this tool is. JavaDoc uses HTML, so add HTML code in your comments if you want to do more than just insert text. This tool is really neat ^.^

And Now... Video

It wouldn't have anything to do with video games if there wasn't video! Now, just like I showed you, run the following code. Use this opportunity to make more targets for your Makefile, and to practice working with JAVA.
// SWING
import javax.swing.JFrame;

/**
	Linux Video Demo
	<p>
	Demonstrates a Panel and things being drawn inside it.

	@author WolfCoder
*/
public class LinuxVideo extends JFrame
{
	private static LinuxVideo linuxVideo;
	private VideoPanel videoPanel;
	/**
		Creates a new Linux Video demonstration.
	*/
	public LinuxVideo()
	{
		// Title
		super("Linux Video Demo");
		// Settings
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// Add things
		videoPanel = new VideoPanel();
		getContentPane().add(videoPanel);
		pack();
		// Show
		setVisible(true);
	}
	/**
		Program beginning.

		@param args Ignored command line arguments.
	*/
	public static void main(String[] args)
	{
		// Announce
		System.out.println("Linux Video Demo - WolfCoder (2009)");
		// Create new demo
		linuxVideo = new LinuxVideo();
	}
}



And there's another file too:
// SWING
import javax.swing.JPanel;

// AWT
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

/**
	Video Demo Panel
	<p>
	This is the panel where sample graphics are drawn in the demo.

	@author WolfCoder
*/
public class VideoPanel extends JPanel
{
	private static final Color BACKGROUND_COLOR = new Color(50,50,50);
	private static final Color TEXT_COLOR = new Color(240,10,8);
	private static final Dimension DEFAULT_SIZE = new Dimension(320,240);
	private static final int TEXT_OFFSET = 20;
	/**
		Creates a new Video Demonstration Panel.
	*/
	public VideoPanel()
	{
		// Settings
		setBackground(BACKGROUND_COLOR);
		setPreferredSize(DEFAULT_SIZE);
	}
	/**
		Draws the contents of this panel.

		@param g The graphic context to draw into.
	*/
	public void paintComponent(Graphics g)
	{
		// Draw essential things
		super.paintComponent(g);
		// Draw some text
		g.setColor(TEXT_COLOR);
		g.drawString("Hello, Video Panel in Linux!",0,TEXT_OFFSET);
	}
}



It's your exercise to get a program that shows this when run:
Posted Image

WolfCoder Image Pack

I don't think I'm quite done yet for now-! Let's field test my new JAVA port of the .wip file reader. It can draw the contents of a .wip file easily to the panel you just compiled. Download the wipreader.zip file attached to this tutorial, it contains the two .java files and a .wip file to test it all with.

It should have these three files:
Posted Image

What's in the .wip file is for you to find out~ Hehe, I put some funny pictures in there. If you want to use this for yourself, go and check out the full package you can download here. It was originally designed for OpenGL but I extended it for easy integration with my game engine.

Anyway, I'll give you a quick idea of how a .wip is formatted. It's basically several images which, in themselves, could have multiple frames for sprites and animation. Each image has a width and height that are a power of two (it is up to the image packer about that). That's it. Each image has a name... But we don't know the names! We can still crack this file open, so let's modify the VideoPanel program with the following:
// SWING
import javax.swing.JPanel;

// AWT
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

/**
	Video Demo Panel
	<p>
	This is the panel where sample graphics are drawn in the demo.

	@author WolfCoder
*/
public class VideoPanel extends JPanel
{
	private static final Color BACKGROUND_COLOR = new Color(50,50,50);
	private static final Color TEXT_COLOR = new Color(240,10,8);
	private static final Dimension DEFAULT_SIZE = new Dimension(320,240);
	private static final int TEXT_OFFSET = 20;
	private ImagePack mysteryPack; // Our mystery pack file
	private WolfcoderImage mysteryImage; // Some image...?
	/**
		Creates a new Video Demonstration Panel.
	*/
	public VideoPanel()
	{
		// Settings
		setBackground(BACKGROUND_COLOR);
		setPreferredSize(DEFAULT_SIZE);
		// Load the pack
		mysteryPack = new ImagePack("mystery.wip");
		// Print the number of images inside
		System.out.println(mysteryPack.getImages().length+" images in the pack.");
		// Get one
		mysteryImage = mysteryPack.getImages()[0];
		// What's it called?
		System.out.println("Looking at "+mysteryImage+".");
	}
	/**
		Draws the contents of this panel.

		@param g The graphic context to draw into.
	*/
	public void paintComponent(Graphics g)
	{
		// Draw essential things
		super.paintComponent(g);
		// Draw the mystery image
		mysteryImage.draw(g,8,8+TEXT_OFFSET);
		// Draw some text
		g.setColor(TEXT_COLOR);
		g.drawString("Hello, Video Panel in Linux!",0,TEXT_OFFSET);
	}
}



Here I give you limited information on purpose. Take a look at the two files involved in all this. Just what is getImages() returning? What can a WolfcoderImage also return? What does the terminal say? The above example works if the following is shown:
Posted Image

And there you have it. Now... For the challenges on your own-!

- Look at the other two files. One of them has frames, try animating it as well. You're going to figure out how many frames, and what they all are first, and which one even has multiple frames.

- Make it display something you yourself packed (download the full package on my log). Mind the semicolons! .ipm files can be quirky if you forget them.

- Figure out how the data goes from the .wip file and into a standard JAVA BufferedImage. This should give you an idea on how to invent your own formats. Especially for download-able user content in an MORPG for example.

I hope you had fun and see you next time~

Attached File(s)


This post has been edited by AdamSpeight2008: 12 March 2010 - 08:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Game Programming in Linux for Windows Programmers - Part 4

#2 jetimms  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-April 12

Posted 10 April 2012 - 07:01 PM

First thanks for the series on Linux game programming. I'm finally catching up. I'm loving gcc more than I possibly imagined! It's so easy.

Second, can you provide a link to your WIP file package wrapper/encoder thingie? I saw a link to it just yesterday, but now I can't find it.

:)
Was This Post Helpful? 0
  • +
  • -

#3 jetimms  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-April 12

Posted 11 April 2012 - 05:43 PM

Found it here.

(The link above in the topic is broken.)

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1