Hallo liebe Community,
Ich will ein kleines Spiel mit der SFML programmieren und habe nun angefangen eine kleine Klasse zu programmieren.
Um die Klasse zu testen habe ich eine 2. Testklasse. Erstmal will ich nur einen Text zentriert in meinem Fenster darstellen, doch irgendwie sehe ich keinen Text! Ich denke, dass ich irgendeinen dummen Fehler gemacht habe, den ich nicht erkenne.
Ich hoffe aber, dass ihr mir helfen könnt.
Hier ist meine Klasse "MainWindow":
Header:
|
C-/C++-Quelltext
|
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
|
#pragma once
#include <SFML\Graphics.hpp>
#include <string>
enum TextAlign
{
CENTER = 1,
LEFT,
RIGHT,
};
class MainWindow
{
protected:
sf::RenderWindow *app;
public:
//Ein Fenster erstellen
MainWindow(int w, int h, int bpx, std::string const& title);
//Main-Loop des Programms
virtual void mainLoop() = 0;
//Text im Fenster ausrichten
sf::String alignText(std::string const& text, int align, int top = 0, int margin = 0);
};
|
Implementierung/Cpp:
|
C-/C++-Quelltext
|
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
|
#include "MainWindow.h"
MainWindow::MainWindow(int w, int h, int bpx, const std::string &title)
{
app = new sf::RenderWindow(sf::VideoMode(w, h, bpx), title);
}
sf::String MainWindow::alignText(std::string const& text, int align, int top, int margin)
{
sf::String txt(text);
int txtWidth = static_cast<int>(txt.GetRect().GetWidth());
switch(align)
{
case CENTER:
{
int x = (app->GetWidth() - txtWidth) / 2 + margin;
txt.Move(x, top);
}
case LEFT:
{
txt.Move(margin, top);
}
case RIGHT:
{
int x = app->GetWidth() - txtWidth - margin;
txt.Move(x, top);
}
default:
{
txt.Move(margin, top);
}
}
return txt;
}
|
Meine Testklasse sieht so aus:
Header:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#pragma once
#include "MainWindow.h"
class TestApp : public MainWindow
{
public:
TestApp(int w, int h, int bpx, std::string const& title) :
MainWindow(w, h, bpx, title)
{
mainLoop();
};
void mainLoop();
};
|
Implementierung/Cpp:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include "TestApp.h"
void TestApp::mainLoop()
{
while(app->IsOpened())
{
sf::Event ev;
while(app->GetEvent(ev))
{
switch(ev.Type)
{
case sf::Event::Closed:
app->Close();
}
}
sf::String str = alignText("Hallo, Welt!", CENTER, 10);
str.SetColor(sf::Color(128, 128, 0));
app->Clear(sf::Color(255, 255, 255));
app->Draw(str);
app->Display();
}
}
|
Und dann natürlich meine "main.cpp", also das "Programm" oder wie man es auch nennen mag:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
|
#include "TestApp.h"
int main()
{
TestApp test(640, 480, 32, "Hallo, Welt!");
}
|
Hoffentlich könnt ihr mir helfen:
Wie bereits gesagt, sehe ich keinen Text, jedoch mein Fenster!
Mit freundlichen Grüßen
Bügeleisen
PS: Ich nehme auch gerne Tipps zum Codedesign und Co. entgegen