SDL Console



Author: Boris Lesner
Original project by: Garrett Banuk

Description:
This is a console that can be added to any SDL application. It is similar to Quake and other game consoles but with lots of added features. A console is meant to be a very simple way of interacting with a program and executing commands. Commands are linked to the console with callback functions so that when a command is typed in, a specific function is executed automatically.
SDL_Console is released under GNU/LGPL licence ( see www.gnu.org for more information )

Depends:
SDL_console depends of the following libs :
SDL : www.libsdl.org SDL_image : http://www.libsdl.org/projects/SDL_image

September 19, 2002 SDL_console 1.3 is released! Wacha Clemens again couldn't stop adding stuff to the code.
I has too many features and bugfixes to mention them here. For a complete list see below.
He has completely rewritten the command line and added lots of useful control keys.

September 13, 2002 SDL_console 1.2 is released thanks to Wacha Clemens who added a prompt, autoscrolling commandline, and buxfixes.
I also added CON_Free() which equals to CON_Destroy but without destroying text support of DT ( thanks to Paul Wighton ), CON_Destroy still in the lib for compatibility.

September 03, 2002 Garrett give me the SDL_console project and I hope I could add nice changes to the lib !
But's it's already stable and I think that only a few things would be added.
For now I added a debian package of SDL_Console, the SDL_image support for PNG, JPG, ... images formats, and a shared library of the lib.
SDL_console is now version 1.1.
Boris Lesner.

July 12, 2002 SDL_Console needs a new author! I no longer have the time to work on SDL_Console with my new job and other interests and due to the popularity of SDL_Console, I do not want to let the project stagnate. I'm going to give the project away to someone else to maintain and improve as they see fit. The new author will have full control of the project and may do with it as they please. If you are interested in taking over the project, please email me. Give me some information about your computer science skills, projects you've worked on, etc. After/if I get a sufficient response from interested people, I will decide who will take over the project. Thanks for all the support from everyone. I hope this project is still useful in the future.

Nov 14, 2001 Cort Stratton added a new MSVC project for the console with the new source.

Oct 8, 2001 Finally an update to SDL_console! SDL_console has now reached version 1.0. I figured it's about time since there have not been any bug fixes for a while so the code is pretty solid. There are also lots of new additions to the code and a decent Makefile system now. If anyone wants to write an autoconf system though, be my guest.

June 12, 2001 Seung Chan Lim Has donated a version of SDL_Console with the demo in MSVC project format. You can download it below.

May 15, 2001 Now the console really works with OpenGL. =) Thanks to Cort Stratton for fixing the OpenGL support. The console example source now demonstrates it's use with OpenGL. There is also the non-OpenGL sample source included in the archive. As usual there were a few cosmetic modifications and bug fixes also.

Apr 29, 2001 I added a blinking cursor showing the current editing position due to popular demand. Ugh I didn't realize how ugly this code was. =) It's efficient, just not very neat. Feb 20, 2001 Thanks to Patricia Cruz for adding an OpenGL fix to the Console source. Nov 18, 2000 Lots of new changes here, mainly thanks to Lee Thomason for submitting some of them and motivating me to work on this again. April 16, 1999 Also included is text and font handling routines for bitmap fonts. Bitmap fonts can be loaded up and text can be outputted to the surfaces using the different fonts. These bitmap font routines can be used seperately without the console. The text drawing routines are in the files DT_*

Source:
[Console and Example (SDL_console-1.2.tar.gz)]
[Console and Example for MSVC (SDL_console-1.2.zip)]


Additions and modifications are welcome.
Email me if you have suggestions for other things to be added or fixes.

ScreenShots:
Small console
Stretched to half the screen
Multiple consoles on the same screen

Keyboard controls

Up move command history up
Down move command history down
Left move cursor left
Right move cursor right
Ins toggle overwrite mode (you can set the two different cursor in CON_console.h)
Del delete character above cursor
Backspace delete character left of cursor
Home move cursor to begin of command
End move cursor to end of command
Ctrl-A same as Home
Ctrl-E same as End
Ctrl-C clear commandline
Page-Up move history up
Page-Down move history down
Shift-Home move to top of history
Shift-End move to end of history
Ctrl-L clear history


API:
Heres an overview of the CONSOLE API.

#include "CON_Console.h"

Most of these functions take a pointer to a ConsoleInformation struct. This tells what console the function should proccess on. A new ConsoleInformation pointer is created with Con_Init().

ConsoleInformation *CON_Init( char *FontName, SDL_Surface *DisplayScreen, int lines, SDL_Rect rect )
Takes in a string of the path to the bitmap fontname, the surface the console will be displayed on, the number of lines in the back buffer of the console and an SDL_Rect with the x,y location and width and height. It returns a pointer to a ConsoleInformation struct, or NULL on error.

void CON_Events( SDL_Event *event )
Send SDL events to this to be processed when the console is down. Be sure to set SDL_EnableUNICODE(1); for SDL before the commands are sent to the console so the console can interpet the commands. The events are sent to the console registered with CON_Topmost();

void CON_DrawConsole( ConsoleInformation *console )
Draws the console on the screen.

void CON_Out( ConsoleInformation *console, char *str, ... )
Displays strings to the console, works just like printf()

void CON_CommandExecute( ConsoleInformation *console, char *BackStrings )
Executes a command string passed to it. Usually only used internally by the console API.

void CON_AddCommand( void (*CommandCallback)(ConsoleInformation *console, char *Parameters), char *CommandWord )
The first parameter of this is a pointer to a function that takes a ConsoleInformation pointer and a string pointer as a parameter. The ConsoleInformation pointer is the console the command was typed in, the string is the arguments passed to the command in the console. The second parameter of AddCommand is the string of the command (no space) the user will type into the console to call this function.

void CON_ListCommands( ConsoleInformation *console )
Lists the commands in the console.

void CON_SendFullCommand( int sendOn )
This sets a flag so that the command and the arguments typed in by the user are all sent to the callback function. This is helpful in that one callback function can be used for multiple different commands typed in by the user and easily distinguished.

int CON_Background( ConsoleInformation *console, char *image, int x, int y )
Loads the file from image to the location (x,y) on the background. If this function is passed NULL for image the background is unloaded.

void CON_Alpha( ConsoleInformation *console, unsigned char alpha )
Alpha blends the console into the background, value range is 1-200. If the alpha value passed is 0 alpha blending is turned off.

void CON_Destroy( ConsoleInformation *console )
This shuts down and frees the console resources.

void CON_Free( ConsoleInformation *console )
This shuts down and frees the console resources but not DrawText ressources.

void CON_SetPrompt(ConsoleInformation *console, char *string)
This sets the prompt of the console console to string

void CON_Position( ConsoleInformation *console, int x, int y )
This positions the console in the new x,y location.

int CON_Resize( ConsoleInformation *console, SDL_Rect rect )
This takes an SDL_Rect to resize and move the console window. Use CON_Position() if you are just moving the console though as the resizing function has to reinitialize resources and so takes longer.

Special Keys
Page_UP Page_DOWN scroll around the back buffer of the console TAB tab completes commands END goes back to the bottom of the console



Heres an overview of the DRAW TEXT API.

#include "SDL_DrawText.h"

int DT_LoadFont( char *BitmapName, int flags )
Loads a font from the BitmapName file and returns the number of the font. returns -1 as an error. Pass TRANS_FONT to the flags if the font is transparent. Transparent color is RGB(255,0,255)

void DT_DrawText( char *string, SDL_Surface *surface, int FontType, int x, int y )
Draws text to the screen. First parameter is the text to draw, second is the surface to draw too, third is the font type to draw, last two are the x,y screen location.

int DT_FontHeight( int FontNumber )
returns the font character height.

int DT_FontWidth( int FontNumber )
returns the font character width.

void DT_DestroyDrawText( )
Shuts down and frees all the resources used by the text drawing routines.

Demo info:
In this demo the keys CTRL+[1,2,3] display the consoles 1, 2 or 3 to the screen. To select which console you want to type to press ALT+[1,2,3]. ALT+4 directs input back to the program.

TUXFAMILY