Bladeren bron

Sprite clipping

Eiyeron Fulmincendii 10 jaren geleden
bovenliggende
commit
e416ae4165
3 gewijzigde bestanden met toevoegingen van 80 en 0 verwijderingen
  1. 1 0
      platform/include/Graphics.h
  2. 37 0
      platform/nspire/Graphics.cpp
  3. 42 0
      platform/sfml/Graphics.cpp

+ 1 - 0
platform/include/Graphics.h

@@ -36,6 +36,7 @@ namespace WalrusRPG
         void put_sprite(const WalrusRPG::Graphics::Texture &sheet, int x, int y,
                         const WalrusRPG::Utils::Rect &window);
 
+        void put_sprite_clipping(const Texture &sheet, int x, int y, const WalrusRPG::Utils::Rect &sprite_window, const WalrusRPG::Utils::Rect &clipping_window);
         /*
          * Draws a sprite with clipping given as window and color
          * tinting.

+ 37 - 0
platform/nspire/Graphics.cpp

@@ -40,6 +40,43 @@ void Graphics::put_sprite_tint(const Texture &sheet, int x, int y, const Rect &w
     CXfb::draw_sprite_sheet_tint(sheet.data, x, y, window, color.value);
 }
 
+void Graphics::put_sprite_clipping(const Texture &sheet, int x, int y, const Rect &sprite_window, const Rect &clipping_window)
+{
+    const signed &ss_x = sprite_window.x, ss_y = sprite_window.y;
+    const signed &ss_w = sprite_window.width, &ss_h = sprite_window.height;
+    const signed &cx = clipping_window.x, &cy = clipping_window.y; 
+    const signed &cw = clipping_window.width, &ch = clipping_window.height; 
+    const signed lx = x - cx, ly = y - cy;
+   
+    if(lx < -ss_w || lx > cw) return;
+    if(ly < -ss_h || ly > ch) return;
+
+    signed fx = x, fy = y;
+    signed fssx = ss_x, fssy = ss_y, fssw = ss_w, fssh = ss_h;
+   
+    if(lx < 0) {
+        fssw = ss_w+lx;
+        fssx = -lx;
+        fx = cx;
+    }
+
+    if(lx > cw - ss_w) {
+        fssw -= lx-(cw - ss_w);
+    }
+
+    if(ly > ch - ss_h) {
+        fssh -= ly-(ch - ss_h);
+    }
+
+    if(ly < 0) {
+        fssh = ss_h+ly;
+        fssy = -ly;
+        fy = cy;
+    }
+    
+    CXfb::draw_sprite_sheet(sheet.data, fx, fy, {fssx, fssy, fssw, fssh});
+}
+
 void Graphics::fill(const Pixel &color)
 {
     CXfb::buffer_fill(color);

+ 42 - 0
platform/sfml/Graphics.cpp

@@ -74,6 +74,48 @@ void Graphics::put_sprite_tint(const WalrusRPG::Graphics::Texture &sheet, int x,
     buffer.draw(sprite);
 }
 
+void Graphics::put_sprite_clipping(const Texture &sheet, int x, int y, const Rect &sprite_window, const Rect &clipping_window)
+{
+    const signed &ss_x = sprite_window.x, ss_y = sprite_window.y;
+    const signed &ss_w = sprite_window.width, &ss_h = sprite_window.height;
+    const signed &cx = clipping_window.x, &cy = clipping_window.y; 
+    const signed &cw = clipping_window.width, &ch = clipping_window.height; 
+    const signed lx = x - cx, ly = y - cy;
+   
+    if(lx < -ss_w || lx > cw) return;
+    if(ly < -ss_h || ly > ch) return;
+
+    signed fx = x, fy = y;
+    signed fssx = ss_x, fssy = ss_y, fssw = ss_w, fssh = ss_h;
+   
+    if(lx < 0) {
+        fssw = ss_w+lx;
+        fssx = -lx;
+        fx = cx;
+    }
+
+    if(lx > cw - ss_w) {
+        fssw -= lx-(cw - ss_w);
+    }
+
+    if(ly > ch - ss_h) {
+        fssh -= ly-(ch - ss_h);
+    }
+
+    if(ly < 0) {
+        fssh = ss_h+ly;
+        fssy = -ly;
+        fy = cy;
+    }
+
+    sf::Sprite sprite;
+    sprite.setTexture(sheet.data);
+    sprite.setTextureRect(sf::IntRect(fssx, fssy, fssw, fssh));
+    sprite.setPosition(fx, fy);
+    buffer.draw(sprite);
+}
+
+    
 void Graphics::fill(const Pixel &color)
 {
     buffer.clear(sf::Color(color.r << 3, color.g << 2, color.b << 3, 255));