Skip to content

Commit

Permalink
Refactored code, made separate Game class
Browse files Browse the repository at this point in the history
  • Loading branch information
TanishTuteja committed Apr 7, 2022
1 parent 1296e10 commit fe0d926
Show file tree
Hide file tree
Showing 21 changed files with 661 additions and 430 deletions.
174 changes: 174 additions & 0 deletions Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "rapidxml.hpp"

#include "Game.h"
#include "MyWindow.h"
#include "Screen.h"

LGame::LGame(LWindow &window) : LScreen(window)
{
initObjs();
}

void LGame::handleEvent(SDL_Event &e)
{
players[0].handleEvent(e);
}

void LGame::update()
{
camera = {camera.x, camera.y, window.getWidth(), window.getHeight()};
// Move the dot
players[0].move();
players[0].setCamera(camera);
}

void LGame::render(SDL_Renderer *renderer)
{
for (size_t i = 0; i < renderables.size(); i++)
{
int res = renderables[i]->render(renderer, camera);

if (res != 0)
{
// Handle Error
}
}
}

void LGame::cleanUp()
{
players[0].cleanUp();
tiles[0].cleanUp();
}

bool LGame::initObjs()
{

if (!window.loadTexture(ashTexture, "resources/ash.bmp"))
{
printf("Failed to load ash texture!\n");
return false;
}

Dot ash(ashTexture, *this, 32, 32, 3, 1, 2, 0, 1);
players.push_back(ash);

camera = {0, 0, window.getWidth(), window.getHeight()};

if (!window.loadTexture(tilesTexture, "resources/tiles2.png"))
{
printf("Failed to load tile set texture!\n");
return false;
}

// Load tile map
if (!setTiles())
{
printf("Failed to load tile set!\n");
return false;
}

int uniqueTilesX = tilesTexture.getWidth() / tiles[0].getBox().w;
int uniqueTilesY = tilesTexture.getHeight() / tiles[0].getBox().h;

tileAtlas.init(uniqueTilesX, uniqueTilesY, tiles[0].getBox().w, tiles[0].getBox().h);

for (int i = 0; i < tiles.size(); i++)
{
renderables.push_back(&tiles[i]);
}
for (int i = 0; i < players.size(); i++)
{
renderables.push_back(&players[i]);
}

return true;
}

bool LGame::setTiles()
{
// Open the map
std::ifstream mapStream("resources/map2.tmx");

// If the map couldn't be loaded
if (mapStream.fail())
{
printf("Unable to load map file!\n");
return false;
}

std::stringstream buffer;
buffer << mapStream.rdbuf();

std::string xmlText = buffer.str();

rapidxml::xml_document<> doc; // character type defaults to char
doc.parse<0>(const_cast<char *>(xmlText.c_str()));

rapidxml::xml_node<> *mapNode = doc.first_node("map");
int tileWidth = std::stoi(mapNode->first_attribute("tilewidth")->value());
int tileHeight = std::stoi(mapNode->first_attribute("tileheight")->value());

int numTilesX = std::stoi(mapNode->first_attribute("width")->value());
int numTilesY = std::stoi(mapNode->first_attribute("height")->value());

std::string tilesData = mapNode->first_node("layer")->first_node("data")->value();
int j = 0;

// Initialize the tiles
for (int i = 0; i < numTilesX * numTilesY; ++i)
{
// Read tile from map file
std::string token;
while (j < tilesData.size() && tilesData[j] != ',')
{
token += tilesData[j];
j++;
}
j++;

int tileType = std::stoi(token);

Tile myTile(tilesTexture, *this, (i % numTilesX) * tileWidth, (i / numTilesX) * tileHeight, tileWidth, tileHeight, tileType);
tiles.push_back(myTile);
}

// Close the file
mapStream.close();

mTilesX = numTilesX;
mTilesY = numTilesY;
mTileWidth = tileWidth;
mTileHeight = tileHeight;

return true;
}

SDL_Rect LGame::getTileClip(int tileType)
{
return tileAtlas.tileClips[tileType - 1];
}

int LGame::getLevelWidth()
{
return mTilesX * mTileWidth;
}

int LGame::getLevelHeight()
{
return mTilesY * mTileHeight;
}

int LGame::getWindowWidth()
{
return window.getWidth();
}

int LGame::getWindowHeight()
{
return window.getHeight();
}
50 changes: 50 additions & 0 deletions Game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef GAME_H
#define GAME_H

#include "Screen.h"

#include <vector>

#include <SDL2/SDL.h>
#include "Tile.h"
#include "Player.h"
#include "MyTexture.h"
#include "TileAtlas.h"
#include "Renderable.h"

class LGame : public LScreen
{
private:
std::vector<Tile> tiles;
std::vector<Dot> players;
SDL_Rect camera;

std::vector<Renderable *> renderables;

LTexture tilesTexture;
LTexture ashTexture;

TileAtlas tileAtlas;

int mTilesX;
int mTilesY;
int mTileWidth;
int mTileHeight;

bool initObjs();
bool setTiles();

public:
LGame(LWindow &window);
void handleEvent(SDL_Event &e);
void update();
void render(SDL_Renderer *renderer);
void cleanUp();
SDL_Rect getTileClip(int tileType);
int getLevelWidth();
int getLevelHeight();
int getWindowWidth();
int getWindowHeight();
};

#endif
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CXX = g++

#OBJS specifies which files to compile as part of the project
OBJS = collision.cpp main.cpp MyTexture.cpp MyWindow.cpp Player.cpp Tile.cpp TileAtlas.cpp
OBJS = collision.cpp Game.cpp main.cpp MyTexture.cpp MyWindow.cpp Player.cpp Screen.cpp Tile.cpp TileAtlas.cpp Timer.cpp

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
Expand Down
4 changes: 2 additions & 2 deletions MyTexture.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "MyTexture.h"
#include <SDL.h>
#include <SDL_image.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <string>
#include <iostream>

Expand Down
4 changes: 2 additions & 2 deletions MyTexture.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef TEXTURE_H
#define TEXTURE_H

#include <SDL.h>
#include <SDL_image.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <string>

class LTexture
Expand Down
Loading

0 comments on commit fe0d926

Please sign in to comment.