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)
project(Muistipeli)
set( SFML_BUILD_AUDIO OFF CACHE BOOL "" FORCE )
set( BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE )
add_subdirectory("ext/SFML-2.5.1")
......
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include <string>
#include "../include/GameObject.h"
#include "../include/Component.h"
#include "../include/Sprite.h"
class Application {
public:
Application(int width, int height);
~Application();
void Application::update();
void update(float delta);
void renderObjects();
bool isRunning();
GameObject* newGameObject();
Component* newComponent();
private:
sf::RenderWindow* window;
GameObject* newGameObject(std::string name = "GameObject");
Sprite* newSprite(std::string imagePath);
std::vector<GameObject*> gameObjects;
std::vector<Component*> components;
private:
void onWindowResize();
void onMouseClick(sf::Vector2i* position);
sf::RenderWindow* window;
};
\ No newline at end of file
#pragma once
#include <SFML/Graphics.hpp>
class Component {
public:
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
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <vector>
#include <string>
#include "../include/Component.h"
class GameObject {
friend class Application;
public:
GameObject();
GameObject(std::string _name = "GameObject");
~GameObject();
void addComponent(Component* component);
void removeComponent(Component* component);
sf::Transform getTransform();
sf::Vector2f position;
sf::Vector2f scale;
float rotation;
std::string name;
bool isClickable;
private:
void update(float delta);
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"
#pragma warning( disable : 4244 ) // conversion from int to T, possible loss of data
Application::Application(int width, int height) {
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*>();
components = std::vector<Component*>();
}
Application::~Application() {
for (auto obj : gameObjects) delete obj;
for (auto comp : components) delete comp;
for (auto gameObject : gameObjects) delete gameObject;
for (auto component : components) delete component;
gameObjects.clear();
components.clear();
}
void Application::update() {
void Application::update(float delta) {
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed)
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->draw(sf::CircleShape(128.0f));
for (auto gameObject : gameObjects) {
for (auto component : gameObject->components) {
component->draw(window, gameObject->getTransform());
}
}
window->display();
}
GameObject* Application::newGameObject() {
GameObject* obj = new GameObject();
GameObject* Application::newGameObject(std::string name) {
GameObject* obj = new GameObject(name);
gameObjects.push_back(obj);
return obj;
}
Component* Application::newComponent() {
Component* comp = new Component();
components.push_back(comp);
return comp;
Sprite* Application::newSprite(std::string imagePath) {
Sprite* spr = new Sprite(imagePath);
components.push_back(spr);
return spr;
}
bool Application::isRunning() {
......
......@@ -6,4 +6,12 @@ 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"
GameObject::GameObject() {
GameObject::GameObject(std::string _name) {
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() {
components.clear();
}
void GameObject::update(float delta) {
for (auto component : components) {
component->update(delta);
}
}
void GameObject::addComponent(Component* component) {
components.push_back(component);
}
......@@ -17,4 +29,8 @@ void GameObject::removeComponent(Component* component) {
if (position != components.end()) {
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 @@
#include "../include/Application.h"
#include "../include/GameObject.h"
#include "../include/Component.h"
#include "../include/Sprite.h"
void NewCard(float x, float y, Sprite* sprite);
Application* app;
int cards = 0;
int main() {
Application* app = new Application(256, 256);
GameObject* obj = app->newGameObject();
Component* comp = app->newComponent();
obj->addComponent(comp);
obj->removeComponent(comp);
app = new Application(1280, 720);
sf::Clock clock;
Sprite* cardSprite = app->newSprite("card.png");
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()) {
app->update();
float delta = clock.restart().asSeconds();
//app->gameObjects[0]->rotation += delta * 90.0f;
app->update(delta);
app->renderObjects();
}
delete app;
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