6 min. read
Been through a few revisions since v0.1.0 of the cmdfx Game Engine. This is a short video showcasing the features in v0.1.3 of the engine, which included some new features in the Physics Engine.
The source code in question is the launcher sample available here:
/**
* @file launcher.c
* @author Gregory Mitchell (me@gmitch215.xyz)
* @date 2025-03-28
*
* This is a simple demo program that shows how to use the Physics Engine for CmdFX.
*
* Simply run the program and watch the object fly around the screen.
* You can optionally pass a multiplicative modifier to the tick speed as the first parameter
* to see how fast it will go.
*
* @copyright Copyright (c) 2025
*
*/
#include <cmdfx.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
char** data = Char2DBuilder_createFilled(5, 5, '%');
data[0][0] = 0;
data[4][0] = 0;
data[0][4] = 0;
data[4][4] = 0;
CmdFX_Sprite* sprite = Sprite_create(data, 0, 0);
double speed = 1;
if (argc > 1) {
speed = atof(argv[1]);
if (speed > 0) CmdFX_setTickSpeed((int) (CmdFX_getTickSpeed() * speed));
}
if (argc > 2) {
double friction = atof(argv[2]);
if (friction >= 0) Engine_setDefaultFrictionCoefficient(atof(argv[2]));
}
Canvas_clearScreen();
Canvas_hideCursor();
Engine_enableMotionDebug();
CmdFX_initThreadSafe();
Canvas_hLine(0, Engine_getGroundY(), Canvas_getWidth(), '-');
Canvas_hLine(0, 1, Canvas_getWidth(), '-');
Sprite_draw(2, Canvas_getHeight() - sprite->height - 1, sprite);
Engine_start();
CmdFX_Vector* velocity = Vector_create(2, 2);
Sprite_addForceFor(sprite, velocity, 300 / speed);
sleepMillis(10000 / speed);
Engine_end();
free(velocity);
Sprite_free(sprite);
CmdFX_destroyThreadSafe();
Canvas_showCursor();
return 0;
}
First, we create a 5x5 sprite with a border of %
characters. This is done using the Char2DBuilder_createFilled
function, which creates a 2D array of characters filled with the specified character. The 0
at the end removes the corners of the sprite, making it look like a rounded square. The sprite is then created using the Sprite_create
function, and we leave the Z-index and ANSI codes at 0
for no unnecessary data.
Next, there are some extra parameters for the program. The first parameter is a speed modifier, which is multiplied by the current tick speed. The tick speed in this case is how fast the Physics Engine processes new information. The tick speed by default is 12
, so you can set it to 0.5
to get a slower effect, or 2
for a much faster engine. This allows you to see how fast the object will go if you want to test it out. The second parameter is the default friction coefficient. In Physics, this is used in the calculation for the deceleration of the object when it is moving (or the friction of the object). The default value is 0.5
, but you can set it to 0
to see how fast the object will go without any friction. This is useful for testing out the engine and seeing how fast it can go.
Then, we move on to some canvas commands:
Canvas_clearScreen()
clears the screen so we get a clean slate to work with.Canvas_hideCursor()
hides the cursor so it doesn’t show up on the screen.Engine_enableMotionDebug()
enables the motion debug mode, which shows the motion of each individual sprite drawn on the screen. This is the information tab shown in the top left of the screen shown in the video.CmdFX_initThreadSafe()
initializes the thread-safe mode for the engine. This is used to make sure that the engine is thread-safe and can be used in a multi-threaded environment, which is necessary for the engine to work properly. This method is also automatically called by Engine_start()
if you don’t want to call it manually.Now, we draw some visuals on the screen:
Canvas_hLine(0, Engine_getGroundY(), Canvas_getWidth(), '-')
draws a horizontal line at the bottom of the screen, which is the ground level for the engine. The Engine_getGroundY()
function gets the Y position of the ground level, and Canvas_getWidth()
gets the width of the screen.Canvas_hLine(0, 1, Canvas_getWidth(), '-')
draws a horizontal line at the top of the screen, which is the top of the screen.These are just for visual purposes and doesn’t do anything else other than visualize the top and bottom of the screen.
After we draw the sprite with Sprite_draw(2, Canvas_getHeight() - sprite->height - 1, sprite)
, we start the engine with Engine_start()
. This starts the engine and begins processing the physics for the sprite.
The CmdFX_Vector* velocity = Vector_create(2, 2)
creates a new vector with the specified X and Y values. This is used to set the velocity of the sprite. The Sprite_addForceFor(sprite, velocity, 300 / speed)
adds a force to the sprite for the specified amount of time. The 300 / speed
is the amount of time in milliseconds that the force will be applied to the sprite. This is used to make the sprite move in a certain direction. Like in Physics, forces increase the acceleration of the object, and this is how we can make the object move in a certain direction. The sleepMillis(10000 / speed)
is used to pause the program for the specified amount of time. This is used to give the sprite time to move before the program ends. The 10000 / speed
is the amount of time in milliseconds that the program will pause for. This is used to give the sprite time to move before the program ends.
Finally, we end the engine with Engine_end()
, free the memory for the vector and sprite, and destroy the thread-safe mode with CmdFX_destroyThreadSafe()
. This is done to clean up any resources that were used by the engine and free up memory. Then, we show the cursor again with Canvas_showCursor()
to make sure that the cursor is visible again.
This is a simple demo program that shows how to use the Physics Engine for CmdFX. It is a simple program that shows how to use the engine and how to create a sprite with a border. The program is simple and easy to use, and it is a good starting point for anyone who wants to learn how to use the engine. The engine is still in development, and there are many more features to come in the future. I hope you enjoy using the engine and have fun with it!