summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorLevent Kaya <42411502+lvntky@users.noreply.github.com>2024-11-25 00:44:07 +0300
committerGitHub <noreply@github.com>2024-11-25 00:44:07 +0300
commit8aed83d2e77c006eebf21776487f788ef12216de (patch)
tree14d7dfd2b4c86dcb3b27912513439b4648145733 /examples
parentff43c66c491b443b7522a3b3d716905ee9732b60 (diff)
parent8650f5e1d7bb72625ae5d6cda2974f6fafa6362d (diff)
Merge pull request #8 from dario-loi/bugfixes
Diffstat (limited to 'examples')
-rw-r--r--examples/empty_example.c53
-rw-r--r--examples/framebuf_info.c17
-rw-r--r--examples/line.c26
-rw-r--r--examples/raw_mode.c20
-rw-r--r--examples/rectangle.c61
-rw-r--r--examples/red.c14
-rw-r--r--examples/texture.c74
7 files changed, 265 insertions, 0 deletions
diff --git a/examples/empty_example.c b/examples/empty_example.c
new file mode 100644
index 0000000..352824a
--- /dev/null
+++ b/examples/empty_example.c
@@ -0,0 +1,53 @@
+#define FBGL_IMPLEMENTATION
+//#define FBGL_HIDE_CURSOR
+#define FBGL_USE_FREETYPE
+#include "fbgl.h"
+
+#include <stdio.h>
+#include <stddef.h>
+
+int main()
+{
+ printf("version %s\n", fbgl_version_info());
+ printf("name %s\n", fbgl_name_info());
+
+ fbgl_t buffer;
+ if (fbgl_init("/dev/fb0", &buffer) == -1) {
+ fprintf(stdout, "Error: could not open framebuffer device\n");
+ return -1;
+ }
+ int color = 0x00000000;
+
+ FT_Library library = fbgl_freetype_init();
+ if (!library) {
+ fbgl_destroy(&buffer);
+ return -1;
+ }
+
+ FT_Face face = fbgl_load_font(library, "../asset/font_2.ttf",
+ 24); // Adjust path and size
+ if (!face) {
+ fbgl_freetype_cleanup(library);
+ fbgl_destroy(&buffer);
+ return -1;
+ }
+
+ // Render text to framebuffer
+ fbgl_render_freetype_text(&buffer, library, face, "Hello, World!", 50,
+ 50);
+
+ // Main loop checking for ESC key
+ int l = 0;
+ while (1) {
+ if (fbgl_check_esc_key()) {
+ fprintf(stdout, "ESC pressed\n");
+ break;
+ }
+ //fbgl_set_bg(&buffer, i++); // Set background color to
+ for (int i = 0x000000; i <= 0xFFFFFF; i++) {
+ fbgl_set_bg(&buffer, i);
+ }
+ }
+ fbgl_destroy(&buffer);
+ return 0;
+}
diff --git a/examples/framebuf_info.c b/examples/framebuf_info.c
new file mode 100644
index 0000000..9d149fd
--- /dev/null
+++ b/examples/framebuf_info.c
@@ -0,0 +1,17 @@
+#define FBGL_IMPLEMENTATION
+#include "fbgl.h"
+
+int main(int argc, char *argv[])
+{
+ fbgl_t buffer;
+ if (fbgl_init("/dev/fb0", &buffer) == -1) {
+ fprintf(stdout, "Error: could not open framebuffer device\n");
+ return -1;
+ }
+
+ fprintf(stdout, "Framebuffer width: %d\n", fb_get_width(&buffer));
+ fprintf(stdout, "Framebuffer height: %d\n", fb_get_height(&buffer));
+ fprintf(stdout, "Framebuffer screen size: %d\n", buffer.screen_size);
+
+ return 0;
+}
diff --git a/examples/line.c b/examples/line.c
new file mode 100644
index 0000000..e22a113
--- /dev/null
+++ b/examples/line.c
@@ -0,0 +1,26 @@
+#define FBGL_IMPLEMENTATION
+#include "fbgl.h"
+
+int main(int argc, char *argv[])
+{
+ fbgl_t buffer;
+ if (fbgl_init("/dev/fb0", &buffer) == -1) {
+ fprintf(stdout, "Error: could not open framebuffer device\n");
+ return -1;
+ }
+
+ fbgl_set_bg(&buffer, 0x00FF0000);
+ fbgl_point_t start = { 0, 0 };
+ fbgl_point_t end = { 1020, 1020 };
+ for (int i = 0; i < 1890; i++) {
+ start.x = i;
+ fbgl_draw_line(start, end, 0xFFFFFF, &buffer);
+ usleep(1000); // sleep for 1 millisecond
+ }
+ fbgl_draw_line(start, end, 0x000000, &buffer);
+
+ while (1) {
+ }
+
+ return 0;
+}
diff --git a/examples/raw_mode.c b/examples/raw_mode.c
new file mode 100644
index 0000000..de97e78
--- /dev/null
+++ b/examples/raw_mode.c
@@ -0,0 +1,20 @@
+#define FBGL_IMPLEMENTATION
+#include "fbgl.h"
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+ fbgl_t fb;
+ fbgl_init("/dev/fb0", &fb);
+
+ fbgl_set_bg(&fb, 0xFFFFFF);
+
+ while (1) {
+ if (fbgl_check_esc_key()) {
+ printf("pressed");
+ fbgl_set_bg(&fb, 0x000000);
+ }
+ }
+
+ return 0;
+}
diff --git a/examples/rectangle.c b/examples/rectangle.c
new file mode 100644
index 0000000..1b3a6f7
--- /dev/null
+++ b/examples/rectangle.c
@@ -0,0 +1,61 @@
+#define FBGL_IMPLEMENTATION
+#include "fbgl.h"
+
+int main(int argc, char *argv[])
+{
+ fbgl_t buffer;
+ if (fbgl_init("/dev/fb0", &buffer) == -1) {
+ fprintf(stdout, "Error: could not open framebuffer device\n");
+ return -1;
+ }
+
+ fbgl_set_bg(&buffer, 0xFFFFFF); // Set the background to white
+
+ fbgl_point_t start = { 100, 100 };
+ fbgl_point_t end = { 200, 200 };
+ fbgl_draw_rectangle_outline(start, end, 0xFF0000,
+ &buffer); // Draw red rectangle outline
+
+ fbgl_point_t start2 = { 600, 400 };
+ fbgl_point_t end2 = { 800, 800 };
+ uint32_t colors[] = { 0xFFC00, 0x00FF00, 0x0000FF,
+ 0xFF00FF }; // Yellow, Green, Blue, Magenta
+ size_t color_index = 0;
+
+ // Initial position of the marquee rectangle
+ int dx = 15; // Horizontal speed
+ int dy = 8; // Vertical speed
+
+ while (1) {
+ // Clear the framebuffer (set background)
+ fbgl_set_bg(&buffer, 0xFFFFFF);
+
+ // Draw the moving filled rectangle
+ fbgl_draw_rectangle_filled(start2, end2, colors[color_index],
+ &buffer);
+
+ // Move the filled rectangle by updating its position
+ start2.x += dx;
+ end2.x += dx;
+ start2.y += dy;
+ end2.y += dy;
+
+ // Reverse direction if the rectangle hits the screen boundary
+ if (start2.x <= 0 || end2.x >= buffer.width) {
+ dx = -dx;
+ color_index++;
+ }
+ if (start2.y <= 0 || end2.y >= buffer.height) {
+ dy = -dy;
+ color_index++;
+ }
+ if (color_index >= 4) {
+ color_index = 0;
+ }
+
+ usleep(50000); // Delay to make the animation visible (adjust as needed)
+ }
+
+ fbgl_destroy(&buffer);
+ return 0;
+}
diff --git a/examples/red.c b/examples/red.c
new file mode 100644
index 0000000..c074143
--- /dev/null
+++ b/examples/red.c
@@ -0,0 +1,14 @@
+#define FBGL_IMPLEMENTATION
+#include "fbgl.h"
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+ fbgl_t fb;
+ fbgl_init("/dev/fb0", &fb);
+ fbgl_set_bg(&fb, 0x00FF0000);
+ while (1) {
+ }
+
+ return 0;
+}
diff --git a/examples/texture.c b/examples/texture.c
new file mode 100644
index 0000000..75a67d7
--- /dev/null
+++ b/examples/texture.c
@@ -0,0 +1,74 @@
+#define FBGL_IMPLEMENTATION
+#include "fbgl.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h> // for usleep
+
+int main(int argc, char **argv)
+{
+ // Initialize framebuffer
+ fbgl_t framebuffer;
+ if (fbgl_init(NULL, &framebuffer) != 0) {
+ fprintf(stderr, "Failed to initialize framebuffer.\n");
+ return EXIT_FAILURE;
+ }
+
+ // Load a TGA texture
+ const char *texture_path = argv[1];
+ fbgl_tga_texture_t *texture = fbgl_load_tga_texture(texture_path);
+ if (!texture) {
+ fprintf(stderr, "Failed to load texture.\n");
+ fbgl_destroy(&framebuffer);
+ return EXIT_FAILURE;
+ }
+
+ // Set a background color (e.g., black)
+ fbgl_set_bg(&framebuffer, 0x000000); // Clear the framebuffer to black
+
+ // Texture movement parameters
+ int texture_x = 0; // Initial horizontal position of the texture
+ int texture_y = 100; // Initial vertical position of the texture
+ int dx = 5; // Horizontal speed (adjust for desired marquee speed)
+ int dy = 3; // Vertical speed (adjust for desired marquee speed)
+
+ // Main rendering loop
+ int framesize = 30 * 30;
+ while (framesize) {
+ // Clear the framebuffer (set background)
+ fbgl_set_bg(&framebuffer, 0x000000);
+
+ // Draw the texture at the current position
+ fbgl_draw_texture(&framebuffer, texture, texture_x, texture_y);
+
+ // Move the texture by updating its position
+ texture_x += dx;
+ texture_y += dy;
+
+ // Reverse direction if the texture hits the screen boundary (X-axis)
+ if (texture_x <= 0 ||
+ texture_x + texture->width >= framebuffer.width) {
+ dx = -dx; // Reverse horizontal direction when hitting the left or right edge
+ }
+
+ // Reverse direction if the texture hits the screen boundary (Y-axis)
+ if (texture_y <= 0 ||
+ texture_y + texture->height >= framebuffer.height) {
+ dy = -dy; // Reverse vertical direction when hitting the top or bottom edge
+ }
+
+ usleep(50000); // Delay to make the marquee effect visible (adjust as needed)
+ framesize--;
+ }
+
+ // Wait for the user to press the escape key before exiting
+ printf("Press ESC to exit...\n");
+ while (!fbgl_check_esc_key()) {
+ // You can add additional rendering logic here if needed
+ }
+
+ // Clean up
+ fbgl_destroy_texture(texture);
+ fbgl_destroy(&framebuffer);
+
+ return EXIT_SUCCESS;
+}