Commit a82b6518 authored by TTV19SP Oinonen Henri Elias Kristian's avatar TTV19SP Oinonen Henri Elias Kristian
Browse files

Merge branch 'henkka' into 'master'

Nyt näkyy kortit, jee

See merge request !6
parents 15f26414 c6f62d0d
...@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.2 FATAL_ERROR) ...@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
project(Muistipeli) project(Muistipeli)
set( SFML_BUILD_AUDIO OFF CACHE BOOL "" FORCE ) set( SFML_BUILD_AUDIO OFF CACHE BOOL "" FORCE )
set( BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE )
add_subdirectory("ext/SFML-2.5.1") add_subdirectory("ext/SFML-2.5.1")
......
#pragma once #pragma once
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <vector> #include <vector>
#include <string>
#include "../include/GameObject.h" #include "../include/GameObject.h"
#include "../include/Component.h" #include "../include/Component.h"
#include "../include/Sprite.h"
class Application { class Application {
public: public:
Application(int width, int height); Application(int width, int height);
~Application(); ~Application();
void Application::update(); void update(float delta);
void renderObjects();
bool isRunning(); bool isRunning();
GameObject* newGameObject(); GameObject* newGameObject(std::string name = "GameObject");
Component* newComponent(); Sprite* newSprite(std::string imagePath);
private:
sf::RenderWindow* window;
std::vector<GameObject*> gameObjects; std::vector<GameObject*> gameObjects;
std::vector<Component*> components; std::vector<Component*> components;
private:
void onWindowResize();
void onMouseClick(sf::Vector2i* position);
sf::RenderWindow* window;
}; };
\ No newline at end of file
#pragma once #pragma once
#include <SFML/Graphics.hpp>
class Component { class Component {
public: public:
Component(); Component();
~Component(); virtual ~Component();
virtual void update(float delta);
virtual void draw(sf::RenderWindow* window, sf::Transform transform);
}; };
\ No newline at end of file
#pragma once #pragma once
#include <SFML/Graphics.hpp>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include <string>
#include "../include/Component.h" #include "../include/Component.h"
class GameObject { class GameObject {
friend class Application;
public: public:
GameObject(); GameObject(std::string _name = "GameObject");
~GameObject(); ~GameObject();
void addComponent(Component* component); void addComponent(Component* component);
void removeComponent(Component* component); void removeComponent(Component* component);
sf::Transform getTransform();
sf::Vector2f position;
sf::Vector2f scale;
float rotation;
std::string name;
bool isClickable;
private: private:
void update(float delta);
std::vector<Component*> components; std::vector<Component*> components;
}; };
\ No newline at end of file
#pragma once
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <vector>
#include <string>
#include "../include/Component.h"
class Sprite : public Component {
friend class Application;
private:
Sprite(std::string imagePath);
~Sprite();
void draw(sf::RenderWindow* window, sf::Transform transform);
sf::Sprite spriteObject;
sf::Texture texture;
};
\ No newline at end of file
File added
#include "../include/Application.h" #include "../include/Application.h"
#pragma warning( disable : 4244 ) // conversion from int to T, possible loss of data
Application::Application(int width, int height) { Application::Application(int width, int height) {
window = new sf::RenderWindow(sf::VideoMode(width, height), "Muistipeli"); window = new sf::RenderWindow(sf::VideoMode(width, height), "Muistipeli");
// Move origin to center
window->setView(sf::View(sf::FloatRect(width / -2.0f, height / -2.0f, width, height)));
gameObjects = std::vector<GameObject*>(); gameObjects = std::vector<GameObject*>();
components = std::vector<Component*>(); components = std::vector<Component*>();
} }
Application::~Application() { Application::~Application() {
for (auto obj : gameObjects) delete obj; for (auto gameObject : gameObjects) delete gameObject;
for (auto comp : components) delete comp; for (auto component : components) delete component;
gameObjects.clear(); gameObjects.clear();
components.clear(); components.clear();
} }
void Application::update() { void Application::update(float delta) {
sf::Event event; sf::Event event;
while (window->pollEvent(event)) { while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed) if (event.type == sf::Event::Closed)
window->close(); window->close();
if (event.type == sf::Event::Resized)
onWindowResize();
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
onMouseClick(&sf::Mouse::getPosition(*window));
}
}
void Application::onWindowResize() {
// Update the view so that sprites won't stretch when the window is resized
sf::Vector2u size = window->getSize();
window->setView(sf::View(sf::FloatRect(size.x / -2.0f, size.y / -2.0f, size.x, size.y)));
}
void Application::onMouseClick(sf::Vector2i* position) {
sf::Vector2u size = window->getSize();
printf("MouseClick %f %f\n", position->x - size.x / 2.0f, position->y - size.y / 2.0f);
// Check game objects in reverse order so that objects on top get prioritized
for (auto i = gameObjects.rbegin(); i != gameObjects.rend(); ++i) {
GameObject* gameObject = *i;
if (gameObject->isClickable) {
printf((gameObject->name + '\n').c_str());
// TODO: detect if mouse is inside sprite
}
} }
}
void Application::renderObjects() {
window->clear(); window->clear();
window->draw(sf::CircleShape(128.0f)); for (auto gameObject : gameObjects) {
for (auto component : gameObject->components) {
component->draw(window, gameObject->getTransform());
}
}
window->display(); window->display();
} }
GameObject* Application::newGameObject() { GameObject* Application::newGameObject(std::string name) {
GameObject* obj = new GameObject(); GameObject* obj = new GameObject(name);
gameObjects.push_back(obj); gameObjects.push_back(obj);
return obj; return obj;
} }
Component* Application::newComponent() { Sprite* Application::newSprite(std::string imagePath) {
Component* comp = new Component(); Sprite* spr = new Sprite(imagePath);
components.push_back(comp); components.push_back(spr);
return comp; return spr;
} }
bool Application::isRunning() { bool Application::isRunning() {
......
...@@ -6,4 +6,12 @@ Component::Component() { ...@@ -6,4 +6,12 @@ Component::Component() {
Component::~Component() { Component::~Component() {
}
void Component::update(float delta) {
}
void Component::draw(sf::RenderWindow* window, sf::Transform transform) {
} }
\ No newline at end of file
#include "../include/GameObject.h" #include "../include/GameObject.h"
GameObject::GameObject() { GameObject::GameObject(std::string _name) {
components = std::vector<Component*>(); components = std::vector<Component*>();
name = _name;
isClickable = false;
position = sf::Vector2f(0.0f, 0.0f);
scale = sf::Vector2f(1.0f, 1.0f);
rotation = 0.0f;
} }
GameObject::~GameObject() { GameObject::~GameObject() {
components.clear(); components.clear();
} }
void GameObject::update(float delta) {
for (auto component : components) {
component->update(delta);
}
}
void GameObject::addComponent(Component* component) { void GameObject::addComponent(Component* component) {
components.push_back(component); components.push_back(component);
} }
...@@ -17,4 +29,8 @@ void GameObject::removeComponent(Component* component) { ...@@ -17,4 +29,8 @@ void GameObject::removeComponent(Component* component) {
if (position != components.end()) { if (position != components.end()) {
components.erase(position); components.erase(position);
} }
}
sf::Transform GameObject::getTransform() {
return sf::Transform().translate(position).rotate(rotation).scale(scale);
} }
\ No newline at end of file
#include "../include/Sprite.h"
Sprite::Sprite(std::string imagePath) {
texture.loadFromFile(imagePath);
sf::Vector2u size = texture.getSize();
spriteObject.setTexture(texture);
spriteObject.setOrigin(size.x / 2.0f, size.y / 2.0f);
}
Sprite::~Sprite() {
}
void Sprite::draw(sf::RenderWindow* window, sf::Transform transform) {
window->draw(spriteObject, transform);
}
\ No newline at end of file
...@@ -2,18 +2,46 @@ ...@@ -2,18 +2,46 @@
#include "../include/Application.h" #include "../include/Application.h"
#include "../include/GameObject.h" #include "../include/GameObject.h"
#include "../include/Component.h" #include "../include/Component.h"
#include "../include/Sprite.h"
void NewCard(float x, float y, Sprite* sprite);
Application* app;
int cards = 0;
int main() { int main() {
Application* app = new Application(256, 256); app = new Application(1280, 720);
GameObject* obj = app->newGameObject(); sf::Clock clock;
Component* comp = app->newComponent();
obj->addComponent(comp); Sprite* cardSprite = app->newSprite("card.png");
obj->removeComponent(comp); NewCard(-300.0f, 150.0f, cardSprite);
NewCard(-100.0f, 150.0f, cardSprite);
NewCard(100.0f, 150.0f, cardSprite);
NewCard(300.0f, 150.0f, cardSprite);
NewCard(-300.0f, -150.0f, cardSprite);
NewCard(-100.0f, -150.0f, cardSprite);
NewCard(100.0f, -150.0f, cardSprite);
NewCard(300.0f, -150.0f, cardSprite);
for (auto gameObject : app->gameObjects) {
gameObject->isClickable = true;
}
while (app->isRunning()) { while (app->isRunning()) {
app->update(); float delta = clock.restart().asSeconds();
//app->gameObjects[0]->rotation += delta * 90.0f;
app->update(delta);
app->renderObjects();
} }
delete app; delete app;
return 0; return 0;
}
void NewCard(float x, float y, Sprite* sprite) {
GameObject* card = app->newGameObject("Card " + std::to_string(++cards));
card->addComponent(sprite);
card->scale = sf::Vector2f(0.5f, 0.5f);
card->position = sf::Vector2f(x, y);
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment