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,5 +1,6 @@
SFLAGS += -Ikandinsky/include -Iplatform/stm32f429
objs += $(addprefix kandinsky/src/, line.o text.o font.o rect.o referential.o)
SFLAGS += -Ikandinsky/include
objs += $(addprefix kandinsky/src/, font.o line.o pixel.o rect.o text.o)
tests += $(addprefix kandinsky/test/, set_pixel.o)
FREETYPE_PATH := /usr/local/Cellar/freetype/2.5.5
LIBPNG_PATH := /usr/local/Cellar/libpng/1.6.17
@@ -21,3 +22,4 @@ kandinsky/fonts/rasterizer: kandinsky/fonts/rasterizer.c
@clang -I$(FREETYPE_PATH)/include/freetype2 -L$(FREETYPE_PATH)/lib -lfreetype $< -o $@
products += $(font_files) kandinsky/fonts/rasterizer

View File

@@ -3,8 +3,8 @@
#include <kandinsky/color.h>
#include <kandinsky/line.h>
#include <kandinsky/pixel.h>
#include <kandinsky/rect.h>
#include <kandinsky/referential.h>
#include <kandinsky/text.h>
#include <kandinsky/types.h>

View File

@@ -4,18 +4,18 @@
#include <stdint.h>
#include <kandinsky/config.h>
#if (KD_BITS_PER_PIXEL & (KD_BITS_PER_PIXEL - 1))
#error KD_BITS_PER_PIXEL should be a power of two!
// PoT implies that all pixels are byte-aligned
#endif
#if KD_BITS_PER_PIXEL <= 8
#warning KD_BITS_PER_PIXEL
#warning 8 bpp
typedef uint8_t KDColor;
/*
#elif KD_BITS_PER_PIXEL <= 16
#elif KD_BITS_PER_PIXEL == 16
typedef uint16_t KDColor;
#elif KD_BITS_PER_PIXEL <= 32
#elif KD_BITS_PER_PIXEL == 32
typedef uint32_t KDColor;
#elif KD_BITS_PER_PIXEL <= 64
#elif KD_BITS_PER_PIXEL == 64
typedef uint64_t KDColor;
*/
#else
#error KD_BITS_PER_PIXEL is too large!
#endif

View File

@@ -1,8 +1,9 @@
#ifndef KANDINSKY_LINE_H
#define KANDINSKY_LINE_H
#include <kandinsky/color.h>
#include <kandinsky/types.h>
void KDDrawLine(KDPoint p1, KDPoint p2);
void KDDrawLine(KDPoint p1, KDPoint p2);//, KDColor c);
#endif

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]);
}
}
}