summaryrefslogtreecommitdiff
path: root/README.md
blob: c8f0982e788b01cbc87b7c3fd44ec64ffe7622e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

# fbgl: Lightweight 2D Framebuffer Library for Linux

`fbgl` (Framebuffer Graphics Library) is a minimalistic, **header-only** 2D framebuffer library written in C. Designed for simplicity and performance, `fbgl` provides an intuitive API for directly manipulating the Linux framebuffer device (`/dev/fb0`). Whether you're experimenting with low-level graphics or building lightweight graphical applications, `fbgl` offers the foundation you need.

---

## Features

- **Header-only design**: Include `fbgl.h` and start coding.
- **Direct framebuffer rendering**: Writes directly to `/dev/fb0` for high performance.
- **Simple API**: Easy-to-use functions for initializing, clearing, and drawing.
- **Lightweight**: Minimal dependencies, using only standard Linux libraries.
- **Custom rendering**: Draw pixels, lines, and shapes directly to the framebuffer.

---

## Getting Started

### Prerequisites

- A Linux-based system with framebuffer support.
- Development tools like GCC.
- Access to `/dev/fb0` (requires elevated permissions or proper user configuration).

### Installation

No installation is required! Simply copy the `fbgl.h` file into your project directory and include it in your source files,
while also defining the `FBGL_IMPLEMENTATION` macro in one of your source files.

```c
#define FBGL_IMPLEMENTATION
#include "fbgl.h"
```

---

## Usage

### Example Program

Here’s a simple program that initializes the framebuffer, clears it to a blue color, and draws a red diagonal line.

```c
#include "fbgl.h"
#define FBGL_IMPLEMENTATION
#include <stdio.h>

int main()
{

    fbgl_t buffer;

    // Initialize the framebuffer
    if (fbgl_init("/dev/fb0", &buffer) != 0) {
        fprintf(stderr, "Failed to initialize framebuffer\n");
        return 1;
    }

    printf("Framebuffer size: %dx%d\n", fb_get_width(), fb_get_height());

    // Clear framebuffer to blue
    fbgl_clear(0x0000FFFF); // Blue color

    // Draw a red diagonal line
    for (int i = 0; i < fb_get_width() && i < fb_get_height(); i++) {
        fbgl_put_pixel(i, i, 0xFFFF0000, &buffer); // Red
    }

    // Wait for user input before exiting
    getchar();

    // Clean up
    fbgl_destroy(&buffer);
    return 0;
}
```

Compile the program:

```bash
gcc -o example main.c
```

Run the program with elevated permissions to access `/dev/fb0`:

```bash
sudo ./example
```

---

## API Reference

### Initialization and Cleanup

#### `int fbgl_init(const char *device);`
Initializes the framebuffer.

- **Parameters**:  
  `device`: Path to the framebuffer device (e.g., `/dev/fb0`).

- **Returns**:  
  `0` on success, `-1` on failure.

#### `void fbgl_destroy(void);`
Destroys the framebuffer and releases resources.

---

### Drawing Functions

#### `void fbgl_clear(uint32_t color);`
Fills the entire framebuffer with a specified color.

- **Parameters**:  
  `color`: 32-bit ARGB color (e.g., `0xFFFF0000` for red).

#### `void fbgl_put_pixel(int x, int y, uint32_t color);`
Sets a pixel at the specified position to the given color.

- **Parameters**:  
  `x, y`: Pixel coordinates.  
  `color`: 32-bit ARGB color.

---

### Utility Functions

#### `int fbgl_get_width(void);`
Returns the width of the framebuffer in pixels.

#### `int fbgl_get_height(void);`
Returns the height of the framebuffer in pixels.

---

## How It Works

1. **Framebuffer Device**: `fbgl` uses the Linux framebuffer device (`/dev/fb0`) to directly access the screen memory.
2. **Memory Mapping**: The framebuffer is mapped into user-space memory using `mmap`, allowing for direct pixel manipulation.
3. **Direct Rendering**: Pixels are written directly to the framebuffer, bypassing higher-level graphics APIs.

---

## Limitations

- **Platform-specific**: Works only on Linux systems with framebuffer support.
- **Root permissions**: Access to `/dev/fb0` often requires `sudo`.
- **No hardware acceleration**: Rendering is done in software, so performance depends on CPU speed.

---

## Roadmap

Future improvements for `fbgl` may include:
- Support for double buffering.
- More advanced drawing primitives (e.g., circles, filled polygons).
- Cross-platform abstraction for non-Linux systems.
- Text rendering using bitmap fonts.
- Performance optimizations for large resolutions.

---

## Contributing

Contributions are welcome! If you’d like to improve `fbgl`, add features, or fix bugs:
1. Fork the repository.
2. Create a new branch for your changes.
3. Submit a pull request with a clear description of your updates.

---

## License

`fbgl` is licensed under the MIT License. See the `LICENSE` file for details.

---

## Acknowledgments

- Inspired by the simplicity of low-level graphics programming.
- Thanks to the Linux community for making framebuffer programming accessible!

---

## Showcase

First Texture Rendering

![fist texture render](./docs/texture.gif)

---

## Contact

If you have questions or suggestions, feel free to reach out via GitHub or email.

Happy coding with `fbgl`! 🚀