Skip to content

Commit

Permalink
View: Remove C++ sfView_createFromRect wrapper, API changes, doc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
crumblingstatue committed Oct 18, 2024
1 parent 91989f2 commit 20686ec
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 50 deletions.
15 changes: 4 additions & 11 deletions CSFML/src/Graphics/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@
#include "System/Vector2.h"
#include <SFML/Graphics/View.hpp>

extern "C" sf::View *sfView_create(void) {
extern "C" sf::View *sfView_new(void) {
return new sf::View;
}

extern "C" sf::View *sfView_createFromRect(sfFloatRect rectangle) {
sf::View *view = new sf::View;
view->reset(sf::FloatRect(rectangle.left, rectangle.top, rectangle.width, rectangle.height));

return view;
extern "C" void sfView_del(sf::View *view) {
delete view;
}

extern "C" sf::View *sfView_copy(const sf::View *view) {
extern "C" sf::View *sfView_cpy(const sf::View *view) {
return new sf::View(*view);
}

extern "C" void sfView_destroy(sf::View *view) {
delete view;
}

extern "C" void sfView_setCenter(sf::View *view, sfVector2f center) {
view->setCenter(center.x, center.y);
}
Expand Down
25 changes: 16 additions & 9 deletions examples/spritemark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn main() -> SfResult<()> {
let mut window = RenderWindow::new(
native_mode,
"Spritemark",
Style::NONE,
Style::default(),
&ContextSettings::default(),
)?;
window.set_position(Vector2::new(0, 0));
Expand All @@ -82,6 +82,8 @@ fn main() -> SfResult<()> {
let mut frames_rendered = 0;
let mut sec_clock = Clock::start()?;
let mut fps = 0;
let mut lmb_down = false;
let mut view = View::new()?;

while window.is_open() {
while let Some(event) = window.poll_event() {
Expand All @@ -93,20 +95,25 @@ fn main() -> SfResult<()> {
Event::MouseButtonPressed {
button: Button::Left,
..
} => click_counter += 1,
} => {
click_counter += 1;
lmb_down = true;
}
Event::MouseButtonReleased {
button: Button::Left,
..
} => {
lmb_down = false;
}
Event::Resized { width, height } => {
window.set_view(&View::from_rect(Rect::new(
0.,
0.,
width as f32,
height as f32,
)));
view.reset(Rect::new(0., 0., width as f32, height as f32));
window.set_view(&view);
}
_ => {}
}
}

if Button::Left.is_pressed() {
if lmb_down {
let mp = window.mouse_position();
for _ in 0..25 {
objects.push(Object {
Expand Down
7 changes: 3 additions & 4 deletions src/ffi/graphics_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,9 @@ pub fn sfVertexBuffer_getUsage(vertexBuffer: *const sfVertexBuffer) -> sfVertexB
pub fn sfVertexBuffer_bind(vertexBuffer: *const sfVertexBuffer);
pub fn sfVertexBuffer_isAvailable() -> bool;
// View.cpp
pub fn sfView_create() -> *mut sfView;
pub fn sfView_createFromRect(rectangle: sfFloatRect) -> *mut sfView;
pub fn sfView_copy(view: *const sfView) -> *mut sfView;
pub fn sfView_destroy(view: *mut sfView);
pub fn sfView_new() -> *mut sfView;
pub fn sfView_del(view: *mut sfView);
pub fn sfView_cpy(view: *const sfView) -> *mut sfView;
pub fn sfView_setCenter(view: *mut sfView, center: sfVector2f);
pub fn sfView_setSize(view: *mut sfView, size: sfVector2f);
pub fn sfView_setRotation(view: *mut sfView, angle: f32);
Expand Down
65 changes: 39 additions & 26 deletions src/graphics/view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
crate::{
ffi::graphics as ffi, graphics::FloatRect, sf_box::RawDefault, system::Vector2f, SfBox,
ffi::graphics as ffi, graphics::FloatRect, sf_box::RawDefault, system::Vector2f,
IntoSfResult, SfBox, SfResult,
},
std::ptr::NonNull,
};
Expand All @@ -14,6 +15,37 @@ decl_opaque! {
View;
}

/// Creation
impl View {
/// Creates a default `View` of (0, 0, 1000, 1000)
pub fn new() -> SfResult<SfBox<Self>> {
SfBox::new(unsafe { ffi::sfView_new() }).into_sf_result()
}
/// Creates a view with position and size
///
/// # Arguments
/// * center - The center of the view
/// * size - The size of the view
#[must_use]
pub fn with_center_and_size(center: Vector2f, size: Vector2f) -> SfBox<View> {
let mut view: SfBox<View> = Default::default();
view.set_center(center);
view.set_size(size);
view
}

/// Construct a view from a rectangle
///
/// # Arguments
/// * rectangle - The rectangle defining the zone to display
pub fn from_rect(rectangle: FloatRect) -> SfResult<SfBox<View>> {
let mut new = Self::new()?;
new.reset(rectangle);
Ok(new)
}
}

/// Query properties
impl View {
/// Get the current orientation of a view
///
Expand Down Expand Up @@ -45,29 +77,10 @@ impl View {
pub fn viewport(&self) -> FloatRect {
unsafe { ffi::sfView_getViewport(self) }
}
/// Creates a view with position and size
///
/// # Arguments
/// * center - The center of the view
/// * size - The size of the view
#[must_use]
pub fn new(center: Vector2f, size: Vector2f) -> SfBox<View> {
let mut view: SfBox<View> = Default::default();
view.set_center(center);
view.set_size(size);
view
}

/// Construct a view from a rectangle
///
/// # Arguments
/// * rectangle - The rectangle defining the zone to display
#[must_use]
pub fn from_rect(rectangle: FloatRect) -> SfBox<View> {
let view = unsafe { ffi::sfView_createFromRect(rectangle) };
SfBox::new(view).expect("Failed to create View from Rect")
}
}

/// Set properties
impl View {
/// Set the orientation of a view
///
/// The default rotation of a view is 0 degree.
Expand Down Expand Up @@ -155,19 +168,19 @@ impl View {
impl ToOwned for View {
type Owned = SfBox<Self>;
fn to_owned(&self) -> Self::Owned {
let view = unsafe { ffi::sfView_copy(self) };
let view = unsafe { ffi::sfView_cpy(self) };
SfBox::new(view).expect("Failed to copy View")
}
}

impl RawDefault for View {
fn raw_default() -> NonNull<Self> {
NonNull::new(unsafe { ffi::sfView_create() }).expect("Failed to create view")
NonNull::new(unsafe { ffi::sfView_new() }).expect("Failed to create view")
}
}

impl Drop for View {
fn drop(&mut self) {
unsafe { ffi::sfView_destroy(self) }
unsafe { ffi::sfView_del(self) }
}
}

0 comments on commit 20686ec

Please sign in to comment.