Browse Source

Colours are correct on any video card I think

Stephen Downward 1 year ago
parent
commit
386f9240f4
9 changed files with 114 additions and 56 deletions
  1. 31 14
      drivers/screen.c
  2. 4 2
      drivers/screen.h
  3. 16 11
      kernel/kernel.c
  4. 8 7
      programs/texteditor.c
  5. 15 10
      util/font.c
  6. 5 4
      util/font.h
  7. 19 8
      util/terminal.c
  8. 4 0
      util/types.c
  9. 12 0
      util/types.h

+ 31 - 14
drivers/screen.c

@ -21,24 +21,27 @@ int get_offset_col(int offset);
21 21
 * Public Kernel API functions                            *
22 22
 **********************************************************/
23 23
24
void putpixel(int x, int y, int r, int b, int g) {
24
void putpixel(int x, int y, u32 fg) {
25 25
    unsigned where = x*COLOR_BITS/8 + y*SCN_PITCH; //TODO: Should come from above function
26 26
    u8 *screen = (u8*) VIDEO_ADDRESS;
27
    screen[where] = b;      // BLUE
28
    screen[where + 1] = g;  // GREEN
29
    screen[where + 2] = r;  // RED
27
28
	//TODO: Is there a way to set these all at once?
29
	for (int j = 0; j < COLOR_BITS/8; j++) {
30
		screen[where + j] = (fg >> j*8) & 255;
31
	}
30 32
}
31 33
32
void fillrect(int x, int y, unsigned char r, unsigned char g, unsigned char b, int w, int h) {
34
void fillrect(int x, int y, u32 fg, int w, int h) {
33 35
    unsigned where = x*COLOR_BITS/8 + y*SCN_PITCH;
34 36
    u8 *screen = (u8*) VIDEO_ADDRESS;
35 37
    int i, j;
36 38
37 39
    for (i = 0; i < h; i++) {
38 40
        for (j = 0; j < w; j++) {
39
            screen[where + j*COLOR_BITS/8] = b;
40
            screen[where + j*COLOR_BITS/8 + 1] = g;
41
			screen[where + j*COLOR_BITS/8 + 2] = r;
41
            //TODO: Is there a way to set these all at once?
42
			for (int k = 0; k < COLOR_BITS/8; k++) {
43
				screen[where + (int)(j*COLOR_BITS/8) + k] = (fg >> k*8) & 255;
44
			}
42 45
        }
43 46
        where += SCN_PITCH;
44 47
    }
@ -48,6 +51,20 @@ void scroll_up(u8 lines) { //NOTE: Does not clear the bottom lines!
48 51
    memory_copy((u8 *)(VIDEO_ADDRESS + lines * SCN_PITCH), (u8 *)VIDEO_ADDRESS, (SCN_HEIGHT - lines) * SCN_PITCH);
49 52
}
50 53
54
u32 color_to_pixel(Color_t color)
55
{
56
	//32bpp max
57
58
	//Scale the values properly
59
	u8 r = color.r * ((1 << ADV_VBE_INFO->red_mask) - 1) / 255;
60
	u8 g = color.g * ((1 << ADV_VBE_INFO->green_mask) - 1) / 255;
61
	u8 b = color.b * ((1 << ADV_VBE_INFO->blue_mask) - 1) / 255;
62
	
63
	return (b << ADV_VBE_INFO->blue_position) +
64
		(g << ADV_VBE_INFO->green_position) +
65
		(r << ADV_VBE_INFO->red_position);
66
}
67
51 68
/**********************************************************
52 69
 * Private kernel functions                               *
53 70
 **********************************************************/
@ -84,11 +101,11 @@ void set_cursor_offset(int offset) {
84 101
}
85 102
86 103
void setup_video() {
87
    struct vbe_mode_info_structure *vbe_info = (struct vbe_mode_info_structure *)0x7e00;
104
	struct vbe_mode_info_structure *vbe_info = (struct vbe_mode_info_structure *)0x7e00;
88 105
	ADV_VBE_INFO = vbe_info;
89
    VIDEO_ADDRESS = vbe_info->framebuffer;
90
    COLOR_BITS = vbe_info->bpp;
91
    SCN_WIDTH = vbe_info->width;
92
    SCN_HEIGHT = vbe_info->height;
93
    SCN_PITCH = vbe_info->pitch;
106
	VIDEO_ADDRESS = vbe_info->framebuffer;
107
	COLOR_BITS = vbe_info->bpp;
108
	SCN_WIDTH = vbe_info->width;
109
	SCN_HEIGHT = vbe_info->height;
110
	SCN_PITCH = vbe_info->pitch;
94 111
}

+ 4 - 2
drivers/screen.h

@ -2,6 +2,7 @@
2 2
#define SCREEN_H
3 3
4 4
#include "../cpu/types.h"
5
#include "../util/types.h"
5 6
6 7
/* Screen i/o ports */
7 8
#define REG_SCREEN_CTRL 0x3d4
@ -52,10 +53,11 @@ struct vbe_mode_info_structure {
52 53
struct vbe_mode_info_structure *ADV_VBE_INFO;
53 54
54 55
/* Public kernel API */
55
void putpixel(int x, int y, int r, int g, int b); // 0-255 for each
56
void fillrect(int x, int y, unsigned char r, unsigned char g, unsigned char b, int w, int h);
56
void putpixel(int x, int y, u32 fg);
57
void fillrect(int x, int y, u32 fg, int w, int h);
57 58
void setup_video();
58 59
void scroll_up(u8 lines);
60
u32 color_to_pixel(Color_t color);
59 61
60 62
u8 COLOR_BITS;
61 63
u16 SCN_WIDTH;

+ 16 - 11
kernel/kernel.c

@ -62,16 +62,21 @@ void generate_fractal(u16 x, u16 y, u16 w, u16 h) {
62 62
                Zrt = Zr * Zr + Cr - (Zi * Zi);
63 63
				Zi = 2 * Zi * Zr + Ci;
64 64
				Zr = Zrt;
65
				//if(Q_rsqrt(Zr * Zr + Zi * Zi) > 2) { //Divergent
66
				if(Zr * Zr + Zi * Zi > 4 || Zr * Zr + Zi * Zi < -4) {
67
                    putpixel(x + i, y + j, (numIterations * 10), (numIterations * 30), (numIterations * 50));
68
                    painted = 1;
69
                    break;
65
				
66
				if(Zr * Zr + Zi * Zi > 4 || Zr * Zr + Zi * Zi < -4) //Divergent
67
				{
68
					Color_t p;
69
					p.r = numIterations * 10;
70
					p.g = numIterations * 30;
71
					p.b = numIterations * 50;
72
					putpixel(x + i, y + j, color_to_pixel(p));
73
					painted = 1;
74
					break;
70 75
				}
71
            }
72
            if (painted == 0) {
73
                putpixel(x + i, y + j, 0, 0, 0);
74
            }
75
        }
76
    }
76
			}
77
			if (painted == 0) {
78
				putpixel(x + i, y + j, 0);
79
			}
80
		}
81
	}
77 82
}

+ 8 - 7
programs/texteditor.c

@ -1,6 +1,7 @@
1 1
#include "texteditor.h"
2 2
#include "../util/terminal.h"
3 3
#include "../libc/string.h"
4
#include "../drivers/screen.h"
4 5
5 6
void gen_padding(char *menu) {
6 7
    print("=");
@ -13,12 +14,12 @@ void gen_padding(char *menu) {
13 14
    println("=");
14 15
}
15 16
16
void gen_menu(char *menu) {
17
    gen_padding(menu);
18
    print("|");
19
    print(menu);
20
    println("|");
21
    gen_padding(menu);
17
void gen_menu() {
18
    //Top bar
19
	Color_t red;
20
	red.r = 255;
21
	fillrect(0, 0, color_to_pixel(red), SCN_WIDTH, 9);
22
	print("File    Edit    Exit");
22 23
}
23 24
24 25
void editor() {
@ -26,5 +27,5 @@ void editor() {
26 27
    clear_screen();
27 28
28 29
    //Menu setup
29
    gen_menu("File|Edit");
30
    gen_menu();
30 31
}

+ 15 - 10
util/font.c

@ -1,6 +1,7 @@
1 1
#include "font.h"
2 2
#include "../drivers/screen.h"
3 3
#include "../libc/string.h"
4
#include "../util/types.h"
4 5
5 6
u16 characters[26 * 8 + 31 * 8 + 16*8] = {
6 7
    24,24,0,24,60,60,126,126,
@ -78,13 +79,13 @@ u16 characters[26 * 8 + 31 * 8 + 16*8] = {
78 79
    6,6,6,6,6,0,6,6
79 80
};
80 81
81
void draw_char(int x, int y, char c) {
82
void draw_char(int x, int y, char c, u32 fg) {
82 83
	unsigned where = x*COLOR_BITS/8 + y*SCN_PITCH;
83 84
	u8 *screen = (u8 *) VIDEO_ADDRESS;
84 85
    if (c == 124) { //Custom code for |
85 86
        for(int r = 0; r < 8; r++) {//Rows
86
            putpixel(x + 3, y - r, 255, 255, 255); //TODO: CHANGE THIS
87
            putpixel(x + 4, y - r, 255, 255, 255); //TODO: CHANGE THIS
87
            putpixel(x + 3, y - r, fg); //TODO: CHANGE THIS
88
            putpixel(x + 4, y - r, fg); //TODO: CHANGE THIS
88 89
        }
89 90
    }
90 91
    else {
@ -97,10 +98,10 @@ void draw_char(int x, int y, char c) {
97 98
            uint16 row = characters[c * 8 + r];
98 99
            for (int i = 0; i < 8; i++) {
99 100
                if (row&(1 << i)) {
100
					
101
					screen[where + (int)(i*COLOR_BITS/8)] = 255;
102
					screen[where + (int)(i*COLOR_BITS/8) + 1] = 255;
103
					screen[where + (int)(i*COLOR_BITS/8) + 2] = 255;
101
					//TODO: Is there a way to set these all at once?
102
					for (int j = 0; j < COLOR_BITS/8; j++) {
103
						screen[where + (int)(i*COLOR_BITS/8) + j] = (fg >> j*8) & 255;
104
					}
104 105
				}
105 106
            }
106 107
			where -= SCN_PITCH;
@ -108,10 +109,10 @@ void draw_char(int x, int y, char c) {
108 109
    }
109 110
}
110 111
111
void print_at(int x, int y, char *c) {
112
void print_at(int x, int y, char *c, u32 fg) {
112 113
    int i = 0;
113 114
    while (c[i] != 0) {
114
        draw_char(x + i * 8, y, c[i]);
115
        draw_char(x + i * 8, y, c[i], fg);
115 116
        i++;
116 117
    }
117 118
}
@ -119,5 +120,9 @@ void print_at(int x, int y, char *c) {
119 120
//X is where the middle character goes
120 121
void print_centered(int scn_width, int y, char *c) {
121 122
    int x_left = (scn_width - (strlen(c) * 8)) / 2;
122
    print_at(x_left, y, c);
123
	Color_t fg;
124
	fg.r = 255;
125
	fg.g = 0;
126
	fg.b = 0;
127
    print_at(x_left, y, c, color_to_pixel(fg));
123 128
}

+ 5 - 4
util/font.h

@ -1,7 +1,8 @@
1 1
#ifndef font
2
    #define font
3
    void draw_char(int x, int y, char c);
4
    void print_at(int x, int y, char *c);
5
    void print_centered(int scn_width, int y, char *c);
2
#define font
3
#include "types.h"
4
void draw_char(int x, int y, char c, u32 fg);
5
void print_at(int x, int y, char *c, u32 fg);
6
void print_centered(int scn_width, int y, char *c);
6 7
7 8
#endif

+ 19 - 8
util/terminal.c

@ -6,14 +6,16 @@
6 6
7 7
int offset_x = 0;
8 8
int offset_y = 8; //Higher numbers really means lower on screen
9
int bg[3] = {0, 0, 0};
9
10
u32 bg;
11
u32 fg;
10 12
11 13
void newline() {
12 14
    offset_x = 0;
13 15
    offset_y += char_height;
14 16
    if (offset_y >= SCN_HEIGHT) {
15 17
        scroll_up(char_height);
16
        fillrect(0, SCN_HEIGHT - char_height, bg[0], bg[1], bg[2], SCN_WIDTH, char_height); //Clear last line
18
        fillrect(0, SCN_HEIGHT - char_height, bg, SCN_WIDTH, char_height); //Clear last line
17 19
        offset_y -= char_height;
18 20
    }
19 21
}
@ -30,7 +32,7 @@ void print(char *c) {
30 32
            if (offset_x + 8 > SCN_WIDTH) {
31 33
                newline();
32 34
            }
33
            draw_char(offset_x, offset_y, c[i]);
35
            draw_char(offset_x, offset_y, c[i], fg);
34 36
            offset_x += 8;
35 37
        }
36 38
        i++;
@ -48,19 +50,28 @@ void delete_last() {
48 50
        offset_x = SCN_WIDTH - 8;
49 51
        offset_y -= char_height;
50 52
    }
51
    fillrect(offset_x, offset_y - 11, bg[0], bg[1], bg[2], 8, char_height);
53
    fillrect(offset_x, offset_y - 11, bg, 8, char_height);
52 54
}
53 55
54 56
void clear_screen() {
55
    fillrect(0, 0, bg[0], bg[1], bg[2], SCN_WIDTH, SCN_HEIGHT); //Background
57
    fillrect(0, 0, bg, SCN_WIDTH, SCN_HEIGHT); //Background
56 58
    offset_x = 0;
57 59
    offset_y = 8;
58 60
}
59 61
60 62
void set_background(int r, int g, int b) {
61
    bg[0] = r;
62
    bg[1] = g;
63
    bg[2] = b;
63
	Color_t bg_c;
64
	bg_c.r = r;
65
	bg_c.g = g;
66
	bg_c.b = b;
67
	bg = color_to_pixel(bg_c);
68
69
	//Tmp
70
	Color_t fg_c;
71
	fg_c.r = 0;
72
	fg_c.g = 0xcc;
73
	fg_c.b = 0;
74
	fg = color_to_pixel(fg_c);
64 75
}
65 76
66 77
void set_offset(int x, int y) {

+ 4 - 0
util/types.c

@ -0,0 +1,4 @@
1
#include "types.h"
2
#include "../cpu/types.h"
3
4

+ 12 - 0
util/types.h

@ -0,0 +1,12 @@
1
#ifndef HLTYPES
2
#define HLTYPES
3
#include "../cpu/types.h"
4
5
//Holds high level types. Will possibly move this into the low level types file.
6
7
typedef struct Color
8
{
9
	u8 r, g, b;
10
} Color_t;
11
12
#endif