Reference:FIFE:Minimal:Introduction

From Post-Apocalyptic RPG wiki

Jump to: navigation, search

Code reference.png This article covers outdated code reference information!

Code reference articles are stored at the wiki for legacy reasons. They are often outdated and duplicate Epydoc code documentation and department-specific tutorials. Code reference articles need to be reviewed by the programming department to see if they provide any useful information that is not available elsewhere at the wiki. After they have been reviewed and their unique information has been moved to more appropriate articles, the code reference articles should be deleted.


To those of you who have tried, following the guide of the Fife wiki to creating a minimal client (this) doesn't work. This small guide goes line by line through what needs to be done to get something up and running. Actually doing something will be another article.

The first thing to do is to go to your /fife/clients folder and create a new folder. I've called mine pdemo. You'll need to a little more work. Fife demands a font to work with (even if we don't use it). Copy the file fife/clients/rio_de_hola/fonts/FreeSans.ttf into our new directory.

The next step is to create a 'settings' file, which we'll use python for. Fire up your favorite text editor and create a file settings.py with this content:

BitsPerPixel = 0
FullScreen = 0
InitialVolume = 5
PlaySounds = 1
RenderBackend = "OpenGL"
SDLRemoveFakeAlpha = 1
ScreenWidth = 640
ScreenHeight = 480
LogModules = ['controller']
PychanDebug = True
LogToPrompt = 1
LogToFile = 0
Font = "FreeSans.ttf"
FontGlyphs = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`\'*#=[]\"'

These are 'standard options' that will exist in our demo. The only interesting ones for now are possibly ScreenWidth and ScreenHeight. Also note that you can put a path into the Font option, for example in Rio's case the setting would be /fonts/FreeSans.ttf.

There is one more file to copy and paste: copy fife/clients/rio_de_hola/scripts/common/eventlistenerbase.py to our folder. You can take a look at this in a text editor:

import fife
 
class EventListenerBase(fife.IKeyListener, fife.ICommandListener, fife.IMouseListener, fife.ConsoleExecuter):
	def __init__(self, engine, regKeys=False, regCmd=False, regMouse=False, regConsole=False, regWidget=False):
		self.eventmanager = engine.getEventManager()
 
		fife.IKeyListener.__init__(self)
		if regKeys:
			self.eventmanager.addKeyListener(self)
		fife.ICommandListener.__init__(self)
		if regCmd:
			self.eventmanager.addCommandListener(self)
		fife.IMouseListener.__init__(self)
		if regMouse:
			self.eventmanager.addMouseListener(self)
		fife.ConsoleExecuter.__init__(self)
		if regConsole:
			engine.getGuiManager().getConsole().setConsoleExecuter(self)

It's a very simple class that sets up the event engine. It'll be used by our code later to set up the keys. Below the text I've shown you'll also see a bunch of null routines such as

	def mousePressed(self, evt):
		pass

Obviously we'll ignore these for now. Now let's get to some real code! Start a new file run.py and start typing away:

#!/usr/bin/env python
 
import sys,os
 
def _jp(path):
	return os.path.sep.join(path.split('/'))
 
_paths = ('../../engine/swigwrappers/python', '../../engine/extensions')
for p in _paths:
	if p not in sys.path:
		sys.path.append(_jp(p))

I'm assuming a little knowledge of Python here. After the usual #! and import commands, the function _jp is used to resolve the path structure is correct for all systems (i.e. C:\fife\etc... on Windows compared to maybe ~/fife/etc... on *nix). We only use this function in our code, and that's in the next section where we make sure all of the swig stuff and extensions are in a path somewhere for the rest of the code to work. Once the system has these correct paths, we can continue with the code:

import fife_compat,fife
import eventlistenerbase
from basicapplication import ApplicationBase

fife is of course the main engine. fife_compat is a library that enables us to use some older fife functions (we'll see why later). eventlistenerbase is the file we looked at earlier. basicapplication is quite simply, the framework for a basicapplication. You can study this for yourself, because it's found at /fife/engine/extensions/basicapplication.py.

Now we build our first class:

class ApplicationListener(eventlistenerbase.EventListenerBase):
	def __init__(self,engine):
              # call the case class ini function
	      super(ApplicationListener, self).__init__(engine,regKeys=True,regCmd=True,regMouse=False,regConsole=True)
	      self.engine=engine
              # what keys do we not want to ignore?
	      engine.getEventManager().setNonConsumableKeys([fife.Key.ESCAPE,])
              # do we want to quit? store that here
              self.quit = False
 
	def keyPressed(self,evt):
              # a key was pressed, so we end up here
              # what was the key ascii code?
	      keyval = evt.getKey().getValue()
              # is that the same as the ESCAPE key?
	      if keyval == fife.Key.ESCAPE:
                     # yes, so flag that we want to quit
	            self.quit=True
              # mark the event as having been 'finished with'
	      evt.consume()

This is simply a class that will handle all our input cases, and builds off the eventlistener class we copied over from the Rio demo. As you can see, it does very little other than check for an ESCAPE key. It should be noted that setNonConsumable keys is a depreciated function, which is why we needed to import fife_compat earlier. Next up is our actual game class:

class PDemo(ApplicationBase):
	def __init__(self):
	      super(PDemo,self).__init__()
              # give ourselves an events engine (the one defined above)
	      self.listener=ApplicationListener(self.engine)
 
	def _pump(self):
              # called after everything else, the main game loop...
              # do we need to exit?
	      if self.listener.quit:
                    # yes, so let the fife engine know
                    self.breakRequested=True

It's really simple, as you would expect: setup the listener engine and a main loop. The main loop only checks to see if we need to exit. Here's the final part of the code:

def main():
	app=PDemo()
	app.run()
 
if __name__ == '__main__':
	main()

Standard python code. Save all of this, and then run the file. Windows users: python.exe run.py, or for those on *nix chmod +x run.py; ./run.py. You should get a blank window come up, and the ESCAPE key should exit. You'll also notice that fife complains a little as well:

fife_compat: Deprecation warning - See revision 2636 
 -  Write an IKeyFilter instead of using EventManager.setNonConsumableKeys.
You probably don't need it anyway

Next up is to fix that, and to add a map to scroll around....

Loading a map and scrolling it around

Personal tools