I just put SFML on my computer and am learning its commands and such. I'm having two big issues. First, I'm using Visual Studio Express 2010. I followed the tutorial on this site for setting it up. I'm running it as a win32 Console application so I can avoid the console window popping up. I've included the sfml-main library. I'm using SFML 1.6.
First problem I'm having is calling screen.Clear() (screen is my sf::RenderWindow) causes the program to crash. I know this because if I comment it out, the program will run fine.
Second, when I try to load the image for my 'player' to get something on the screen, it just hangs up when trying to launch the program or it causes a crash. I think there is an issue with loading the image. I've tried .bmp and .png. If I comment that section out it will work fine (also having Clear() commented out.
It will also sometimes exit, but the program will still be running in the background.
Please help me know what I'm doing wrong.
Here is code:
main.cpp
#include <SFML/Graphics.hpp>
#include "player.h"
int main()
{
sf::RenderWindow screen(sf::VideoMode(sf::VideoMode::GetMode(0)), "Testing", sf::Style::Fullscreen);
sf::Event events;
Player player;
bool running = true;
while(running == true)
{
while(screen.GetEvent(events))
{
if(events.Type == sf::Event::Closed)
{
running = false;
}
if(events.Type == sf::Event::KeyPressed)
{
if(events.Key.Code == sf::Key::Escape)
{
running = false;
}
}
player.MoveTowards(float(events.MouseMove.X), float(events.MouseMove.Y));
}
if(screen.IsOpened())
{
screen.Clear();
screen.Draw(player.GetSprite());
screen.Display();
}
}
screen.Close();
return 0;
}
player.h
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
class Player
{
private:
sf::Image playerImage;
sf::Sprite playerSprite;
float xPos;
float yPos;
float width;
float height;
float heading;
float xVel;
float yVel;
public:
Player();
Player(float xStart, float yStart);
void MoveTowards(float xTarget, float yTarget);
sf::Sprite GetSprite();
};
player.cpp
#include "player.h"
#include <cmath>
const float RAD2DEGREE = 57.3248;
const float DEGREE2RAD = .0174444444;
Player::Player()
{
width = 64;
height = 64;
if(playerImage.LoadFromFile("player.bmp"))
{
playerSprite.SetImage(playerImage);
}
xPos = 1680 / 2 + width / 2;
yPos = 1050 / 2 + height / 2;
playerSprite.SetPosition(xPos, yPos);
xVel = 20;
yVel = 20;
}
Player::Player(float startX, float startY)
{
}
void Player::MoveTowards(float xTarget, float yTarget)
{
heading = atan2(yTarget - yPos, xTarget - xPos) * RAD2DEGREE;
float xDirect = cos(heading * DEGREE2RAD) * RAD2DEGREE;
float yDirect = sin(heading * DEGREE2RAD) * RAD2DEGREE;
xPos += xVel * xDirect;
yPos += yVel * yDirect;
if(xPos < 0)
{
xPos = 0;
}
if(xPos + width > 1050)
{
xPos = 1650 - width;
}
if(yPos < 0)
{
yPos = 0;
}
if(yPos + height > 1680)
playerSprite.SetRotation(heading);
playerSprite.SetPosition(xPos, yPos);
}
sf::Sprite Player::GetSprite()
{
return playerSprite;
}