Code Archive

Trail Engine

A set of functions to make trails. For example: you can create 2 trails to make a combustion engine leave a glowing trail.
Or swords that leave a trail in the air.
etc.

Line - triangle intersection in 3D

This code will calculate whether or not a line will intersect with a triangle in 3D space, and store the intersection coordinates in ix#, iy#, iz# if made global.

Note that the vectors 1 and 2 have to be created in order to work.
The D3D plugin by Cloggy is only used for visualisation of the line. Leaving those commands out will not affect the function.

DirectX export function

ExportDirectX(obj, filename$)
obj: object to export.
filename$: file to export.
Returns 1 if succeeded, 0 if file exist or filename$ is not a DirectX file.

This only works on objects with indices. This means that memblock objects won't work. So do file formats that don't support indices.
The example can load DirectX and 3D studio models, and exports them using the function to a new file.
(Make sure the file doesn't exist)

The dialog code is from JessT.

I have experienced that DBO files can't be exported with this functions.

Color Dialog call winAPI

This code will use user32.dll and comdlg32.dll to call a default font dialog.

SBChooseColor(defaultColor, memCustomColors)
- defaultColors: This is the default selected color in the dialog.
- memCustomColors: In the color, there are 16 custom color boxes. If you specify a memblock number here, the custom colors are saved in this memblock.
The structure is very simple:

byte 0: red
byte 1: green
byte 2: blue
byte 3: 0 (always 0. So this means you can't just write a dword with the rgb() value)
(X16)

Call Font Dialog box

This code uses user23.dll and comdlg32.dll to call a standard Font Dialog box.
SBChooseFont(defaultSize, defaultColor)
- defaultSize: The default selected size for text
- defaultColor: The default selected color for text
returns string:
"(FontName);(FontSize);(boolItalic);(boolUnderline);(boolStrikeOut);(boolBold);(Color)"

You can extract each data by using the other functions:
SBGetFontName(s$), SBGetFontSize(s$), SBGetFontItalic(s$), SBGetFontUnderline(s$), SBGetFontStrikeOut(s$), SBGetFontBold(s$) and SBGetFontColor(s$) which each return their data in their own type.

MakeObjectDisc

Makes a UV-mapped disc with the size and amount of polygons you specify.

Console functions

A set of functions for creating a console and reading/writing it. Example code included.

Functions
AllocConsole - Creates the console.
FreeConsole - Destroys the console.
ConsoleInput - Gets a line of input entered in the console. Waits until the return key is pressed.
WriteConsole - Writes a string of text to the console. Supports \n and \r for newline and carriage return, respectively.
SetConsoleCursorPosition - Sets the cursor position. The coordinates of the top left of the console are (0, 0).
ClearConsole - Erases everything in the console and resets the cursor position.

Change endianness of a value

Switches a 16/32-bit value between big/little endian.

Image file dimension reader

A function to read the dimensions of image files (supports BMP, PNG, and DDS) without having to actually load the image into memory. Comes with example code.

Speedy animation function (monster, animation, play/playing)

This time saving function will loop a certain animation ( currently idle,die,hit,move,attack ) for any monster, and can also check if the animation is already playing, or check which animation is play

FrameChecker - Gives you a text file with .x models' frame count!

A useful little snippet i wrote which will load up all the x models in the directory, and output a text file with their frame numbers. Also at the code snippet board at tgc.

Download exe here
FrameChecker.zip

inkey$() example

At first I had trouble learning this code [inkey$() to program keys to do certain commands](im new to programming) i finally figured it out and wanted to post it in case others needed it Smiling

Useful mouse co-ordinate saver/detector, with image positioning!

A useful program which can load and position (in real-time) images onto the screen, and stores click coordinates into a text file. very easy to use, and very flexible.
Use this for finding the coordinates of buttons, or where to place a picture onto the screen, or where to draw an object, ect. just load images onto the screen, and get clicking!
please credit if used!

Free (unused) object finder with range

This is my version of the popular 'free object' snippet, except you can choose a range of numbers you want your object to be created in.
In case you didnt know, this snippet goes through a range of numbers (specified by you) and finds the next free (unused) object available.
It is easily modified for sounds or images, ect.
Enjoy!

How to automatically detect the maximum screen resolution

This snippet demonstrates how to automatically detect the user's screen resolution. This lets people with smaller screens run your programs without having to degrade performance for the power users. There's no need to bombard the user with a menu of screen resolutions to pick from either - it's all figured out automatically.

It only works if it's compiled in 640x480 Windowed or Windowed Fullscreen mode through the IDE, but after it auto detects you can set it back to Fullscreen Exclusive mode with set window off.

Widescreen Function

Call widescreen() with the desired aspect ratio and the function will stretch and letterbox the camera viewport appropriately. The default ratio is usually 1.333. The standard widescreen ratio is 1.777.

Maze Generator

This snippet generates a 2D maze. It does this graphically in an image you can paste and also in a 2D Array. Each 'cell' in the array has a North, East, South and West wall which, during generation, may be "knocked down" to produce the maze.

The maze is generated by randomly picking a route by knocking down walls into cells which meet 2 criteria...

Cubic Bézier Curves

This application will generate a set of Cubic Bézier Curves and will bounce them around in a box.

Find the difference between two angles

This function will return the difference in degrees between two angles. The purpose is that it is intelligent enough to know that 350 and 10 are only 20 degrees apart, not 330.

Prims Algorithm - Grid Map Generator

This is a basic example of how a slightly modified Prims Algorithm can be used to generate a grid or web of connections between points on a map.

File IO Data Engine

This FileIO Data Engine can load, modify, and save files in a format similar to the Windows INI. This is the syntax:

BOF
[field1]
key1: parameter1, parameter2, parameter3
key2: parameter1, parameter2, parameter3

[field2]
key1: parameter1, parameter2, parameter3
key2: parameter1, parameter2, parameter3
EOF

These are the functions for loading, saving, modifing, and extracting data from the engine.

Fio_init() - call this first
Fio_reset() - erase all data
Fio_addfield(name$) - add a field, call with field name

A simple and direct resource management tool

Allows you to quickly identify free resource indexes

I've been seeing a lot of posts on the TGC forums regarding methods of easily tracking the available resources. This is a very simple (though I feel effective) method for finding the next available resource index. Since it only tracks forward you can't go back and re-use an index. This isn't usually a problem except in the case where you have limited indexes to work with namely with lights, animations and memblocks.

DBPro Tutorial - The Ins and Outs of Functions

Here author Steve Vink introduces us to the wonders of functions. This is a shorter version of the tutorial, the full length verison can be downloaded - details at the end.

DarkBASIC Professional Tutorial - :7&-$ =;: Encryption

Sometimes we need to protect data, without introducing over-complex routines. A perfect example is High Score tables. It would not be wise to save the information in a plain text file. Conversely, we need to be able to read and write the information efficiently. We'll use the example of saving high score data to illustrate how data can be encrypted and decrypted very easily. Along the way, we'll introduce reading from and writing to files, using bitwise operations and the advantages of not-so random numbers.

Full tutorial and Project files in Newsletter:

DBPro Tutorial - Asset Management

This month author Steve Vink takes us on a spring cleaning tour and talks about Asset Management. This is a shorter version of the tutorial, the full length verison can be downloaded - details at the end.

DBPro Beginners - 2D Side Scrolling Game Engine, Part 2

For all beginner DarkBASIC Professional programmers, Mike S is back with the second and final part of his series of tutorials covering developing a complete 2D side scrolling game with tile map system. This is a monster sized tutorial! The download runs to over 10MB, but it's worth it for all the media, example code and EXEs you get. Building on what you learnt last issue you now get to make media for your game, add in scrolling backgrounds and even a hero. The zip file contains the DBPro source code, media, EXE and tutorial in Microsoft Word format.

by Micheal Shah

DBPro Beginners - 2D Side Scrolling Game Engine, Part 1

For all beginner DarkBASIC Professional programmers, Mike S is back with the first part of a new series of tutorials that will cover developing a complete 2D side scrolling game. In this part you will learn how to create the scrolling engine used to display the tiled background of your game. By the end of it you'll have a basic 2D map editor working that can span many screens in size.

Project and Media can be downloaded from the newsletter link below

by Micheal Shah

http://www.thegamecreators.com/data/newsletter/newsletter_issue_34.html#14

Landscapes

Welcome to yet another tutorial that'll teach you an important component for all your game creation needs. In this tutorial, I'm going to show you how to create a simple landscape, that'll get good performance on any system, and still look good.The full media and source can be downloaded, details at the end of the tutorial.
by Michael Shah

http://www.thegamecreators.com/data/newsletter/newsletter_issue_28.html#17

File Browser v2

This is a file browser I wrote some time in 2004. It uses no media, instead it generates everything itself. The function is about 500 lines long but the important thing is it works.

Also note that this was originally written for use in DBC.

One thing though, you may want to try and implement some mouse click resets. At the moment it is currently counting a variable from 0 -> 10 which, if you are using a fast computer, will result in very fast browsing!

Useful physics related functions

Contains functions such as:
Rotate point
Convex collision
Bouncing
etc.


INSTRUCTIONS
To rotate a point, use RotateX and rotateY to get the new x and y point position.

To check if a point is inside a convex shape, use ConvexCollision. The first x/y values are the location of the point, the others create a line. If it returns 1 when used on every line in the convex shape, the point is in the shape. The lines must go round the center of the shape clockwise.

GUI Functions

Some functions for GUI elements such as check boxes, labels, buttons etc. All use custom graphics.
-
Example program shows how to use them.

Robotic Arm Game

This is a VERY simple game demonstration of how to use limbs, mouse input and a 2nd camera. The aim is to position the end "node" of the arm inside a block, pick it up, manoeuvre it over the blue base pad and drop it on. There is no scoring - its just a demo!

Simple Scancode Monitor

This is a very simple application which monitors your keypreses and displays the Scancode and the ASCII code of the key. Useful when trying to working which keystate ID's you need to monitor.

Remove leading and/or trailing whitespace

Standard trim, rtrim, and ltrim functions to remove leading and trailing whitespace characters.

Comma Separated Values Tokenizer

This code tokenizes a comma separated values string.

Call Csv_addval() with the string you want to tokenize
Csv_getval() returns a token (this returns "CSV_EMPTY" when there are no tokens left)
Call Csv_reset() to clear and tokens left in memory

Note: If you have the extends plugin you can replace the call to "str_find_char(Csv_str, ",")" with "str find char(Csv_str, ",", 1)" and delete the str_find_char() function.

Easy-to-Use Text Search Function

This function will take two strings and search the first string for the second one. It returns a 1 if the string is found, and a 0 if it is not. The third variable is the Case Flag, when set to 0 the function is not case sensitive, any other value will make it Case Sensitive. Please note that this function was made for text adventures, so essentially it searches a sentence for a word and it will not find the search string if it doesn't have spaces around it. Example: Searching "I like cheese" for "like" will return a 1, while searching "Ilikecheese" for "like" will return 0.

Image Memblock Functions

These easy to use functions allow images to be created using memblocks within dark basic pro code. This can be used to create your own images and to edit loaded images.

The shapes included are:
-dot
-line
-circle (not filled)
-box (filled)

The top left pixel of an image memblock is (1,1) when working with these functions. The bottom right is therefore (width,height). Although memblocks allow transparency to be edited, this is ignored in my functions.

The necessary code is below the remark "functions". Everything above is just an example.

Download any file from the internet with DBPro without ftp.

This simple function will enable you to download any file from any url, without ftp.

'from$' is the full url, and 'to$' is the full download path (including the filename and extension) you want to save it as on your harddrive. The function returns a value of 0 if it succeeded, 1 if it failed.

You could also precompile it as a separate executable, running in hidden mode (set from the project settings in the ide), and execute it from your main application so that it runs in the background without affecting your main app.

A* Pathfinding

Project Homepage:http://jt0.org/projects/astar

What This is an A* Pathfinding algorithm implimented in native DarkBASIC Professional code as a simple set of functions.

How The basis of it include first initializing the 'Map' and other needed variables (1 function call), then setting up all the allowable area's on the map itself (1 function called many times), then from there, it's simply a matter of calling the Pathing function, which will return a memory block containing all the path information requested.

Notes

Hello world V1.3

This is an example of Hello World.

* V1.3 - Removed typo
* V1.2 - Fixed small compile error
* V1.1 - Optimized it from 38 lines to two
* V1.0 - Code written

Convert hostname (web address) to IP

Performs a lookup of a hostname and returns its IP address. If the lookup fails the function returns a blank string.

You must first call WSAStartup() to initialize the Winsock DLL prior to doing the lookup, and call WSACleanup() when operations are complete.

HSV to RGB DWORD

This will convert a HSV value into an RGB DWORD. There is an example which will draw a box with given HSV values.

The hsv2dword function takes the 3 components as floats as described by the remark preceding the function.

Timer Based Movement

This is an example template I use for the beginning of projects. It contains the basic layout required for a timer-based movement system.


Theme & Icons by N.Design Studio