halsafar/libtmx-parser added to project

This commit is contained in:
2022-03-21 15:26:13 -04:00
parent 66b8de6f40
commit dc43e59ff6
16 changed files with 422 additions and 16 deletions

View File

@ -33,7 +33,7 @@ void Map::LoadMap(std::string path, int sizeX, int sizeY, int scale)
std::fstream mapFile;
mapFile.open(path);
int srcX, srcY;
width = tSize*scale*sizeX;
height = tSize*scale*sizeY;
@ -45,7 +45,6 @@ void Map::LoadMap(std::string path, int sizeX, int sizeY, int scale)
srcY = atoi(&c) * tileSize;
mapFile.get(c);
srcX = atoi(&c) * tileSize;
AddTile(srcX, srcY, x*scaledSize, y*scaledSize);
mapFile.ignore(2,',');
}
}

View File

@ -1,6 +1,6 @@
{
"GameName":"Beagle Rescue",
"Developers": "Alan Youngblood, Simon Zaleski, Daniel Rinaldi",
"LibraryDevelopers": "Sam Lantinga, Dave Gamble, Carl Birch, Job Vranish, David Lafreniere",
"LibraryDevelopers": "Sam Lantinga, Dave Gamble, Carl Birch, Job Vranish, David Lafreniere, Bayle Jonathan",
"SpecialThanks":"Nic Allen, Brian Lhota, Rodrigo Monteiro"
}

View File

@ -16,6 +16,7 @@
#include "PlayerController.h"
#include "ProjectileComponent.h"
#include "TileComponent.h"
#include "TileMapComponent.h"
#include "UITextComponent.h"
#endif /* SRC_COMPONENTS_H_ */

View File

@ -0,0 +1,79 @@
/*
* TileMapComponent.h
*
* Created on: Mar 21, 2020
* Author: ayoungblood
*/
#ifndef SRC_ECS_TILEMAPCOMPONENT_H_
#define SRC_ECS_TILEMAPCOMPONENT_H_
#include "ECS.h"
#include "SDL2/SDL.h"
#include "../assetmgr/AssetManager.h"
#include <fstream>
#include <iostream>
#include <string>
#include <tuple>
class TileMapComponent : public Component
{
public:
TransformComponent *transform;
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
Vector2D position;
std::tuple <SDL_Rect,SDL_Rect> tile;
TileMapComponent() = default;
~TileMapComponent()
{
SDL_DestroyTexture(texture);
}
TileMapComponent(std::string id, int tsize, int tscale)
{
setTex(id);
// position.x = xpos;
// position.y = ypos;
//
// srcRect.x = srcX;
// srcRect.y = srcY;
srcRect.w = srcRect.h = tsize;
// destRect.x = xpos;
// destRect.y = ypos;
destRect.w = destRect.h = tsize * tscale;
}
void update() override
{
destRect.x = position.x - Game::camera.x;
destRect.y = position.y - Game::camera.y;
}
void draw() override
{
//iterate through rows and columns of the map to draw the tiles
TextureManager::Draw(texture, srcRect, destRect, SDL_FLIP_NONE);
}
std::tuple<SDL_Rect,SDL_Rect> getTile(char currentTile, int i)
{
std::tuple<SDL_Rect,SDL_Rect> tileTuple;
tileTuple = std::make_tuple(srcRect,destRect);
return tileTuple;
}
void setTex(std::string id)
{
texture = Game::assets->GetTexture(id);
}
};
#endif /* SRC_ECS_TILEMAPCOMPONENT_H_ */

View File

@ -34,9 +34,7 @@ private:
SDL_RendererFlip spriteFlip = SDL_FLIP_NONE;
std::tuple <SDL_Rect, SDL_Rect> letter;
public:
public:
UITextComponent(std::string id, std::string textToPrint, int letterW, int letterH, int letterScale)
{
setTex(id);

View File

@ -10,7 +10,7 @@
#include "Game.hpp"
#include "../assetmgr/TextureManager.h"
#include "../assetmgr/Map.h"
// #include "../assetmgr/Map.h"
#include "../ecs/Components.h"
#include "Collision.h"
#include <string>
@ -21,8 +21,10 @@
// #include "../ui/UIText.h"
#include "../ui/UINineSlice.h"
#include "../cjson/cJSON.h"
#include "tmxparser.h"
Map* map;
// Map* map;
Manager manager;
UINineSlice* my9Slice;
UINineSlice* scoreboard9Slice;
@ -117,7 +119,17 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize SDL audio mixer!, Error: %s", SDL_GetError());
}
// Trying out the halsafar/libtmx-parser
tmxparser::TmxReturn error;
tmxparser::TmxMap map;
error = tmxparser::parseFromFile("assets/maps/testmap.tmx", &map, "assets/textures/tiles/");
// current_time = SDL_GetTicks();
if (!error)
{
printf("Yay! Tile map loaded with no errors.\n");
std::cout << "Map width: " << map.width << " tiles wide by: " << map.height << " tiles high" << std::endl;
}
assets->AddTexture("terrain", "assets/textures/tiles/br-tiles.png");
assets->AddTexture("player", "assets/textures/actors/firefighter.png");
@ -133,7 +145,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
assets->AddSoundClip("bark1","assets/audio/sfx/Bark1.wav");
assets->AddSoundClip("bark2","assets/audio/sfx/Bark2.wav");
map = new Map("terrain",globalScale,16);
// map = new Map("terrain",globalScale,16);
// std::string myText = "Find lost puppies!\nThey need your help!";
std::string myText = "Press U to Start";
@ -171,7 +183,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
//ecs implementation
map->LoadMap("assets/maps/br-map-color.txt",70,45, globalScale);
// map->LoadMap("assets/maps/br-map-color.txt",70,45, globalScale);
player.addComponent<TransformComponent>(860*globalScale,640*globalScale,22,42,globalScale);
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
@ -298,10 +310,10 @@ void Game::update()
camera.x = 0;
if (camera.y < 0)
camera.y = 0;
if (camera.x > map->width-camera.w)
camera.x = map->width-camera.w;
if (camera.y > map->height-camera.h)
camera.y = map->height-camera.h;
// if (camera.x > map->width-camera.w)
// camera.x = map->width-camera.w;
// if (camera.y > map->height-camera.h)
// camera.y = map->height-camera.h;
if (Game::debugCollisionBoxes)
{
for (auto& c: colliders)