Jump to content
  • Advertisement
Sign in to follow this  
mrhodes

Unity Question about code structure

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all,

I am struggling with some code design and would like some ideas and suggestions from the community.

Up until now I have always made a "Display" class to create a 2D openGl screen, either full or Windowed. Now, to be clear, I haven't really gotten far in my programming, I always seem to get tripped up here when it comes to an event loop, and the main game loop.

I am wondering what others are doing ? I am thinking that maybe now I should make a more general class, "CApp" for example, that will initialize the screen, load resources and run the game and event loop. Is something like this more common practice then breaking things down into classes for Display, Input, etc ?


Sorry if my question isn't fully clear, I'm still figuring out exactly what I am trying to do here, lol.

My goal is to make a 2D RPG like the old school Final Fantasy, and Dragon Warrior games. No online stuff.

Thanks for any advice regarding this.

Michael

Share this post


Link to post
Share on other sites
Advertisement
I think you're thinking about things too generally, and your CApp class looks like a god class. Your classes should be specific and only be able to accomplish one task or manipulate one type of data.

example, most of the time when you talk about CApp, it's a window class. All it should do is be able to create and manipulate a window. It shouldn't know anything about resources or how to draw.

The resource manager is another good example that shouldn't be able to do anything else except load resources, manage what resources stay in memory and which get release. It shouldn't know how to use those resources, or who uses them.

Start simpler than a FF clone, hack together some little games and you'll start to see how things fit together better. I remember back when I first started programming with QB and the first thing I started making was a map editor for my dream rpg. That lasted about a week before I realized I hadn't the slightest a clue what I got myself into.

Share this post


Link to post
Share on other sites
mrhodes, it's hard to give advice without seeing examples of the work you have done so far.

When I make programs in SDL I usually just use the same game loop code all the time, that is:


#include "SDL.h"
#include "SDL_ttf.h"

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

int main( int argc, char* args[] )
{
//Start SDL
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *screen = NULL;
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
SDL_WM_SetCaption("SDL Program", NULL);

SDL_Event event;
bool quit = false;

while(!quit)
{
while(SDL_PollEvent(&event))
{

if(event.type == SDL_QUIT)
{
quit = true;
}
}
SDL_Flip(screen);
}

//Quit SDL
SDL_Quit();

return 0;
}



This is pretty much the game loop and is not something I ever mess with, I then build the classes I need for my game like enemy, player, utilities etc and put all that into a game class.

Incidentally I've wanted to make an 2D RPG like final fantasy too but it is a lot of work.

Share this post


Link to post
Share on other sites
Generally, it is wise to adhere to the single responsibility principle:

You'd have classes for displaying your content, for processing input, playing sound files, updating the game state etc.

Note that there might be "container","manager" or "glue" classes which are responsible for bringing the individual pieces togehter. Your CApp class, for example, might be the main class that represents the application as a whole and which contains the several subsystems (i.e. display, input, sound etc.)

Often, input and display are provided by the same windowing system (e.g. Windows or X11) and thus you might use a "glue" class that connects the various parts of that windowing system with your subsystems (e.g. PAINT events could be passed to the display system whereas MOUSE or KEY events - i.e. input events - are passed to the input system).

Additionally subsystems often have to be linked, e.g. the game needs to receive input events or the display system needs to know the current game state.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!