Ok, so here is a
compile-able piece of code. The movement is glitchy, but whatever, the image draws on screen just fine. The black box issue comes in whenever I give the sprite flashlight its own class, which is why I had all the random bits of code.
#include "stdafx.h"
#include "SFML\Graphics.hpp"
int _tmain(int argc, _TCHAR* argv[])
{
sf::RenderWindow window;
window.create(sf::VideoMode(500, 500, 32), "Test");
sf::Sprite flashlight;
sf::Texture texture;
texture.loadFromFile("images/light.png");
flashlight.setTexture(texture);
const sf::Vector2i MAX_FLASHLIGHT_VELOCITY = sf::Vector2i(5, 5);
sf::Vector2f flashlightVelocity(0, 0);
while(window.isOpen()){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
flashlightVelocity.x -= 1;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
flashlightVelocity.x += 1;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
flashlightVelocity.y += 1;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
flashlightVelocity.y -= 1;
}
if(flashlightVelocity.x > MAX_FLASHLIGHT_VELOCITY.x){
flashlightVelocity.x = MAX_FLASHLIGHT_VELOCITY.x;
}
if(flashlightVelocity.x < MAX_FLASHLIGHT_VELOCITY.x * -1){
flashlightVelocity.x = MAX_FLASHLIGHT_VELOCITY.x * -1;
}
if(flashlightVelocity.y > MAX_FLASHLIGHT_VELOCITY.y){
flashlightVelocity.y = MAX_FLASHLIGHT_VELOCITY.y;
}
if(flashlightVelocity.y < MAX_FLASHLIGHT_VELOCITY.y * -1){
flashlightVelocity.y = MAX_FLASHLIGHT_VELOCITY.y * -1;
}
flashlight.move(flashlightVelocity);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
window.clear();
window.draw(flashlight);
window.display();
}
return 0;
}
Like I said, that works. This will be long, but here is the full code of the flashlight class just so you guys can see what I am working with:
flashlight.h
#include "stdafx.h"
#include "SFML\Graphics.hpp"
#pragma once
#include "stdafx.h"
#include "ImageManager.h"
class Flashlight{
public:
Flashlight();
~Flashlight();
//individual functions for independent collision movement, set both x and y for regular movement
void AddVelocityX(float increment);
void AddVelocityY(float increment);
void AddVelocity(sf::Vector2f increment);
void SetVelocityX(float velocity);
void SetVelocityY(float velocity);
void SetVelocity(sf::Vector2f velocity);
sf::Vector2f &GetVelocity();
sf::Sprite &GetLight();
sf::Sprite &GetShader();
private:
void CheckVelocities();
//class objects
ImageManager imgr;
float velocityX, velocityY;
sf::Sprite light;
sf::Sprite shader;
};
flashlight.cpp
#include "stdafx.h"
#include "Flashlight.h"
const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;
const sf::Vector2i MAX_FLASHLIGHT_VELOCITY = sf::Vector2i(5, 5);
Flashlight::Flashlight(){
//light.setOrigin(light.getGlobalBounds().width / 2, light.getGlobalBounds().height / 2);
light.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
light.setTexture(imgr.GetImage("images/light.png"));
shader.setOrigin(SCREEN_WIDTH, SCREEN_HEIGHT);
shader.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
light.setTexture(imgr.GetImage("images/shader.png"));
velocityX = 0;
velocityY = 0;
}
Flashlight::~Flashlight(){
}
void Flashlight::AddVelocityX(float increment){
velocityX += increment;
}
void Flashlight::AddVelocityY(float increment){
velocityY += increment;
}
void Flashlight::AddVelocity(sf::Vector2f increment){
velocityX += increment.x;
velocityY += increment.y;
}
void Flashlight::SetVelocityX(float velocity){
velocityX = velocity;
}
void Flashlight::SetVelocityY(float velocity){
velocityY = velocity;
}
void Flashlight::SetVelocity(sf::Vector2f velocity){
velocityX = velocity.x;
velocityY = velocity.y;
}
sf::Vector2f &Flashlight::GetVelocity(){
if(velocityX > MAX_FLASHLIGHT_VELOCITY.x){
velocityX = MAX_FLASHLIGHT_VELOCITY.x;
}
if(velocityX < MAX_FLASHLIGHT_VELOCITY.x * -1){
velocityX = MAX_FLASHLIGHT_VELOCITY.x * -1;
}
if(velocityY > MAX_FLASHLIGHT_VELOCITY.y){
velocityY = MAX_FLASHLIGHT_VELOCITY.y;
}
if(velocityY < MAX_FLASHLIGHT_VELOCITY.y * -1){
velocityY = MAX_FLASHLIGHT_VELOCITY.y * -1;
}
return sf::Vector2f(velocityX, velocityY);
}
void Flashlight::CheckVelocities(){
if(velocityX >= MAX_FLASHLIGHT_VELOCITY.x){
velocityX = MAX_FLASHLIGHT_VELOCITY.x;
}
if(velocityY >= MAX_FLASHLIGHT_VELOCITY.y){
velocityY = MAX_FLASHLIGHT_VELOCITY.y;
}
}
sf::Sprite &Flashlight::GetLight(){
return light;
}
sf::Sprite &Flashlight::GetShader(){
return shader;
}
I draw these sprites the exact same way as I did in the compile-able code:
window.clear(sf::Color::Black);
window.draw(flashlight.GetLight());
window.draw(flashlight.GetShader());
window.display();
These functions can be seen in the flashlight.cpp code. Sorry that's a little long, it's just hard to find a balance between too much code and incomplete code. Please continute to bear with me.