Started with Camera class

This commit is contained in:
Riyyi
2015-03-19 00:55:59 +01:00
parent 996dd153a8
commit 7b8807b940
11 changed files with 139 additions and 24 deletions
+9
View File
@@ -0,0 +1,9 @@
Book
~~~~
Michael Dawson - Beginning C++ Through Game Programming, Third Edition - 2010
Video
~~~~
https://www.youtube.com/user/geraldmcalister/videos
+21
View File
@@ -0,0 +1,21 @@
#include "camera.h"
Camera::Camera()
{
}
Camera::~Camera()
{
}
void Camera::SetNewView(sf::RenderWindow *window) {
this->view = sf::View(sf::FloatRect(0, 0, 1280, 720));
window->setView(view);
}
void Camera::MoveCamera(sf::RenderWindow *window, sf::Vector2f move) {
this->view.move(move.x, move.y);
window->setView(view);
}
+19
View File
@@ -0,0 +1,19 @@
#ifndef CAMERA_H
#define CAMERA_H
#include <SFML/Graphics.hpp>
class Camera
{
public:
Camera();
~Camera();
void SetNewView(sf::RenderWindow *window);
void MoveCamera(sf::RenderWindow *window, sf::Vector2f move);
private:
//sf::Vector2f position;
sf::View view;
};
#endif // CAMERA_H
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+2
View File
@@ -16,6 +16,8 @@ int main()
settings.antialiasingLevel = 16; settings.antialiasingLevel = 16;
sf::RenderWindow window(sf::VideoMode(1280, 720), gameName, windowStyle, settings); sf::RenderWindow window(sf::VideoMode(1280, 720), gameName, windowStyle, settings);
window.clear(sf::Color(0, 0, 0));
window.display();
//window.setVerticalSyncEnabled(true); //window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60); window.setFramerateLimit(60);
+34
View File
@@ -31,3 +31,37 @@ void Map::Render(sf::RenderWindow* window) {
} }
} }
} }
//void drawMap(sf::RenderWindow* window)
//{
// sf::Sprite tile;
// int itr = 0;
// int location;
// int maxWidth = nearbyint((view.getCenter().x + view.getSize().x / 2) / map->tileSet->tileWidth) + 1;
// int minWidth = nearbyint((view.getCenter().x - view.getSize().x / 2) / map->tileSet->tileWidth) - 1;
// int maxHeight = nearbyint((view.getCenter().y + view.getSize().y / 2) / map->tileSet->tileHeight) + 1;
// int minHeight = nearbyint((view.getCenter().y - view.getSize().y / 2) / map->tileSet->tileHeight) - 1;
// for (int y = minHeight; y < maxHeight; y++)
// {
// if (y < 0)
// {
// y = 0;
// }
// for (int x = minWidth; x < maxWidth; x++)
// {
// if (x < 0)
// {
// x = 0;
// }
// location = map->data[y][x];
// if (location != 0)
// {
// tile.setTexture(*map->tileSet->data[location]);
// tile.setPosition(sf::Vector2f(x * map->tileSet->tileWidth, y * map->tileSet->tileHeight));
// window->draw(tile);
// }
// itr++;
// }
// }
//}
+1
View File
@@ -56,6 +56,7 @@ void MapLoad::Parser(std::string* content, Map* map) {
tileSheetLocation.replace(0, 2, "data"); tileSheetLocation.replace(0, 2, "data");
sf::Image tileSheet; sf::Image tileSheet;
tileSheet.loadFromFile(tileSheetLocation); tileSheet.loadFromFile(tileSheetLocation);
tileSheet.createMaskFromColor(sf::Color::White);
for(int y = 0; y < height; y++) { for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) { for(int x = 0; x < width; x++) {
sf::Texture* texture = new sf::Texture(); sf::Texture* texture = new sf::Texture();
+4 -2
View File
@@ -21,7 +21,8 @@ SOURCES += main.cpp \
inputmanager.cpp \ inputmanager.cpp \
state/maingame.cpp \ state/maingame.cpp \
mapload.cpp \ mapload.cpp \
map.cpp map.cpp \
camera.cpp
HEADERS += \ HEADERS += \
inputmanager.h \ inputmanager.h \
@@ -30,4 +31,5 @@ HEADERS += \
gamestate.h \ gamestate.h \
state/maingame.h \ state/maingame.h \
mapload.h \ mapload.h \
map.h map.h \
camera.h
+34 -9
View File
@@ -18,6 +18,9 @@ void MainGame::Initialize(sf::RenderWindow* window) {
this->map = new Map(); this->map = new Map();
MapLoad mapLoad; MapLoad mapLoad;
mapLoad.Load(this->map, "data/map/level1.json"); mapLoad.Load(this->map, "data/map/level1.json");
this->camera = new Camera();
this->camera->SetNewView(window);
} }
void MainGame::Update(sf::RenderWindow* window) { void MainGame::Update(sf::RenderWindow* window) {
@@ -27,27 +30,49 @@ void MainGame::Update(sf::RenderWindow* window) {
if(inputManager.IsPressed(InputManager::Left)) { if(inputManager.IsPressed(InputManager::Left)) {
std::cout << "LEFT" << std::endl; std::cout << "LEFT" << std::endl;
this->entityManager->Get("test0")->velocity.x = -1; this->entityManager->Get("test0")->velocity.x = -1.5;
this->camera->MoveCamera(window, sf::Vector2f(-1.5, 0));
} }
if(inputManager.IsPressed(InputManager::Right)) { if(inputManager.IsPressed(InputManager::Right)) {
std::cout << "RIGHT" << std::endl; std::cout << "RIGHT" << std::endl;
this->entityManager->Get("test0")->velocity.x = 1; this->entityManager->Get("test0")->velocity.x = 1.5;
} this->camera->MoveCamera(window, sf::Vector2f(1.5, 0));
if(inputManager.IsPressed(InputManager::Down)) {
std::cout << "DOWN" << std::endl;
this->entityManager->Get("test0")->velocity.y = 1;
} }
if(inputManager.IsPressed(InputManager::Up)) { if(inputManager.IsPressed(InputManager::Up)) {
std::cout << "UP" << std::endl; std::cout << "UP" << std::endl;
this->entityManager->Get("test0")->velocity.y = -1; this->entityManager->Get("test0")->velocity.y = -1.5;
this->camera->MoveCamera(window, sf::Vector2f(0, -1.5));
} }
if(inputManager.IsPressed(InputManager::Down)) {
std::cout << "DOWN" << std::endl;
this->entityManager->Get("test0")->velocity.y = 1.5;
this->camera->MoveCamera(window, sf::Vector2f(0, 1.5));
}
if(inputManager.IsPressed(InputManager::Up)
&& inputManager.IsPressed(InputManager::Down)) {
this->entityManager->Get("test0")->velocity.x = 0;
this->entityManager->Get("test0")->velocity.y = 0;
}
if(inputManager.IsPressed(InputManager::Left)
&& inputManager.IsPressed(InputManager::Right)) {
this->entityManager->Get("test0")->velocity.x = 0;
this->entityManager->Get("test0")->velocity.y = 0;
}
// if(inputManager.IsPressed(InputManager::Up)
// && inputManager.IsPressed(InputManager::Right)) {
// this->entityManager->Get("test0")->velocity.y = -1.125;
// this->entityManager->Get("test0")->velocity.x = 1.125;
// }
this->entityManager->Update(); this->entityManager->Update();
if(inputManager.IsPressed(InputManager::LoadMap)) { if(inputManager.IsPressed(InputManager::LoadMap)) {
+2
View File
@@ -7,6 +7,7 @@
#include "entitymanager.h" #include "entitymanager.h"
#include "inputmanager.h" #include "inputmanager.h"
#include "mapload.h" #include "mapload.h"
#include "camera.h"
class MainGame : public GameState class MainGame : public GameState
{ {
@@ -19,6 +20,7 @@ public:
private: private:
EntityManager* entityManager; EntityManager* entityManager;
Map* map; Map* map;
Camera* camera;
}; };
#endif // MAINGAME_H #endif // MAINGAME_H