for example, I have this sprite:
https://imgur.com/UOl0DVN (https://imgur.com/UOl0DVN)
I need this for drag the sprite only from that area, but I'll take care of that.
This is my code:
piece.cs
using System;using System.Collections.Generic;using SFML.Graphics;namespace puzzlegame
{ public class piece
:Sprite
{ Texture txtr
; public bool is_mOver,is_mReleased
; public List
<string> lst_pos
= new List
<string>(); public piece
() { } public void setTexture
(int num
){ string path
=""; if(num
==1) path
=@"E:\puzzle_game\shape1.png"; if(num
==2) path
=@"E:\puzzle_game\shape2.png"; if(num
==3) path
=@"E:\puzzle_game\shape3.png"; if(num
==4) path
=@"E:\puzzle_game\shape4.png"; if(num
==5) path
=@"E:\puzzle_game\shape5.png"; txtr
= new Texture
(path
); this.Texture = txtr
; } public void checkColoredArea
(int mx,
int my
){ int px,py
; int charPos,sz
; for(int i
=0;i
<lst_pos
.Count;i
++){ charPos
=lst_pos
[i
].IndexOf(":"); sz
=lst_pos
[i
].Length; px
=int.Parse(lst_pos
[i
].Substring(0,sz
-(sz
-charPos
))); py
=int.Parse(lst_pos
[i
].Substring(charPos
+1,sz
-(charPos
+1))); if(mx
==px
&& my
==py
){ is_mOver
=true; Console
.WriteLine("it's over shape"); } else{ is_mOver
=false; } } } public void fillPosxy
(){ Color colortmp
; Image img
= this.txtr.CopyToImage(); for(int i
=0;i
<txtr
.Size.X;i
++){ for(int j
=0;j
<txtr
.Size.Y;j
++){ colortmp
=img
.GetPixel((uint)j,
(uint)i
); string pos
= (j
+this.Position.X)+":"+(i
+this.Position.Y); if(colortmp
.A!=0){ //Console.WriteLine(pos); lst_pos
.Add(pos
); } } } Console
.WriteLine("lst_pos: "+lst_pos
.Count); } }}
Program.cs
using System;using SFML.Audio;using SFML.Graphics;using SFML.Window;using System.Collections.Generic;using SFML.System;namespace puzzlegame
{ class Program
{ public static bool is_r,is_l,is_u,is_d,is_e,is_i
; public static RenderWindow rw
; public static float velx,vely
; public static Vector2i mouse
; public static bool idle_der
=true,idle_izq
; public static bool isReleased
; public static List
<piece
> lst_piece1
= new List
<piece
>(5); public static void Main
(string[] args
) { rw
= new RenderWindow
(new VideoMode
(850,
650),
"puzzle"); rw
.SetActive(true); rw
.SetVerticalSyncEnabled(true); rw
.SetFramerateLimit(60); rw
.KeyPressed += OnKeyPressed
; rw
.KeyReleased += OnKeyReleased
; rw
.MouseMoved += OnMouseMoved
; rw
.MouseButtonPressed += OnMouseButtonPressed
; rw
.MouseButtonReleased += OnMouseButtonReleased
; Clock clck
= new Clock
(); Time timeSinceLastUpdate
=Time
.Zero; Time timePerFrame
=Time
.FromSeconds(1
.0f
/60
.0f
); loadPiece
(); lst_piece1
[0].fillPosxy(); while(rw
.IsOpen==true){ Time elapsedTime
= clck
.Restart(); timeSinceLastUpdate
+=elapsedTime
; while(timeSinceLastUpdate
>timePerFrame
) { timeSinceLastUpdate
-=timePerFrame
; rw
.DispatchEvents(); update
(timePerFrame
); } draw
(); } } static void update
(Time deltatime
){ if(is_r
){ velx
= 1f
; } if(is_l
){ velx
= -1f
; } if(is_u
){ vely
= -1f
; } if(is_d
){ vely
= 1f
; } lst_piece1
[0].checkColoredArea(mouse
.X,mouse
.Y); rw
.SetTitle("is_mOver: "+lst_piece1
[0].is_mOver+" | mx: "+mouse
.X+", my: "+mouse
.Y+" | colored pixels: "+lst_piece1
[0].lst_pos.Count); } static void draw
(){ rw
.Clear(Color
.Cyan); for(int i
=0;i
</*lst_piece1.Count*/1;i
++) rw
.Draw(lst_piece1
[i
]); rw
.Display(); } static void OnMouseMoved
(object sender, MouseMoveEventArgs e
){ mouse
= Mouse
.GetPosition(rw
); } static void OnMouseButtonPressed
(object sender, MouseButtonEventArgs e
){ if(e
.Button == Mouse
.Button.Left){ isReleased
= false; } } static void OnMouseButtonReleased
(object sender, MouseButtonEventArgs e
){ if(e
.Button == Mouse
.Button.Left){ isReleased
= true; } } static void OnKeyReleased
(object sender, KeyEventArgs e
){ if(e
.Code == Keyboard
.Key.Right){ is_r
=false; velx
=0f
; idle_der
=true; idle_izq
=false; } if(e
.Code == Keyboard
.Key.Left){ is_l
=false; velx
=0f
; idle_izq
=true; idle_der
=false; } if(e
.Code == Keyboard
.Key.Up){ is_u
=false; vely
=0f
; } if(e
.Code == Keyboard
.Key.Down){ is_d
=false; vely
=0f
; } if(e
.Code == Keyboard
.Key.Space) is_e
=false; if(e
.Code == Keyboard
.Key.I){ is_i
=false; } } static void OnKeyPressed
(object sender, KeyEventArgs e
){ if(e
.Code == Keyboard
.Key.Right){ is_r
=true; is_l
=false; idle_der
=false; idle_izq
=false; } if(e
.Code == Keyboard
.Key.Left){ is_l
=true; is_r
=false; idle_der
=false; idle_izq
=false; } if(e
.Code == Keyboard
.Key.Up) is_u
=true; if(e
.Code == Keyboard
.Key.Down) is_d
=true; if(e
.Code == Keyboard
.Key.Escape) Environment
.Exit(0); if(e
.Code == Keyboard
.Key.Space) is_e
=true; if(e
.Code == Keyboard
.Key.I){ is_i
=true; } } static void loadPiece
(){ for(int i
=0;i
<5;i
++){ piece pz
= new piece
(); pz
.Position = new Vector2f
(550,
90); pz
.setTexture(i
+1); lst_piece1
.Add(pz
); } } }}
when the cursor enters the colored area prints a message to console, but "is_mOver" variable of sprite always figure as false at window title. here's a video for better explanation:
https://imgur.com/a/bx8jv8l (https://imgur.com/a/bx8jv8l)