Run two instances of a SFML C++ app in Xcode
I wonder if there is a way of running an app in Xcode, and then, without closing it, launching another instance of the same app.
I want to know this because I want to create a program for network communication and need two instances to test how the program behaves.
The program is written in C++ using SFMLs network module.
Obviously, I can get the desired effect by creating a new identical program and running it, but it would be easier if they where the same (save me the copy/paste before each run).
I'm running Xcode 6.4.
1 answer
-
answered 2018-10-12 11:33
nvoigt
I have never done this with XCode, but with any other IDE I would simply open the project twice, so two XCode instances are running, both on the same folder. That way you have "shared" code and your changes are automatically there when you recompile and restart both instances.
See also questions close to this topic
-
Is assignment of std::array from braced list of values allowed in c++?
In c++ primer(5th Edition), it is mentioned that std::array assignment from braced list of values is not allowed.
Because the size of the right-hand operand might differ from the size of the left-hand operand, the array type does not support assign and it does not allow assignment from a braced list of values.
Below code is given as an example.
std::array<int, 10> a1 = {0,1,2,3,4,5,6,7,8,9}; std::array<int, 10> a2 = {0}; // elements all have value 0 a1 = a2; // replaces elements in a1 a2 = {0}; // error: cannot assign to an array from a braced list
However, when I compile this code with c++11 compiler it works fine. Is this allowed now or am I missing something?
-
ROOT(CERN) calling method creates segfault even without trying the first line (seems like it's SetBranchAddress)
I try to read root file as usual using SetBranchAddress method, but when i run getHist() method it gives me segfault and even doesn't print "called getHist".
I use ROOT 6.10 and compile my code with gcc (Ubuntu 4.8.5-4ubuntu2) 4.8.5 using make.
#define nSec 110
using namespace std;
void getHist(TString filename, TH1D* h1Hnum) { cout << "called getHist" << endl;
// TChain *theChain1 = new TChain("T"); TFile *file = new TFile(filename); TTree *theChain1 = (TTree*)file->Get("T"); if (!theChain1) { std::cout << "Error: tree not found." << std::endl; delete file; return; } cout << filename << endl; // theChain1->Add(filename); Int_t EventID1; Int_t nPart1; Int_t nColl1; Int_t TrackID1[100000]; Int_t ParentID1[100000]; Int_t StationID1[100000]; Double_t X1[100000]; Double_t Y1[100000]; Double_t Z1[100000]; Double_t Momentum1[100000]; Double_t Px1[100000]; Double_t Py1[100000]; Double_t Pz1[100000]; Double_t Time1[100000]; Double_t PdgID1[100000]; theChain1->SetBranchAddress("EventID", &EventID1); theChain1->SetBranchAddress("nPart", &nPart1); theChain1->SetBranchAddress("nColl", &nColl1); theChain1->SetBranchAddress("TrackID", TrackID1); theChain1->SetBranchAddress("ParentID", ParentID1); theChain1->SetBranchAddress("StationID", StationID1); theChain1->SetBranchAddress("X", X1); theChain1->SetBranchAddress("Y", Y1); theChain1->SetBranchAddress("Z", Z1); theChain1->SetBranchAddress("Momentum", Momentum1); theChain1->SetBranchAddress("Px", Px1); theChain1->SetBranchAddress("Py", Py1); theChain1->SetBranchAddress("Pz", Pz1); theChain1->SetBranchAddress("Time", Time1); theChain1->SetBranchAddress("PdgID", PdgID1); Long_t nEv1 = theChain1->GetEntries(); ////////// Loop 1 ////////// for (Long_t j = 0; j < nEv1; ++j) { theChain1->GetEntry(j); // h1nColl->Fill(nColl1); Int_t nPhot[nSec] = {0}; Bool_t isChecked[nSec] = {false}; for (Int_t i = 0; i < nPart1; ++i){ if (StationID1[i] < 0) continue; if (isChecked[StationID1[i]]) continue; nPhot[StationID1[i]] ++; if (nPhot[StationID1[i]] > 20.){ // h1Hnum->Fill(StationID[i]); isChecked[StationID1[i]] = true; } } Int_t numOfHits = 0; for (int i = 0; i < nSec; ++i) if (isChecked[i]) numOfHits++; h1Hnum->Fill(numOfHits); } cout << "returning hist" << endl;
}
int main(int argc, char** argv){
#define NHIST 10 TH1D *array[NHIST]; cout << "start looping" << endl; // printHello(); // getHist(filename); for (Int_t i = 0; i < NHIST; ++i) { TString filename = "newData_"; filename += TString (std::to_string(i+1)); filename += TString(".root"); array[i] = new TH1D(TString("Hit Number"+std::to_string(i)),TString("HitNumber"+std::to_string(i)), nSec, -0.5, nSec-0.5); cout << "trying to get hist "<< filename << endl; getHist(filename, array[i]); cout << "trying to make new hist " << endl; cout << "trying to set color" << endl; array[i]->SetLineColor(i); // delete htemp; } cout << "finish looping" << endl; TApplication *app = new TApplication("name", &argc, argv); cout << "TApp created" << endl; TCanvas *c = new TCanvas(); array[0]->Draw(); for (Int_t i = 1; i < NHIST; ++i) { array[i]->Draw("SAME"); } // c->Show(); // app->Run(); std::cin; delete[] array; delete c; return 0;
}
It prints: start looping trying to get hist newData_1.root Segmentation fault (core dumped)
But if i comment all lines with SetBranchAdress that works and method getHist is called in loop. (actual output is to long but believe me, there's all thing that are printed by cout)
-
Different output in debug vs in run mode (Reading from a file) c++
I am trying to simply read each line from a file and output it to the console with a cout. However, the result changes depending if i debug it or simply run it. I don't understand why. Also note that the file contains only the sentence outputted to the console when I debug the code.
If you can't see the images this is the code :
int main() { ifstream ifs("/Users/manefzahra/CLionProjects/untitled1/workk.txt", ios::in); char ligne[256]; // same problem whether i use ifs.getline or getline! string s; if(!ifs){cout << "not opened";} while(getline(ifs,s)) { cout << s; } return 0; }
Here is the code in the main.cpp
Thank you!
- how to solve an error when i make a app to archive to update in app store
-
Is there any way to access pressesbegan method on WKInterfaceButton
I have a WKInterfaceButton in Xcode and when I configure the action for it, it only executes it when the tap has ended, that is, when my finger is lifted.
@IBAction func kickButton() { //Action for this button }
I want to be able to perform the same action but when the touch has just started, like the pressesBegan action in iOS applications. I don't know if there is a way to do this.
-
Old Mac Software So can't Use Databases (Xcode)(SQL)
I have swift 2.1 and my Xcode is v6 my mac is v10.1.1. I am doing a school project on Xcode code at the moment and I know I can't post the application on the app store so please don't comment on that; thank you. The problem I am facing is that due to my old mac I cant import anything and or download anything therefore when I try to make a login section to my news reader application I am facing trouble. I cant import sqlite as it needs news software and I cant use SQL as all the SQL type tools need newer software. I want a database on my mac which I can code. To do well on the project I need to my code more complex so If anyone has any ideas that would be great.
On a side note; my app is basically a news reader which parses in words to a api which brings back news articles which can be clicked on and opened in a web view, can anyone give me any suggestions that would make this more complex?
-
Circle collision glitching and breaking the pool prototype
I'm implementing a 2D ball to ball collision using SFML for drawing. I've added static and dynamic response,however I encountered a problem I didn't see discussed anywhere. Collision works just fine when there are 2 balls, but sometimes if I add a 3rd ball it just breaks. 1st ball bounces off a wall, while 3rd and 2nd collide,then 2nd ball glitches through 1st ball,supercharging both and they don't obey the laws of physics.
i can't post images,so here the link to the gif: https://drive.google.com/file/d/0B27p1fiTRRJoRXd6QmxYdjM3SGZJNVFyRkR4SVFOeEVCWlZR/view?usp=sharing
Here's a function to tell whether or not the balls are colliding:
bool Ball::isColliding(Ball & ball) { float distance = std::sqrt(((pos.x - ball.pos.x) * (pos.x - ball.pos.x)) + ((pos.y - ball.pos.y) * (pos.y - ball.pos.y))); float radiusSum = this->radius + ball.radius; if (distance <= radiusSum) { return true; } return false; }
And function to resolve collison: void Ball::resolveCollison(Ball &ball) {
//Static resolution float fDistance = std::sqrt(((pos.x - ball.pos.x) * (pos.x - ball.pos.x)) + ((pos.y - ball.pos.y) * (pos.y - ball.pos.y))); float fOverlap = 0.5f * (fDistance - radius - ball.radius); //Displace current ball pos.x -= fOverlap * (pos.x - ball.pos.x) / fDistance; pos.y -= fOverlap * (pos.y - ball.pos.y) / fDistance; //Displace target ball ball.pos.x += fOverlap * (pos.x - ball.pos.x) / fDistance; ball.pos.y += fOverlap * (pos.y - ball.pos.y) / fDistance; //Dynamic resolution //Normal float nx = (ball.pos.x - pos.x ) / fDistance; float ny = (ball.pos.y - pos.y ) / fDistance; //Tangent float tx = -ny; float ty = nx; float dpTan1 = vel.x * tx + vel.y * ty; float dpTan2 = ball.vel.x * tx + ball.vel.y * ty; float dpNorm1 = vel.x * nx + vel.y * ny; float dpNorm2 = ball.vel.x * nx + vel.y * ny; float m1 = (dpNorm1 * (mass - ball.mass) + 2.0f * ball.mass * dpNorm2) / (mass + ball.mass); float m2 = (dpNorm2 * (ball.mass - mass) + 2.0f * mass * dpNorm1) / (mass + ball.mass); vel.x = tx * dpTan1 + (nx * m1); vel.y = ty * dpTan1 + (ny * m1); ball.vel.x = tx * dpTan2 + (nx * m2); ball.vel.y = ty * dpTan2 + (ny * m2); }
I am aware this ain't the most elegant solution, but it's my first time doing it. Anyway, I also update the ball(s) every loop like that:
void Ball::update() { //friction - do not touch acc.x = -vel.x * 0.001f; acc.y = -vel.y * 0.001f; //updating the position vel += acc; pos += vel; circ.setPosition(pos); }
Also note Ball is just a class to hold balls' velocity,acceleration and position vector,to initialize them, and to draw them using sf::CircleShape (named circ).
The balls are supposed to collide realistically (or as realistically as I can, as there is no angular momentum ) using elastic collision, but instead they keep glitching through, but just in this very instance. I'm not sure why this is happening,so any help is greatly appreciated.
-
Texture.loadFromFile decreased my fps.Should be 60 instead of 30fps
When I start game fps is about 60 but on holding button "W" my fps decrease to 30.On release that button it became 60 again.This happens because line of code: model->Load("img/characters.png", rec).
If I'll comment line like this "//" character gonna move smoothly with 60fps but w/o turning to top.
Entity *player; void heroMovement() { if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { sf::IntRect top(32, 224, 32, 32); this->player->Load("img/characters.png", top); calculateTileAnimation(0, 32, 64, top , this->player); velocity.y -= character_speed; } } void calculateTileAnimation(int firstTile , int sizeTile , int lastTile,sf::IntRect rec , Entity *model) { model->Load("img/characters.png", rec); // This line decreasing fps if (clock.getElapsedTime().asSeconds() < 0.3f) { rec.left += sizeTile; if (rec.left == lastTile) rec.left = firstTile; clock.restart(); } } void Load(std::string filename, sf::IntRect rec) { this->texture->loadFromFile(filename, rec); this->setTexture(*this->texture); }
Need to fix it , on holding button "w" character should be turned on top and move with 60fps.
-
C++ / SFML: Printing convex shapes to the screen using two recursive calls only displays the shapes from the first recursive call and not the second
I am using SFML and coding in C++. The program I am writing must be a recursive implementation.
My goal is to create a function that recursively draws a square to the screen in different positions and rotations dependent upon the previously drawn square.
Each subsequent square should be smaller than the previous function call and rotated 45 degrees to the left( from the left corner of the previous square ) or 45 to the right of the previous square.
Each new square spawns two more squares etc..
My idea is to pass the upper left point and the upper right point of a square to two different recursive function calls and use these points as starting points for the subsequent squares.
While the squares generated will also pass upper left and right corners to recursive function calls etc..
The code I have developed is not displaying both squares that should have been generated from the recursive function calls. Only one side is being shown.
I have developed the following code (Please forgive my code.. I haven't been coding in C++ for too long..)
DRIVER of PROGRAM ( main.cpp )
#include <SFML/System.hpp> #include <SFML/Graphics.hpp> #include <SFML/Window.hpp> #include "PTree.hpp" using namespace std; using namespace sf; int main( int argc, char* argv[ ] ) { double L = 0.0; // Length of square sides int N = 0; // Number of times to call recursive function L = atol( argv[ 1 ] ); N = atoi( argv[ 2 ] ); Vector2f vPoint; vPoint.x = 0; vPoint.y = 0; // Create and Display Window PTree tree( L, N ); return 0; }
( PTree.hpp )
#ifndef PTREE_H #define PTREE_H using namespace std; using namespace sf; class PTree /*:public sf::Drawable, public sf::Transformable*/{ public: // Constructor PTree( double L, int N ); // Destructor ~PTree(); // Recursive function to draw Pythagorias Tree void pTree( double L, int N, Vector2f vPoint, Vector2f vOrigin, float rotation ); private: float width = 0; float height = 0; int originX = 0; int originY = 0; float rotation = 0; RenderWindow window; int angle1 = 0; int angle2 = 0; }; #endif // PTREE_H included
( PTree.cpp )
#include <SFML/System.hpp> #include <SFML/Graphics.hpp> #include <SFML/Window.hpp> #include <math.h> #include "PTree.hpp" #include <iostream> using namespace std; using namespace sf; // Constructor PTree::PTree( double L, int N ) { width = ( 6 * L ); height = ( 4 * L ); Vector2f vPoint = { width/2, height - 1 }; Vector2f vOrigin; vOrigin.x = L/2; vOrigin.y = L; /* vPoint.x = width/2; vPoint.y = height - 1; */ window.create( VideoMode( width, height ), "Pythagoras Fractal Tree" ); pTree( L, N, vPoint, vOrigin, 0 ); } // Destructor PTree::~PTree(){} /*###########################################################################*/ // Recursive function to draw Pythagorias Tree void PTree::pTree( double L, int N, Vector2f vPoint, Vector2f vOrigin, float rotation ) { Vector2f vPointR; if( N < 1 ) { return; } // Define a convex shape called convexSquare ConvexShape convexSquare( 4 ); convexSquare.setPoint( 0, Vector2f( 0, 0 )); convexSquare.setPoint( 1, Vector2f( 0, L )); convexSquare.setPoint( 2, Vector2f( L, L )); convexSquare.setPoint( 3, Vector2f( L, 0 )); convexSquare.setOutlineThickness( 1.f ); convexSquare.setFillColor( Color::Black ); convexSquare.setOutlineColor( Color::White ); convexSquare.setPosition( vPoint ); convexSquare.setOrigin( vOrigin ); convexSquare.setRotation( rotation ); while( window.isOpen( )) { Event event; while( window.pollEvent( event )) { if( event.type == Event::Closed ) { window.close( ); } } if( N >= 0 ) { window.draw( convexSquare ); window.display( ); L = ( L * ( sqrt(2)/2 )); N = N - 1; rotation = rotation - 135; cout << "LOOPS:" << N << endl; //left vPoint = convexSquare.getTransform( ).transformPoint( convexSquare.getPoint( 0 )); vOrigin = convexSquare.getPoint( (angle1) ); pTree( L, N, vPoint, vOrigin, rotation ); angle1 = (( angle1 + 1 ) % 4 ); //right vPointR = convexSquare.getTransform( ).transformPoint( convexSquare.getPoint( 3 )); vOrigin = convexSquare.getPoint( 2 ); pTree( L, N, vPointR, vOrigin, rotation-90 ); } } cout << "X value =" << vPoint.x << " Y value = " << vPoint.y << endl;
So far I have tried to return various points of the convex shapes for the second recursive call to the function pTree. This did not display anything either.
Initially I was only using Vector2f vPoint and modifying it prior to each recursive call but after exhausting my knowledge base for a solution I created a new variable specifically for the right side squares called Vector2f vPointR.
The SFML documentation does not provide sufficient examples for noobs like myself. The API is essentially a list of options with minimal examples if any for each function. Ive searched the internet to the best of my ability to see if I am passing the wrong points but could not find an answer.
The one thing that did work ( although not entirely correct ) was when I switched the recursive calls... meaning I moved the call for the right side squares before the call for the left side squares but the problem with this is that the left side s quares were not displaying.
At this point I am also trying to work out the proper rotation for each square but this is the least of my problems.
Is there an issue with the way I am trying to display these squares recursively?
I am not sure where to go from here other than Stack Overflow for help.
Thanks for your time and expertise.