-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
116 lines (95 loc) · 2.75 KB
/
Copy pathGame.cpp
File metadata and controls
116 lines (95 loc) · 2.75 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// Created by luniminex on 10/7/23.
//
#include <fstream>
#include "Game.h"
Game::Game() :
grid_(16, 16, 50),
wfc_(std::make_shared<TileGrid>(grid_))
{
handle_ = Handle::GetInstance();
running_ = true;
want_quit_ = false;
keys_['x'] = false; //exit
keys_['w'] = false; //iteration
}
void Game::Start() {
handle_->Init();
FileHandler::Initialize();
CreateWindow();
handle_->CreateRenderer(SDL_RENDERER_ACCELERATED);
LoadTiles();
wfc_.Start();
Play();
}
void Game::Play() {
const float fps = 60.f;
const float delay = 1000.f / fps;
while(running_){
Uint64 current = SDL_GetTicks64();
HandleEvents();
//update
wfc_.Iteration();
//draw
SDL_SetRenderTarget(handle_->GetRenderer(), nullptr);
SDL_SetRenderDrawColor(handle_->GetRenderer(), 128, 128, 128, 255);
SDL_RenderClear(handle_->GetRenderer());
Draw();
SDL_RenderPresent(handle_->GetRenderer());
Uint64 frameTime = SDL_GetTicks64() - current;
if(static_cast<float>(frameTime) < delay){
SDL_Delay(static_cast<Uint32>(delay - static_cast<float>(frameTime)));
}
CheckQuit();
}
Quit();
}
void Game::HandleEvents() {
SDL_Event event;
while (SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
std::cout<<"Wants to end"<<std::endl;
want_quit_ = true;
}
else if(event.type == SDL_KEYDOWN){
switch (event.key.keysym.sym) {
case SDLK_e:
keys_['e'] = true;
want_quit_ = true;
}
}
}
}
void Game::CheckQuit() {
if(want_quit_){
running_ = false;
}
}
void Game::Quit() {
SDL_Quit();
}
void Game::DrawGrid() {
SDL_SetRenderDrawColor(handle_->GetRenderer(), 0, 0, 0, 255);
int width = handle_->GetWindowInfo().width;
int height = handle_->GetWindowInfo().height;
int cellSize = static_cast<int>(grid_.GetCellSize());
for(int i = 1; i < grid_.GetGridSize().x; i++){
SDL_RenderDrawLine(handle_->GetRenderer(),0, i*cellSize,width,i*cellSize);
}
for(int i = 1; i < grid_.GetGridSize().y; i++){
SDL_RenderDrawLine(handle_->GetRenderer(),i*cellSize, 0,i*cellSize,height);
}
}
void Game::CreateWindow() {
int width = static_cast<int>(grid_.GetGridSize().x * grid_.GetCellSize());
int height = static_cast<int>(grid_.GetGridSize().y * grid_.GetCellSize());
handle_->CreateWindow("WaveFunctionCollapse", width, height, SDL_WINDOW_SHOWN);
}
void Game::LoadTiles() {
wfc_.LoadTiles("../tilesets/circles");
}
void Game::Draw() {
DrawGrid();
//if(wfc_.IsFinished())
wfc_.DrawCollapsedTiles(handle_->GetRenderer());
}