Game architecture

Started by
21 comments, last by Alberth 7 years, 7 months ago

If you have many more textures/models, you should store them in an array or a hashmap, of course.

Advertisement
Well im trying to plan ahead, but i currently have no idea how to make everything communicate with what they need and also not make it spagetthi.

Welcome to real-life programming :)

You're basically trying to climb Mount Everest, you want to reach the top, but you don't really know how to climb at all. You buy maps, look at them closely, ask advice everywhere in the town at the foot of the mountain, but these people seem to speak a different language.

In cases like these, the message I see is "I have got lots to learn" (yay, I just love that!). I tend to just pick a direction that looks like it will work, and start climbing. With the information you have today, pick a direction that avoids all the bear traps you know about, and go in. Write that program.

The idea is if you have no clue what you're doing, there is no point in trying to prepare. Without an idea what you're up against, you don't know what an adequate counter-measure is. The only path forward is to go ahead, and find out.

Note this is not just for beginners. I have been programming a few decades, but at times, I get an assignment where also don't know where to begin, or how to approach it, or what will happen. I have learned however, sitting down at the start of the path doesn't help much.

Instead, I build small test programs (that I discard later) to test the field, try some part I am worried about. Alternatively, you can write a small part of the program first. Then make a new second version that does more. Nobody said you have to deliver the final result in one attempt. After a few decades of programming, I still can't do that either :)

I have programmed before, i have made a ASCII mapeditor, hexagon map editor and some small games with 1-2 sprites and so on. But the code has usually been quite a sphagetti. Now im trying to better myself by planning what i need, max 2 steps ahead, and trying to wrtie code that is not so much sphagetti :D.

Ok i have a spritesheet. I can read it in and render its pieces where i want to. Now i would like to connect the image to the actual tiles. I hae a single tile class that will handle all the tiles except NPC and players. But i need to tell each tile what it is and what is its texture and parameters. where should i put it and what a good way to do it ? (Currently in progress of making a map editor for my game.)

But i need to tell each tile what it is and what is its texture and parameters. where should i put it and what a good way to do it ?

The simple way is to do:


struct Tile {
   Texture * texture;
   .. etc ..
};

This gives each tile a texture and other parameters related to rendering. It's simple, it's easy, and it works. It will probably be perfectly fine for you for now.

The big disadvantage is has is that it couples logical state (the tile) with rendering state (the texture). This, by the way, is why it's nice to have explicit dependencies. You can see that the tile depends on the texture to function. If this seems ugly, good. It sort of is, and making the dependency explicit makes it very obvious that it's ugly so that it gets in your face until you fix it.

The slightly-more-advanced solution is to give tiles in your tile map a type, which is just a logical identifier, perhaps an enumeration value that says if the tile is a floor, wall, et cetera.


struct Tile {
  TileType type;
  .. etc ..
};

Your map is a collection of Tile objects, when it's time to render the tiles you look at each tile you're about to render and decide based on the type which texture to draw (perhaps also based on a tile set, that is, a dungeon tileset versus a city tileset). This divorces the tile and the texture, the former no longer depends directly on the latter. Instead, the relationship between the two is understood by something at a higher level of abstraction that both tile and texture, one that already understands that both exist.

Ok. So, i have a spritesheet wiht my sprites. Now do i need some intermediary object that connects texture to a type. for example on my spritesheet first sprite is grass so i want type GRASS to point to the first sprite on the sheet. Do i but somewhere a map where type and texture are connected? And also since different tiles have different properties depending on type can i use something similar to give tiles properties? Like i have a class or struct that holds the properties for a tile and and then a map or something to connect them together.

What would be better Tile as class or Tile as struct? Problem arises from the fact that i need a SDL_Rect to render my Tile on screen. SDL_Rect has the location and width and height. In class i can use constructor to make the rect and then its simple to render it. In struct how should i make the rect? Do i have to make the rectbeforehand and then init Tile with it or should i just give it x and y and then some function calculates and makes the correct tile? But if i need a function to make a Tile struct then it seems class would be better.

Tile as class or Tile as struct

Classes and structs are essentially the same.

There is a slight difference that default accessors are private for classes and public for structs and things inheriting from them, but that's all.

You can use functions, variables and constructors the same way.


class MyClass
{
public:
    int x;
    int y;
};

Is the same as


struct MyStruct
{
    int x;
    int y;
};

EDIT: Added a missing word.

Hello to all my stalkers.

Then is there a good way to save class to file? I have a map, i want to save it, what would be a good way to do so, save all parameters differently, save objects, or is there some clever trick?

I had an interesting idea. Lets say i make the mapeditor in Java then i save the map. Then i want to read it in a program made in C++. can i make somekind of unified standard in what i can save the map and also easily read it in C++. I obviously cant save objects. So do i save text or something?

This topic is closed to new replies.

Advertisement