/* Included all */ use lexsdl::*; /* Included functions */ use sdl2_sys::SDL_RenderSetLogicalSize; use sdl2_sys::SDL_RenderFillRect; use sdl2_sys::SDL_SetTextureColorMod; /* Included types */ use std::ffi::CString; use libc::c_int; use sdl2_sys::SDL_Rect; use lexlib::Vec2; // include bytes of a texture. const PURPLE_GUY_TEXTURE_WALK_DATA: &[u8] = include_bytes!("assets/purpleGuy_walk.png"); fn main(){unsafe{ // initialize sdl and image with the LEXSDL dafaults. LEXSDL_Init(0); LEXSDL_InitIMG(0); // title for the window let window_title = CString::new("LEXSDL Rust Example - Sprites").unwrap(); //create the window and renderer LEXSDL_CreateWindow(window_title.as_ptr(),0); LEXSDL_CreateRenderer(0); // Set a size for the rendering target. SDL_RenderSetLogicalSize(LEXSDL_GetRenderer(), 64,64); // counter intuitively the draw color will be the background color LEXSDL_SetDrawColor(0x00,0x88,0xFF,0xFF); // direction of the texture of the ring in the sky let ring_file = CString::new("examples/assets/ring001.png").unwrap(); // load the texture of the sky ring let ring_texture_id = LEXSDL_TextureLoad(ring_file.as_ptr()); // load the byte data of the guy sprite let purple_guy_texture_id = LEXSDL_TextureLoadBytes(PURPLE_GUY_TEXTURE_WALK_DATA.as_ptr() as *const u8, PURPLE_GUY_TEXTURE_WALK_DATA.len() as c_int); // create the sprite of the purple guy let purple_guy_texture_walk_sprite_id = LEXSDL_CreateSprite(purple_guy_texture_id, 4, 6, SDL_Rect {x: 0, y: 0, w: 4, h: 4}); // create the sprite of the sky ring let ring_sprite_id = LEXSDL_CreateSprite(ring_texture_id, 4, 12, SDL_Rect {x: 0, y: 0, w: 5, h: 5}); //setup variables for the guy let mut guy_pos = Vec2::new(4,60); let guy_speed = 16; let mut guy_direction = false; let mut frame_cnt = 0; // variables for changing color of the ring let mut ring_color_change_dir = false; let mut ring_red = 0; let mut ring_green = 0; let mut ring_blue = 0; while LEXSDL_EventQuit() == 0 { // handle default events LEXSDL_HandleEvents(); // start a new frame LEXSDL_NewFrame(); frame_cnt += 1; // fill the area with a rect as background SDL_RenderFillRect(LEXSDL_GetRenderer(),std::ptr::null()); // quick dirty color change if ring_red == 0 { ring_color_change_dir = false } if !ring_color_change_dir { if ring_red < 255 { ring_red += 1; } if ring_red > 100 && ring_green < 255 { ring_green += 1; } if ring_green > 100 && ring_blue < 255 { ring_blue += 1; } if ring_red > 250 && ring_green > 250 && ring_blue > 250 { ring_color_change_dir = true; } } else { if ring_blue > 0 { ring_blue -=1; } if ring_blue < 150 && ring_green > 0 { ring_green -= 1; } if ring_green < 150 && ring_red > 0 { ring_red -= 1; } } // change color if the ring SDL_SetTextureColorMod(LEXSDL_SpriteGetTexture(ring_sprite_id),ring_red,ring_green,ring_blue); // play the animation of the ring LEXSDL_SpritePlay(ring_sprite_id,10,10,4,4); // play the animation of the guy depending of his direction if guy_direction { LEXSDL_SpritePlayFlip(purple_guy_texture_walk_sprite_id,guy_pos.x,guy_pos.y,4,4); } else { LEXSDL_SpritePlay(purple_guy_texture_walk_sprite_id,guy_pos.x,guy_pos.y,4,4); } // check the frame count to move the guy if frame_cnt == guy_speed { frame_cnt = 0; if guy_direction { if guy_pos.x <= 0 { guy_direction = false; } guy_pos.x -= 1; } else { if guy_pos.x >= 60 { guy_direction = true; } guy_pos.x += 1; } } // finally show the frame LEXSDL_ShowFrame(); } // termination LEXSDL_Terminate(); LEXSDL_Quit(); }}