created Game class; main.cpp calls the Game class to render the game

This commit is contained in:
2024-12-17 02:41:42 +01:00
parent 6c9fdc5dc7
commit 01804fd008
5 changed files with 110 additions and 21 deletions

6
.gitignore vendored
View File

@@ -14,4 +14,8 @@ Chinczyk188/res/SFML
# EN: Executable created by g++ (mingw). # EN: Executable created by g++ (mingw).
# 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
View 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
View 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();
};

View File

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

View File

@@ -1,24 +1,16 @@
#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 (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(); while(game.running()) {
window.draw(shape);
window.display();
}
return 0; game.update();
game.render();
}
return 0;
} }