Skip to content

Commit

Permalink
Game.h updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Zem4ik committed Feb 14, 2017
1 parent 2ec3b7e commit 4751263
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 44 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ add_library(TMX STATIC
SDLXX/tmx/TMX_data.cpp
SDLXX/tmx/TMX_data.h
tinyxml2/tinyxml2.cpp
tinyxml2/tinyxml2.h)
tinyxml2/tinyxml2.h SDLXX/tmx/TMX_tile.cpp SDLXX/tmx/TMX_tile.h)

add_library(SDLXX STATIC ${SOURCE_FILES})
target_compile_definitions(SDLXX PRIVATE ${DEFINES})
Expand Down
4 changes: 2 additions & 2 deletions SDLXX/SDLXX_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bool SDLXX::SDL_image::initialized = false;

SDLXX::SDL_image::SDL_image(Uint32 flags) {
#ifndef SDLXX_RELEASE
Log::log("Initializing SDL image system...");
Log::log("Initializing SDL tmx_image system...");
#endif
{
std::lock_guard<std::mutex> lock(mutex);
Expand Down Expand Up @@ -39,7 +39,7 @@ SDLXX::SDL_image::SDL_image(Uint32 flags) {

SDLXX::SDL_image::~SDL_image() {
#ifndef SDLXX_RELEASE
Log::log("Cleaning up SDL image system...");
Log::log("Cleaning up SDL tmx_image system...");
#endif
std::lock_guard<std::mutex> lock(mutex);
IMG_Quit();
Expand Down
9 changes: 5 additions & 4 deletions SDLXX/base/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ namespace SDLXX {
SDL_QueryTexture(t, &format, &access, &width, &height);
}

Texture(const std::string &path, SDL_Renderer *renderer) {
Texture(const std::string &path, SDL_Renderer *renderer, int w, int h) {
SDL_Surface *surface = IMG_Load(path.c_str());
if(surface == nullptr) {
throw Exception("Unable to load image", IMG_GetError());
throw Exception("Unable to load tmx_image", IMG_GetError());
}
SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 0, 0xFF, 0xFF)); // FIXME: For what?
SDL_Rect stretchRect;
stretchRect.x = 0;
stretchRect.y = 0;
stretchRect.w = 30;
stretchRect.h = 30;
stretchRect.w = w;
stretchRect.h = h;
//FIXME: chack size of surface aster bliting
SDL_BlitScaled( surface, NULL, surface, &stretchRect );


Expand Down
92 changes: 75 additions & 17 deletions SDLXX/test/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const float SCALE = 30.f;
const float DEG = 57.29577f;

class Game : public Scene {
private:
struct TextureHolder {
Texture *texture;
SDL_Rect rect;
};
public:

Game(const std::string &title) : Scene(title) {
Expand Down Expand Up @@ -52,8 +57,38 @@ class Game : public Scene {

map = new TMX_map();
map->init("resources/level.tmx");
MAP_HEIGHT = map->tmx_layers[0].height * 60;
MAP_WIDTH = map->tmx_layers[0].width * 60;
DEFAULT_BOX_SIZE = 64;
MAP_HEIGHT = map->tmx_layers[0].height * DEFAULT_BOX_SIZE;
MAP_WIDTH = map->tmx_layers[0].width * DEFAULT_BOX_SIZE;


map2 = new TMX_map();
map2->init("resources/level2.tmx");

for (std::vector<TMX_tileset>::const_iterator tileset = map2->tmx_tilesets.begin();
tileset != map2->tmx_tilesets.end(); ++tileset) {
if (!tileset->tmx_image.source.empty()) {
int raw = tileset->tilecount / tileset->columns;
int width = DEFAULT_BOX_SIZE * tileset->columns;
int height = DEFAULT_BOX_SIZE * raw;
textures.push_back(new Texture(tileset->tmx_image.source, w.getSDLRenderer(), width, height));
for (int i = 0; i < raw; ++i) {
for (int j = 0; j < tileset->columns; ++j) {
textureHolders.push_back(
{textures[textures.size() - 1],
{j * DEFAULT_BOX_SIZE, i * DEFAULT_BOX_SIZE, DEFAULT_BOX_SIZE, DEFAULT_BOX_SIZE}});
}
}
} else {
for (std::vector<TMX_tile>::const_iterator tile = tileset->tiles.begin();
tile != tileset->tiles.end(); ++tile) {
textures.push_back(new Texture(tile->tmx_image.source, w.getSDLRenderer(), DEFAULT_BOX_SIZE,
DEFAULT_BOX_SIZE));
textureHolders.push_back(
{textures[textures.size() - 1], {0, 0, DEFAULT_BOX_SIZE, DEFAULT_BOX_SIZE}});
}
}
}

//default camera position
camera = {0, 0};
Expand All @@ -63,13 +98,14 @@ class Game : public Scene {
for (int i = 0; i < vec.size(); ++i) {
for (int j = 0; j < vec[i].size(); ++j) {
if (vec[i][j] != 0) {
setStaticBox(j * 60, i * 60, 30, 30);
setStaticBox(j * DEFAULT_BOX_SIZE, i * DEFAULT_BOX_SIZE, DEFAULT_BOX_SIZE / 2,
DEFAULT_BOX_SIZE / 2);
}
}
}

b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(30 / SCALE, 30 / SCALE);
dynamicBox.SetAsBox(DEFAULT_BOX_SIZE / 2 / SCALE, DEFAULT_BOX_SIZE / 2 / SCALE);

// Define the dynamic body. We set its position and call the body factory.
b2BodyDef bodyDef;
Expand All @@ -93,7 +129,8 @@ class Game : public Scene {
circleBody->CreateFixture(&circleShape, 2);*/


image = new Texture("resources/Downloads/Level/Objects/Box.png", w.getSDLRenderer());
image = new Texture("resources/Downloads/Level/Objects/Box.png", w.getSDLRenderer(), DEFAULT_BOX_SIZE,
DEFAULT_BOX_SIZE);
}

void onDestroy() override {
Expand All @@ -115,9 +152,13 @@ class Game : public Scene {
delete gravity;
gravity = nullptr;*/

for (std::vector<Texture *>::iterator iterator = textures.begin(); iterator != textures.end(); ++iterator) {
delete *iterator;
}
delete drawer;
delete image;
delete map;
delete map2;
}

void onPause() override {
Expand Down Expand Up @@ -173,19 +214,32 @@ class Game : public Scene {

for (b2Body *it = world->GetBodyList(); it != 0; it = it->GetNext()) {
if (it->GetUserData() == &boxName) {
//window->setTitle(std::to_string(it->GetPosition().x * SCALE) + " " + std::to_string(it->GetPosition().y * SCALE));
camera.x = (int) (it->GetPosition().x * SCALE - SCREEN_WIDTH / 2);
camera.y = (int) (it->GetPosition().y * SCALE - SCREEN_HEIGHT / 2);
if (camera.x < -30) {
camera.x = -30;
camera.y = (int) (it->GetPosition().y * SCALE - SCREEN_HEIGHT/ 2);
if (camera.x < 0) {
camera.x = 0;
}
if (camera.x + SCREEN_WIDTH > MAP_WIDTH) {
camera.x = MAP_WIDTH - SCREEN_WIDTH;
}
if (camera.x + SCREEN_WIDTH > MAP_WIDTH - 30) {
camera.x = MAP_WIDTH - SCREEN_WIDTH - 30;
if (camera.y < 0) {
camera.y = 0;
}
if (camera.y < -30) {
camera.y = -30;
if (camera.y + SCREEN_HEIGHT > MAP_HEIGHT) {
camera.y = MAP_HEIGHT - SCREEN_HEIGHT;
}
if (camera.y + SCREEN_HEIGHT > MAP_HEIGHT - 30) {
camera.y = MAP_HEIGHT - SCREEN_HEIGHT - 30;
}
}
window->setTitle(std::to_string(camera.x) + " " + std::to_string(camera.y));


std::vector<std::vector<int>> vec = map2->tmx_layers[0].tmx_data.data;
for (int i = 0; i < vec.size(); ++i) {
for (int j = 0; j < vec[i].size(); ++j) {
if (vec[i][j] != 0) {
SDL_Rect rect = {j * DEFAULT_BOX_SIZE - camera.x, i * DEFAULT_BOX_SIZE - camera.y, DEFAULT_BOX_SIZE, DEFAULT_BOX_SIZE};
textureHolders[vec[i][j] - 1].texture->render(renderer.getSDLRenderer(), &textureHolders[vec[i][j] - 1].rect, &rect);
}
}
}
Expand Down Expand Up @@ -218,9 +272,9 @@ class Game : public Scene {
b2Vec2 local_vec = shape->m_vertices[0];
int height = (int) (shape->m_vertices[2].y * 2 * SCALE);
int width = (int) (shape->m_vertices[2].x * 2 * SCALE);
SDL_Rect renderQuad = {(int) ((pos.x + local_vec.x) * SCALE - camera.x),
(int) ((pos.y + local_vec.y) * SCALE - camera.y), height,
width};
SDL_Rect renderQuad = {(int) ((pos.x + local_vec.x) * SCALE - camera.x + DEFAULT_BOX_SIZE / 2),
(int) ((pos.y + local_vec.y) * SCALE - camera.y + DEFAULT_BOX_SIZE / 2), DEFAULT_BOX_SIZE,
DEFAULT_BOX_SIZE};


SDL_Point point = {(int) (shape->m_vertices[2].x * SCALE), (int) (shape->m_vertices[2].y * SCALE)};
Expand All @@ -238,14 +292,18 @@ class Game : public Scene {

b2Draw *drawer = nullptr;
Texture *image = nullptr;
std::vector<Texture *> textures;
std::vector<TextureHolder> textureHolders;

SDL_Point camera;
int SCREEN_WIDTH;
int SCREEN_HEIGHT;
int MAP_WIDTH;
int MAP_HEIGHT;
int DEFAULT_BOX_SIZE;

TMX_map *map;
TMX_map *map2;
};

#endif // SDLXX_GAME_H
3 changes: 2 additions & 1 deletion SDLXX/test/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ namespace SDLXX {
void onCreate(Window &w) override {
Log::log("[" + getTitle() + "] Scene created");
window = &w;
image2 = new Texture("resources/menu.png", w.getSDLRenderer());
//TODO: change image2 size
image2 = new Texture("resources/menu.png", w.getSDLRenderer(), 30, 30);
}

void onDestroy() override {
Expand Down
13 changes: 8 additions & 5 deletions SDLXX/tmx/TMX_Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ TMX::RenderOrder TMX_Utils::renderOrderFromString(const char *string) {
if (string == NULL) {
return TMX::RenderOrder::UNKNOWN;
}
if (!strcmp(string, "right_down")) {
if (!strcmp(string, "right-down")) {
return TMX::RenderOrder::RIGHT_DOWN;
}
if (!strcmp(string, "right_up")) {
if (!strcmp(string, "right-up")) {
return TMX::RenderOrder::RIGHT_UP;
}
if (!strcmp(string, "left_down")) {
if (!strcmp(string, "left-down")) {
return TMX::RenderOrder::LEFT_DOWN;
}
if (!strcmp(string, "left_up")) {
if (!strcmp(string, "left-up")) {
return TMX::RenderOrder::LEFT_UP;
}
return TMX::RenderOrder::UNKNOWN;
Expand Down Expand Up @@ -164,7 +164,10 @@ std::string TMX_Utils::getAttributeString(const tinyxml2::XMLElement *element, c

//parsing matrix string

std::vector<std::vector<int>> TMX_Utils::parseMatrix(int width, int height, const char *data) {
std::vector<std::vector<int>> TMX_Utils::parseMatrix(unsigned int width, unsigned int height, const char *data) {
if (data == nullptr) {
return std::vector<std::vector<int>>();
}
std::vector<std::vector<int>> matrix(height, std::vector<int>(width));
std::string number;
for (int i = 0; i < height; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion SDLXX/tmx/TMX_Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class TMX_Utils {

//parsing matrix string

static std::vector<std::vector<int>> parseMatrix(int width, int height, const char *data);
static std::vector<std::vector<int>> parseMatrix(unsigned int width, unsigned int height, const char *data);
};


Expand Down
3 changes: 3 additions & 0 deletions SDLXX/tmx/TMX_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ TMX_data::TMX_data() {
}

void TMX_data::init(const tinyxml2::XMLElement *element, int height, int width) {
if (element == nullptr) {
return;
}
encoding = TMX_Utils::getEncoding(element);
compression = TMX_Utils::getCompression(element);
data = TMX_Utils::parseMatrix(width, height, element->GetText());
Expand Down
3 changes: 3 additions & 0 deletions SDLXX/tmx/TMX_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ TMX_image::TMX_image() {
}

void TMX_image::init(const tinyxml2::XMLElement *element) {
if (element == nullptr) {
return;
}
format = TMX_Utils::getAttributeString(element, "format");
id = TMX_Utils::getAttributeInt(element, "id");
source = TMX_Utils::getAttributeString(element, "source");
Expand Down
3 changes: 3 additions & 0 deletions SDLXX/tmx/TMX_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ TMX_layer::TMX_layer() {
}

void TMX_layer::init(const tinyxml2::XMLElement *element) {
if (element == nullptr) {
return;
}
name = TMX_Utils::getAttributeString(element, "name");
x = TMX_Utils::getAttributeInt(element, "x");
y = TMX_Utils::getAttributeInt(element, "y");
Expand Down
7 changes: 6 additions & 1 deletion SDLXX/tmx/TMX_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ bool TMX_map::init(const std::string fileDir) {
backgroundColor = TMX_Utils::getAttributeString(map, "backgroundcolor");
nextObjectID = TMX_Utils::getAttributeInt(map, "nextobjectid");

tmx_tileset.init(map->FirstChildElement("tileset"));
for (const tinyxml2::XMLElement *e = map->FirstChildElement("tileset");
e != NULL; e = e->NextSiblingElement("tileset")) {
TMX_tileset tmxTileset;
tmxTileset.init(e);
tmx_tilesets.push_back(tmxTileset);
}

for (const tinyxml2::XMLElement *e = map->FirstChildElement("layer");
e != NULL; e = e->NextSiblingElement("layer")) {
Expand Down
2 changes: 1 addition & 1 deletion SDLXX/tmx/TMX_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TMX_map {
std::string backgroundColor;
int nextObjectID;

TMX_tileset tmx_tileset;
std::vector<TMX_tileset> tmx_tilesets;
std::vector<TMX_layer> tmx_layers;

TMX_map();
Expand Down
3 changes: 3 additions & 0 deletions SDLXX/tmx/TMX_offset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ TMX_offset::TMX_offset() {
}

void TMX_offset::init(const tinyxml2::XMLElement *element) {
if (element == nullptr) {
return;
}
x = TMX_Utils::getAttributeInt(element, "x");
y = TMX_Utils::getAttributeInt(element, "y");
}
13 changes: 13 additions & 0 deletions SDLXX/tmx/TMX_tile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "TMX_tile.h"

TMX_tile::TMX_tile() {
id = -1;
}

void TMX_tile::init(const tinyxml2::XMLElement *element) {
if (element == nullptr) {
return;
}
id = TMX_Utils::getAttributeInt(element, "id");
tmx_image.init(element->FirstChildElement("image"));
}
17 changes: 17 additions & 0 deletions SDLXX/tmx/TMX_tile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef SDLXX_TMX_TILE_H
#define SDLXX_TMX_TILE_H

#include "TMX_image.h"

class TMX_tile {
public:
int id;
TMX_image tmx_image;

TMX_tile();

void init(const tinyxml2::XMLElement *element);
};


#endif //SDLXX_TMX_TILE_H
8 changes: 8 additions & 0 deletions SDLXX/tmx/TMX_tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ TMX_tileset::TMX_tileset() {
}

void TMX_tileset::init(const tinyxml2::XMLElement *element) {
if (element == nullptr) {
return;
}
firstgid = TMX_Utils::getAttributeInt(element, "firstgid");
source = TMX_Utils::getAttributeString(element, "source");
name = TMX_Utils::getAttributeString(element, "name");
Expand All @@ -24,6 +27,11 @@ void TMX_tileset::init(const tinyxml2::XMLElement *element) {
columns = TMX_Utils::getAttributeInt(element, "columns");

tmx_image.init(element->FirstChildElement("image"));
for (const tinyxml2::XMLElement *e = element->FirstChildElement("tile"); e != NULL; e = e->NextSiblingElement("tile")) {
TMX_tile tile;
tile.init(e);
tiles.push_back(tile);
}
}

#include "TMX_tileset.h"
Loading

0 comments on commit 4751263

Please sign in to comment.