This is a one-file C SDL2 project. It is meant for beginners to let
them create SDL-based projects without any pointer specific knowledge.
It offers a micro-library that help create any grid-based user interface.
The only dependency of this project is the SDL2 library.
For those who can deal with pointers and multi-file project with CMake, it is recommended to use the Basic C SDL2 game as a base for any grid-based project/game and the Flying plane SDL2 animation for more sophisticated projects/games.
A list of examples, projects and tutorials could be found at SDL/SDL2 samples and projects
You can use any IDE with C/C++ and SDL2 support like Code::Blocks.
You can follow the following tutorial: How to setup SDL2 with Code::Blocks on Windows
Just copy all the content of src/main.c
and put it in place of any code in
your default main.c
or main.cpp
.
Try to execute the code as it is to check that it is running successfully.
You can now start editing the draw
function (at line #1725).
The micro-library offers some basic functions to start coding the UI:
set_background_color(color)
Set the background color to one of the predefined colors
set_cell_color(x, y, color)
Set the color of the cell at coordinates (x, y)
set_grid_border_color(color)
Set the grid (and cells) border color
delay(ms)
Wait a specified number of milliseconds
get_key()
Get the pressed key
draw_char(c, at_x, at_y, color)
Write a character at position (at_x, at_y) with a specific color
draw_text(text, at_x, at_y, space, color)
Write a text at position (at_x, at_y) with a specific color
The space parameter represents the spacing between characters
The dimensions and the colors of the screen and the grid could be modified.
You have just to update the values of the following (main.c
, line 2):
// Define screen dimensions
#define SCREEN_WIDTH (800)
#define SCREEN_HEIGHT (600)
// Max grid dimension
#define GRID_MAX_X_CELLS (10)
#define GRID_MAX_Y_CELLS (10)
#define GRID_DEFAULT_MARGIN (20)
#define GRID_DEFAULT_COLOR COLOR_WHITE
#define GRID_DEFAULT_BORDER_SIZE (2)
#define GRID_DEFAULT_BORDER_COLOR COLOR_GRAY
The full list of features can be found in the src/main.c
documentation section.
Draw a red dot at coordinates x = 1, y = 2 :
void draw(int x_cells, int y_cells)
{
set_cell_color(1, 2, COLOR_RED);
}
Draw a horizontal blue line:
void draw(int x_cells, int y_cells)
{
for(int x = 0; x < x_cells; x++)
{
set_cell_color(x, 1, COLOR_BLUE);
}
}
Draw a horizontal blue line animated:
void draw(int x_cells, int y_cells)
{
for(int x = 0; x < x_cells; x++)
{
set_cell_color(x, 1, COLOR_BLUE);
delay(100);
}
}
Implement a simple/interactive animation:
See the branch example/animation.
Display text:
void draw(int x_cells, int y_cells)
{
draw_text("Hello world", 1, 1, 1, COLOR_RED);
}
See the branch example/display-text.
Implement a calculator:
See the branch example/calculator.
Implement a simple text editor:
See the branch example/text-editor.
Implement a simple painting app:
See the branch example/paint.
Implement mouse hover:
See the branch example/mouse-hover.
On Debian/Ubuntu based distributions, use the following command:
sudo apt install git build-essential pkg-config cmake cmake-data libsdl2-dev
# Clone this repo
git clone https://gitlab.com/aminosbh/sdl2-pointerless.git
cd sdl2-pointerless
# Create a build folder
mkdir build
cd build
# Build
cmake ..
make
# Run
./sdl2-pointerless
See IDE_USAGE.md for details.
Author: Amine B. Hassouna @aminosbh
This project is distributed under the terms of the MIT license <LICENSE>.