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

Author Topic: CSFML program structure  (Read 10309 times)

0 Members and 1 Guest are viewing this topic.

Merovingen

  • Newbie
  • *
  • Posts: 2
    • View Profile
CSFML program structure
« on: February 11, 2017, 08:30:09 am »
Hello! I'm use SFML along C++ and i  think it very pretty library to game dev. But i'm interested in use C binding, but i'm know C language not as good as C++ and i have few little questions. Sorry if it very stupid, but i want to understand.

So, first of all it is sfVector2f. For example, in SFML we have a choice - in sf::CircleShape of move method or setPosition method put only Vector2f:

    sf::RectangleShape gun(sf::Vector2f(10.f, 50.f));
    gun.setOrigin(sf::Vector2f(5.f, 45.f));
 

or 2 floats:

        bullet.setPosition(x, y);
        bullet.setOrigin(2,2);
 

In CSFML only sfVector2f in everywere? I forced to declared many vectors for size, for positions and other to setting my sprite or primitive:

    sfVector2f vec  = { 10, 10 };
    sfVector2f vec2 = { 30, 30 };

    // ...another many many definitions of vectors to other things in my program

    sfRectangleShape_setSize(shape, vec);
    sfRectangleShape_setPosition(shape, vec2);
 

Or exist any simple way to do it? I tryed coding like this:

    sfRectangleShape_setSize(shape, (sfVector2f) {10, 10});
 

But i think this code is ugly.  :-\

Second question - is memory allocation to many objects in programm. For example. In C++ i use std::list array to store my objects:

    std::list<Bullet *> bullet_list;

    // ...

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
    {
        if (fireTimer.getElapsedTime().asMilliseconds() > 200)
        {
            Bullet *bullet = new Bullet(gun.getPosition().x,
                                        gun.getPosition().y,
                                        gun.getRotation());
            bullet_list.push_back(bullet);
            fireTimer.restart();
        }
    }

    // ...

    for (auto i=bullet_list.begin(); i != bullet_list.end();)
    {
        Bullet *b = *i;

        // some actions with object

        if (!b->life) {
            i=bullet_list.erase(i);
            delete b;
        } else ++i;
    }
 

So easy and no one problems. May be i bad know C, but i'm no have idea how to do some one like this in C :) May be something wrong to design the program so and possible make it another? And it is possible to code applications and games in C and CSFML like a C++ and SFML? Thank you.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: CSFML program structure
« Reply #1 on: February 11, 2017, 08:55:49 am »
CSFML is not designed to be a friendly library for the end user, but more to be a simple bridge to build bindings for other languages. So yes, it probably lacks some convenience stuff that would make the C programmer's life easier.

But honestly, I don't know why one would use SFML directly in C. I assume you're forced to (for work or school maybe?).

Quote
I forced to declared many vectors for size, for positions and other to setting my sprite or primitive
C allows variable declaration in the middle of blocks since 1999 (the C99 standard). So you can declare vectors only where they are used, no need to have a huge list at the beginning of your function.

Quote
Second question - is memory allocation to many objects in programm. For example. In C++ i use std::list array to store my objects
The standard C library provides nothing to ease the use of lists or arrays. But there are many source codes or libraries that can help, just pick one and use it.
Laurent Gomila - SFML developer

Merovingen

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: CSFML program structure
« Reply #2 on: February 11, 2017, 05:25:09 pm »
Laurent, thank you for answer! And special thanks for SFML, it perfect lib, people like you are worthy of admiration.  ;)

I assume you're forced to (for work or school maybe?).

No, it only research interest. I like C, and i try to use it with different libraries, discover new in language.

C allows variable declaration in the middle of blocks since 1999 (the C99 standard).

The standard C library provides nothing to ease the use of lists or arrays. But there are many source codes or libraries that can help, just pick one and use it.

Ok, Thanks! It very helpful for me.

Turbine

  • Full Member
  • ***
  • Posts: 102
    • View Profile
Re: CSFML program structure
« Reply #3 on: March 11, 2017, 07:31:41 am »
You could just use C for your game logic and C++ for SFML - if you really want to learn C. Remember, C is very simple and doesn't do a lot. SFML would be very complex if it wasn't for the higher-level C++ bindings?