Question: I'm having issues making a NPN Transistor in SFML C++. I have a photo of the Transistor below and the letters must be shown in
I'm having issues making a NPN Transistor in SFML C++. I have a photo of the Transistor below and the letters must be shown in the program. Below that is all of my the program I have already done. Everything works except the NPNTrans.cpp but if anything needs to be adjusted please post it in the answer section. If you need the entire program please let me know for now I'm posting the main.cpp, the NPNTrans.cpp and the NPNTrans.h included in the program.

Main.cpp
#include "CircuitElement.h"
#include "Wire.h" #include "Lamp.h" #include "Battery.h" #include "NPNTrans.h"
#include
#include
using namespace std;
int main() { int x, y;
// A 2 dimensional array of CircuitElement pointers. Array // elements correspond to a location on the conceptual grid // in the SFML drawing window. const int ROWS = 10; const int COLUMNS = 10; CircuitElement* element[COLUMNS][ROWS];
// Files stream for reading in the user's text file describing how // to draw a circuit diagram. ifstream diagramFile;
// Temporary variable need to hold the values read in from the user's // diagram text file. string elementType; int elementX, elementY; string elementConnections;
// Initalize the the CircuitElement* array to all NULL. NULL // array elements indicate that no CircuitElement should be // drawn. for(x = 0; x
// Open the user's diagram text file. diagramFile.open("diagram.txt");
// Check for file errors if(!diagramFile.is_open()) { cout
// Loop through the file reading in one line at a time. // // Seperated by spaces, each line must specify an element // type, column on the grid, row on the grid, and connections. // For example: // // battery 1 1 SN // // Provided elements types are: battery, wire, and lamp. // Teams can choose the names of the elements they will be // adding. // // Possible element connections values are: NS, NE*, NW*, // SN, SE*, SW*, EN*, ES*, EW, WN*, WS*, WE,and ALL*. // (Values denoted with are * only valid for wire elements.)
while(!diagramFile.eof()) { diagramFile >> elementType >> elementX >> elementY >> elementConnections;
if(elementType == "battery") { element[elementX][elementY] = new Battery(elementConnections, "Battery", "Bx"); } else if(elementType == "lamp") { element[elementX][elementY] = new Lamp(elementConnections, "Lamp", "Lx"); } else if(elementType == "wire") { element[elementX][elementY] = new Wire(elementConnections); } else if(elementType == "Transistor") { element[elementX][elementY] = new NPNTrans(elementConnections, "NPNTrans", "NP"); } }
// Close the user diagram text file diagramFile.close();
// Create the SFML drawing window. sf::RenderWindow window(sf::VideoMode((100 * COLUMNS), (100 * ROWS)), "SFML works!");
while (window.isOpen()) { // Check to see if the user close the window. sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); }
// Clear the window to all white window.clear(sf::Color::White);
// Loop through the columns and rows of the the CircuitElement array. // If the give array element is not NULL update the elements grid position // and pass the element to the window's draw function. for(x = 0; x setGridPosition(sf::Vector2f(x, y)); window.draw(*(element[x][y])); } } }
// Display the circuit elements in the window. window.display(); }
return 0; }
_____________________________________________________________________________
NPNTrans.cpp
#include
#include
#include "Arc.h"
#include "NPNTrans.h"
NPNTrans::NPNTrans() {
} NPNTrans::NPNTrans(const std::string& inConnections, const std::string inLabel, const std::string inId) { setConnections(inConnections); setLabel(inLabel); setId(inId); } void NPNTrans::draw(sf::RenderTarget& target, sf::RenderStates states) const { sf::VertexArray vertices;
// apply the transform states.transform *= getTransform();
// our particles don't use a texture states.texture = NULL;
// draw the vertex arrays
sf::CircleShape circle(30); circle.setPosition(-30,-10); circle.setFillColor(sf::Color::White); circle.setOutlineThickness(4); circle.setOutlineColor(sf::Color::Black);
target.draw(circle, states);
sf::RectangleShape line(sf::Vector2f(50,2)); line.setPosition(-25,10); line.setFillColor(sf::Color::Black); line.setOutlineThickness(1); line.setOutlineColor(sf::Color::Black); line.rotate(0);
target.draw(line, states);
sf::RectangleShape line2(sf::Vector2f(50,2)); line.setPosition(0,-40); line.setFillColor(sf::Color::Black); line.setOutlineThickness(1); line.setOutlineColor(sf::Color::Black); line.rotate(90);
target.draw(line, states);
sf::RectangleShape line3(sf::Vector2f(30,2)); line.setPosition(-10,-10); line.setFillColor(sf::Color::Black); line.setOutlineThickness(1); line.setOutlineColor(sf::Color::Black); line.rotate(25);
target.draw(line3, states);
sf::RectangleShape line4(sf::Vector2f(30,2)); line.setPosition(-30,-30); line.setFillColor(sf::Color::Black); line.setOutlineThickness(1); line.setOutlineColor(sf::Color::Black); line.rotate(-25);
target.draw(line4, states);
sf::CircleShape triangle(30, 3); circle.setPosition(-40,-20); circle.setFillColor(sf::Color::Black); circle.setOutlineThickness(4); circle.setOutlineColor(sf::Color::Black);
vertices.clear(); vertices.setPrimitiveType(sf::TrianglesStrip);
vertices.append(sf::Vertex(sf::Vector2f( 50, 2),sf::Color::Black)); vertices.append(sf::Vertex(sf::Vector2f( 50, -2),sf::Color::Black)); vertices.append(sf::Vertex(sf::Vector2f( 10, 2),sf::Color::Black)); vertices.append(sf::Vertex(sf::Vector2f( 10, -2),sf::Color::Black));
target.draw(vertices, states);
vertices.clear(); vertices.setPrimitiveType(sf::TrianglesStrip);
vertices.append(sf::Vertex(sf::Vector2f( -50, 2),sf::Color::Black)); vertices.append(sf::Vertex(sf::Vector2f( -50, -2),sf::Color::Black)); vertices.append(sf::Vertex(sf::Vector2f( -10, 2),sf::Color::Black)); vertices.append(sf::Vertex(sf::Vector2f( -10, -2),sf::Color::Black));
target.draw(vertices, states);
drawId(target, states); drawLabel(target, states); }
____________________________________________________________________________
NPNTrans.h
#ifndef NPNTRANS_H_INCLUDED #define NPNTRANS_H_INCLUDED
#include "Arc.h" #include "Component.h"
#include
class NPNTrans : public Component { public:
NPNTrans(); NPNTrans(const std::string& inConnections, const std::string inLabel, const std::string inId);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; };
#endif // NPNTRANS_H_INCLUDED
|NPN Transistor |NPN Transistor
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
