i would try to help you but your code is too long and an lazy tonight
.
Sorry but i have to say it:
/*rant coming*/
Why most of the "help" requested by users involve <threads> in this forum?
Yes we like speed, we need speed but: Threads ARE NOT speed, threads COULD and only COULD bring speed is are used in the correct way.
The correct way is:
You have the need: So you need more speed because your actual implementation will be slow/impossible without a boost.
You have the knowledge so: You have tested and understood the code that will use threads and know how to use threads.
I don't really know what is the damn obsession with threads
Let me list some problems:
-Globals: A lot of pointless globals.
Why are bad?
sf::RenderWindow window;
bool game = true; //this goes to false only if the window is closed, and ends the program
bool gotChar = false; //if the user inputs a wasd key
char inputChar; //which key was pressed
sf::Sprite sprite[100][100]; //the game world. each sprite is 50 x 50
float viewX, viewY; //the top left corners of the view, in terms of the game world
bool readyToDraw = true;
A lot of data races!!! Read what
is data raceSynchronization:Atomic in C++while (game) { // input thread
game = false; // Input thread
while (game) { // display thread
while (game) { // main thread
//main
gotChar = false;
window.setView(view);
readyToDraw = true;
//display
window.clear();
while (readyToDraw) {
//input
while (window.pollEvent(event)) {
gotChar = true;
readyToDraw = false;
Bad optimization:
On C/C++ for loops conditions are executed every time, insted of once as happens in several others programming languages.
while (readyToDraw)
{
x = viewX / 50; //THIS
y = viewX / 50; // THIS
ay = y;
for (x; x < ((viewX + view.getSize().x) / 50) /*THIS*/; ++x) {
for (y; y < ((viewY + view.getSize().y) / 50)/*THIS*/; ++y) {
window.draw(sprite[x][y]);
}
y = ay; //resets y to what it was before the loop
}
}
//(viewX + view.getSize().x) //This line is executed as many times as the loop repeats.
THIS can be moved out to from the inner loop, naive example:
/* Defined in some part
struct viewData
{
float viewX , viewY ;
float halfX , halfY ;
}
*/
/* Having any pointer or reference of a viewData named myViewData
and when needed recalculate its members.
*/
while (readyToDraw)
{
x = myViewData.halfX ;
y = myViewData.halfY ;
ay = y;
const sf::Vector2f &size = view.getSize() ;
float condA = ( myViewData.viewX + size.x ) / 50 ;
float condB = ( myViewData.viewY + size.y ) / 50 ;
for (x; x < condA ; ++x)
{
for (y; y < condB ; ++y)
{
window.draw(sprite[x][y]);
}
y = ay; //resets y to what it was before the loop
}
}
Another optimization problem:
A lot of 'if' when a 'switch-case' is possible and when non of the if statements are in a 'probability order'
Switch-case. It's both more readable and more optimizable.
Wikipedia talks more about it.if (inputChar == 'w') {
view.move(0, 20);
viewY += 20;
}
/* Using else if on the code below will be more optimized that an if-if-if...if
I don't think an 'a' can be magically turned on a 's' or 'd' .. etc in the same loop
*/
if (inputChar == 'a') {
view.move(-20, 0);
viewX -= 20;
}
if (inputChar == 's') {
view.move(0, -20);
viewY -= 20;
}
if (inputChar == 'd') {
view.move(20, 0);
viewX += 20;
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::W) {
inputChar = 'w';
gotChar = true;
readyToDraw = false;
}
if (event.key.code == sf::Keyboard::A) {
inputChar = 'a';
gotChar = true;
readyToDraw = false;
}
if (event.key.code == sf::Keyboard::S) {
inputChar = 's';
gotChar = true;
readyToDraw = false;
}
if (event.key.code == sf::Keyboard::D) {
inputChar = 'd';
gotChar = true;
readyToDraw = false;
}
And probably an SFML team member or Hapax
will recommend you a good C++ book.