Kandinsky now uses KDSetPixel for framebuffer access

This commit is contained in:
Romain Goyet
2015-09-01 11:37:12 +02:00
parent 25b95d1278
commit d70d3b4e4a
10 changed files with 70 additions and 39 deletions

View File

@@ -1,8 +1,8 @@
#include <kandinsky/line.h>
#include <kandinsky/referential.h>
#include <kandinsky/pixel.h>
void KDDrawLine(KDPoint p1, KDPoint p2) {
void KDDrawLine(KDPoint p1, KDPoint p2) { //, KDColor c) {
for (KDCoordinate x = p1.x; x<p2.x; x++) {
// COLOR(KDPOINT(x, p1.y)) = 0xFF;
KDSetPixel(KDPOINT(x, p1.y), 0xFF /* c */);
}
}

42
kandinsky/src/pixel.c Normal file
View File

@@ -0,0 +1,42 @@
#include <kandinsky/pixel.h>
#include <kandinsky/config.h>
#include <assert.h>
static KDPoint sOrigin = {.x = 0, .y = 0};
#define BIT_MASK(high, low) ((((uint32_t)1<<((high)-(low)+1))-1)<<(low))
#define BIT_VALUE(value, high, low) (((value)<<(low))&BIT_MASK(high, low))
static inline void set_color(KDColor * address, int8_t high_bit, int8_t low_bit, KDColor value) {
assert(high_bit-low_bit+1 <= 8*sizeof(KDColor)); // Interval too large
assert(low_bit >= 0); // Interval starts too early
assert(low_bit < 8*sizeof(KDColor)); // Interval ends too late
KDColor mask = ((((KDColor)1<<(high_bit-low_bit+1))-1)<<low_bit);
assert(address >= KD_FRAMEBUFFER_ADDRESS);
assert(address < (KD_FRAMEBUFFER_ADDRESS+(KD_FRAMEBUFFER_WIDTH*KD_FRAMEBUFFER_HEIGHT*KD_BITS_PER_PIXEL)/8));
*address = (*address & ~mask) | (value & mask);
}
void KDSetOrigin(KDPoint origin) {
sOrigin = origin;
}
KDPoint KDGetOrigin() {
return sOrigin;
}
void KDSetPixel(KDPoint p, KDColor c) {
assert(p.x >= 0);
assert(p.x < KD_FRAMEBUFFER_WIDTH);
assert(p.y >= 0);
assert(p.y < KD_FRAMEBUFFER_HEIGHT);
int pixels_offset = p.y*(KD_FRAMEBUFFER_WIDTH) + p.x;
int bits_offset = (KD_BITS_PER_PIXEL)*pixels_offset;
const uint8_t color_bitsize = 8*sizeof(KDColor);
// Notice: the "const" here is rather important. Indeed, it hints the compiler
// for the following "modulo", which helps us avoid idivmod.
int color_offset = bits_offset/color_bitsize;
int8_t low_bit = bits_offset % color_bitsize;
int8_t high_bit = low_bit + KD_BITS_PER_PIXEL - 1;
set_color((KDColor *)(KD_FRAMEBUFFER_ADDRESS+color_offset), high_bit, low_bit, c);
}

View File

@@ -1,9 +1,15 @@
#include <kandinsky/rect.h>
#include <kandinsky/referential.h>
#include <kandinsky/pixel.h>
#include <string.h>
void KDFillRect(KDRect rect, KDColor color) {
for (KDCoordinate y = rect.y ; y < (rect.y + rect.height); y++) {
memset(KDPixelAddress((KDPoint){.x=rect.x, .y=y}), color, rect.width);
//for (KDCoordinate y = rect.y ; y < (rect.y + rect.height); y++) {
//memset(KDPixelAddress((KDPoint){.x=rect.x, .y=y}), color, rect.width);
//}
KDPoint p;
for (p.x = rect.x; p.x<(rect.x+rect.width-1); p.x++) {
for (p.y = rect.y; p.y<(rect.y+rect.height-1); p.y++) {
KDSetPixel(p, color);
}
}
}

View File

@@ -1,20 +0,0 @@
#include <kandinsky/referential.h>
#include <kandinsky/config.h>
static KDPoint sOrigin = {.x = 0, .y = 0};
void KDSetOrigin(KDPoint origin) {
sOrigin = origin;
}
KDPoint KDGetOrigin() {
return sOrigin;
}
KDColor * KDPixelAddress(KDPoint p) {
KDPoint origin = KDGetOrigin();
return (KDColor *)(KD_FRAMEBUFFER_ADDRESS
+ KD_FRAMEBUFFER_WIDTH*(p.y + origin.y)
+ (p.x + origin.x));
}

View File

@@ -1,12 +1,12 @@
#include <kandinsky/text.h>
#include <kandinsky/referential.h>
#include <kandinsky/pixel.h>
#include <string.h>
#include "font.h"
void KDDrawChar(char character, KDPoint p) {
for (int j=0; j<BITMAP_FONT_CHARACTER_HEIGHT;j++) {
for (int i=0; i<BITMAP_FONT_CHARACTER_WIDTH;i++) {
COLOR(KDPOINT(p.x+i, p.y+j)) = 0xFF-bitmapFont[character-BITMAP_FONT_FIRST_CHARACTER][j][i];
KDSetPixel(KDPOINT(p.x+i, p.y+j), 0xFF-bitmapFont[character-BITMAP_FONT_FIRST_CHARACTER][j][i]);
}
}
}