4
« on: March 09, 2009, 04:17:05 am »
Hey guys, I'm having a similar problem, but different (not the joystick issue). The difference is that my code will not run UNLESS I omit a few lines of code from it, and I don't see why those few lines would be a problem to the program. I've ran the same code in other instances (drafts, etc) and it has ran without problem.
I know there are two areas within the code that draws santa and the block twice, that isn't the problem, I've changed it around when trying to run the program - to no avail.
Here's the problematic code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include "Collision.h"
#include "Map.h"
using namespace std;
using namespace sf;
int main()
{
//Load collision
Collision coll;
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");
// Load the sprite image from a file
sf::Image imgBlock;
if (!imgBlock.LoadFromFile("blocks.jpg"))
return EXIT_FAILURE;
sf::Image imgSanta;
if (!imgSanta.LoadFromFile("santa.png"))
return EXIT_FAILURE;
// Create the sprite
sf::Sprite block(imgBlock);
sf::Sprite santa(imgSanta);
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
// Get elapsed time
float ElapsedTime = App.GetFrameTime();
/*///////////////////////////////////////////////
//Declare map & level variables
MAP map;
char level[1600][640];
//Open file stream;
ifstream file;
//Load
map.mapLoad(file, level);
//num holder
int num;
//Display
for(int y = 0; y < 1600; y++)
{
for(int x = 0; x < 640; x++)
{
num = level[y][x];
if(num == '1')
{
block.SetPosition(x*32, y*32);
App.Draw(block);
}
if(num == '2')
{
App.Draw(santa);
}
}
}
////////////////////////////////// */
if (App.GetInput().IsKeyDown(sf::Key::Left))
{
if(coll.detectCollision(santa, block) != true)
{
santa.Move(-100 * ElapsedTime, 0);
}
else
{
santa.SetPosition((santa.GetPosition().x + 3), (santa.GetPosition().y));
}
}
if (App.GetInput().IsKeyDown(sf::Key::Right))
{
if(coll.detectCollision(santa, block) != true)
{
santa.Move(+100 * ElapsedTime, 0);
}
else
{
santa.SetPosition((santa.GetPosition().x - 3), (santa.GetPosition().y));
}
}
if (App.GetInput().IsKeyDown(sf::Key::Up))
{
if(coll.detectCollision(santa, block) != true)
{
santa.Move(0, -100 * ElapsedTime);
}
else
{
santa.SetPosition((santa.GetPosition().x), (santa.GetPosition().y) + 3);
}
}
if (App.GetInput().IsKeyDown(sf::Key::Down))
{
if(coll.detectCollision(santa, block) != true)
{
santa.Move(0, 100 * ElapsedTime);
}
else
{
santa.SetPosition((santa.GetPosition().x), (santa.GetPosition().y) - 3);
}
}
// Rotate the sprite
if (App.GetInput().IsKeyDown(sf::Key::A)) santa.Rotate(- 100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::D)) santa.Rotate(+ 100 * ElapsedTime);
// Clear screen
App.Clear();
// Display sprite in our window
App.Draw(santa);
App.Draw(block);
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
Here's the code that won't make the program run unless omitted (its in the code above):
/*///////////////////////////////////////////////
//Declare map & level variables
MAP map;
char level[1600][640];
//Open file stream;
ifstream file;
//Load
map.mapLoad(file, level);
//num holder
int num;
//Display
for(int y = 0; y < 1600; y++)
{
for(int x = 0; x < 640; x++)
{
num = level[y][x];
if(num == '1')
{
block.SetPosition(x*32, y*32);
App.Draw(block);
}
if(num == '2')
{
App.Draw(santa);
}
}
}
////////////////////////////////// */
Map class, if that's the issue:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <fstream>
using namespace sf;
using namespace std;
#define HEIGHT 1600;
#define WIDTH 600;
class MAP
{
public:
void mapLoad(ifstream& file, char level[1600][640]);
};
void MAP::mapLoad(ifstream &file, char level[1600][640])
{
//Input map file
file.open("level.txt");
//Holds the information line by line
std::string line;
//Height of the file
int fileHeight = 0;
while(std::getline(file, line))
{
for(int fileWidth = 0; fileWidth < 10; fileWidth++)
{
level[fileHeight][fileWidth] = line.at(fileWidth);
}
fileHeight++;
}
//Close file stream
file.close();
}
All help is appreciated!