@@ -2,7 +2,6 @@ use get_error;
2
2
use rwops:: RWops ;
3
3
use std:: error;
4
4
use std:: fmt;
5
- use std:: io;
6
5
use std:: os:: raw:: { c_int, c_long} ;
7
6
use std:: path:: Path ;
8
7
use sys:: ttf;
@@ -14,7 +13,14 @@ use super::font::{
14
13
15
14
/// A context manager for `SDL2_TTF` to manage C code initialization and clean-up.
16
15
#[ must_use]
17
- pub struct Sdl2TtfContext ;
16
+ pub struct Sdl2TtfContext ( ( ) ) ;
17
+
18
+ impl Clone for Sdl2TtfContext {
19
+ fn clone ( & self ) -> Self {
20
+ // This should not return an error because SDL_ttf is already initialized
21
+ init ( ) . unwrap ( )
22
+ }
23
+ }
18
24
19
25
// Clean up the context once it goes out of scope
20
26
impl Drop for Sdl2TtfContext {
@@ -54,7 +60,7 @@ impl Sdl2TtfContext {
54
60
point_size : u16 ,
55
61
) -> Result < Font < ' ttf , ' r > , String > {
56
62
let raw = unsafe { ttf:: TTF_OpenFontRW ( rwops. raw ( ) , 0 , point_size as c_int ) } ;
57
- if ( raw as * mut ( ) ) . is_null ( ) {
63
+ if raw. is_null ( ) {
58
64
Err ( get_error ( ) )
59
65
} else {
60
66
Ok ( internal_load_font_from_ll ( raw, Some ( rwops) ) )
@@ -72,7 +78,7 @@ impl Sdl2TtfContext {
72
78
let raw = unsafe {
73
79
ttf:: TTF_OpenFontIndexRW ( rwops. raw ( ) , 0 , point_size as c_int , index as c_long )
74
80
} ;
75
- if ( raw as * mut ( ) ) . is_null ( ) {
81
+ if raw. is_null ( ) {
76
82
Err ( get_error ( ) )
77
83
} else {
78
84
Ok ( internal_load_font_from_ll ( raw, Some ( rwops) ) )
@@ -82,22 +88,21 @@ impl Sdl2TtfContext {
82
88
83
89
/// Returns the version of the dynamically linked `SDL_TTF` library
84
90
pub fn get_linked_version ( ) -> Version {
85
- unsafe { Version :: from_ll ( * ttf:: TTF_Linked_Version ( ) ) }
91
+ Version :: from_ll ( unsafe { * ttf:: TTF_Linked_Version ( ) } )
86
92
}
87
93
88
94
/// An error for when `sdl2_ttf` is attempted initialized twice
89
95
/// Necessary for context management, unless we find a way to have a singleton
90
96
#[ derive( Debug ) ]
91
97
pub enum InitError {
92
- InitializationError ( io :: Error ) ,
98
+ InitializationError ( String ) ,
93
99
AlreadyInitializedError ,
94
100
}
95
101
96
102
impl error:: Error for InitError {
97
103
fn source ( & self ) -> Option < & ( dyn error:: Error + ' static ) > {
98
104
match * self {
99
- InitError :: AlreadyInitializedError => None ,
100
- InitError :: InitializationError ( ref error) => Some ( error) ,
105
+ InitError :: InitializationError ( _) | InitError :: AlreadyInitializedError => None ,
101
106
}
102
107
}
103
108
}
@@ -115,19 +120,20 @@ impl fmt::Display for InitError {
115
120
116
121
/// Initializes the truetype font API and returns a context manager which will
117
122
/// clean up the library once it goes out of scope.
123
+ #[ doc( alias = "TTF_Init" ) ]
118
124
pub fn init ( ) -> Result < Sdl2TtfContext , InitError > {
119
- unsafe {
120
- if ttf:: TTF_WasInit ( ) == 1 {
121
- Err ( InitError :: AlreadyInitializedError )
122
- } else if ttf:: TTF_Init ( ) == 0 {
123
- Ok ( Sdl2TtfContext )
124
- } else {
125
- Err ( InitError :: InitializationError ( io:: Error :: last_os_error ( ) ) )
126
- }
125
+ if unsafe { ttf:: TTF_Init ( ) } == 0 {
126
+ Ok ( Sdl2TtfContext ( ( ) ) )
127
+ } else {
128
+ Err ( InitError :: InitializationError ( get_error ( ) ) )
127
129
}
128
130
}
129
131
130
132
/// Returns whether library has been initialized already.
131
133
pub fn has_been_initialized ( ) -> bool {
132
- unsafe { ttf:: TTF_WasInit ( ) == 1 }
134
+ amount_of_times_initialized ( ) != 0
135
+ }
136
+
137
+ fn amount_of_times_initialized ( ) -> c_int {
138
+ unsafe { ttf:: TTF_WasInit ( ) }
133
139
}
0 commit comments