Skip to content

Commit 9683fcb

Browse files
committed
Hello, World!(文字列)の表示処理を追加。
1 parent dd70817 commit 9683fcb

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

src/font.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,29 @@
44

55
use font_def::FONT_DATAS;
66

7-
pub struct Font;
7+
pub struct Font {
8+
size_width: u16,
9+
size_height: u16,
10+
}
811

912
impl Font {
10-
pub fn get_charactor(ch: char) -> [u8; 16] {
13+
pub fn new() -> Self {
14+
Font {
15+
size_width: 8,
16+
size_height: 16,
17+
}
18+
}
19+
20+
pub fn get_character(&self, ch: char) -> [u8; 16] {
1121
let index = ch as usize;
1222
return FONT_DATAS[index];
1323
}
24+
25+
pub fn font_width(&self) -> u16 {
26+
return self.size_width;
27+
}
28+
29+
pub fn font_height(&self) -> u16 {
30+
return self.size_height;
31+
}
1432
}

src/font_def.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// 本フォントデータは「30日でできる! OS自作入門」に付属するCD-ROM内のフォントデータファイルを元に作成したものです。
44
//
55

6+
// 8 x 16のフォントデータ
67
#[allow(unused_imports)]
78
pub static FONT_DATAS: [[u8; 16]; 256] = [
89
[0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0],

src/graphics.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct Graphics {
1010
vram_address: u32,
1111
screen_x: u16,
1212
screen_y: u16,
13+
font: Font,
1314
}
1415

1516
impl Graphics {
@@ -18,6 +19,7 @@ impl Graphics {
1819
vram_address: 0x06000000,
1920
screen_x: 240,
2021
screen_y: 160,
22+
font: Font::new(),
2123
}
2224
}
2325

@@ -71,7 +73,7 @@ impl Graphics {
7173
}
7274

7375
pub fn draw_char(&self, ch:char, x:u16, y:u16, color:&RGB) {
74-
let char_data:[u8; 16] = Font::get_charactor(ch);
76+
let char_data:[u8; 16] = self.font.get_character(ch);
7577
for index in 0..15 {
7678
let byte_data:u8 = char_data[index];
7779
let offset_y = index as u16;
@@ -86,6 +88,14 @@ impl Graphics {
8688
}
8789
}
8890

91+
pub fn draw_string(&self, string:&str, x:u16, y:u16, color:&RGB) {
92+
let mut offset_x: u16 = 0;
93+
for character in string.chars() {
94+
self.draw_char(character, (x + offset_x), y, color);
95+
offset_x += self.font.font_width();
96+
}
97+
}
98+
8999
pub fn width(&self) -> u16 {
90100
return self.screen_x;
91101
}

src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ pub extern "C" fn entry() {
3636
graphics.draw_circle(graphics.width() - (radius + 1), (radius + 1), radius, &RGB::light_yellow());
3737

3838
// character
39-
let start_x:u16 = 0;
40-
let start_y:u16 = 0;
41-
graphics.draw_char('a', start_x, start_y, &RGB::white());
42-
graphics.draw_char('b', start_x + (8 * 1), start_y, &RGB::white());
43-
graphics.draw_char('c', start_x + (8 * 2), start_y, &RGB::white());
39+
graphics.draw_string("Hello, World!", 0, 0, &RGB::white());
4440

4541
loop {}
4642
}

0 commit comments

Comments
 (0)