Featured Member Projects > Ninjabyte Electronics (hardware)

Graphics and other system routines

(1/13) > >>

DarkestEx:
Hello, in this topic you can suggest and vote for graphics and drawing routines that I we will use in he official API and system.

To start, I want to have some sprite routines, but I can't get them to work:

--- Code: ---void oled_bdraws(int16_t x, int16_t y, uint8_t width, uint8_t height, const uint8_t bitmap[]) {
uint8_t ow = width;

if(width + x > BUFFER_WIDTH)
width -= width + x - BUFFER_WIDTH;
if(height + y > BUFFER_HEIGHT)
height -= height + y - BUFFER_HEIGHT;
if(width + x <= 0 || height + y <= 0)
return;

for(int yi = y; yi < (y + height); yi++) {
for(int xi = x; xi < (x + width); xi++) {
if((yi - y) < 0 || (xi - x) < 0)
continue;
if(bitmap[(yi - y) * ow + (xi - x)] > 0)
screen_buffer[yi * ow + xi] = bitmap[(yi - y) * ow + (xi - x)];
}
}
}
--- End code ---

The language for all routines should be C, but many other languages can be ported pretty easily I guess.
There is one framebuffer called screen_buffer[128*128]. It is using a palette. If you want to make your own functions, just make them take inputs (coordiantes, colors, etc.) and let them write to screen_buffer. The color 0x00 is transparent when drawn.

c4ooo:
Here is an optimized circle drawing routine in java:

--- Code: ---    public void drawCircle(int x, int y, int r) {
        double angle, x1, y1;
        for (angle = 0; angle < 90; angle += 0.1) {
            x1 = r * Math.cos(angle * Math.PI / 180);
            y1 = r * Math.sin(angle * Math.PI / 180);
            setPixel((int) (x + x1), (int) (y + y1)); //bottom left
            setPixel((int) (x + x1), (int) (y - y1)); //top left
            setPixel((int) (x - x1), (int) (y + y1)); //nottom right
            setPixel((int) (x - x1), (int) (y - y1)); // top right
        }
    }

--- End code ---
I dont really know how you handle doubles/floats in c :P

setPixer is self explanatory:

--- Code: ---    public void setPixel(int x, int y) {
        if (x >= 0 && x < width && y >= 0 && y < height) {
            pixels[x + width * y] = color;
        }
    }

--- End code ---

Drawing filled circles is a bit more tricky:


--- Code: ---
    public void drawFillCircle(int x, int y, int r) {
        double angle, x1, y1;
        for (angle = 0; angle < 90; angle += 0.1) {
            x1 = r * Math.cos(angle * Math.PI / 180);
            y1 = r * Math.sin(angle * Math.PI / 180);
            setPixel((int) (x + x1), (int) (y + y1)); //bottom left
            setPixel((int) (x - x1), (int) (y + y1)); //nottom right
            for (double i = x - x1; i <= x + x1; i++) {
                setPixel((int) i, (int) (y + y1));
                setPixel((int) i, (int) (y - y1));
            }
        }
    }

--- End code ---

Hope it helps you in some way ;)

semiprocoder:
Since graphics has extensive trig, have an index of already done trig values so that you don't have to always do the trig. For more accuracy you would obiously need to use full trig, but I think you could do angle vals 1-90 or something without wasting too much ram, like have a 360 b devoted to trig index, and a bit more to arc values

c4ooo:
There will be no accuracy increase from using 1-360, just a speed decrease. Basicly all i need to do is calculate a quarter slice of the circle, then flip the values over the center to get the rest. ;)

semiprocoder:
Yeah thats why I was saying 1-90 instead of 1-360  :P

The 360 was 90*4(for num bytes in a float)=360

Navigation

[0] Message Index

[#] Next page

Go to full version