-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContext.mbt
More file actions
120 lines (111 loc) · 2.9 KB
/
Context.mbt
File metadata and controls
120 lines (111 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
///|
pub struct Context {
windows : Array[Window]
}
///|
pub suberror InitializeSDLError {
InitializeSDLError(String)
} derive(Show)
///|
/// Create a new context.
/// This function initializes the SDL library.
/// It returns a new `Context` instance.
pub fn Context::new() -> Context raise {
let res = @raw.sdl_Init(@raw.SDL_INIT_VIDEO)
if !res {
raise InitializeSDLError("Failed to initialize SDL")
}
Context::{ windows: [] }
}
///|
pub suberror CreateWindowError {
CreateWindowError(String)
} derive(Show)
///|
/// Create a new window.
/// ## Parameters
/// - `title`: The title of the window.
/// - `width`: The width of the window.
/// - `height`: The height of the window.
///
/// It returns a new `Window` instance.
pub fn Context::createWindow(
self : Self,
title : String,
width~ : Int,
height~ : Int,
) -> Window raise {
let window = @raw.sdl_CreateWindow(title, width, height, 0UL)
if window.is_null() {
raise CreateWindowError("Failed to create SDL window")
}
let window = Window::{ base: window, renderer: None }
self.windows.push(window)
window
}
///|
fn Context::has_event(self : Self) -> Bool {
ignore(self)
let event = @raw.nullptr()
@raw.sdl_PollEvent(event)
}
///|
/// Get all events from the event queue.
///
/// It returns an array of `Event` instances.
pub fn Context::getEvents(self : Self) -> Array[Event] {
ignore(self)
let events : Array[Event] = []
while self.has_event() {
let event = @raw.new_sdl_event()
let _ = @raw.sdl_PollEvent(event)
events.push(event)
}
events
}
///|
/// Get a timer instance.
///
/// It returns a `Timer` instance.
pub fn Context::getTimer(self : Self) -> Timer {
ignore(self)
Timer::{ }
}
///|
/// Quit the SDL library.
/// This function should be called before the program exits.
pub fn Context::quit(self : Self) -> Unit {
self.windows.each(w => @raw.sdl_DestroyWindow(w.inner()))
@raw.sdl_Quit()
}
//pub fn Context::createPoint(x: Double, y: Double) -> Point {
// Point::{ x, y }
//}
//
//pub fn Context::createLine(start: Point, end: Point) -> Line {
// Line::{ start, end }
//}
//
//pub fn Context::createRect(self: Self, anchor~: (Double, Double), width~: Double, height~: Double) -> Rect {
// ignore(self)
// let anchor = Point::{ x: anchor.0, y: anchor.1 }
// Rect::{ anchor, width, height }
//}
//
//pub fn Context::createVertex(self: Self, pos: (Double, Double), color~: Color = Black, tex_coord~: (Double, Double) = (0, 0)) -> Vertex {
// ignore(self)
// Vertex::{
// pos,
// color,
// tex_coord,
// }
//}
//
//pub fn Context::createTriangle(self: Self, p1: (Double, Double), p2: (Double, Double), p3: (Double, Double), color~: Color = Black, tex_coord~: (Double, Double)=(0, 0)) -> Triangle {
// ignore(self)
// Triangle::{
// v1: self.createVertex(p1, color~, tex_coord~),
// v2: self.createVertex(p2, color~, tex_coord~),
// v3: self.createVertex(p3, color~, tex_coord~)
// }
//}