Browse Source

Started with Camera class

master
Riyyi 9 years ago
parent
commit
7b8807b940
  1. 9
      source.txt
  2. 21
      src/camera.cpp
  3. 19
      src/camera.h
  4. 16
      src/data/map/level1.json
  5. 10
      src/data/map/level1.tmx
  6. 2
      src/main.cpp
  7. 34
      src/map.cpp
  8. 1
      src/mapload.cpp
  9. 6
      src/rpg.pro
  10. 37
      src/state/maingame.cpp
  11. 2
      src/state/maingame.h

9
source.txt

@ -0,0 +1,9 @@
Book
~~~~
Michael Dawson - Beginning C++ Through Game Programming, Third Edition - 2010
Video
~~~~
https://www.youtube.com/user/geraldmcalister/videos

21
src/camera.cpp

@ -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
src/camera.h

@ -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

16
src/data/map/level1.json

File diff suppressed because one or more lines are too long

10
src/data/map/level1.tmx

File diff suppressed because one or more lines are too long

2
src/main.cpp

@ -16,6 +16,8 @@ int main()
settings.antialiasingLevel = 16;
sf::RenderWindow window(sf::VideoMode(1280, 720), gameName, windowStyle, settings);
window.clear(sf::Color(0, 0, 0));
window.display();
//window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);

34
src/map.cpp

@ -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
src/mapload.cpp

@ -56,6 +56,7 @@ void MapLoad::Parser(std::string* content, Map* map) {
tileSheetLocation.replace(0, 2, "data");
sf::Image tileSheet;
tileSheet.loadFromFile(tileSheetLocation);
tileSheet.createMaskFromColor(sf::Color::White);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
sf::Texture* texture = new sf::Texture();

6
src/rpg.pro

@ -21,7 +21,8 @@ SOURCES += main.cpp \
inputmanager.cpp \
state/maingame.cpp \
mapload.cpp \
map.cpp
map.cpp \
camera.cpp
HEADERS += \
inputmanager.h \
@ -30,4 +31,5 @@ HEADERS += \
gamestate.h \
state/maingame.h \
mapload.h \
map.h
map.h \
camera.h

37
src/state/maingame.cpp

@ -18,6 +18,9 @@ void MainGame::Initialize(sf::RenderWindow* window) {
this->map = new Map();
MapLoad mapLoad;
mapLoad.Load(this->map, "data/map/level1.json");
this->camera = new Camera();
this->camera->SetNewView(window);
}
void MainGame::Update(sf::RenderWindow* window) {
@ -27,27 +30,49 @@ void MainGame::Update(sf::RenderWindow* window) {
if(inputManager.IsPressed(InputManager::Left)) {
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)) {
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::Up)) {
std::cout << "UP" << std::endl;
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;
this->entityManager->Get("test0")->velocity.y = 1.5;
this->camera->MoveCamera(window, sf::Vector2f(0, 1.5));
}
if(inputManager.IsPressed(InputManager::Up)) {
std::cout << "UP" << std::endl;
if(inputManager.IsPressed(InputManager::Up)
&& inputManager.IsPressed(InputManager::Down)) {
this->entityManager->Get("test0")->velocity.x = 0;
this->entityManager->Get("test0")->velocity.y = 0;
}
this->entityManager->Get("test0")->velocity.y = -1;
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();
if(inputManager.IsPressed(InputManager::LoadMap)) {

2
src/state/maingame.h

@ -7,6 +7,7 @@
#include "entitymanager.h"
#include "inputmanager.h"
#include "mapload.h"
#include "camera.h"
class MainGame : public GameState
{
@ -19,6 +20,7 @@ public:
private:
EntityManager* entityManager;
Map* map;
Camera* camera;
};
#endif // MAINGAME_H

Loading…
Cancel
Save