Jump to content
  • Advertisement
Sign in to follow this  
AnthonyVelazquez

Making my game code cleaner/easier to work with

This topic is 905 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

Hello, so for the past week I have been working on a 2D SFML game written in C++ but the more I program, the more it starts to get disorganized. Aside from how I name my methods. I feel like my code is all over the place.
 
 
There are alot of files so for an easy example of what im talking about, look at my GameEngine and stdafx files since I feel that's where the source of my problem is. Also is there anything I could remove and incorporate into Lua? I hear alot of people use it for their game logic instead of hardcoding it into the game. Any advice would be appreciated.
 
 
GameEngine.cpp
#include "stdafx.h"
#include "GameEngine.h"

GameEngine::GameEngine(void)
{
	GameState = Uninitialized;
	GameTitle = "2D SFML RPG";
	ScreenWidth = 1920;
	ScreenHeight = 1080;
	Section = 0;
	GameView.reset(sf::FloatRect(0,0,ScreenWidth,ScreenHeight));
	GameView.setViewport(sf::FloatRect(0,0,1,1));
}

void GameEngine::Start()
{
	if(GameState != Uninitialized)
		return;
	
	GameWindow.create(sf::VideoMode::getDesktopMode(), GameTitle);
	GameWindow.setFramerateLimit(30);
	GameWindow.setVerticalSyncEnabled(true);
	Running = true;
	GameState = State::Title;
	Loop();
}

void GameEngine::Loop()
{
	while(Running)
	{
		GameWindow.setView(GameView);
		GameWindow.clear();
		Event(GameState);
		switch(GameState)
		{
			case State::Title:
				Menu.Show(GameWindow);
				break;
			case State::CharacterCreation:
				CCreate.Show(GameWindow);
				MapLocation = Location::HomeTown;
				break;
			case State::Play:
				MapSelection();
				break;
		}
		GameWindow.display();
	}
}

void GameEngine::Event(State GState)
{
	while(GameWindow.pollEvent(event))
	{
		if(event.type == sf::Event::Closed)
			Running = false;
		switch(GState)
		{
			case State::Title:
				Menu.Event(GameState,Running,event,GameWindow);
				break;
			case State::CharacterCreation:
				CCreate.Event(GameState,Running,event,GameWindow,Character);
				break;
			case State::Play:
				GameMap.Event(GameState,Running,event,GameWindow,GameView);
				Character.Event(event,GameWindow,GameView,PlayerLocation);
				TestNPC.Event(event);
				break;
		}
	}
}

void GameEngine::MapSelection()
{
	switch(MapLocation)
	{
		case Location::HomeTown:
				GameMap.Show(GameWindow);
				Character.Show(GameWindow);
				TestNPC.Load(MapLocation);
				TestNPC.Show(GameWindow,PlayerLocation);
				break;
	}
}

GameEngine::~GameEngine(void)
{
}

GameEngine.h

#pragma once

#include "MainMenu.h"
#include "CharacterCreator.h"
#include "Map.h"

class GameEngine
{
private:
	sf::RenderWindow GameWindow;
	sf::Event event;
	sf::View GameView;
	int ScreenHeight;
	int ScreenWidth;
	int Section;
	char *GameTitle;
	bool Running;
	sf::Sprite PlayerLocation;
	MainMenu Menu;
	CharacterCreator CCreate;
	Map GameMap;
	Player Character;
	NPC TestNPC;
	Location MapLocation;
public:
	GameEngine(void);
	~GameEngine(void);
	void Start();
	void Loop();
	void Event(State GState);
	void MapSelection();
};

stdafx.h

#pragma once
enum Location {HomeTown, Dungeon1};
static Location MapLocation;
#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <fstream>
#include <SFML\Graphics.hpp>

#include "Player.h"
#include "NPC.h"

enum State {Uninitialized, Splash, Title, CharacterCreation, Play};
static State GameState;

static sf::Color Red = sf::Color(255,0,0);
static sf::Color Black = sf::Color(0,0,0);
static sf::Color White = sf::Color(255,255,255);

Share this post


Link to post
Share on other sites
Advertisement


static State GameState;


With this in a header file, every cpp file that includes it is going to get its own internally-linked copy of the variable named GameState. This is probably not what you want.

Why does this variable need to be in the header, anyway? I searched for it in your github, and I only saw it in GameEngine.cpp. I suppose that's why you haven't noticed any bugs yet.

Share this post


Link to post
Share on other sites

 


static State GameState;


With this in a header file, every cpp file that includes it is going to get its own internally-linked copy of the variable named GameState. This is probably not what you want.

Why does this variable need to be in the header, anyway? I searched for it in your github, and I only saw it in GameEngine.cpp. I suppose that's why you haven't noticed any bugs yet.

 

Didn't notice that issue. Thanks for pointing it out.  I honestly can't remember why I put it there.

Share this post


Link to post
Share on other sites

You could try showing your game and asking about this on the SFML forum too (unless you did and I missed it).

Share this post


Link to post
Share on other sites

I would probably make a game state class rather than doing all those switch statements in your GameEngine functions.
This could either be a polymorphic class, or a template class with an integer template argument, or a class that does these switch statements for you.
I would do a polymorphic class. vfunc overhead should be negligible as the gamestate should rarely change.

same with map location.

Edited by nfries88

Share this post


Link to post
Share on other sites

the more I program, the more it starts to get disorganized

 

No, the more you stuff things into a class the more it gets disorganized.  :wink:

 

A tipical work around to not chase waterfalls here is to follow the SRP (Single Responsability Principle). In your case you basically would separate the game, game state, and engine in classes having unique responsabilities. A game has game states which handles the updating and rendering of the game, game states have player information that is not persistent through the entire game and implements game logic scripts, and an engine can contain low-level stuff such as a OS window in your example/catch keyboard presses and releases to be used by the game/load data resources. You compose high level modules of the game using the lower ones. Later you'll se that that is not only pottentially adds to code organization and maintence but also adds to memory performance which is a optimization topic for another discussion. Note that I'm assuming you're a beginner here for me to not describe complex solutions. As a starting point, always keep code simple and maintanable.

 

 


Also is there anything I could remove and incorporate into Lua?

 

Game logic scripts. But that is a topic for another discussion as well. You should hardcode them in the game and in game states in the abscence of Lua.

Edited by Irlan Robson

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!