Hi guys, I'm new to SFML and only a novice at c++. In order to practice I was trying to make my own 2d game. I was trying to make it so the background is a tiled background and use a loop to render each one onto the screen since its 11x9 so i didnt have to individually set each sprite. But this loop is giving me the error First-chance exception at 0x003a6618 in GameTest.exe: 0xC0000005: Access violation reading location 0x60023e54. The code itself compiles and runs and when the game gets to the point
for (int y = 0; y < cellsY; y++)
{
for (int x = 0; x < cellsX; x++)
{
broken(broke);
broke++;
bgTile[x][y] = *sPointer;
bgTile[x][y].setPosition((x*64.f), (y*64.f));
}
}
is when i get the error, after the two loops have been run. (the console reads that its breaking after the 99th break output which would mean it has to be breaking on
bgTile[x][y] = *sPointer;
bgTile[x][y].setPosition((x*64.f), (y*64.f));
heres the entire file so you can see what i did wrong since that one snippet probably isnt a major help
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#define cellsX 11
#define cellsY 9
#define cellSize 64
int gameState = 0;
void broken(int breakNum)
{
std::cout << "Break : " << breakNum << "." << std::endl;
return;
}
int main()
{
// Create our window
sf::RenderWindow window(sf::VideoMode(cellsX*cellSize, cellsY*cellSize), "Game Test");
sf::Texture grassTile;
sf::Texture mainChar;
sf::Sprite bgTile[cellsX-1][cellsY-1];
sf::Sprite mnChar;
sf::Sprite gTile;
sf::Sprite * sPointer;
sPointer = &gTile;
if(!grassTile.loadFromFile("grass.png"))
{
return 0;
}
if (!mainChar.loadFromFile("mainchar.png"))
{
return 0;
}
mnChar.setTexture(mainChar);
mnChar.setPosition(5.f*cellSize, 4.f*cellSize);
gTile.setTexture(grassTile);
gTile.setPosition(0.f, 0.f);
int broke = 1;
for (int y = 0; y < cellsY; y++)
{
for (int x = 0; x < cellsX; x++)
{
broken(broke);
broke++;
bgTile[x][y] = *sPointer;
bgTile[x][y].setPosition((x*64.f), (y*64.f));
}
}
sf::Clock updateClock;
sf::Time updateTime = sf::milliseconds(333);
//Starts the game loop
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
broken(broke);
broke++;
if (updateClock.getElapsedTime().asMilliseconds() >= updateTime.asMilliseconds())
{
switch (gameState)
{
case 0:
{
broken(broke);
broke++;
for (int y = 0; y < cellsY; y++)
{
for (int x = 0; x < cellsX; x++)
{
window.draw(bgTile[x][y]);
broken(broke);
broke++;
}
}
window.draw(mnChar);
broken(broke);
broke++;
}
}
window.display();
}
}
}
return 0;
}
Please, i only need help fixing the one access violation. Please dont try to correct the sloppyness of my coding, this is all me screwing around with c++ after moving from c# and learning sfml. the codes going to get cleaner once i get all this simple crap down