-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
68 lines (63 loc) · 2.02 KB
/
Copy pathGame.cpp
File metadata and controls
68 lines (63 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Game class function definitions
*/
// Include libraries
#include <SFML/Graphics.hpp>
// Include local header files
#include "Object3D.h"
#include "Game.h"
// Create Game object with certain screen settings
Game::Game() {
// Create 640x480 screen and scale 320x240 to it
win = new sf::RenderWindow(sf::VideoMode({640, 480}), "SFML-3DRenderer");
sf::View view(sf::Vector2f(160, 120), sf::Vector2f(320, 240));
win->setView(view);
win->setPosition(
sf::Vector2i(sf::VideoMode::getDesktopMode().size.x / 2 - 320,
sf::VideoMode::getDesktopMode().size.y / 2 - 240));
// Set framelimit to 60fps (Cannot be on at same time as vsync)
// win->setFramerateLimit(60);
win->setVerticalSyncEnabled(true); // Uncomment for vsync
// Create an obj_3d
obj_3d = new Object3D();
}
// Game loop
void Game::gameLoop() {
// While the window is open, check if window has been closed, then render game
while (win->isOpen()) {
// While events are polled, check if window close event has occured, and
// close window if so
while (const std::optional event = win->pollEvent()) {
if (event->is<sf::Event::Closed>()) {
win->close();
}
}
// Calc 2d pos of 3d points
obj_3d->getInputs(sf::Keyboard::Key::Left, sf::Keyboard::Key::Right, sf::Keyboard::Key::LShift,
sf::Keyboard::Key::Space, sf::Keyboard::Key::Down, sf::Keyboard::Key::Up,
sf::Keyboard::Key::Q, sf::Keyboard::Key::W, sf::Keyboard::Key::A,
sf::Keyboard::Key::S, sf::Keyboard::Key::Z, sf::Keyboard::Key::X,
sf::Keyboard::Key::E, sf::Keyboard::Key::R);
// Set the 2d position of each 3d point
obj_3d->set2DPosOfPoints();
// Render Game
renderGame();
}
}
// Render Game
void Game::renderGame() {
// Clear screen
win->clear();
// Draw obj_3d
obj_3d->renderObject3D(win);
// Draw text in obj_3d
obj_3d->renderTextObject(win);
// Display all objects drawn to screen
win->display();
}
// Delete all Game related objects
Game::~Game() {
// Delete obj_3d and window
delete obj_3d;
delete win;
}