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

Author Topic: Problem with image loading with arrays.  (Read 2550 times)

0 Members and 1 Guest are viewing this topic.

Ghostly

  • Newbie
  • *
  • Posts: 1
    • View Profile
Problem with image loading with arrays.
« on: August 27, 2012, 02:22:55 am »
Hello, I'm new to using the SFML API, and been using C++ for just a few months, with other graphics APIs, however, I've just started using arrays, and I'm having problems loading my images.

What I want to do is have an array of an specific image type (i.e. tiles) to be able to load them at start-up, and use them at will, by simply using imgTiles[tileimagenum], but I'm stuck at the loading. I keep getting this error at run-time:
http://i.imgur.com/rJzHe.png

And the Autos window, looks like this:
http://i.imgur.com/v1QSQ.png

Here's the code that I'm using:
Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>

// Image Variables //
sf::Image * imgTile;
const int tileCount = 2;

// Image Functions //
void loadImages(void)
{
std::stringstream ss;

// Tile Images //
imgTile = new sf::Image[tileCount];
for (int i = 1; i <= tileCount; i++)
{
ss << "\\Content\\Graphics\\Tiles\\" << i << ".png";
imgTile[i].LoadFromFile(ss.str());
ss.str().clear();
}
}

void destroyImages(void)
{
delete [] imgTile; imgTile = NULL;
}

// Main Function //
int main(int argc, char *argv[])
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
   
// Load our images.
loadImages();

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
destroyImages();
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}

I hope I can get some help on this, and if there's a better way of doing it, please let me know.

Regards,
Ghostly.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with image loading with arrays.
« Reply #1 on: August 27, 2012, 03:09:36 am »
Use vectors, use raii, what you do is not c++ way and this problem is not sfml one.
imgTile = new sf::Image[tileCount];
 
Allocate array from 0 to tileCount-1.
for (int i = 1; i <= tileCount; i++)
Use elements from 1 to tileCount.
How could that work?
Back to C++ gamedev with SFML in May 2023

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: Problem with image loading with arrays.
« Reply #2 on: August 27, 2012, 06:18:13 am »
What is wrong with using static-sized arrays? If you know you have a fixed amount of elements or will not surpass a certain amount, why use vectors? How is that not "the C++ way"?

Using arrays is fine, but correct, the for loop should look like this:
for (int i = 0; i < tileCount; i++)

The first element of the array is at imgTile[0]. The consequence is that the highest accessible index is at tileCount - 1.

Array access is nothing but a pointer addition. If you access element imgTile[1], it's the same as *(imgTile + 1), ie the second element in the array.
JSFML - The Java binding to SFML.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with image loading with arrays.
« Reply #3 on: August 27, 2012, 08:55:49 am »
Global variables and doing initialization and clean up with global functions looks C. That's what classes, ctors and dtors are for.
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with image loading with arrays.
« Reply #4 on: August 27, 2012, 09:32:28 am »
What is wrong with using static-sized arrays? If you know you have a fixed amount of elements or will not surpass a certain amount, why use vectors? How is that not "the C++ way"?
He uses a dynamic array allocated with new[]. There is almost never a reason to prefer it over std::vector, as it is more error-prone, requires manual management and doesn't offer the utilities of STL containers.

By the way, static arrays shouldn't be used either. std::array replaces them and fixes many of their problems, without being slower.

There are further mistakes, for example the if statement in the event loop doesn't use curly brackets, and the stringstream isn't cleared correctly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: