# How can I make this cleaner...

This topic is 1762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok im making a Visual Novel and im starting to run into a problem. I wanna fix it before it gets worse lol. So the way I have been programming this is that every picture has a different number assigned to it. I have 3 switch statements, one for input, one for setting textures, and one for displaying. Is there a way to simplify this?

#include "Backgrounds.h"

int main()
{
window.create(sf::VideoMode(1920,1080), "_______: The Beginning",sf::Style::Fullscreen);
window.setKeyRepeatEnabled(false);
window.setMouseCursorVisible(false);
sf::Time time = sf::seconds(2);
sf::Text text1("Ryuuji : To think I ended up dying in a shitty place like this...", font, 50); //77 Characters
text1.setPosition(100,720);
sf::Text text2("One moment I'm going out with this cute girl and the nexy, I get stabbed in the chest with", font, 50); //77 Characters
text2.setPosition(100,790);
sf::Text text3("a spear. What to do now...just wait? Hmm, I wish I had more time to at least say goodbye", font, 50); //77 Characters
text3.setPosition(100,860);
sf::Text text4("to my family. I never even got my first kiss...how pathetic can I be. I never even got to", font, 50); //77 Characters
text4.setPosition(100,930);
sf::Text text5("touch her boobs. I wonder if I can remember how she looked like. Let me try...", font, 50); //77 Characters
text5.setPosition(100,1000);

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{window.close();}
if (event.type == sf::Event::MouseMoved)
{
mousesprite.setPosition(event.mouseMove.x,event.mouseMove.y);
}
if (event.type == sf::Event::KeyPressed)
{
if(event.key.code == sf::Keyboard::Escape)
{window.close();}

switch(game_state)
{
case 0:
if(event.key.code == sf::Keyboard::Up)
{
}
if(event.key.code == sf::Keyboard::Down)
{
}
if(event.key.code == sf::Keyboard::Return)
{
{
case 0:
game_state = 3; //Introduction
break;
case 1:
break;
case 2:
game_state = 2; //Credits
break;
case 3:
window.close();
break;
default:
break;
}
}
break;
case 1:
break;
////////////////////////////////////Credits
case 2:
break;
///////////////////////////////////Introduction
case 3:
if(event.key.code == sf::Keyboard::Return)
{
game_state = 4;
}
default:
break;
}
}

}
/////////////////////////////////////////////////
switch(game_state)
{
BG.setTexture(_0);

break;
BG.setTexture(_1);
break;
case 2:	//Credits
BG.setTexture(_2);
break;
case 3:	//Intro
BG.setTexture(_3);

break;
case 4:	//BG1
BG.setTexture(_4);
break;
default:
break;
}
//window.clear(sf::Color(0,0,255));

window.draw(BG);
if(game_state == 3)
{
window.draw(npc1);
window.draw(textframe);
window.draw(text1);
window.draw(text2);
window.draw(text3);
window.draw(text4);
window.draw(text5);
}
window.draw(mousesprite);
window.display();
}

return 0;
}


Any help would be appreciated

##### Share on other sites

What language/tools are you using? C#? That changes the terminology somewhat, though not the concepts.

To massively simplify, there are two general approaches here: object-oriented and functional. There's a ton of information out there that you can read up on, but I'll try and summarize some suggestions.

First thing is that you should probably break off the three switch statements and put them in separate functions, so you have a function for input, a function for setting the texture, and a function for displaying. This both makes it easier to manage and prevents your code from accidentally modifying the wrong variable.

Second thing is to change the switch statement for some other flow-control paradigm. Handling this in an object oriented way is common: you have an object for the current state, which just means that there's a data structure that has the value of the background texture, etc. That way, instead of having a switch statement, you can just write BG.setTexture(current_game_state.background_texture);

There's a ton of information about object oriented programming and object classes, but it partially depends on exactly which language you're using, because they each do it slightly differently.

You can also do it functionally, by passing a background-drawing-function to a game-state-function, but object-oriented is probably easier to figure out if you're new-ish at this.

Did that help?

##### Share on other sites

Yes it did. The language is C++ by the way. I will look more into it. Thanks

##### Share on other sites

C++ also has pointers, which are the simplest way to handle this...if you know how to use pointers. But the general idea is that you have a variable that holds another variable instead of directly holding a value. Looking up references and pointers will probably help.

##### Share on other sites

If this is going to be an extensive visual novel (and I'm not completely clear on exactly what that constituted, but it's not really relevant), you're going to want to make your code as data driven as possible.

I.E., you don't want your text in the code itself.  You want your code written to read the data in from external files.  this will require you come up with some format to store that data in, and how to tie your text and visuals together in a way that allows the code to associate, load, and display them as you expect.

If this isn't a dynamic novel, but something more like a graphic novel, that shouldn't be too hard.  You could use a markup language like json or xml and define page elements that tie visual data to text data.  The page elements themselves could contain some sort of order attribute, tag, or property that tells the code what order to display them in as well.

If it's more dynamic, the above method still applies, with the exception that the ordering gets more complicated, and possibly there are more complex relationships between visuals and text as well, but that doesn't change the benefit of having your data in a separate file.

##### Share on other sites

According to your code, you probably want to spent some time on the following things (I think you should probably google them):

• Object Orientated Programming (OOP)
• Indentation
• C++ coding style

A good book on C++ will learn you these things. Without these things your code will become a mess (well, not using OOP isn't too bad, but it's harder to write "clean" code). If you want a modern book focused on games using SFML, I recommend SFML Game Development (google it, again). It will however require you to know the basics of C++ already. It's made for games, but it shows you nicely how to implement a state machine, do OOP and learn other valuable things.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634135
• Total Posts
3015754
×