-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
91 lines (77 loc) · 3.2 KB
/
main.cpp
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
#include <iostream>
#include <vector>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <sys/time.h>
#include <GL/glut.h>
#include "main.h"
#include "camera.h"
#include "renderable.h"
#include "fileIO.h"
#include "world.h"
#include "mob.h"
#include "inventory.h"
long int getCurrentTime(){
struct timeval tp;
gettimeofday(&tp, NULL);
long int currentTime = tp.tv_sec * 1000 + tp.tv_usec / 1000;
return currentTime;
}
int main(int argc, char **argv){
glutInit(&argc, argv);
Camera *camera = new Camera(800, 600);
double msPerTick = 1000.0 / Settings::tickRate, fps = 0;
long int currentTime = getCurrentTime();
long int timeSinceFPSCalculation = currentTime;
int frames = 0;
VOX_Graphics::textureAtlas = VOX_FileIO::loadBitmapTexture("res/textures.bmp");
VOX_World::blocks = VOX_FileIO::initBlocks();
VOX_Inventory::items = VOX_FileIO::initItems();
VOX_World::biomes = VOX_FileIO::initBiomes();
VOX_World::World *world = new VOX_World::World(1337);
VOX_Mob::Player *player = new VOX_Mob::Player(world, 2.0f, 90.f, 2.0f);
world->setPlayer(player);
camera->setFollowing(player);
bool running = true;
while (running){
running = camera->handleEvents();
camera->preRender();
// Strictly 3D space rendering
world->render();
// Text & HUD rendering
camera->pre2DRender();
VOX_Graphics::renderString(8, camera->height - 13, std::string("FPS: ") + std::to_string(fps));
sf::Vector3f playerPos = player->getPosition();
VOX_Graphics::renderString(8, camera->height - 26, std::string("[x,y,z]: ") + std::to_string(playerPos.x)
+ ", " + std::to_string(playerPos.y) + ", " + std::to_string(playerPos.z));
sf::Vector3f lookingAt = player->getLookingAt();
VOX_World::BlockData *blockLookingAt = world->getBlock(lookingAt.x, lookingAt.y, lookingAt.z);
VOX_Graphics::renderString(8, camera->height - 39, std::string("Looking at: ") +
BLOCK_NAME(lookingAt.x, lookingAt.y, lookingAt.z) + " (" + std::to_string(blockLookingAt->lighting) + ":" +
std::to_string(blockLookingAt->other) + ":" + std::to_string(blockLookingAt->meta) + ":"
+ std::to_string(blockLookingAt->id) + "): " + std::to_string(lookingAt.x) +
", " + std::to_string(lookingAt.y) + ", " + std::to_string(lookingAt.z));
VOX_Graphics::renderString(8, camera->height - 52, std::string("Faces Rendered : ") +
std::to_string(world->facesRendered));
player->renderInventory(camera->width);
// Cleanup
camera->postRender();
frames ++;
while((currentTime + msPerTick) < getCurrentTime()){
if(!camera->focused) continue;
player->setMouseChange(camera->getRelativeMousePosition());
player->update();
camera->update();
currentTime += msPerTick;
world->update();
}
if(frames > 100){
fps = (frames / ((getCurrentTime() - timeSinceFPSCalculation) / 1000.0));
timeSinceFPSCalculation = currentTime;
frames = 0;
}
}
delete camera;
delete world;
return 0;
}