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

Author Topic: Fill Array with Shapes without using vectors  (Read 2093 times)

0 Members and 1 Guest are viewing this topic.

Kazz

  • Newbie
  • *
  • Posts: 3
    • View Profile
Fill Array with Shapes without using vectors
« on: May 20, 2017, 05:26:06 pm »
Hello

My question is simple :

I would like to create a matrix of Shapes without using vector or any c++ stuff:

This code crashes :
sf::CircleShape** fillHexMap(int size) {
        sf::CircleShape **hex = NULL;
        hex = (sf::CircleShape **) malloc(size * sizeof(sf::CircleShape*));
        int i, j;

        for (i = 0; i < size; i++)
                hex[i] = (sf::CircleShape *) malloc(size * sizeof(sf::CircleShape));

        for (i = 0; i < size; i++) {
                for (j = 0; j < size; j++) {
                        hex[i][j] = sf::CircleShape::CircleShape(ROWSIZE, 6);
                }
        }

        return hex;
}

//Initialisation would be like but it crashes :

sf::CircleShape** matrix = fillHexMap(SIZE);
 


I just want to create this matrix like I can do for a matrix of int :

int** fillTabMap(int size) {
        int **tab = NULL;
        tab = (int **) malloc(size * sizeof(int*));
        int i, j;

        for (i = 0; i < size; i++)
                tab[i] = (int*)malloc(size * sizeof(int));

        for (i = 0; i < size; i++) {
                for (j = 0; j < size; j++) {
                        tab[i][j] = 0;
                }
        }

        return tab;

}

//Initialisation is and it works :

int** matrix = fillTabMap(SIZE);
 

Thx for help !

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Fill Array with Shapes without using vectors
« Reply #1 on: May 20, 2017, 06:10:17 pm »
or any c++ stuff:
Maybe use SFML then? :P

You're not doing yourself any favors by avoiding C++ containers and practices.
If you want to write C, you shouldn't try and go with a trade off, of using C++ libraries, they don't mix (well).

Additionally, it's fine to put some restriction up on yourself for fun and playing around, but once you have to ask others to spend their free time fixing your problems due to the abscure restrictions, then you really have to ask yourself if that's fair.

This code crashes :
This is not a problem description.
You have a debugger that can output where exactly it crashes and why. I even suspect that this doesn't even compile, because I don't think you can call a constructor like that.

Additionally, I have no idea if you can safely construct C++ classes with malloc. If anything, you could allocate the space and then maybe use placement-new (oh no C++ feature) to create the class in place.

Again, if you want to write C, then use the CSFML binding.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Fill Array with Shapes without using vectors
« Reply #2 on: May 20, 2017, 06:48:05 pm »
Quote
I have no idea if you can safely construct C++ classes with malloc. If anything, you could allocate the space and then maybe use placement-new (oh no C++ feature) to create the class in place.
That's pretty accurate for someone who has no idea ;D

This is indeed the problem, malloc doesn't call the constructor. Well, actually the real problem is that you try to use a C++ library in C code. You can't do that. Either use a C library, or write real C++.
Laurent Gomila - SFML developer

Kazz

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Fill Array with Shapes without using vectors
« Reply #3 on: May 20, 2017, 07:30:43 pm »
Thx for answers

I'll try to use CSFML but unfortunately it's not documented much, on YT or SFML website..

First I'll try to install it

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Fill Array with Shapes without using vectors
« Reply #4 on: May 20, 2017, 09:45:46 pm »
CSFML is a binding, which means that it has the exact same API, just with a syntax adapted to the target language. So the C++ documentation and tutorials still apply.
Laurent Gomila - SFML developer

Kazz

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Fill Array with Shapes without using vectors
« Reply #5 on: May 20, 2017, 09:48:10 pm »
Seems like there is no way to install CSFML on Visual Studio or CodeBlock

Visual Studio always return me "cannot open file 'csfml-graphics.lib'"

Is it normal that there is no csfml-xxx-d  dll file ?


EDIT: I finaly succeed to install on CodeBlock, but sfRenderWindow_clear(window, sfBlack); won't work, like if sfBlack wasn't recognize

EDIT2 : I also installed it on visual studio, now working correctly, I'm converting my old c++ code, in c one
« Last Edit: May 21, 2017, 12:05:36 am by Kazz »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Fill Array with Shapes without using vectors
« Reply #6 on: May 21, 2017, 02:06:32 pm »
If you have new questions with CSFML in the future, please ask them in the dedicated sub-forum instead of continuing this thread.
Laurent Gomila - SFML developer