Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Improve knightos/display.h #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion include/knightos/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,20 @@ void draw_short(SCREEN *screen, unsigned char x, unsigned char y, unsigned short
**/
void draw_byte(SCREEN *screen, unsigned char x, unsigned char y, unsigned char value);

/*.*
/**
* Draws a signed short at x, y
pixelherodev marked this conversation as resolved.
Show resolved Hide resolved
**/
void draw_signed(SCREEN *screen, unsigned char x, unsigned char y, signed short value);

/**
* Draws a float at x,y
**/

void draw_float(SCREEN* screen, unsigned char x, unsigned char y, float value);

/**
* Draws a long at x, y
* NYI

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO is better, it's more commonly used

**/
void draw_long(SCREEN *screen, unsigned char x, unsigned char y, unsigned long value);

Expand Down
40 changes: 40 additions & 0 deletions src/knightos/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,46 @@ void draw_short(SCREEN *screen, unsigned char x, unsigned char y, unsigned short
screen; x; y; value;
}

void draw_signed(SCREEN* screen, unsigned char x, unsigned char y, signed short value) {
if(value < 0){
draw_char(screen, x, y, '-');
x += 4;
value = -value;
}
draw_short(screen, x, y, value);
return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the explicit return?

}

void draw_float(SCREEN* screen, unsigned char x, unsigned char y, float value){
/* Implementation is weird and slow because of non-working snprintf. Indeed, %f format strings are not yet implemented */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aviallon what was wrong with snprintf?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%f doesn't work at all (it's ignored). I don't know if it is standard, but every libc I know supports it. And it really is useful.

Copy link
Member

@MaxLeiter MaxLeiter Jun 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember why we didn't implement %f, and there's probably a good reason, but you may want to experiment with https://github.com/KnightOS/libc/blob/master/src/format.c

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worst case, I'd like to see %f support using this function as a base.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this function has a major issue :
it does not support floats whose integer part is greater than INT_MAX...
In order to have full float support, I would have to go char by char... which may be even slower 😞

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know when this PR is done.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I will do. If you have any idea on a fast implementation for that, I'm all ears 😝

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First idea: don't worry about fast. It can be sped up later if we need to.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know when this is ready for another review :)

unsigned int integer_part;
unsigned int frac_part;
unsigned int integer_part_len;
bool sgn = (value < 0);


value = sgn ? -value : value;

integer_part = (unsigned int)value;
frac_part = ((value - (float)integer_part)*10000);

integer_part_len = (log10u(integer_part)) + 1;

if(sgn){
draw_char(screen, x, y, '-');
x += 4;
}

draw_short(screen, x, y, integer_part);
x += integer_part_len*4;
draw_char(screen, x, y, '.');

x += 4;
draw_short(screen, x, y, frac_part);

return;
}

void invert_pixel(SCREEN *screen, char x, char y) {
__asm
POP BC ; return
Expand Down