Skip to content

Commit 12fc115

Browse files
authored
Merge pull request kotetuco#4 from kotetuco/feature/fill_graphic
図形描画処理を実装。
2 parents 77714f7 + 7ba5338 commit 12fc115

File tree

4 files changed

+203
-19
lines changed

4 files changed

+203
-19
lines changed

src/gba_color.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// kotetuco, 2017
3+
//
4+
5+
use rgb::RGB;
6+
7+
pub trait GBAColor {
8+
fn convert_u16_color(&self) -> u16;
9+
}
10+
11+
impl GBAColor for RGB {
12+
fn convert_u16_color(&self) -> u16{
13+
return (((self.b >> 3) as u16) << 10) + (((self.g >> 3) as u16) << 5) + (self.r >> 3) as u16;
14+
}
15+
}

src/graphics.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// kotetuco, 2017
3+
//
4+
5+
use rgb::RGB;
6+
use gba_color::GBAColor;
7+
8+
pub struct Graphics {
9+
vram_address: u32,
10+
screen_x: u16,
11+
screen_y: u16,
12+
}
13+
14+
impl Graphics {
15+
pub fn new() -> Self {
16+
Graphics {
17+
vram_address: 0x06000000,
18+
screen_x: 240,
19+
screen_y: 160,
20+
}
21+
}
22+
23+
pub fn draw_dot(&self, x:u16, y:u16, color:&RGB) {
24+
let offset: u32 = ((y * self.screen_x) + x) as u32;
25+
let vram: *mut u16 = (self.vram_address + (offset * 2)) as *mut u16;
26+
unsafe {
27+
*vram = color.convert_u16_color();
28+
}
29+
}
30+
31+
pub fn draw_box(&self, x:u16, y:u16, width:u16, height:u16, color:&RGB) {
32+
for offset_y in 0..height {
33+
for offset_x in 0..width {
34+
let valid_x = if x + offset_x > self.screen_x { self.screen_x } else { x + offset_x };
35+
let valid_y = if y + offset_y > self.screen_y { self.screen_y } else { y + offset_y };
36+
self.draw_dot(valid_x, valid_y, color);
37+
}
38+
}
39+
}
40+
41+
#[no_mangle]
42+
pub fn draw_circle(&self, center_x:u16, center_y:u16, r:u16, color:&RGB) {
43+
let mut x: u16 = r;
44+
let mut y: u16 = 0;
45+
let mut f: i32 = 3 - ((r as i32) * 2);
46+
47+
// draw center
48+
self.draw_dot(center_x, center_y, color);
49+
50+
loop {
51+
if x < y {
52+
break;
53+
}
54+
55+
self.draw_dot(center_x + x, center_y + y, color);
56+
self.draw_dot(center_x - x, center_y + y, color);
57+
self.draw_dot(center_x + x, center_y - y, color);
58+
self.draw_dot(center_x - x, center_y - y, color);
59+
self.draw_dot(center_x + y, center_y + x, color);
60+
self.draw_dot(center_x - y, center_y + x, color);
61+
self.draw_dot(center_x + y, center_y - x, color);
62+
self.draw_dot(center_x - y, center_y - x, color);
63+
64+
if f >= 0 {
65+
x -= 1;
66+
f -= (x * 4) as i32;
67+
}
68+
y += 1;
69+
f += (4 * y + 2) as i32;
70+
}
71+
}
72+
73+
pub fn width(&self) -> u16 {
74+
return self.screen_x;
75+
}
76+
77+
pub fn height(&self) -> u16 {
78+
return self.screen_y;
79+
}
80+
}

src/lib.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// kotetuco, 2017
3-
//
3+
//
44

55
#![feature(lang_items)]
66
#![no_std]
@@ -10,22 +10,23 @@
1010
extern crate compiler_builtins;
1111
extern crate rlibc;
1212

13+
mod rgb;
14+
mod gba_color;
15+
mod graphics;
16+
17+
use rgb::RGB;
18+
use rgb::RGBDef;
19+
use graphics::Graphics;
20+
1321
#[no_mangle]
1422
pub extern "C" fn entry() {
1523
init_graphic();
1624

17-
let color:u16 = convert_u16_color(0, 255, 0);
18-
let vram_address:u32 = 0x06000000;
19-
20-
for y in 0..160 {
21-
for x in 0..240 {
22-
let offset: u32 = ((y * 240) + x) as u32;
23-
let vram: *mut u16 = (vram_address + (offset * 2)) as *mut u16;
24-
unsafe {
25-
*vram = color;
26-
}
27-
}
28-
}
25+
let graphics: Graphics = Graphics::new();
26+
graphics.draw_box(20, 20, 100, 100, &RGB::light_red());
27+
graphics.draw_box(70, 50, 100, 100, &RGB::light_green());
28+
graphics.draw_box(120, 80, 100, 100, &RGB::light_blue());
29+
graphics.draw_circle(25, 25, 20, &RGB::light_yellow());
2930

3031
loop {}
3132
}
@@ -40,10 +41,6 @@ fn init_graphic() {
4041
}
4142
}
4243

43-
fn convert_u16_color(r:u8, g:u8, b:u8) -> u16{
44-
return (((b >> 3) as u16) << 10) + (((g >> 3) as u16) << 5) + (r >> 3) as u16;
45-
}
46-
4744
#[allow(private_no_mangle_fns)]
4845
#[no_mangle]
4946
#[lang = "panic_fmt"]
@@ -53,5 +50,3 @@ extern "C" fn panic_fmt() -> ! {
5350

5451
#[lang = "eh_personality"]
5552
pub extern fn eh_personality() {}
56-
57-

src/rgb.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// kotetuco, 2017
3+
//
4+
5+
pub struct RGB {
6+
pub r: u8,
7+
pub g: u8,
8+
pub b: u8,
9+
}
10+
11+
pub trait RGBDef {
12+
fn black() -> Self;
13+
fn light_red() -> Self;
14+
fn light_green() -> Self;
15+
fn light_yellow() -> Self;
16+
fn light_blue() -> Self;
17+
fn light_purple() -> Self;
18+
fn light_pale_blue() -> Self;
19+
fn white() -> Self;
20+
fn light_gray() -> Self;
21+
fn dark_red() -> Self;
22+
fn dark_green() -> Self;
23+
fn dark_yellow() -> Self;
24+
fn dark_blue() -> Self;
25+
fn dark_purple() -> Self;
26+
fn dark_pale_blue() -> Self;
27+
fn dark_gray() -> Self;
28+
}
29+
30+
impl RGBDef for RGB {
31+
fn black() -> RGB {
32+
return RGB { r: 0x00, g: 0x00, b: 0x00,};
33+
}
34+
35+
fn light_red() -> RGB {
36+
return RGB { r: 0xff, g: 0x00, b: 0x00,};
37+
}
38+
39+
fn light_green() -> RGB {
40+
return RGB { r: 0x00, g: 0xff, b: 0x00,};
41+
}
42+
43+
fn light_yellow() -> RGB {
44+
return RGB { r: 0xff, g: 0xff, b: 0x00,};
45+
}
46+
47+
fn light_blue() -> RGB {
48+
return RGB { r: 0x00, g: 0x00, b: 0xff,};
49+
}
50+
51+
fn light_purple() -> RGB {
52+
return RGB { r: 0xff, g: 0x00, b: 0xff,};
53+
}
54+
55+
fn light_pale_blue() -> RGB {
56+
return RGB { r: 0x00, g: 0xff, b: 0xff,};
57+
}
58+
59+
fn white() -> RGB {
60+
return RGB { r: 0xff, g: 0xff, b: 0xff,};
61+
}
62+
63+
fn light_gray() -> RGB {
64+
return RGB { r: 0xc6, g: 0xc6, b: 0xc6,};
65+
}
66+
67+
fn dark_red() -> RGB {
68+
return RGB { r: 0x84, g: 0x00, b: 0x00,};
69+
}
70+
71+
fn dark_green() -> RGB {
72+
return RGB { r: 0x00, g: 0x84, b: 0x00,};
73+
}
74+
75+
fn dark_yellow() -> RGB {
76+
return RGB { r: 0x84, g: 0x84, b: 0x00,};
77+
}
78+
79+
fn dark_blue() -> RGB {
80+
return RGB { r: 0x00, g: 0x00, b: 0x84,};
81+
}
82+
83+
fn dark_purple() -> RGB {
84+
return RGB { r: 0x84, g: 0x00, b: 0x84,};
85+
}
86+
87+
fn dark_pale_blue() -> RGB {
88+
return RGB { r: 0x00, g: 0x84, b: 0x84,};
89+
}
90+
91+
fn dark_gray() -> RGB {
92+
return RGB { r: 0x84, g: 0x84, b: 0x84,};
93+
}
94+
}

0 commit comments

Comments
 (0)