VGA Color not Working

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

VGA Color not Working

Post by Caffeine »

Hi, I am trying to be able to plot pixels of any color using hexadecimal values as input. Any tips on how to do this?

My code so far:

Code: Select all

void plotPixle(int x, int y, unsigned char color) {
    /*
    Plot a pixle in a certain color to the screen

    Parameters
    ----------
        x : int
            The x position of the pixle
        y : int
            The y position of the pixle
        color : unsigned char
            The color inputted in hexadecimal
    */

    switch (mbi->framebuffer_bpp) {
    case 8:
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * y + 1 * x + i] = 4; }
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * (y + i) + 1 * x] = 4; }
        break;
    case 15:
        break;
    case 16:
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * y + 2 * x + i] = color; }
        break;
    case 24:
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * y + 3 * x + i] = color; }
        break;
    case 32:
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * y + 4 * x + i] = color; }
        break;
    default:
        // error
        break;
    }
}
Changing the color value between 0-255 changes how bright it is. Anything above 255 does not work.
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: VGA Color not Working

Post by klange »

Nearly everything about the code you posted is wrong. You are passing color as an unsigned char, so of course it only accepts values from 0-255. Anything else will wrap around and stay in that range. The logic for every single one of your bpp switch cases is wrong. I have no idea what you're trying to do but none of it makes sense. The one that is closest to being reasonable is the 32bpp case, but you are still assigning the same value to every channel and are only ever going to get shades of gray because of that. In all of the other cases, you are just completely wrong and I can't even begin to understand what you think you're doing.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: VGA Color not Working

Post by Caffeine »

klange wrote:Nearly everything about the code you posted is wrong. You are passing color as an unsigned char, so of course it only accepts values from 0-255. Anything else will wrap around and stay in that range. The logic for every single one of your bpp switch cases is wrong. I have no idea what you're trying to do but none of it makes sense. The one that is closest to being reasonable is the 32bpp case, but you are still assigning the same value to every channel and are only ever going to get shades of gray because of that. In all of the other cases, you are just completely wrong and I can't even begin to understand what you think you're doing.
Yeah no I just figured it out and I am so sorry. I completely misunderstood what it was doing. Sorry for the mistake.
Post Reply