Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Small questions about game programming  (Read 2072 times)

0 Members and 1 Guest are viewing this topic.

Hengad

  • Newbie
  • *
  • Posts: 19
    • View Profile
Small questions about game programming
« on: September 01, 2015, 03:25:47 pm »
Some questions about game programming;

1. How should I build my game loop? Or more specificly where? Should I create it to main directly, or should I have class that has function for game loop, and then I just create object and call the function in main? Is there any performance differences between these 2 options?

2. What is best way to create different cursor? Is it just fine to make cursor invisible and draw own cursor to position of mouse? Or is this too heavy thing to handle?

3. How should I implement drawing inventory? Or anything that contains buttons, should I make class that checks for clicks inside specific coordinates or is there better way to do it?

4. How do I know is my code good/efficient? What can I do to improve it?

5. How I should make character dialogues? If there is lot to talk about, it is pain to write that.. Maybe I should use Lua to write the dialogues and then just implement them to characters with c++?

6. Imagine I have created game. I have all textures in folders. Anyone can go and change them? He could take for example wall texture and make it transparent, and with this he sees through walls? Is there a way to precent this? Same with .exe file that runs the game, is it possible to crack the code and edit it?

Here are questions I am wondering about most. I don't expect to get answers to all of them but it would be nice.

Verra

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Small questions about game programming
« Reply #1 on: September 01, 2015, 04:36:22 pm »
These questions all have a million different answers. It might best to start working on some small games and see what works for you best. There are also lots of game programming with sfml books out there that you might want to take a look at.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Small questions about game programming
« Reply #2 on: September 01, 2015, 05:00:47 pm »
As Verra mentioned, many of your questions are a little too broad to be able to answer them completely. I'll try to help though by giving you some starting points so that you can research further. Again, as Verra mentioned, I also recommend starting with something really simple (something like pong) before you jump in to something that involves character dialog, inventory, etc. You may also consider buying the books about SFML. I've never read them myself, but I have heard good things.

1. It would be fine to implement your game loop in the main function. That would be a good place to start until you get to the point where it isn't suitable for you. Then you can refactor things into a class if you need it. You can even just make a class with an update function and call it from main. Something like
int main(void)
{
   bool GameIsRunning = true;
   GameManager game;
   // ...
   while(GameIsRunning == true)
   {
      game.update();
   }
}
But as I said, just start with the logic in the main function and go from there. The choices you are considering won't have a noticeable difference in performance. What is more important is figuring out how you want to organize your code.

Once you get to implementing your game loop you should read the Fix Your Timestep article. It has some concepts that might seem a bit advanced, but they are things worth considering.

2. Yes you can make a sprite and draw it in the position of the cursor, and I believe that is what a lot of people do. However, this might cause the cursor to slightly "lag" behind where the real cursor is due to the real cursor continuing to mov between your game steps. This would be something easy to test for yourself, so you should give it a try and see if it is acceptable to you. There have been talks on this forum about SFML supporting a "Hardware" cursor, but I haven't paid close enough attention to know what has come of that. You can probably find the threads if you search the forums.

3. Yes, you can just make a class that checks for clicks inside specific coordinates. SFML makes this very easy to do. A common thing to do is to make your button class very simple without much game logic. It would allow users of the class (such as other classes) to register a callback function, and then when the button is pressed it will call that callback function to notify that something was pressed. This makes your button class reusable for all of the buttons in your game.

4. Don't worry about performance until it becomes a problem, especially if you are a beginner. Many beginners get too focused on this and get caught up in trying to do premature optimizations, making their code harder to understand and less maintainable. However, if you look through the SFML tutorials, they mention some pitfalls to avoid, such as copying textures around. You can also look at the Windows Task manager to view your programs CPU and memory usage (though your CPU usage will be at 100% unless you limit your framerate or do some small sleeps in your gameloop). If you really want to dive into it, though, you would use a tool called a profiler to analyze performance of your program. There are several out there if you search the internet for them, though they can be a bit difficult to use. Again, I recommend not worrying about performance unless it is an actual problem you are seeing when you run your game.

5. This question is a bit too broad. Are you asking how to display the dialog on the screen? How to trigger the dialog to be shown on the screen? Or how to store the dialog in your game? For storage, you could consider writing them to some text file, and then reading that file in your game. That might be the easiest.

6. You may just need to accept the fact that people may tamper with your game files. You are going to give yourself a headache trying to prevent people from doing that because it is very non-trivial. Even major games such as minecraft can't prevent people from making new texture packs and things. In fact, many games embrace the fact that people want to make "mods" and modify things. All you can really do is make it slightly harder for people to find your textures to be modified if you really care about trying to protect your game files. For example, you could zip up all of your texture files and then use an external library such as PhysicsFs to pull them directly out of the zip file. This will  require people to take an extra step to find your textures, but of course it doesn't prevent them from figuring it out. There is no easy way to prevent it happening at all.
« Last Edit: September 01, 2015, 05:04:15 pm by Arcade »

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: Small questions about game programming
« Reply #3 on: September 01, 2015, 08:03:39 pm »
I know someone already answers, but to give you my opinion:

1. How should I build my game loop? Or more specificly where? Should I create it to main directly, or should I have class that has function for game loop, and then I just create object and call the function in main? Is there any performance differences between these 2 options?

It shouldn't matter. Any performance difference will be so low you wouldn't be able to measure it. In terms of performance in general, as long as you don't do anything overly stupid, 2D games (what I assume you're doing) aren't really effected by performance all that much.

2. What is best way to create different cursor? Is it just fine to make cursor invisible and draw own cursor to position of mouse? Or is this too heavy thing to handle?

Use whatever method you like best. Again, any performance "issues" with such little things would be impossible to notice.

3. How should I implement drawing inventory? Or anything that contains buttons, should I make class that checks for clicks inside specific coordinates or is there better way to do it?

There's lot of information on GUI programming out there. If you're starting out, I suggest looking for a simple GUI library (I've heard about SFGUI, not sure if that's what you're looking for, though) and learning out it works before trying to roll your own. There isn't a single way to program GUIs in C++, so there's no "wrong" or "right" way to do it (that isn't to say you can't make a bad GUI library, but that they're many ways to make one).

4. How do I know is my code good/efficient? What can I do to improve it?

I suggest reading up on good programming practices (Scott Meyer's Effective C++ is a good book, also check out http://gameprogrammingpatterns.com/contents.html). But performance, again, shouldn't really be an issue. This isn't like the Atari days. You don't need to micro-manage every little piece of your code (maybe if you're writing an OS kernel, but that's entirely different).

5. How I should make character dialogues? If there is lot to talk about, it is pain to write that.. Maybe I should use Lua to write the dialogues and then just implement them to characters with c++?

Do not use Lua. Lua should be reserved for when you need to program behavior in files, not data, like dialog. You can just save dialog inside text files.

6. Imagine I have created game. I have all textures in folders. Anyone can go and change them? He could take for example wall texture and make it transparent, and with this he sees through walls? Is there a way to precent this? Same with .exe file that runs the game, is it possible to crack the code and edit it?

Any files left out in the open, the user can edit. If you really want to keep users from accessing the files, you could try custom binary formats.

To save a float in binary:

void save(const char *filename, float value)
{
    std::ofstream stream(filename, std::ios::binary | std::ios::trunc);

    stream.write(reinterpret_cast<const char*>(&value), sizeof(float);
}
 

To load it back:
float load(const char *filename)
{
    std::ifstream stream(filename, std::ios::binary);

    float value;
    stream.read(reinterpret_cast<char*>(&value), sizeof(float));

    return value;
}
 

Just store whatever data you need in that and you'll be good. It does come with the downside that you need to convert any asset that uses the format into it before you use it, though. So it might be useful to create a small library for any custom formats, and then create a small program that will allow you to convert files to and from the format and just access the (now converted) files through the library in your game.

If you need it really secure, you'd have to do some tricks in order to try to make it so the format cannot be easily decoded. But as long as the game's single-player, you shouldn't worry. If the player wants to see through a wall, let them do it.

You also cannot keep them out entirely. No matter what format you use, your game has to decode it one way or another. You can put as many barriers in their way as you'd like, but your game will still have the code to decode the files, so any "hacker" determined enough can and will get to the files. If you were selling the game, you'd typically make it hard enough that you'd hope the hacker would simply pay the money for the game instead. :)
« Last Edit: September 01, 2015, 08:08:58 pm by GraphicsWhale »

Hengad

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Small questions about game programming
« Reply #4 on: September 02, 2015, 06:35:07 pm »
Thanks @Arcade and @GraphicsWhale for your effort to answer. I understood new things, and your links were helpful too.

 

anything