Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: detect if mouse is at colored area of a sprite  (Read 1548 times)

0 Members and 1 Guest are viewing this topic.

qwerty

  • Newbie
  • *
  • Posts: 4
    • View Profile
detect if mouse is at colored area of a sprite
« on: December 11, 2022, 07:33:52 am »
for example, I have this sprite:

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:

Quote
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);
                }
        }
}

Quote
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
« Last Edit: December 12, 2022, 04:23:40 pm by qwerty »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: detect if mouse is at colored area of a sprite
« Reply #1 on: December 12, 2022, 10:59:13 am »
Are you just showing what you've achieved or do you have a question and forgot to actually ask it? :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

qwerty

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: detect if mouse is at colored area of a sprite
« Reply #2 on: December 12, 2022, 04:22:38 pm »
Are you just showing what you've achieved or do you have a question and forgot to actually ask it? :D

the last part of my topic is the problem itself.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: detect if mouse is at colored area of a sprite
« Reply #3 on: December 12, 2022, 04:51:44 pm »
Hard to say really. Use your debugger to see what the values actually are and check if maybe some reference breaks or similar.

Btw you can use string interpolation for building strings, so you don't need to use the plus sign: $"is_mOver: {lst_piece1[0].is_mOver}| mx: {mouse.X}, my: {mouse.Y} | colored pixels: {lst_piece1[0].lst_pos.Count}"
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/