Commit ec052adf authored by Laihela's avatar Laihela
Browse files

Alustava pääluokkien toteutus

parent 297332de
/build/
\ No newline at end of file
build/
\ No newline at end of file
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
project(Muistipeli)
add_subdirectory(ext/SFML-2.5.1)
set( SFML_BUILD_AUDIO OFF CACHE BOOL "" FORCE )
add_executable(application application.cpp)
add_subdirectory("ext/SFML-2.5.1")
target_link_libraries(application PUBLIC sfml)
\ No newline at end of file
file(GLOB INC_FILES "include/*.h")
file(GLOB SRC_FILES "src/*.cpp")
include_directories("ext/SFML-2.5.1/include")
add_executable(main ${SRC_FILES} ${INC_FILES} )
target_link_libraries(main sfml-system sfml-graphics)
\ No newline at end of file
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
\ No newline at end of file
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include "../include/GameObject.h"
#include "../include/Component.h"
class Application {
public:
Application(int width, int height);
~Application();
void Application::update();
bool isRunning();
GameObject* newGameObject();
Component* newComponent();
private:
sf::RenderWindow* window;
std::vector<GameObject*> gameObjects;
std::vector<Component*> components;
};
\ No newline at end of file
#pragma once
class Component {
public:
Component();
~Component();
};
\ No newline at end of file
#pragma once
#include <algorithm>
#include <vector>
#include "../include/Component.h"
class GameObject {
public:
GameObject();
~GameObject();
void addComponent(Component* component);
void removeComponent(Component* component);
private:
std::vector<Component*> components;
};
\ No newline at end of file
#include "../include/Application.h"
Application::Application(int width, int height) {
window = new sf::RenderWindow(sf::VideoMode(width, height), "Muistipeli");
gameObjects = std::vector<GameObject*>();
components = std::vector<Component*>();
}
Application::~Application() {
for (auto obj : gameObjects) delete obj;
for (auto comp : components) delete comp;
gameObjects.clear();
components.clear();
}
void Application::update() {
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed)
window->close();
}
window->clear();
window->draw(sf::CircleShape(128.0f));
window->display();
}
GameObject* Application::newGameObject() {
GameObject* obj = new GameObject();
gameObjects.push_back(obj);
return obj;
}
Component* Application::newComponent() {
Component* comp = new Component();
components.push_back(comp);
return comp;
}
bool Application::isRunning() {
return window->isOpen();
}
\ No newline at end of file
#include "../include/Component.h"
Component::Component() {
}
Component::~Component() {
}
\ No newline at end of file
#include "../include/GameObject.h"
GameObject::GameObject() {
components = std::vector<Component*>();
}
GameObject::~GameObject() {
components.clear();
}
void GameObject::addComponent(Component* component) {
components.push_back(component);
}
void GameObject::removeComponent(Component* component) {
std::vector<Component*>::iterator position = std::find(components.begin(), components.end(), component);
if (position != components.end()) {
components.erase(position);
}
}
\ No newline at end of file
#include <SFML/Graphics.hpp>
#include "../include/Application.h"
#include "../include/GameObject.h"
#include "../include/Component.h"
int main() {
Application* app = new Application(256, 256);
GameObject* obj = app->newGameObject();
Component* comp = app->newComponent();
obj->addComponent(comp);
obj->removeComponent(comp);
while (app->isRunning()) {
app->update();
}
delete app;
return 0;
}
\ 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