OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:34 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Limine Bootloader displaying text onto a square.
PostPosted: Sun Apr 09, 2023 7:17 am 
Offline

Joined: Sun Apr 09, 2023 7:12 am
Posts: 1
Does anyone know how to do this.

Code:
#include <stdint.h>
#include <stddef.h>
#include <limine.h>

// The Limine requests can be placed anywhere, but it is important that
// the compiler does not optimise them away, so, usually, they should
// be made volatile or equivalent.

static volatile struct limine_framebuffer_request framebuffer_request = {
    .id = LIMINE_FRAMEBUFFER_REQUEST,
    .revision = 0
};

// GCC and Clang reserve the right to generate calls to the following
// 4 functions even if they are not directly called.
// Implement them as the C specification mandates.
// DO NOT remove or rename these functions, or stuff will eventually break!
// They CAN be moved to a different .c file.

void *memcpy(void *dest, const void *src, size_t n) {
    uint8_t *pdest = (uint8_t *)dest;
    const uint8_t *psrc = (const uint8_t *)src;

    for (size_t i = 0; i < n; i++) {
        pdest[i] = psrc[i];
    }

    return dest;
}

void *memset(void *s, int c, size_t n) {
    uint8_t *p = (uint8_t *)s;

    for (size_t i = 0; i < n; i++) {
        p[i] = (uint8_t)c;
    }

    return s;
}

void *memmove(void *dest, const void *src, size_t n) {
    uint8_t *pdest = (uint8_t *)dest;
    const uint8_t *psrc = (const uint8_t *)src;

    if (src > dest) {
        for (size_t i = 0; i < n; i++) {
            pdest[i] = psrc[i];
        }
    } else if (src < dest) {
        for (size_t i = n; i > 0; i--) {
            pdest[i-1] = psrc[i-1];
        }
    }

    return dest;
}

int memcmp(const void *s1, const void *s2, size_t n) {
    const uint8_t *p1 = (const uint8_t *)s1;
    const uint8_t *p2 = (const uint8_t *)s2;

    for (size_t i = 0; i < n; i++) {
        if (p1[i] != p2[i]) {
            return p1[i] < p2[i] ? -1 : 1;
        }
    }

    return 0;
}

// Halt and catch fire function.
static void hcf(void) {
    asm ("cli");
    for (;;) {
        asm ("hlt");
    }
}

// The following will be our kernel's entry point.
// If renaming _start() to something else, make sure to change the
// linker script accordingly.

void main_window(void) {
    // Define the parameters for the square.
    struct limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0];
    int x = 0;
    int y = 0;
    int size = 2000;

    // Draw the square.
    for (int i = 0; i < size; i++) {
        uint32_t *fb_ptr = framebuffer->address;
        int row = y + i;
        int col_start = x;
        int col_end = x + size - 1;
        int col_count = col_end - col_start + 1;
        int fb_pitch = framebuffer->pitch / 4;
        uint32_t color = 0x7F7F7F; // White color.

        for (int j = 0; j < col_count; j++) {
            *(fb_ptr + row * fb_pitch + col_start + j) = color;
        }
    }
}

void draw_window(void) {

    // Define the parameters for the square.
    struct limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0];

    int x = 100;
    int y = 100;
    int size = 100;

    // Draw the square.
    for (int i = 0; i < size; i++) {
        uint32_t *fb_ptr = framebuffer->address;
        int row = y + i;
        int col_start = x;
        int col_end = x + size - 1;
        int col_count = col_end - col_start + 1;
        int fb_pitch = framebuffer->pitch / 4;
        uint32_t color = 0xFFFFFF; // White color.

        for (int j = 0; j < col_count; j++) {
            *(fb_ptr + row * fb_pitch + col_start + j) = color;
        }
    }
}


void _start(void) {
    // Ensure we got a framebuffer.
    if (framebuffer_request.response == NULL
     || framebuffer_request.response->framebuffer_count < 1) {
        hcf();
    }

    // Fetch the first framebuffer.
    struct limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0];

    // Note: we assume the framebuffer model is RGB with 32-bit pixels.

    main_window();
    draw_window();

    // We're done, just hang...
    hcf();
}


Top
 Profile  
 
 Post subject: Re: Limine Bootloader displaying text onto a square.
PostPosted: Wed Apr 12, 2023 3:51 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
It's the same as drawing text into any other bitmap. You'll need a font and some code to render glyphs from that font.

In an OS kernel, you probably want to keep it simple and use a bitmap font. You can find some more information on the wiki. Keep in mind the example code may be very slow, since it isn't optimized for speed.

You might find even more information by researching how to render bitmap fonts to textures for games. The process of plotting pixels to a texture is pretty much the same as plotting pixels to your framebuffer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 71 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group