-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
61 lines (56 loc) · 1.64 KB
/
Game.cpp
File metadata and controls
61 lines (56 loc) · 1.64 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
/*
Game class function definitions
*/
// Include libraries
#include <SFML/Graphics.hpp>
// Include local header files
#include "ImgObject.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-ImageManip");
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 ImgObject rect_obj
rect_obj = new ImgObject();
}
// 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();
}
}
// Shift pixels in image and put image into texture
rect_obj->shiftPixels();
rect_obj->loadImgIntoTex();
// Render Game
renderGame();
}
}
// Render Game
void Game::renderGame() {
// Clear screen
win->clear();
// Draw image
rect_obj->renderImgObject(win);
// Display all objects drawn to screen
win->display();
}
// Delete all Game related objects
Game::~Game() {
// Delete char objects, char object array and window
delete rect_obj;
delete win;
}