11use raylib:: drawing:: RaylibDrawHandle ;
2+ use raylib:: ffi:: { DrawTexture , free, LoadImageFromMemory , LoadTextureFromImage , Texture , UnloadImage , UnloadTexture } ;
23use raylib:: prelude:: * ;
3- use crate :: time:: DELTA_TIME ;
4+ use crate :: const_c;
5+ use crate :: game:: Game ;
6+ use crate :: time:: { TIME } ;
47
5- const CREDITS_SPEED : f32 = 100.0 ;
6- const CREDITS_DURATION : f32 = 10.0 ;
8+ const CREDITS_DURATION : f32 = 6.0 ;
79
810pub ( crate ) struct Credits {
911 pub title : String ,
10- pub authors : Vec < String > ,
12+ pub author : String ,
13+ pub acknowledgements : Vec < String > ,
1114 pub version : String ,
1215 pub description : String ,
1316 pub website : String ,
1417 pub license : String ,
15- offset : f32 ,
18+ logo_texture : Texture ,
1619}
1720
1821impl Credits {
1922 pub fn new ( ) -> Credits {
2023 Credits {
2124 title : String :: from ( "Pong50th" ) ,
22- authors : vec ! [ String :: from( "PerfectlyFineCode" ) ] ,
25+ author : String :: from ( "PerfectlyFineCode" ) ,
26+ acknowledgements : vec ! [
27+ String :: from( "[raylib-rs] Paul Clement (github.com/deltaphc)" )
28+ ] ,
2329 version : String :: from ( "0.1.0" ) ,
2430 description : String :: from ( "A pong game celebrating 50th anniversary, made with raylib-rs" ) ,
2531 website : String :: from ( "github.com/perfectlyfinecode/pong50th" ) ,
2632 license : String :: from ( "MIT" ) ,
27- offset : 0.0 ,
33+ logo_texture : unsafe {
34+ let image_data = include_bytes ! ( "..\\ res\\ images\\ logo.png" ) ;
35+ let img = LoadImageFromMemory ( const_c ! ( ".png" ) ,
36+ image_data. as_ptr ( ) as * const _ ,
37+ image_data. len ( ) as i32 ) ;
38+
39+ let tex = LoadTextureFromImage ( img) ;
40+ UnloadImage ( img) ;
41+ tex
42+ } ,
2843 }
2944 }
3045
31- pub fn update ( & mut self ) {
32- let delta_time = unsafe { DELTA_TIME } ;
33- // scroll credits for duration of CREDITS_DURATION
34- self . offset += CREDITS_SPEED * delta_time;
46+ pub fn update ( & mut self , game : & mut Game ) {
47+ let time = unsafe { TIME } ;
48+ if time > CREDITS_DURATION as f64 {
49+ game. game_state = crate :: game:: GameState :: Playing ;
50+ game. time_since_last_score = time;
51+ }
3552 }
3653
3754 pub fn draw_credits ( & mut self , d : & mut RaylibDrawHandle ) {
38- let mut y = self . offset ;
55+ let mut y = d. get_screen_height ( ) as f32 / 2.0 - 50.0 - ( self . acknowledgements . len ( ) as f32 * 30.0 ) - 30.0 * 3.0 - 150.0 ;
56+ // center text x
57+ let x = d. get_screen_width ( ) as f32 / 2.0 ;
58+
59+ let title = format ! ( "Title: {}" , & self . title) ;
60+ let author = format ! ( "By {}" , & self . author) ;
61+ let version = format ! ( "Version: {}" , & self . version) ;
62+ let description = format ! ( "Description: {}" , & self . description) ;
63+ let website = format ! ( "Website: {}" , & self . website) ;
64+ let license = format ! ( "License: {}" , & self . license) ;
65+
66+ let title_width = measure_text ( & title, 48 ) ;
67+ let author_width = measure_text ( & author, 48 ) ;
68+ let version_width = measure_text ( & version, 20 ) ;
69+ let description_width = measure_text ( & description, 20 ) ;
70+ let website_width = measure_text ( & website, 20 ) ;
71+ let license_width = measure_text ( & license, 20 ) ;
72+
73+ let title_x = ( x - title_width as f32 / 2.0 ) as i32 ;
74+ let author_x = ( x - author_width as f32 / 2.0 ) as i32 ;
75+ let version_x = ( x - version_width as f32 / 2.0 ) as i32 ;
76+ let description_x = ( x - description_width as f32 / 2.0 ) as i32 ;
77+ let website_x = ( x - website_width as f32 / 2.0 ) as i32 ;
78+ let license_x = ( x - license_width as f32 / 2.0 ) as i32 ;
79+
80+ // fade in and fade out credits
81+ let time = unsafe { TIME } ;
82+ let mut alpha = 1.0 ;
83+ if time < CREDITS_DURATION as f64 / 2.0 {
84+ alpha = time as f32 ;
85+ }
86+ else if time > CREDITS_DURATION as f64 / 4.0 {
87+ alpha = ( CREDITS_DURATION as f64 - time) as f32 ;
88+ }
89+
90+ let fade_out_color = Color :: WHITE . fade ( alpha) ;
91+
92+ unsafe {
93+ let img = self . logo_texture ;
94+ DrawTexture ( img,
95+ x as i32 - ( img. width as f32 / 2.0 ) as i32 ,
96+ y as i32 ,
97+ ffi:: Color {
98+ r : fade_out_color. r ,
99+ g : fade_out_color. g ,
100+ b : fade_out_color. b ,
101+ a : fade_out_color. a ,
102+ } ) ;
103+ y += img. height as f32 + 50.0 as f32 ;
104+ }
105+
39106
40107 // draw title
41- d. draw_text ( & self . title , 10 , y as i32 , 40 , Color :: WHITE ) ;
108+ d. draw_text ( title. as_str ( ) , title_x , y as i32 , 40 , fade_out_color ) ;
42109 y += 50.0 ;
43- // draw authors
44- for author in & self . authors {
45- d. draw_text ( & author, 10 , y as i32 , 20 , Color :: WHITE ) ;
110+
111+ // draw author
112+ d. draw_text ( author. as_str ( ) , author_x, y as i32 , 40 , fade_out_color) ;
113+ y += 50.0 ;
114+ // draw acknowledgements
115+ for author in & self . acknowledgements {
116+ let author = format ! ( "{}" , author) ;
117+ let author_width = measure_text ( & author, 20 ) ;
118+ let author_x = ( x - author_width as f32 / 2.0 ) as i32 ;
119+
120+ d. draw_text ( author. as_str ( ) , author_x, y as i32 , 20 , fade_out_color) ;
46121 y += 30.0 ;
47122 }
48123 // draw version
49- d. draw_text ( & self . version , 10 , y as i32 , 20 , Color :: WHITE ) ;
124+ d. draw_text ( version. as_str ( ) , version_x , y as i32 , 20 , fade_out_color ) ;
50125 y += 30.0 ;
51126 // draw description
52- d. draw_text ( & self . description , 10 , y as i32 , 20 , Color :: WHITE ) ;
127+ d. draw_text ( description. as_str ( ) , description_x , y as i32 , 20 , fade_out_color ) ;
53128 y += 30.0 ;
54129 // draw website
55- d. draw_text ( & self . website , 10 , y as i32 , 20 , Color :: WHITE ) ;
130+ d. draw_text ( website. as_str ( ) , website_x , y as i32 , 20 , fade_out_color ) ;
56131 y += 30.0 ;
57132 // draw license
58- d. draw_text ( & self . license , 10 , y as i32 , 20 , Color :: WHITE ) ;
59-
133+ d. draw_text ( license. as_str ( ) , license_x, y as i32 , 20 , fade_out_color) ;
60134 }
61135}
0 commit comments