Skip to content

Commit

Permalink
Merge pull request #3 from egormkn/dev-vlad
Browse files Browse the repository at this point in the history
Dev vlad
  • Loading branch information
egormkn authored Feb 14, 2017
2 parents 22d67d0 + 4751263 commit 94b6db7
Show file tree
Hide file tree
Showing 21 changed files with 260 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
.idea/

# SDL settings
cmake/LibraryPaths.cmake
cmake\LibraryPaths.cmake
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ add_library(TMX STATIC
SDLXX/tmx/TMX_Utils.h
SDLXX/tmx/TMX_map.cpp
SDLXX/tmx/TMX_map.h
SDLXX/tmx/testTiny.cpp
SDLXX/tmx/TMX_tileset.cpp
SDLXX/tmx/TMX_tileset.h
SDLXX/tmx/TMX_offset.cpp
Expand All @@ -116,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
11 changes: 6 additions & 5 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 Expand Up @@ -71,7 +72,7 @@ namespace SDLXX {
}

//Renders texture at given point
void render(SDL_Renderer *renderer, int x = 0, int y = 0, SDL_Rect *clip = nullptr, SDL_Rect *dest = nullptr, double angle = 0.0,
void render(SDL_Renderer *renderer, SDL_Rect *clip = nullptr, SDL_Rect *dest = nullptr, double angle = 0.0,
SDL_Point *center = nullptr,
SDL_RendererFlip flip = SDL_FLIP_NONE) {
//Set rendering space and render to screen
Expand Down
189 changes: 151 additions & 38 deletions SDLXX/test/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "../base/Scene.h"
#include "../base/Texture.h"
#include "Box2DDrawer.h"
#include "../tmx/TMX_map.h"
#include "../tmx/TMX_map.h"

#include <Box2D/Box2D.h>

Expand All @@ -17,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 All @@ -27,14 +34,15 @@ class Game : public Scene {
Log::log("[" + getTitle() + "] Scene destructed");
}

void setWall(int x, int y, int w, int h) {
void setStaticBox(int x, int y, int w, int h) {
b2PolygonShape groundBox;
groundBox.SetAsBox(w/SCALE, h/SCALE);
groundBox.SetAsBox(w / SCALE, h / SCALE);

b2BodyDef groundBodyDef;
groundBodyDef.position.Set(x/SCALE, y/SCALE);
groundBodyDef.position.Set(x / SCALE, y / SCALE);
b2Body *groundBody = world->CreateBody(&groundBodyDef);
groundBody->CreateFixture(&groundBox, 1);
groundBody->SetUserData(&staticBoxName);
}

void onCreate(Window &w) override {
Expand All @@ -45,19 +53,64 @@ class Game : public Scene {
world = std::make_unique<b2World>(gravity);
drawer = new Box2DDrawer(window->getSDLRenderer(), 30.f);
drawer->SetFlags(0xFF);
world->SetDebugDraw(drawer);
//world->SetDebugDraw(drawer);

map = new TMX_map();
map->init("resources/level.tmx");
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};

setWall(400, 490, 2000, 10);
setWall(55, 438, 64, 60);
setWall(710, 435, 100, 60);

std::vector<std::vector<int>> vec = map->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) {
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;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(600/SCALE, 100/SCALE);
bodyDef.position.Set(600 / SCALE, 100 / SCALE);
body = world->CreateBody(&bodyDef);

body->CreateFixture(&dynamicBox, 2);
Expand All @@ -66,18 +119,18 @@ class Game : public Scene {


/* Circle */
b2CircleShape circleShape;
circleShape.m_radius = 100/SCALE;
/*b2CircleShape circleShape;
circleShape.m_radius = 100 / SCALE;
b2BodyDef bodyDef2;
bodyDef2.type = b2_dynamicBody;
bodyDef2.position.Set(400/SCALE, 100/SCALE);
bodyDef2.position.Set(400 / SCALE, 100 / SCALE);
b2Body *circleBody = world->CreateBody(&bodyDef2);
circleBody->CreateFixture(&circleShape, 2);
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 @@ -99,8 +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 All @@ -114,28 +172,28 @@ class Game : public Scene {
}

void handleEvent(Event &e) override {
if(e.getType() == SDL_KEYDOWN) {
if(e.getEvent().key.keysym.sym == SDLK_ESCAPE) {
if (e.getType() == SDL_KEYDOWN) {
if (e.getEvent().key.keysym.sym == SDLK_ESCAPE) {
finish();
} else if(e.getEvent().key.keysym.sym == SDLK_UP) {
for(b2Body *it = world->GetBodyList(); it != 0; it = it->GetNext()) {
} else if (e.getEvent().key.keysym.sym == SDLK_UP) {
for (b2Body *it = world->GetBodyList(); it != 0; it = it->GetNext()) {
if (it->GetUserData() == &boxName) {
it->ApplyLinearImpulse(b2Vec2(0.f, -1000/SCALE), body->GetWorldCenter(), true);
it->ApplyLinearImpulse(b2Vec2(0.f, -1000 / SCALE), body->GetWorldCenter(), true);
}
}
} else if(e.getEvent().key.keysym.sym == SDLK_RIGHT) {
if(e.getEvent().key.repeat == 0) {
for(b2Body *it = world->GetBodyList(); it != 0; it = it->GetNext()) {
} else if (e.getEvent().key.keysym.sym == SDLK_RIGHT) {
if (e.getEvent().key.repeat == 0) {
for (b2Body *it = world->GetBodyList(); it != 0; it = it->GetNext()) {
if (it->GetUserData() == &boxName) {
it->ApplyLinearImpulse(b2Vec2(1000/SCALE, 0.f), body->GetWorldCenter(), true);
it->ApplyLinearImpulse(b2Vec2(1000 / SCALE, 0.f), body->GetWorldCenter(), true);
}
}
}
} else if(e.getEvent().key.keysym.sym == SDLK_LEFT) {
if(e.getEvent().key.repeat == 0) {
for(b2Body *it = world->GetBodyList(); it != 0; it = it->GetNext()) {
} else if (e.getEvent().key.keysym.sym == SDLK_LEFT) {
if (e.getEvent().key.repeat == 0) {
for (b2Body *it = world->GetBodyList(); it != 0; it = it->GetNext()) {
if (it->GetUserData() == &boxName) {
it->ApplyLinearImpulse(b2Vec2(-1000/SCALE, 0.f), body->GetWorldCenter(), true);
it->ApplyLinearImpulse(b2Vec2(-1000 / SCALE, 0.f), body->GetWorldCenter(), true);
}
}
}
Expand All @@ -151,20 +209,46 @@ class Game : public Scene {
renderer.setColor(Color(0xFFFFFFFF));
renderer.clear();

for(b2Body *it = world->GetBodyList(); it != 0; it = it->GetNext()) {
if (it->GetUserData() == &boxName) {
b2Vec2 pos = it->GetPosition();
float angle = it->GetAngle();
SDL_Rect clip = {
0,0,30,30
};
SDL_GetWindowSize(window->getSDLWindow(), &SCREEN_WIDTH, &SCREEN_HEIGHT);
window->setTitle(std::to_string(SCREEN_WIDTH) + " " + std::to_string(SCREEN_HEIGHT));

SDL_Rect renderQuad = {0,0,30,30};
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 < 0) {
camera.x = 0;
}
if (camera.x + SCREEN_WIDTH > MAP_WIDTH) {
camera.x = MAP_WIDTH - SCREEN_WIDTH;
}
if (camera.y < 0) {
camera.y = 0;
}
if (camera.y + SCREEN_HEIGHT > MAP_HEIGHT) {
camera.y = MAP_HEIGHT - SCREEN_HEIGHT;
}
}
}
window->setTitle(std::to_string(camera.x) + " " + std::to_string(camera.y));


image->render(renderer.getSDLRenderer(), (int) (pos.x * SCALE), (int) (pos.y * SCALE), &clip, &renderQuad, angle * DEG);
} else {
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);
}
}
}

for (b2Body *it = world->GetBodyList(); it != 0; it = it->GetNext()) {
if (it->GetUserData() == &boxName) {
renderBox(renderer, it);
} else if (it->GetUserData() == &staticBoxName) {
renderBox(renderer, it);
/*b2Vec2 position = it->GetPosition();
SDL_Rect fillRect = {
Expand All @@ -181,16 +265,45 @@ class Game : public Scene {
renderer.render();
}

void renderBox(Renderer &renderer, b2Body *boxBody) {
b2Vec2 pos = boxBody->GetPosition();
float angle = boxBody->GetAngle();
const b2PolygonShape *shape = (b2PolygonShape *) boxBody->GetFixtureList()->GetShape();
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 + 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)};
image->render(renderer.getSDLRenderer(), nullptr, &renderQuad, angle * DEG, &point);
}

private:
int pos = 0;
Window *window = nullptr;

std::unique_ptr<b2World> world;
b2Body *body = nullptr;
std::string boxName = "box";
std::string staticBoxName = "static_box";

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
Loading

0 comments on commit 94b6db7

Please sign in to comment.