-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored code, made separate Game class
- Loading branch information
1 parent
1296e10
commit fe0d926
Showing
21 changed files
with
661 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.