SFML help please!

Okay, I've been working on this for a few hours, trying to learn SFML, but it's not working! I'm pretty sure int main() is not running for some BIZARRE reason... When I run it, it doesn't print anything to the screen (I'm using printing as my debugging system). Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdlib.h>
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

sf::Mutex WindowMutex;
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "MySFML");

void ExampleMenu();
void AniExampleMenu();
void Updates();

namespace Stuffs
{
	int ThreadDisplay = 1;
	int ThreadAnimate = 1;
	int ThreadUpdates = 1;
}


//Functions
void Display(void * UserData)
{
	while (Stuffs::ThreadDisplay)
	{
		ExampleMenu();
	}
	return;
}

void Animate(void * UserData)
{
	while (Stuffs::ThreadAnimate)
	{
		AniExampleMenu();
	}
	return;
}

void BackgroundChecks(void * UserData)
{
	while (Stuffs::ThreadUpdates)
	{
		Updates();
	}
	return;
}

void init()
{
	return;
}

int main()
{
	std::cout<<"MAIN\n";
	init();
	
	std::cout<<"Defining Threads\n";
	
	sf::Thread DisplayThread(&Display);
	sf::Thread AnimateThread(&Animate);
	sf::Thread UpdatesThread(&BackgroundChecks);
	std::cout<<"Will it openz?\n";
	if (App.IsOpened())
	{
		std::cout<<"Launching Threads\n";
		DisplayThread.Launch();
		AnimateThread.Launch();
		UpdatesThread.Launch();
		std::cout<<"Threads Launched\n";
	}
	std::cout<<"Bye bye!\n";
	DisplayThread.Wait();
	AnimateThread.Wait();
	UpdatesThread.Wait();
	return 0;
}

void ExampleMenu()
{
	WindowMutex.Lock();
	App.Clear();
	WindowMutex.Unlock();
	
	WindowMutex.Lock();
	App.Display();
	WindowMutex.Unlock();
	return;
}

void AniExampleMenu()
{
	sf::Event Event;
	WindowMutex.Lock();
	while (App.GetEvent(Event))
	{
		 if (Event.Type == sf::Event::Closed)
		 {
			App.Close();
		 }
	}
	WindowMutex.Unlock();
	return;
}

void Updates()
{
	
	return;
}


Thank you for any help you can give!

- Kyle
Last edited on
Is this by any chance a Windows application (as opposed to a console application)? If so, Windows applications don't have consoles by default. Anything sent to the standard output gets thrown away (with a possible exception if the caller redirects the output to a file or something).
I'm not particularly experienced with threading, but I'd guess it has something to do with the fact you are trying to access App from different threads.
@naraku9333:
It may be that they are all trying to access it, it try mutexing it...

@helios:
And it is simply a .cpp compiled with MinGW

- Kyle
Last edited on
Okay, so when I use mutexes the program works perfectly. But does this not defeat the purpose of using multiple threads? I would believe that it will now work equally as slowly...

- Kyle


EDIT: I'm putting the new code above.
Last edited on
Multiple threads can't all be playing in the same file/memory space as each other. Leads to data inconsistencies. Could have one thread trying to read something at the same as another thread is trying to write that exact data.

Basically threads aren't always the answer.
@ResidentBiscuit:
Thanks. I guess I won't be using threads then!

However, I have a new problem... The program only runs correctly on my laptop. I have no F***ing clue why either! My laptop is AMD as my desktop is Intel, that's the only difference I really see. But I would believe that it should still work; I am running the EXACT same program right from my flash drive...
Thanks for any help!

- Kyle
Last edited on
Do you get any errors?
None that I see... How would I get them to show? If you're talking about errors while compiling, then no, but otherwise...

- Kyle
Topic archived. No new replies allowed.