gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Memory Upgrades
Compare Prices
Corporate Awards
Online Education
Laptop Batteries
GPS
Remote Online Backup
Promote Your Website
Computer Deals
Calling Cards
Promotional Pens
Corporate Gifts
Best Price
Find Software

 


Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Simple DirectMedia Layer: Gaming Platform for C++, Java, and More
By Victor Volkman

Go to page: 1  2  3  Next  

Gentlemen, Start Your 3D Gaming Engines!

In gaming, more so than any other programming regime, it is important to specify the platform correctly from the start. Will you support Windows, Linux, and OS X? Indeed, many people would cite the revolutionary Quake game written for OpenGL in 1996 as the granddaddy of game platforms. However, to reach Quake-level gameplay standards you will also need world-class audio support, network connectivity, user-input device support, and real-time management capabilities, just to name a few… The solution to both problems, cross-platform support and the extras that make a game exciting, is Simple DirectMedia Layer (SDL). This is a cross-platform multimedia library designed to provide low-level access to audio, keyboard, mouse, joystick, OpenGL, and 2D video framebuffer. SDML supports almost every platform I can think of, including Linux, Windows, all MacOs variants, WinCE, Dreamcast, and others. Because it is distributed on GNU LGPL v2, you can use SDL freely in commercial programs as long as you link with the supplied DLL.

SDML shows up in MPEG players, hardware emulators, and many popular games, including the award winning Linux port of Civilization: Call To Power. SDML is written in C, but works with C++ natively, and has bindings to several other languages, including Ada, Eiffel, Java, Lua, ML, Perl, PHP, Pike, Python, and Ruby. The sky is the limit with SDL, which is the engine for my favorite Open Source flight simulator GL-117. In fact, there are now 568 games built on top of the SDL engine (of which 450 can build Win32 executables) registered on the SDML homepage.



Click here for a larger image.

Figure 1: The view from GL-117

ALIENS in Your Midst!

The best way to get inside a game engine is to look at some sample code. This time, you're going inside a 1980s Space Invaders clone game by Sam Lantinga, called simply aliens. Remarkably, the complete game source code is only 560 lines of code! I wish I could lay out the entire thing in this article, but it exceeds my boundaries just a little. Instead, I will focus with laser-like intensity on the specifics that matter the most and ignore bits which are rote or repetitious. [I'll be using line numbers from aliens.c if you want to follow along in Visual Studio.]

Figure 2: Screenshot of Aliens game

The first thing to notice is that "aliens" leverages code from two other SDL projects to help get the job done: SDL_mixer and SDL_image. SDL_mixer is a platform-independent sound mixing library plug-in for SDL apps. It allows your apps to play multiple samples along with music without having to code a mixing algorithm. For example, you would use this to allow gunfire noises to seamlessly mix into background music. It also simplifies loading and playing samples and music from all sorts of file formats. SDL_image is a platform-independent graphics loading plug-in for SDL apps. It allows your apps to read BMP, PNM (PPM/PGM/PBM), XPM, LBM, PCX, GIF, JPEG, PNG, TGA, and TIFF format files. So, be sure to download both packages if you want to follow along!

The main() Business

The main() function, shown below, is where the action starts, of course. In Line #524, you ask SDL_Init() to initialize the audio and video subsystems. Other options include turning on joystick, timer, and CD-ROM access as well as an option to run the event manager in a separate thread. In Line #531, you start using the SDL_Mixer manager with a call to Mix_OpenAudio(). In this example, you're using low-quality audio suitable for older/slower CPUs: 11Khz, 8-bit unsigned samples, one channel (mono), and a 512 byte buffer. Next, you open up the video with SDL_SetVideoMode() for 640x480 display using whatever bits per pixel is appropriate for the device. You ask for a system-memory buffer and request a "full screen" display, meaning that you want your window to dominate the windowing system. Other possible options include double-buffering (smoother animation), OpenGL context, resizable window, and asynchronous repaints.

521 main(int argc, char *argv[])
522 {
523    /* Initialize the SDL library */
524    if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
525       fprintf(stderr, "Couldn't initialize SDL: %s\n",
                  SDL_GetError());
526       exit(2);
527    }
528    atexit(SDL_Quit);
529
530    /* Open the audio device */
531    if ( Mix_OpenAudio(11025, AUDIO_U8, 1, 512) < 0 ) {
532       fprintf(stderr,
533       "Warning: Couldn't set 11025 Hz 8-bit audio\n-
           Reason: %s\n",
534       SDL_GetError());
535    }
536
537    /* Open the display device */
538    screen = SDL_SetVideoMode(640, 480, 0,
          SDL_SWSURFACE|SDL_FULLSCREEN);
539    if ( screen == NULL ) {
540       fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
541       SDL_GetError());
542       exit(2);
543    }
544
545    /* Initialize the random number generator */
546    srand(time(NULL));
547
548    /* Load the music and artwork */
549    if ( LoadData() ) {
550       /* Run the game */
551       RunGame();
552
553       /* Free the music and artwork */
554       FreeData();
555    }
556
557    /* Quit */
558    Mix_CloseAudio();
559    exit(0);
560 }

Go to page: 1  2  3  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Other Java Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.
Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation
Whitepaper: XML Processing in Applications--Take the Next Step
Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES