feat: ajout fonctions graphiques 2D + math + tick dans le timer
This commit is contained in:
parent
2ce9d70b48
commit
c76554dd4a
10
include/2d.h
10
include/2d.h
|
@ -1,3 +1,13 @@
|
|||
#include "types.h"
|
||||
|
||||
typedef struct vertex2d{
|
||||
u32 x;
|
||||
u32 y;
|
||||
} vertex2d __attribute__ ((packed));
|
||||
|
||||
|
||||
void line(u32 x1, u32 y1, u32 x2, u32 y2, u8 color);
|
||||
void linev(vertex2d *A, vertex2d *B, u8 color);
|
||||
void hline(u32 x1, u32 x2, u32 y, u8 color);
|
||||
void trianglefilled(vertex2d *A, vertex2d *B, vertex2d *C, u8 color);
|
||||
void triangle(vertex2d *A, vertex2d *B, vertex2d *C, u8 color);
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
#define sgn(x) ((x<0)?-1:((x>0)?1:0));
|
||||
|
||||
u32 abs(int x);
|
||||
random(u32 lower, u32 upper);
|
||||
u32 rand();
|
||||
void randomize();
|
||||
|
|
93
lib/2d.c
93
lib/2d.c
|
@ -8,6 +8,11 @@
|
|||
|
||||
/* Affiche une ligne entre les points spécifiés */
|
||||
|
||||
void linev(vertex2d *A, vertex2d *B, u8 color)
|
||||
{
|
||||
line(A->x,A->y,B->x,B->y,color);
|
||||
}
|
||||
|
||||
void line(u32 x1, u32 y1, u32 x2, u32 y2, u8 color)
|
||||
{
|
||||
s32 dx, dy, sdx, sdy;
|
||||
|
@ -49,3 +54,91 @@ void line(u32 x1, u32 y1, u32 x2, u32 y2, u8 color)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Affiche une ligne horizontale entre les points spécifiés */
|
||||
|
||||
void hline(u32 x1, u32 x2, u32 y, u8 color) {
|
||||
if (x2>x1)
|
||||
for(;x1<=x2;x1++)
|
||||
writepxl(x1, y, color);
|
||||
else
|
||||
for(;x2<=x1;x2++)
|
||||
writepxl(x2, y, color);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Affiche un triangle rempli entre les points spécifiés */
|
||||
|
||||
void trianglefilled(vertex2d *AA, vertex2d *BB, vertex2d *CC, u8 color) {
|
||||
vertex2d *A,*B,*C;
|
||||
u32 a, b, y, last;
|
||||
int dx1, dx2, dx3, dy1, dy2, dy3 , sa, sb;
|
||||
A=AA;
|
||||
B=BB;
|
||||
C=CC;
|
||||
if (AA->y > BB->y) {
|
||||
A=BB;
|
||||
B=AA;
|
||||
}
|
||||
if (BB->y > CC->y) {
|
||||
C=BB;
|
||||
B=CC;
|
||||
}
|
||||
if (AA->y > CC->y) {
|
||||
C=AA;
|
||||
A=CC;
|
||||
}
|
||||
if(A->y == C->y) { //meme ligne
|
||||
a = b = A->x;
|
||||
if(B->x < a) a = B->x;
|
||||
else if(B->x > b) b = B->x;
|
||||
if(C->x < a) a = C->x;
|
||||
else if(C->x > b) b = C->x;
|
||||
hline(a, b, A->y, color);
|
||||
return;
|
||||
}
|
||||
dx1 = B->x - A->x;
|
||||
dy1 = B->y - A->y;
|
||||
dx2 = C->x - A->x;
|
||||
dy2 = C->y - A->y;
|
||||
dx3 = C->x - B->x;
|
||||
dy3 = C->y - B->y;
|
||||
sa = 0;
|
||||
sb = 0;
|
||||
|
||||
if(B->y == C->y)
|
||||
last = B->y;
|
||||
else
|
||||
last = B->y-1;
|
||||
|
||||
for(y=A->y; y<=last; y++) {
|
||||
a = A->x + sa / dy1;
|
||||
b = A->x + sb / dy2;
|
||||
sa += dx1;
|
||||
sb += dx2;
|
||||
hline(a, b, y, color);
|
||||
}
|
||||
|
||||
sa = dx3 * (y - B->y);
|
||||
sb = dx2 * (y - A->y);
|
||||
for(; y<=C->y; y++) {
|
||||
a = B->x + sa / dy3;
|
||||
b = A->x + sb / dy2;
|
||||
sa += dx3;
|
||||
sb += dx2;
|
||||
hline(a, b, y, color);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Affiche un triangle entre les points spécifiés */
|
||||
|
||||
void triangle(vertex2d *AA, vertex2d *BB, vertex2d *CC, u8 color) {
|
||||
line(AA->x,AA->y,BB->x,BB->y,color);
|
||||
line(BB->x,BB->y,CC->x,CC->y,color);
|
||||
line(CC->x,CC->y,AA->x,AA->y,color);
|
||||
}
|
||||
|
|
46
lib/math.c
46
lib/math.c
|
@ -1,4 +1,5 @@
|
|||
#include "types.h"
|
||||
#include "timer.h"
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
@ -10,3 +11,48 @@ u32 abs(int x) {
|
|||
return (u32) x;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Fonction qui initialise le générateur de nombre aléatoire */
|
||||
|
||||
static u32 seed=0x12341234;
|
||||
|
||||
void randomize() {
|
||||
seed=gettimer();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Fonction qui renvoie un nombre aléatoire */
|
||||
|
||||
u32 rand()
|
||||
{
|
||||
u32 next = seed;
|
||||
int result;
|
||||
|
||||
next *= 1103515245;
|
||||
next += 12345;
|
||||
result = (unsigned int) (next / 65536) % 2048;
|
||||
|
||||
next *= 1103515245;
|
||||
next += 12345;
|
||||
result <<= 10;
|
||||
result ^= (unsigned int) (next / 65536) % 1024;
|
||||
|
||||
next *= 1103515245;
|
||||
next += 12345;
|
||||
result <<= 10;
|
||||
result ^= (unsigned int) (next / 65536) % 1024;
|
||||
|
||||
seed = next;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Fonction qui renvoie un nombre aléatoire borné */
|
||||
|
||||
u32 random(u32 lower, u32 upper) {
|
||||
return (rand() % (upper - lower + 1)) + lower;
|
||||
}
|
||||
|
|
11
lib/timer.c
11
lib/timer.c
|
@ -9,6 +9,16 @@ static u8 curs[4] = { "-\\|/" };
|
|||
|
||||
static u8 curspos = 0;
|
||||
|
||||
static u32 time = 0;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Récupère la valeur du timer */
|
||||
|
||||
u32 gettimer() {
|
||||
return time;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Handler d'interruption de la souris IRQ 0 */
|
||||
|
@ -20,6 +30,7 @@ void timer()
|
|||
pushad();
|
||||
showchar(0, 0, curs[curspos], 7);
|
||||
curspos = (curspos + 1) & 0x3;
|
||||
time++;
|
||||
irqendmaster();
|
||||
popad();
|
||||
popf();
|
||||
|
|
|
@ -7,11 +7,9 @@
|
|||
#include "asm.h"
|
||||
#include "cpu.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "2d.h"
|
||||
#include "ansi.c"
|
||||
|
||||
static cpuinfo cpu;
|
||||
static u8 noproc[] = "\033[31mInconnu\033[0m\000";
|
||||
static u8 warnmsg[] =
|
||||
"\033[99C\033[8D\033[37m\033[1m[ \033[36mNON\033[37m ]\033[0m\000";
|
||||
static u8 okmsg[] =
|
||||
|
@ -71,20 +69,13 @@ int main(void)
|
|||
warning();
|
||||
else
|
||||
ok();
|
||||
|
||||
strcpy(&noproc, &cpu.detectedname);
|
||||
getcpuinfos(&cpu);
|
||||
|
||||
|
||||
printf
|
||||
(" -Detection du processeur\r\033[1m Revision \t:%d\r Modele \t:%d\r Famille \t:%d\r Nom cpuid\t:%s\rJeux d'instruction\t:%s\033[0m\000",
|
||||
cpu.stepping, cpu.models, cpu.family, &cpu.detectedname,
|
||||
|
||||
&cpu.techs);
|
||||
ok();
|
||||
static u8 field[]=" \000";
|
||||
static u8 field[]=" \000";
|
||||
static u8 item[]=" \000";
|
||||
static u8 cmd_reboot[]="REBOOT\000";
|
||||
static u8 cmd_mode[]="MODE\000";
|
||||
static u8 cmd_clear[]="CLEAR\000";
|
||||
static u8 cmd_detectcpu[]="DETECTCPU\000";
|
||||
static u8 cmd_test2d[]="TEST2D\000";
|
||||
while (true) {
|
||||
print("\r\n# ");
|
||||
getstring(&field);
|
||||
|
@ -92,6 +83,36 @@ int main(void)
|
|||
strgetitem(&field, &item, ' ', 0);
|
||||
strtoupper(&item);
|
||||
if (strcmp(&item,&cmd_reboot)==0) reboot();
|
||||
if (strcmp(&item,&cmd_mode)==0) setvmode(0x84);
|
||||
if (strcmp(&item,&cmd_clear)==0) fill(0x00);
|
||||
if (strcmp(&item,&cmd_detectcpu)==0) detectcpu();
|
||||
if (strcmp(&item,&cmd_test2d)==0) test2d();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void test2d() {
|
||||
setvmode(0x84);
|
||||
fill(0x00);
|
||||
struct vertex2d a,b,c;
|
||||
randomize();
|
||||
for(int i=0;i<100;i++)
|
||||
{
|
||||
a.x=random(0, 800);
|
||||
a.y=random(0, 600);
|
||||
b.x=random(0, 800);
|
||||
b.y=random(0, 600);
|
||||
c.x=random(0, 800);
|
||||
c.y=random(0, 600);
|
||||
trianglefilled(&a,&b,&c,random(0, 16));
|
||||
triangle(&a,&b,&c,2);
|
||||
}
|
||||
}
|
||||
|
||||
void detectcpu()
|
||||
{
|
||||
cpuinfo cpu;
|
||||
u8 noproc[] = "\033[31mInconnu\033[0m\000";
|
||||
strcpy(&noproc, &cpu.detectedname);
|
||||
getcpuinfos(&cpu);
|
||||
printf("\r\nDetection du processeur\r\033[1m Revision \t:%d\r Modele \t:%d\r Famille \t:%d\r Nom cpuid\t:%s\rJeux d'instruction\t:%s\033[0m\r\n\000",cpu.stepping, cpu.models, cpu.family, &cpu.detectedname,&cpu.techs);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue