Sign in to follow this  
EstViking

Game architecture

Recommended Posts

Im trying to write a 2d game in C++ using SDL2. How should i set up all the media loading(textures and stuff) so that i could access them wherever i need em?

Share this post


Link to post
Share on other sites

So if i have a spritesheet, i pass a refrence of the spritesheet object to all tiles(static refrence) and then use clips to render pieces from it?

Share this post


Link to post
Share on other sites

Why would a tile need to store its graphical representation? Just its property values would be enough.

 

Wouldn't it be simpler to pass the reference to the sub-system that draws the map?

 

Also, assuming you're going to have more than one sprite sheet, make a media store object, that contains all media, and pass that around. That makes it much easier to switch between different sprites, even if they are on different sheets.

Share this post


Link to post
Share on other sites

can i make the media store to a singelton? Then whereever i need it i can just ask for the instance and then i can render what i need from it.

Share this post


Link to post
Share on other sites

You can, it's not a great idea though, because it's letting you get sloppy and not think about the impact taking the dependency on the resource loading system actually has on the rest of your code.

Share this post


Link to post
Share on other sites

if i load in the spritesheet in the beginning(simple game not many sheets will come) that wont have that much imact or will it ?

 

I'm not talking about "impact" in terms of memory, I'm talking about impact in terms of turning your code into brittle spaghetti.

 

But yes, you should be loading your sprite sheets as early as you can for a small game, regardless of the method by which you then transport them to the rest of your program. For a small game you can probably fit all your resources in RAM at once and loading them up front probably won't create too much of a delay.

Share this post


Link to post
Share on other sites

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.

 

Would it be good that i write a Module that handles media loading and rendering. And then map or tile calls the render function from media module and renders a given sprite or something ?

Edited by EstViking

Share this post


Link to post
Share on other sites

Planning ahead is good. But there is danger in planning too far ahead when you don't know what you are doing, that can lead to over-engineering as you try to "plan" for every eventuality. Most of which you won't actually encounter

 

Focus instead on what you need now: a way to load your textures, and a way to get those textures to things that need them. So write that. Write a "module" as you say which can load textures from the disk and hand out pointers to those textures when asked (ideally such that if you ask for texture "foo.png," you load it from the disk unless you've already loaded it, in which case you just hand out another pointer to it). Pass this module to anything that would need to access the textures, and move on to writing the next stage of your game.

Share this post


Link to post
Share on other sites
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 :)

Edited by Alberth

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Edited by Lactose!

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
So do i save text or something?

That would work.

 

Basically, you have to decide how to store each tile of your map in the file. For example you could do

28, 41, green
29, 41, blue

This would be one line for each tile, first the x position, then the y position, then the colour (not idea what you have, maybe you want to store something else there).

 

Your data does not need to be text, you can also just write binary data, but the big advantage of text is that you can read it, and even write it your self. Another advantage is that there are standard formats that you can use to store your data, like INI, JSON, YAML, or XML. There are readers and writers for many programming languages for such formats, which means you can just use a library instead of having to read and write the data yourself from/to file.

 

As an example, some YAML data of a game I am writing (but not very fast :P )

name: First scenario
category: Who wins?
description: May the best man win

map:
 - {x: 1, y: 1, tile: path_none}
 - {x: 1, y: 2, tile: path_none}
 - {x: 1, y: 3, tile: path_none}
 - {x: 2, y: 2, tile: path_none}

I am writing this game, using these files to load scenarios etc, and I don't even have a map editor. I just edit these file by hand, which works great for testing.

 

Note that reading lines like "28, 41, green" is quite doable. Reading data like my YAML file is a lot more work by hand.

Edited by Alberth

Share this post


Link to post
Share on other sites

Hmm... ill look into them. I would make the maps by hand but each map consists of 710 tiles so by hand its a lot of work + by having a map editor, i would have worked out many things about the game, like the hierarchic structure of a map and pieces and whatever it will consist of.

Share this post


Link to post
Share on other sites

I would make the maps by hand but each map consists of 710 tiles so by hand its a lot of work
Why do you need a fully filled map at first?

 

Make a fiile with 710 "flat grass" tiles, or whatever you have as common fill tile. Just change a small part of the map to test your world loading. As I just showed above, my current world is exactly 4 hexagons. big. It's not an interesting world to play, but it's enough to make code for drawing the world, and displaying a list of scenario files.

Later, when I have all the graphics and the code, I can make an editor, and try to make an interesting game world as a level to play.

 

Making a level editor is a lot of work, I prefer to postpone that until I know my game is going to work.

 

 

by having a map editor, i would have worked out many things about the game, like the hierarchic structure of a map and pieces and whatever it will consist of.
True, but why do you write the editor in a different language then?

 

The game must be able to store and display the world too. That code can be used both for the game and for the editor, if you use the same language for both :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this