Okay, I have fix a complete try program
Main function
#include <SFML\Graphics.hpp>
#include "game_state.h"
#include "main_menu.h"
game_state coreState;
int main(int argc, char* argv[])
{
sf::RenderWindow window(sf::VideoMode(1280, 768), "Test", sf::Style::Close);
window.setVerticalSyncEnabled(true);
coreState.setWindow(&window);
coreState.SetState(new main_menu());
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
window.clear();
coreState.Update();
coreState.Rendere();
window.display();
}
return 0;
}
game_State.h
#pragma once
#include "SFML\Graphics.hpp"
#include "SFML\Audio.hpp"
class tiny_state {
public:
virtual void Initialize(sf::RenderWindow *window)
{
}
virtual void Update(sf::RenderWindow *window)
{
}
virtual void Render(sf::RenderWindow *window)
{
}
virtual void Destroy(sf::RenderWindow *window)
{
}
};
class game_state {
public:
game_state()
{
this->state = NULL;
}
void setWindow(sf::RenderWindow *window)
{
this->window = window;
}
void SetState(tiny_state *state)
{
if (this->state != NULL)
{
this->state->Destroy(this->window);
}
this->state = state;
if (this->state != NULL) {
this->state->Initialize(this->window);
}
}
void Update()
{
if (this->state != NULL)
{
this->state->Update(this->window);
}
}
void Rendere()
{
if (this->state != NULL)
{
this->state->Render(this->window);
}
}
private:
sf::RenderWindow *window;
tiny_state *state;
};
extern game_state coreState;
Main_menu.h
#pragma once
#include "game_state.h"
class main_menu : public tiny_state
{
public:
void Initialize(sf::RenderWindow *window);
void Update(sf::RenderWindow *window);
void Render(sf::RenderWindow *window);
void Destroy(sf::RenderWindow *window);
private:
sf::Font font;
sf::String s1;
sf::Text characterName;
};
Main_menu.cpp
#include "main_menu.h"
#include <string>
void main_menu::Initialize(sf::RenderWindow *window)
{
this->font.loadFromFile("Morris.ttf");
this->characterName.setString(s1);
this->characterName.setFont(this->font);
this->characterName.setCharacterSize(15U);
this->characterName.setPosition(500, 500);
}
void main_menu::Update(sf::RenderWindow *window)
{
sf::Event event;
while (window->pollEvent(event))
{
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode == '\b')
{
if (s1.getSize() > 0)
s1.erase(s1.getSize() - 1, 1);
}
else
{
if (s1.getSize() < 10)
{
s1 += static_cast<char>(event.text.unicode);
if (event.text.unicode < 128)
{
this->characterName.setString(s1);
}
}
}
}
}
this->characterName.setString(s1);
}
void main_menu::Render(sf::RenderWindow *window)
{
window->draw(this->characterName);
}
void main_menu::Destroy(sf::RenderWindow *window)
{
}