Hi there,
This is my first C++ Programm and my first Project with SFML and i want to build a 2D "Chessfield" like game, where each Fields in my GameArea has its own Sprite.
Later on the GameArea should know which Fields are visible to the player and therefore draw only these that should be drawn. Currently my GameArea consists of a 10 x 10 Field array (wich is stored one dimensional because i couldnt get a dynamic 2 dimensional array running in c++) each Field has a draw function requireing the RenderWindow object and a position Vector.
The GameArea has a Draw function on its own which should decide wich fields to call and wich position to give these Sprite.
Because its my first Project i worked with the XCode Template and use the same Ressources (the img) and only tried to apply my Strukture to is.
Now i get a EXC_BAD_ACCESS (code=EXC_I386_GPFLT) Message in my Fields draw function.
Im Working on my Mac Book Pro with Maverick and the CLANG package of SFML.
To give better understanding of my code i upload the whole Solution named GoS (Game of Stones)
i have no clue what I'm doing wrong and for these who aren't interested in downloading a XCode solution i will paste my 2 classes and their .h files in Code sections below.
my main.cpp
[/font]
int main(int, char const**)
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(1200, 600), "SFML window");
GameArea area(10,10);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
window.clear();
area.draw(&window);
window.display();
}
return EXIT_SUCCESS;
}
[/font]
field.h
[/font]
#include <iostream>
#include <SFML/Graphics.hpp>
class Field
{
public:
Field(sf::Texture *texture, sf::IntRect rect);
~Field();
void draw(sf::RenderWindow *window, sf::Vector2f position);
private:
sf::Sprite _sprite;
sf::Texture *_texture;
};
[/font]
field.cpp
[/font]
#include "Field.h"
Field::Field(sf::Texture *texture, sf::IntRect rect)
: _sprite(), _texture(texture)
{
_sprite.setTexture(*_texture);
_sprite.setTextureRect(rect);
}
Field::~Field()
{
}
void Field::draw(sf::RenderWindow *window, sf::Vector2f position)
{
_sprite.setPosition(position);
window->draw(_sprite);
}
[/font]
GameArea.h
[/font]
#include <iostream>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"
#include "Field.h"
class GameArea{
public:
GameArea(int x, int y);
~GameArea();
void draw(sf::RenderWindow *window);
private:
int _x, _y;
Field *_fields;
sf::Texture _texture;
};
[/font]
GameArea.cpp
[/font]
#include "GameArea.h"
GameArea::GameArea(int x, int y)
: _texture()
{
if (!_texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
return;
}
_fields = (Field *)malloc(x * y * sizeof (Field *));
for (int i = 0; i < x * y; i++) {
_fields[i] = Field(&_texture, sf::IntRect(0, 0, 32, 32));
}
_x = x;
_y = y;
}
GameArea::~GameArea()
{
free(_fields);
}
void GameArea::draw(sf::RenderWindow *window)
{
int x, y;
for (int i = 0; i < _x * _y; i++) {
y = i % _y;
x = i / _y;
_fields[i].draw(window,sf::Vector2f(x * 32, y * 32));
}
}
p.s.: sry i dont know how to get spoiler tags to work so bear with the whole load of sourcecode
edit: i had to remove the img used as texture becouse of the size limitation