RBXE Pixel Engine is written in C using internally a pixel buffer using OpenGL for visualisation. It's meant to be extremely simple, lightweight, and easy to use.
Add the same time i'm working on a game (using this pixel engine) called Atic Atac. More info on that you find here
#define RBXE_ENGINE
#include <rbxe.h>
#define WIDTH 800
#define HEIGHT 600
#define SCALE 4
#define FULLSCREEN TRUE
int main(void) {
if (!rbxeStart("Hello", WIDTH, HEIGHT, SCALE, FULLSCREEN)) return EXIT_FAILURE;
while (rbxeRun()) {
if (rbxeKeyPressed(KEY_ESCAPE)) {
break;
}
}
return rbxeEnd();
}
As a header only solution, you need to define
RBXE_ENGINE
before including rbxe.h to access the
implementation details.
#define RBXE_ENGINE
#include <rbxe.h> /* Basic pixel engine */
#include <rbxe-font.h> /* Only if you need font support */
#include <rbxe-sprite.h> /* Only if you need sprite support */
#include <rbxe-math.h> /* Only if you need math support */
You should only define RBXE_ENGINE
in a single translation unit.
To install GLEW:
brew install glew
To install GLEW and GLFW
sudo apt-get install libglew-dev
sudo apt-get install libglfw3-dev
To build ALL examples, simply use:
make all
Compile a specific example by including rbxe.h as a header only solution:
make example/mandelbrot.c
The resulting executable will be called mandelbrot and is created in the bin folder.
To delete the compiled binaries and object files, do:
make clean
int rbxeStart(const char* title, int winwidth, int scale, int fullscreen);
Creates a window with title
, width
and height
specified by the first three
arguments. The fourth argument reprepresent the scale
of
the pixel buffer that is allocated and returned to be rendered. The pixel
buffer is a contiguous array of type Pixel in row-major order. The size in memory
of the pixel buffer is exactly the width by the height by the size of
the internal pixel_info
struct. (See rbxeGetBuffer()
).
The last parameter fullscreen
defines if the engines starts fullscreen or windowed mode.
It returns a non-zero if the engine started correctly, otherwise it returns zero.
int rbxeStep(void);
This function flushes the previous frame from the screen, swaps the rendering
buffers, polls for inputs, and checks if the rendering window is still opened. It
returns zero if the window was closed, otherwise it returns a non-zero value.
Unless using rbxeRun()
, this functions should be called once every frame.
void rbxeRender(const pixel_info* pixbuf);
Renders the pixel buffer passed as argument to the screen. This function can be
called more than once each frame and you can pass any texture that has the
same resolution and size in memory as the one returned by rbxeStart()
.
Otherwise, it results in undefined behaviour. Rendering a pixel with an alpha value
of zero will result in the pixel having the color of the background or what was
already rendered on that pixel. An alpha value of 255 will result in the exact
linear RGB color of the rendered pixel. All values in between are automatically
interpolated.
int rbxeRun(const pixel_info* pixbuf);
This function is exactly the same as calling rbxeStep()
and
rbxeRender()
. It clears the screen from the previous frame, renders the
pixel buffer passed as argument and checks for input. It returns a non-zero
value if the rendering window is still opened, otherwise it returns zero. This
function does all what's necesary to run and render with RBXE and it's most useful
as the check to the main rendering loop.
int rbxeEnd(Px* pixbuf);
Deallocates the pixel buffer and closes the render window along with OpenGL libraries.
void rbxeScreenSize(int* width, int* height);
void rbxeWindowSize(int* width, int* height);
These functions return the width and height of the initialized screen and window respectively, inside the first and second pointers passed to the function. Expects valid integers as arguments.
void rbxeBackgroundColor(pixel_info color);
Sets the color of the background to the color specified by the passed argument. It has permanent state, so it does not need to be called every frame, but it can be changed at any time. The background color is set to black by default.
int rbxeKeyDown(const int key);
int rbxeKeyPressed(const int key);
int rbxeKeyReleased(const int key);
int rbxeMouseDown(const int button);
int rbxeMousePressed(const int button);
int rbxeMouseReleased(const int button);
These keyboard and mouse input functions return 0 or 1 depending on the state
of the key or button passed as argument. The values to be tested are defined by
macros in rbxe.h with the syntax KEY_NAME and MOUSE_NAME where NAME is the
identifier of the key or button. For mouse input, the values are
MOUSE_LEFT
, MOUSE_RIGHT
and MOUSE_MIDDLE
. Some keyboard
examples are KEY_ESCAPE
, KEY_SPACE
, KEY_CONTROL
,
KEY_LEFT
, KEY_RIGHT
, and for character keys KEY_W
.
These macro definitions have the same identifiers and values as glfw input codes,
they are essentially shorter and less verbose versions.
char rbxeKeyChar(void);
Returns the last character key pressed by the user.
void rbxeMousePos(int* xptr, int* yptr);
Gets the X and Y position of the mouse cursor relative to the pixel buffer in screen coordinates and puts those values in the pointers passed in as arguments. It will return negative or larger than the screen numbers if the mouse is outside of the screen. Returns valid pixel positions if the mouse is inside the pixel buffer and the render window. Expects valid integers as arguments.
void rbxeMouseVisible(int);
Sets the native mouse cursor from your OS to be invisible if you pass a 0, or visible if you pass any other value.
double rbxeTime(void);
Returns time from the moment RBXE was initialized in seconds.
void rbxeClear(const int value);
Clears the buffer with the given value
.
void rbxeSetPixel(const int x, const int y, const pixel_info color);
Set a pixel at x
and y
position with the specified color
.
void rbxePlotLine(int x1, int y1, int x2, int y2, const pixel_info color);
Plots a line from x1
,y1
to x2
,y2
with the specified color
.
void rbxePlotCircle(int xc, int yc, int r, const pixel_info color);
Plot a circle at xc
,yc
with radius r
and the specified color
.
void rbxeFontInit(void);
Initialize the font engine.
void rbxeFontDrawChar(int x, int y, const unsigned char ch, pixel_info fg, pixel_info bg);
Draw a single character.
void rbxeFontDrawString(int x, int y, char* str, pixel_info fg, pixel_info bg);
Draw a string.
Sprite* rbxeSpriteLoad(char* filename, int cell_width, int cell_height);
Description follows when implementation finished
Sprite* rbxeSpriteClone(Sprite* sprite);
Description follows when implementation finished
void rbxeSpriteRender(Sprite* sprite);
Description follows when implementation finished
void rbxeSpriteUpdate(Sprite* sprite);
Description follows when implementation finished
void rbxeSpriteSetPosition(Sprite* sprite, float x, float y);
Description follows when implementation finished
void rbxeSpriteSetVelocity(Sprite* sprite, float x, float y);
Description follows when implementation finished
void rbxeSpriteSetActive(Sprite* sprite, int active);
Description follows when implementation finished
void rbxeSpriteSetHealth(Sprite* sprite, int health);
Description follows when implementation finished
It is higly recommened that you use the mentioned drawing funcctions to access the pixel buffer. However, there are situations where you might like to direct access the pixel buffer.
Main and only tyepedefed structure used by the engine. The simple pixel engine handles only 4 channel RGBA buffers.
typedef struct pixel_info {
unsigned char r, g, b, a;
} Px;
By initializing RBXE you are given a 8-bit RGBA pixel buffer in a row-major order where the coordinate (0, 0) corresponds to the top left pixel of the image. At each frame you render from the CPU side by directly accessing and modifying the values of the pixel buffer giving you fast and direct control over every pixel in the screen.
You can get access to the buffer by calling:
pixel_info* pixbuf = rbxeGetBuffer();
This is an example on how you can plot a pixel at position (x, y) to red.
Access to width
you have by calling rbxeScreenSize()
pixel_info red = {255, 0, 0, 255};
pixbuf[y * width + x] = red;
Where width
is the fourth argument given to rbxeStart()
.