]> git.seodisparate.com - LudumDare45_StartWithNothing/commitdiff
More work on agnostic_interface/raylib_impl
authorStephen Seo <seo.disparate@gmail.com>
Mon, 20 Feb 2023 07:04:13 +0000 (16:04 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 20 Feb 2023 07:04:13 +0000 (16:04 +0900)
src/agnostic_interface.rs
src/agnostic_interface/raylib_impl.rs
src/original_impl.rs

index 95a67fdf4be3288ac18db747d11bad88a6b351c1..0efd40517deb47df158ae0829264404a4a20f1b0 100644 (file)
@@ -6,8 +6,12 @@ use crate::faux_quicksilver::{Circle, Color, Rectangle, Transform, Vector};
 
 pub trait ImageInterface {
     fn draw(&mut self, x: f32, y: f32, color: Color) -> Result<(), String>;
-    fn draw_sub(&mut self, sub_rect: Rectangle, x: f32, y: f32, color: Color)
-        -> Result<(), String>;
+    fn draw_sub(
+        &mut self,
+        sub_rect: Rectangle,
+        dest_rect: Rectangle,
+        color: Color,
+    ) -> Result<(), String>;
     fn draw_transform(
         &mut self,
         x: f32,
@@ -19,8 +23,7 @@ pub trait ImageInterface {
     fn draw_sub_transform(
         &mut self,
         sub_rect: Rectangle,
-        x: f32,
-        y: f32,
+        dest_rect: Rectangle,
         color: Color,
         transform: Transform,
         origin: Vector,
@@ -66,13 +69,6 @@ pub trait GameInterface {
     fn end_drawing(&mut self) -> Result<(), String>;
 
     fn draw_circle(&mut self, circle: Circle, color: Color) -> Result<(), String>;
-    fn draw_circle_ex(
-        &mut self,
-        circle: Circle,
-        color: Color,
-        origin: Vector,
-        rot: f32,
-    ) -> Result<(), String>;
     fn draw_circle_transform(
         &mut self,
         circle: Circle,
index e6f27df92207678b34101cacd199ec586683d1d0..1812577387771c4483b8a50f31e93ebc6d662771 100644 (file)
@@ -28,14 +28,40 @@ fn fqcolor_to_color(c: crate::faux_quicksilver::Color) -> ffi::Color {
     }
 }
 
+fn fqrect_to_rect(r: crate::faux_quicksilver::Rectangle) -> ffi::Rectangle {
+    ffi::Rectangle {
+        x: r.x,
+        y: r.y,
+        width: r.w,
+        height: r.h,
+    }
+}
+
+fn fqvector_to_vector2(v: crate::faux_quicksilver::Vector) -> ffi::Vector2 {
+    ffi::Vector2 { x: v.x, y: v.y }
+}
+
 #[derive(Clone, Debug)]
 struct RaylibImage {
     image: ffi::Image,
+    texture: Option<ffi::Texture>,
 }
 
 #[derive(Clone, Debug)]
 struct RaylibImageHandler {
-    image: Rc<RaylibImage>,
+    image: Rc<RefCell<RaylibImage>>,
+}
+
+impl RaylibImageHandler {
+    fn image_to_texture(&mut self) -> Result<(), String> {
+        if self.image.borrow().texture.is_none() {
+            unsafe {
+                self.image.borrow_mut().texture =
+                    Some(ffi::LoadTextureFromImage(self.image.borrow().image));
+            }
+        }
+        Ok(())
+    }
 }
 
 impl ImageInterface for RaylibImageHandler {
@@ -45,17 +71,46 @@ impl ImageInterface for RaylibImageHandler {
         y: f32,
         color: crate::faux_quicksilver::Color,
     ) -> Result<(), String> {
-        todo!()
+        self.image_to_texture()?;
+        unsafe {
+            ffi::DrawTexture(
+                *self
+                    .image
+                    .borrow()
+                    .texture
+                    .as_ref()
+                    .ok_or_else(|| String::from("RaylibImage has no Texture!"))?,
+                x.round() as i32,
+                y.round() as i32,
+                fqcolor_to_color(color),
+            );
+        }
+        Ok(())
     }
 
     fn draw_sub(
         &mut self,
         sub_rect: crate::faux_quicksilver::Rectangle,
-        x: f32,
-        y: f32,
+        dest_rect: crate::faux_quicksilver::Rectangle,
         color: crate::faux_quicksilver::Color,
     ) -> Result<(), String> {
-        todo!()
+        self.image_to_texture()?;
+        unsafe {
+            ffi::DrawTexturePro(
+                *self
+                    .image
+                    .borrow()
+                    .texture
+                    .as_ref()
+                    .ok_or_else(|| String::from("RaylibImage has no Texture!"))?,
+                fqrect_to_rect(sub_rect),
+                fqrect_to_rect(dest_rect),
+                ffi::Vector2 { x: 0.0, y: 0.0 },
+                0.0,
+                fqcolor_to_color(color),
+            );
+        }
+        Ok(())
     }
 
     fn draw_transform(
@@ -72,8 +127,7 @@ impl ImageInterface for RaylibImageHandler {
     fn draw_sub_transform(
         &mut self,
         sub_rect: crate::faux_quicksilver::Rectangle,
-        x: f32,
-        y: f32,
+        dest_rect: crate::faux_quicksilver::Rectangle,
         color: crate::faux_quicksilver::Color,
         transform: crate::faux_quicksilver::Transform,
         origin: Vector,
@@ -82,15 +136,20 @@ impl ImageInterface for RaylibImageHandler {
     }
 
     fn get_w(&self) -> usize {
-        todo!()
+        self.image.borrow().image.width as usize
     }
 
     fn get_h(&self) -> usize {
-        todo!()
+        self.image.borrow().image.height as usize
     }
 
     fn get_wh_rect(&self) -> crate::faux_quicksilver::Rectangle {
-        todo!()
+        crate::faux_quicksilver::Rectangle {
+            x: 0.0,
+            y: 0.0,
+            w: self.image.borrow().image.width as f32,
+            h: self.image.borrow().image.height as f32,
+        }
     }
 }
 
@@ -113,7 +172,18 @@ impl FontInterface for RaylibFontHandler {
         y: f32,
         color: crate::faux_quicksilver::Color,
     ) -> Result<(), String> {
-        todo!()
+        unsafe {
+            let cstring = CString::from_vec_unchecked(s.as_bytes().into());
+            ffi::DrawTextEx(
+                self.font.font,
+                cstring.as_ptr(),
+                ffi::Vector2 { x, y },
+                size as f32,
+                (size / 10) as f32,
+                fqcolor_to_color(color),
+            );
+        }
+        Ok(())
     }
 }
 
@@ -189,7 +259,7 @@ impl MusicInterface for RaylibMusicHandler {
 }
 
 struct RaylibGame {
-    images: HashMap<String, Rc<RaylibImage>>,
+    images: HashMap<String, Rc<RefCell<RaylibImage>>>,
     fonts: HashMap<String, Rc<RaylibFont>>,
     sounds: HashMap<String, Rc<RaylibSound>>,
     music: HashMap<String, Rc<RefCell<RaylibMusic>>>,
@@ -218,7 +288,10 @@ impl Drop for RaylibGame {
     fn drop(&mut self) {
         unsafe {
             for (_, image) in &self.images {
-                ffi::UnloadImage(image.image);
+                if let Some(texture) = image.borrow_mut().texture.take() {
+                    ffi::UnloadTexture(texture);
+                }
+                ffi::UnloadImage(image.borrow().image);
             }
             for (_, font) in &self.fonts {
                 ffi::UnloadFont(font.font);
@@ -317,16 +390,6 @@ impl GameInterface for RaylibGame {
         Ok(())
     }
 
-    fn draw_circle_ex(
-        &mut self,
-        circle: crate::faux_quicksilver::Circle,
-        color: crate::faux_quicksilver::Color,
-        origin: crate::faux_quicksilver::Vector,
-        rot: f32,
-    ) -> Result<(), String> {
-        todo!()
-    }
-
     fn draw_circle_transform(
         &mut self,
         circle: crate::faux_quicksilver::Circle,
@@ -361,7 +424,15 @@ impl GameInterface for RaylibGame {
         origin: crate::faux_quicksilver::Vector,
         rot: f32,
     ) -> Result<(), String> {
-        todo!()
+        unsafe {
+            ffi::DrawRectanglePro(
+                fqrect_to_rect(rect),
+                fqvector_to_vector2(origin),
+                rot,
+                fqcolor_to_color(color),
+            );
+        }
+        Ok(())
     }
 
     fn draw_rect_transform(
@@ -386,7 +457,10 @@ impl GameInterface for RaylibGame {
             let cstring: CString = CString::from_vec_unchecked(path_buf);
             let image = ffi::LoadImage(cstring.as_ptr());
             let raylib_image_handler = RaylibImageHandler {
-                image: Rc::new(RaylibImage { image }),
+                image: Rc::new(RefCell::new(RaylibImage {
+                    image,
+                    texture: None,
+                })),
             };
             self.images
                 .insert(path_str.to_owned(), raylib_image_handler.image.clone());
index 3c5c8695bc2278edc0e8bc32fa67d6942987ca9a..d0859525b338ab4dee22c7fab9ec3f94960acc0d 100644 (file)
@@ -1152,8 +1152,7 @@ impl Fish {
         fish_img
             .draw_sub_transform(
                 self.body_rect,
-                body_rect.x,
-                body_rect.y,
+                body_rect,
                 self.color,
                 transform * body_tr,
                 Vector {
@@ -1176,8 +1175,7 @@ impl Fish {
         fish_img
             .draw_sub_transform(
                 self.tail_rect,
-                tail_rect.x,
-                tail_rect.y,
+                tail_rect,
                 self.color,
                 transform * tail_tr,
                 Vector {