All right so I am trying to create a program that draws a picture of A when the user presses A. I havn't gotten anywhere near that far but just so you understand what the end goal is. Anyways I tried to make a function called DrawKey(string key)
I have it set up with no errors the issue is it's not drawing the sprite. I have no idea where I went wrong please help.
static void DrawKey(string key)
{
sf::RenderWindow Key(sf::VideoMode(800, 600), "A");
sf::Style::None;
while (Key.isOpen())
{
//Sprites
map<string, sf::Sprite> Sprite;
map<string, sf::Texture> Tex;
sf::Texture ATex;
ATex.loadFromFile("Assets/Images/A.jpg");
sf::Texture DTex;
DTex.loadFromFile("Assets/Images/D.jpg");
sf::Texture ETex;
ETex.loadFromFile("Assets/Images/E.jpg");
sf::Texture QTex;
sf::Texture STex;
QTex.loadFromFile("Assets/Images/Q.jpg");
STex.loadFromFile("Assets/Images/S.jpg");
sf::Texture WTex;
WTex.loadFromFile("Assets/Images/W.jpg");
sf::Texture XTex;
XTex.loadFromFile("Assets/Images/X.jpg");
sf::Texture ZTex;
ZTex.loadFromFile("Assets/Images/Z.jpg");
sf::Texture EscTex;
EscTex.loadFromFile("Assets/Images/esc.jpg");
Tex["A", "D", "E", "Q", "S", "W", "X", "Z", "Esc"] = ATex, DTex, ETex, QTex, STex, WTex, XTex, ZTex, EscTex;
sf::Sprite ASprite;
ASprite.setTexture(ATex);
sf::Sprite DSprite;
ASprite.setTexture(Tex["D"]);
sf::Sprite ESprite;
ASprite.setTexture(Tex["E"]);
sf::Sprite QSprite;
ASprite.setTexture(Tex["Q"]);
sf::Sprite SSprite;
ASprite.setTexture(Tex["S"]);
sf::Sprite WSprite;
ASprite.setTexture(Tex["W"]);
sf::Sprite XSprite;
ASprite.setTexture(Tex["X"]);
sf::Sprite ZSprite;
ASprite.setTexture(Tex["Z"]);
sf::Sprite EscSprite;
ASprite.setTexture(Tex["Esc"]);
Sprite["A", "D", "E", "Q", "S", "W", "X", "Z", "Esc"] = ASprite, DSprite, ESprite, QSprite, SSprite, WSprite, XSprite, ZSprite, EscSprite;
// Process events
sf::Event event;
while (Key.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
Key.close();
}
// Clear screen
Key.clear(sf::Color::Transparent);
Key.draw(Sprite[key]);
Key.display();
}
Yes i know I can make it write a letter I want it as a image. Thanks for your help i'm kind of new to all of this.
The comma operator in C++ is a common trip-up point for a lot of beginners. It evaluates all expressions in it (and takes into account their side effects) but only returns the right-most value.
Sprite["A", "D", "E", "Q", "S", "W", "X", "Z", "Esc"] = ASprite, DSprite, ESprite, QSprite, SSprite, WSprite, XSprite, ZSprite, EscSprite;
is equivalent (since none of the operands to the various comma expressions have side effects) to:
Sprite["Esc"] = EscSprite;
I'm curious if hitting Esc
draws the EscSprite
as expected.
You make ask, "Well why doesn't the program crash when I try to access the "A" key from the map?
Key.draw(Sprite[key]);
The answer is that the []
operator on a std::map
will retrieve the value at that key, unless that key does not exist, in which case it will default construct the value and return that.