Jump to content

  • Log In with Google      Sign In   
  • Create Account

aanthonyz1

Member Since 22 Feb 2011
Offline Last Active Mar 29 2016 12:44 AM

#5283941 Making my game code cleaner/easier to work with

Posted by aanthonyz1 on 28 March 2016 - 04:29 PM

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



#4810689 Allegro Tiles

Posted by aanthonyz1 on 14 May 2011 - 09:03 AM

Basically, I have one bmp file that has all my tiles inside.I would have to blit each 16x16 tile into a different Tile array number?





PARTNERS