created Game class; main.cpp calls the Game class to render the game
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -15,3 +15,7 @@ Chinczyk188/res/SFML
|
|||||||
# PL: Plik wykonywalny tworzony przez g++ (mingw).
|
# PL: Plik wykonywalny tworzony przez g++ (mingw).
|
||||||
output.exe
|
output.exe
|
||||||
Chinczyk188/output.exe
|
Chinczyk188/output.exe
|
||||||
|
|
||||||
|
# EN: Don't include include directory with Doxygen documentation.
|
||||||
|
# PL: Aby nie brać pod uwagę katalogu include z dokumentacją Doxygen.
|
||||||
|
Chiczyk188/include
|
||||||
71
Chinczyk188/Game.cpp
Normal file
71
Chinczyk188/Game.cpp
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
#include "Game.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void Game::initVariables() {
|
||||||
|
|
||||||
|
this->window = nullptr;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::initWindow() {
|
||||||
|
|
||||||
|
this->videoMode = sf::VideoMode::getDesktopMode();
|
||||||
|
this->videoMode.width /= 2;
|
||||||
|
this->videoMode.height /= 2;
|
||||||
|
|
||||||
|
this->window = new sf::RenderWindow(this->videoMode, "Chinczyk188", sf::Style::Default);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Game::Game() {
|
||||||
|
|
||||||
|
this->initVariables();
|
||||||
|
this->initWindow();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Game::~Game() {
|
||||||
|
|
||||||
|
delete this->window;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool Game::running() const {
|
||||||
|
|
||||||
|
return this->window->isOpen();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::pollEvents() {
|
||||||
|
|
||||||
|
while (this->window->pollEvent(this->ev)) {
|
||||||
|
switch (this->ev.type) {
|
||||||
|
case sf::Event::Closed:
|
||||||
|
this->window->close();
|
||||||
|
break;
|
||||||
|
case sf::Event::KeyPressed:
|
||||||
|
if (this->ev.key.code == sf::Keyboard::Escape) this->window->close();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::update() {
|
||||||
|
|
||||||
|
this->pollEvents();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::render() {
|
||||||
|
|
||||||
|
// EN: Render logic
|
||||||
|
// PL: Mechanizm renderowania
|
||||||
|
this->window->clear(sf::Color(255, 0, 0, 255));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// PL: Mechanizm wyświetlania
|
||||||
|
// W celu wyświetlenia na ekranie wyniku renderowania
|
||||||
|
this->window->display();
|
||||||
|
|
||||||
|
}
|
||||||
22
Chinczyk188/Game.hpp
Normal file
22
Chinczyk188/Game.hpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
class Game {
|
||||||
|
private:
|
||||||
|
sf::RenderWindow* window;
|
||||||
|
sf::VideoMode videoMode;
|
||||||
|
sf::Event ev;
|
||||||
|
|
||||||
|
void initVariables();
|
||||||
|
void initWindow();
|
||||||
|
|
||||||
|
public:
|
||||||
|
Game();
|
||||||
|
virtual ~Game();
|
||||||
|
|
||||||
|
const bool running() const;
|
||||||
|
void pollEvents();
|
||||||
|
|
||||||
|
void update();
|
||||||
|
void render();
|
||||||
|
};
|
||||||
@@ -9,7 +9,7 @@ OUTPUT = output.exe
|
|||||||
CPPSTD = c++17
|
CPPSTD = c++17
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$(CC) -g "*.cpp" $(CFLAGS) $(DEPS) $(LINK) -std=$(CPPSTD) -static -static-libgcc -fno-keep-inline-dllexport -o $(OUTPUT)
|
$(CC) -g "*.cpp" $(DEPS) $(LINK) -std=$(CPPSTD) -static -static-libgcc -fno-keep-inline-dllexport -o $(OUTPUT)
|
||||||
|
|
||||||
run: default
|
run: default
|
||||||
$(OUTPUT)
|
$(OUTPUT)
|
||||||
|
|||||||
@@ -1,23 +1,15 @@
|
|||||||
#include <SFML/Graphics.hpp>
|
#include "Game.hpp"
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
|
|
||||||
sf::CircleShape shape(100.f);
|
|
||||||
shape.setFillColor(sf::Color::Green);
|
|
||||||
|
|
||||||
while (window.isOpen())
|
Game game;
|
||||||
{
|
|
||||||
sf::Event event;
|
while(game.running()) {
|
||||||
while (window.pollEvent(event))
|
|
||||||
{
|
game.update();
|
||||||
if (event.type == sf::Event::Closed)
|
|
||||||
window.close();
|
game.render();
|
||||||
}
|
|
||||||
|
|
||||||
window.clear();
|
|
||||||
window.draw(shape);
|
|
||||||
window.display();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user