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

Author Topic: I want to make my cursor  (Read 17599 times)

0 Members and 1 Guest are viewing this topic.

luffy50660

  • Newbie
  • *
  • Posts: 1
    • View Profile
I want to make my cursor
« on: November 24, 2017, 11:20:55 am »
int main ()
{
        //Initialisation des variables
        sfRenderWindow *window;
        sfVideoMode video_mode;
       
        sfTexture *texture;
        sfSprite *sprite;

        sfTexture *texture2;
        sfSprite *cursor;
       
        sfTexture *texture1;
        sfSprite *sprite1;
       
        sfIntRect rect;

        sfEvent event;
       
        sfVector2f duck_vector;
        duck_vector.x = 0;
        duck_vector.y = randnum();
       
        sfVector2f invers_duck;
        invers_duck.x = -1;
        invers_duck.y = 1;
       
        sfClock *clock; //Variable du temps
        sfTime time;
        float seconds;
        clock = sfClock_create();
       
        //Taille de la fenetre
        video_mode.width = 1280;
        video_mode.height = 769;
        video_mode.bitsPerPixel = 32;
       
        //Attribution des valeurs au rectangle
        rect.top = 0;
        rect.left = 0;
        rect.width = 110;
        rect.height = 769;
       
        //Creation du background
        texture = sfTexture_createFromFile("background.png", NULL);
        sprite = sfSprite_create();

        //Creation du canard
        texture1 = sfTexture_createFromFile("spritesheet.png", NULL);
        sprite1 = sfSprite_create();
        sfSprite_setPosition(sprite1, duck_vector);
       
        //Creation de la fenetr + curseur
        window = sfRenderWindow_create(video_mode, "Duck Hunt", sfDefaultStyle, NULL);
       
        sfWindow_setMouseCursorVisible(window, sfFalse);
        texture2 = sfTexture_createFromFile("mire.png", NULL);
        cursor = sfSprite_create();
       
        sfSprite_setTexture(sprite, texture, sfTrue);
        sfSprite_setTexture(sprite1, texture1, sfTrue);
        sfSprite_setTexture(cursor, texture2, sfTrue);
        sfSprite_setTextureRect(sprite1, rect);

        sfVector2i cursor_coord;
        cursor_coord.x = 1280;
        cursor_coord.y = 769;
        sfMouse_setPosition(cursor_coord, window);
       
        //Affichage de la fenetre + sprites
        while (sfRenderWindow_isOpen(window)) {
                time = sfClock_getElapsedTime(clock);
                seconds = time.microseconds / 2500000.0;
                if (seconds > 0.10) {
                        if (rect.left == 220)
                                rect.left = 0;
                        else
                                rect.left += 110;
                       
                        sfSprite_setTextureRect(sprite1, rect);
                        sfClock_restart(clock);
                }
                sfSprite_move(sprite1, duck_vector);
                while (sfRenderWindow_pollEvent(window, &event)) {
                        if (event.type == sfEvtMouseMoved) {
                        }
                        if (event.type == sfEvtClosed)
                                sfRenderWindow_close(window);
                }
                sfSprite_setPosition(sprite1, duck_vector);
                if (sfSprite_getPosition(sprite1).x >= 1200) {
                        invers_duck.x = -invers_duck.x;
                        sfSprite_scale(sprite1, invers_duck);
                }
                if (sfSprite_getPosition(sprite1).x < -10) {
                        invers_duck.x = -invers_duck.x;
                        sfSprite_scale(sprite1, invers_duck);
                }
                sfRenderWindow_drawSprite(window, sprite, NULL);
                sfRenderWindow_drawSprite(window, sprite1, NULL);
                sfRenderWindow_drawSprite(window, cursor, NULL);
                sfRenderWindow_display(window);
        }
        sfRenderWindow_destroy(window);
        return (0);
}

Hello everyone,
I would like to change the default cursor of my mouse, by a chart because I am doing the game duck hunt. I did some stuff but it does not work ...
Can you help me ?
« Last Edit: November 24, 2017, 11:37:30 am by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: I want to make my cursor
« Reply #1 on: November 24, 2017, 11:33:48 am »
"it does not work" is not a problem description. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MOP

  • Newbie
  • *
  • Posts: 19
  • Expect the progress
    • View Profile
Re: I want to make my cursor
« Reply #2 on: November 24, 2017, 08:55:17 pm »
Hey luffy50660,

first of all, clear created "objects" and set it to NULL, to let dangling pointer no chance to act.

If i understand your problem right, you wanna use a custom mouse cursor picture, instead of the OS Cursor?

Code: [Select]
sfWindow_setMouseCursorVisible(window, sfFalse);
// Sets the OS Cursor invisible
...

sfSprite_setTexture(cursor, texture2, sfTrue);
// Sets your custom texture to the sprite
...

sfVector2i cursor_coord;
cursor_coord.x = 1280;
cursor_coord.y = 769;
sfMouse_setPosition(cursor_coord, window);
// Sets your invisible OS Mouse Cursor at application start to bottom right
// there is no sense in there
...

In your main loop, i dont find anything about update your cursor position.

------------------------------------

First set the Initial Position of Your Cursor Sprite and update it in your main loop.

I made some minimal running example.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <SFML/Audio.h>
#include <SFML/Graphics.h>
#include <SFML/Window.h>
#include <SFML/System.h>

int main()
{
    // Variables
    sfVideoMode mode = {600, 600, 32};
    sfEvent EventLoop;
    sfRenderWindow *screen;
    sfSprite *Cursor_Sprite;
    sfTexture *Cursor_Texture;
    sfVector2f Cursor_Position;

    // Init Stuff
    screen = sfRenderWindow_create(mode, "Minimal Example", sfClose, NULL);

    Cursor_Texture = sfTexture_createFromFile("MouseCursor.png", sfFalse);
    Cursor_Sprite = sfSprite_create();

    // Check created Stuff
    if(screen == NULL || Cursor_Sprite == NULL || Cursor_Texture == NULL)
        return 0;

    // Get Initial Position
    Cursor_Position.x = sfMouse_getPosition(screen).x;
    Cursor_Position.y = sfMouse_getPosition(screen).y;

    // Set Texture to Sprite
    sfSprite_setTexture(Cursor_Sprite, Cursor_Texture, sfFalse);
    sfSprite_setPosition(Cursor_Sprite, Cursor_Position);

    // Set Windows Cursor invisible
    sfRenderWindow_setMouseCursorVisible(screen, sfFalse);

    // Main Loop
    while (sfRenderWindow_isOpen(screen))
    {
        // Proceed Events
        while (sfRenderWindow_pollEvent(screen, &EventLoop))
        {
            if(EventLoop.type == sfEvtClosed)
                sfRenderWindow_close(screen);
        }

        // Update Stuff
        Cursor_Position.x = sfMouse_getPosition(screen).x;
        Cursor_Position.y = sfMouse_getPosition(screen).y;
        sfSprite_setPosition(Cursor_Sprite, Cursor_Position);

        // Draw Stuff
        sfRenderWindow_drawSprite(screen, Cursor_Sprite, NULL);

        // Display it
        sfRenderWindow_display(screen);

        // Clear Window
        sfRenderWindow_clear(screen, sfBlack);
    }

    // Clean up
    sfSprite_destroy(Cursor_Sprite);
    Cursor_Sprite = NULL;
    sfTexture_destroy(Cursor_Texture);
    Cursor_Texture = NULL;
    sfRenderWindow_destroy(screen);
    screen = NULL;

    return 0;
}

Hope that will solve your problem

Greetz MOP

@Exploiter
passing argument 1 of 'sfMouse_getPosition' from incompatible pointer type [-Wincompatible-pointer-types]

Please make an update for csfml, and make magic stuff with CPP.

I saw a topic about this at the year: on: 2012
https://en.sfml-dev.org/forums/index.php?topic=7761.msg51675#msg51675

Thats atleast 5 Years...

Is there no support anymore for the C Binding ? That makes me sad...
« Last Edit: November 24, 2017, 09:00:56 pm by MOP »
My Github Repo:
Snake
Deep Deep Deep in Development

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: I want to make my cursor
« Reply #3 on: November 27, 2017, 12:05:48 pm »
passing argument 1 of 'sfMouse_getPosition' from incompatible pointer type [-Wincompatible-pointer-types]

Please make an update for csfml, and make magic stuff with CPP.

I saw a topic about this at the year: on: 2012
https://en.sfml-dev.org/forums/index.php?topic=7761.msg51675#msg51675

Thats atleast 5 Years...

Is there no support anymore for the C Binding ? That makes me sad...

Yes, there is support for the C binding, through highly welcomed community contributions for example. If you see something that can be improved, we have a set of Contribution Guidelines which should also apply for CSFML.  ;)
SFML / OS X developer

MOP

  • Newbie
  • *
  • Posts: 19
  • Expect the progress
    • View Profile
Re: I want to make my cursor
« Reply #4 on: November 27, 2017, 11:16:02 pm »
Hey Hiura,

thanks for replay. As you saw in my last post, i wrote "magic stuff with CPP", in this answer you can see, i dont have any bit of knowledge of C++.

In my highly noobyness i would say, just append another Function with an other function argument in the Mouse.cpp ( https://github.com/SFML/CSFML/blob/master/src/SFML/Window/Mouse.cpp )
and rename the other function.
Use a generic define in Mouse.h File and go?

Code: [Select]
////////////////////////////////////////////////////////////
// Renamed
sfVector2i sfMouse_getPosition_WN(const sfWindow* relativeTo)
{
    sf::Vector2i sfmlPos;
    if (relativeTo)
        sfmlPos = sf::Mouse::getPosition(relativeTo->This);
    else
        sfmlPos = sf::Mouse::getPosition();

    sfVector2i position = {sfmlPos.x, sfmlPos.y};
    return position;
}

////////////////////////////////////////////////////////////
// with RenderWindow
sfVector2i sfMouse_getPosition_RW(const sfRenderWindow* relativeTo)
{
    sf::Vector2i sfmlPos;
    if (relativeTo)
        sfmlPos = sf::Mouse::getPosition(relativeTo->This);
    else
        sfmlPos = sf::Mouse::getPosition();

    sfVector2i position = {sfmlPos.x, sfmlPos.y};
    return position;
}

In the Mouse.h
Code: [Select]
...
CSFML_WINDOW_API sfVector2i sfMouse_getPosition_WN(const sfWindow* relativeTo); // The Normal Function
CSFML_WINDOW_API sfVector2i sfMouse_getPosition_RW(const sfRenderWindow* relativeTo); // The RenderWindow Function
...
#define sfMouse_getPosition(X) _Generic((X), sfWindow*: sfMouse_getPosition_WN, sfRenderWindow*: sfMouse_getPosition_RW)(X)
...

I just make a minimal example with the generic stuff
Code: [Select]
float Test1(const float* A)
{
    return A[0] + 2.0f;
}

int Test2(const int* B)
{
    return B[0] + 4;
}

#define Test(X) _Generic((X), float*: Test1, int*: Test2)(X)


int main (void)
{
    float Float_Array[2] = {5.0f, 0}; // just for testing a pointer type
    int Int_Array[2] = {10, 0}; // just for testing a pointer type
    printf("Test Float: %f\n", Test(Float_Array)); // Outputs 5.00 + 2.00 = 7.0000
    printf("Test Integer: %d\n", Test(Int_Array)); // Outputs 10 + 4 = 14
    return 0;
}

(compiled -std=c11)

Maybe theres a easier way in magic cpp. Not easy to oversee the billion of semantics in CPP.

Greetz MOP
My Github Repo:
Snake
Deep Deep Deep in Development

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: I want to make my cursor
« Reply #5 on: November 28, 2017, 07:37:49 am »
CSFML can't use C11 features, at least for now. But there's no need for "magic" there, you know the type of your window so just call the correct function -- which does exist, by the way: see the extra sfMouse_* functions in RenderWindow.h.
Laurent Gomila - SFML developer

MOP

  • Newbie
  • *
  • Posts: 19
  • Expect the progress
    • View Profile
Re: I want to make my cursor
« Reply #6 on: November 28, 2017, 09:46:44 am »
Hey Laurent,

thanks for feedback. That hint must be a secret ! :o
I should read some of the auto-suggestions exactlier if i type some functions in my editor ;-)

Greetz MOP
My Github Repo:
Snake
Deep Deep Deep in Development

AntonioMollica

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: I want to make my cursor
« Reply #7 on: September 01, 2018, 09:48:43 pm »
Hey Laurent,

thanks for feedback. That hint must be a secret ! :o
I should found gnc phen375 sale and  the auto-suggestions exactlier if i type some functions in my editor ;-)

Greetz MOP

I know this this post is old but please if you can let us know that did any of these worked or not and if worked then which one?
« Last Edit: October 10, 2020, 03:48:32 pm by AntonioMollica »