main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include "Crown.h"
  2. #include <time.h>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <ft2build.h>
  6. #include FT_FREETYPE_H
  7. #include FT_GLYPH_H
  8. #include "WorldTerrain.h"
  9. #include "perlin.h"
  10. void DrawAxis();
  11. void DrawGrid(float xStart, float zStart, float xStep, float zStep);
  12. void DrawCube(float x, float y, float z);
  13. void DrawVoxelGrid(float xStart, float yStart, float zStart, float xStep, float yStep, float zStep);
  14. using namespace Crown;
  15. MovableCamera* cam;
  16. WorldTerrain* terrain;
  17. int groundLevel = 0;
  18. int seed;
  19. int octaves = 4, freq = 2;
  20. int currentFog = 10;
  21. float fogDensity = 0.1f;
  22. bool attached = true;
  23. class MainScene: public Crown::Scene, EventReceiver {
  24. public:
  25. MainScene()
  26. {
  27. GetDevice()->GetEventDispatcher()->RegisterEventReceiver(this);
  28. mDevice = GetDevice();
  29. }
  30. virtual ~MainScene()
  31. {
  32. if (terrain)
  33. delete terrain;
  34. }
  35. void HandleEvent(const Event& event)
  36. {
  37. if (event.event_type == ET_KEYBOARD)
  38. {
  39. if (event.keyboard.type == KET_RELEASED)
  40. {
  41. if (event.keyboard.key == KK_ESCAPE)
  42. {
  43. if (mDevice) {
  44. mDevice->StopRunning();
  45. }
  46. }
  47. if (event.keyboard.key == KK_SPACE) {
  48. seed = rand();
  49. terrain->RandomMap(groundLevel, seed, octaves, freq);
  50. }
  51. if (event.keyboard.key == KK_UP) {
  52. groundLevel += 1;
  53. terrain->RandomMap(groundLevel, seed, octaves, freq);
  54. }
  55. if (event.keyboard.key == KK_DOWN) {
  56. groundLevel -= 1;
  57. terrain->RandomMap(groundLevel, seed, octaves, freq);
  58. }
  59. if (event.keyboard.key == KK_R) {
  60. octaves += 1;
  61. terrain->RandomMap(groundLevel, seed, octaves, freq);
  62. }
  63. if (event.keyboard.key == KK_F) {
  64. if (octaves > 1)
  65. octaves -= 1;
  66. terrain->RandomMap(groundLevel, seed, octaves, freq);
  67. }
  68. if (event.keyboard.key == KK_T) {
  69. freq += 1;
  70. terrain->RandomMap(groundLevel, seed, octaves, freq);
  71. }
  72. if (event.keyboard.key == KK_G) {
  73. if (freq > 1)
  74. freq -= 1;
  75. terrain->RandomMap(groundLevel, seed, octaves, freq);
  76. }
  77. if (event.keyboard.key == KK_Y) {
  78. currentFog+=10;
  79. glFogf(GL_FOG_END, currentFog);
  80. }
  81. if (event.keyboard.key == KK_H) {
  82. currentFog-=10;
  83. glFogf(GL_FOG_END, currentFog);
  84. }
  85. if (event.keyboard.key == KK_1) {
  86. terrain->ToggleGrid();
  87. }
  88. if (event.keyboard.key == KK_2) {
  89. terrain->CycleViewDistance();
  90. }
  91. if (event.keyboard.key == KK_3) {
  92. static bool fogActive = true;
  93. if (fogActive)
  94. glDisable(GL_FOG);
  95. else
  96. glEnable(GL_FOG);
  97. fogActive = ! fogActive;
  98. }
  99. if (event.keyboard.key == KK_0) {
  100. attached = !attached;
  101. }
  102. }
  103. if (event.keyboard.key == KK_Q)
  104. {
  105. terrain->LOL();
  106. }
  107. }
  108. }
  109. virtual void OnLoad() {
  110. SceneNode* sn = new SceneNode(this, NULL, Vec3(), Angles(), true);
  111. cam = AddMovableCamera(
  112. sn,
  113. Vec3((CHUNKS_COUNT+2)/2.0f*CHUNK_SIZE*CubeSize, CHUNKS_COUNT_H*0.7*CHUNK_SIZE*CubeSize, (CHUNKS_COUNT+2)/2.0f*CHUNK_SIZE*CubeSize),
  114. //Vec3(0, 1.5*CHUNK_SIZE*CubeSize, 0),
  115. Angles(0, 0, 0),
  116. true,
  117. 90.0f,
  118. 1.59f,
  119. true,
  120. 1.5,
  121. 1);
  122. cam->SetActive(true);
  123. cam->SetSpeed(0.1);
  124. cam->SetFarClipDistance(150.0f);
  125. GetDevice()->GetRenderer()->SetClearColor(Color(0.457f, 0.754f, 1.0f, 1.0f));
  126. srand(time(NULL));
  127. seed = rand();
  128. terrain = new WorldTerrain();
  129. terrain->RandomMap(groundLevel, seed, octaves, freq);
  130. timer = Crown::Timer::GetInstance();
  131. beforeTime = timer->GetMilliseconds();
  132. }
  133. virtual void RenderScene() {
  134. Scene::RenderScene();
  135. cam->Render();
  136. Mat4 identity;
  137. identity.LoadIdentity();
  138. GetDevice()->GetRenderer()->SetMatrix(MT_MODEL, identity);
  139. DrawAxis();
  140. if (attached)
  141. terrain->SetPlayerPosition(cam->GetPosition());
  142. terrain->Render();
  143. if (timer->GetMilliseconds() - beforeTime > 1000)
  144. {
  145. Renderer* renderer = mDevice->GetRenderer();
  146. Str title = "Fps: " + Str(renderer->GetFPS()) + " - Regenerated Chunks: " + Str(terrain->RegeneratedChunks) + " - Px: " + Str(terrain->GetPlayerPosition().x) + ", Px: " + Str(terrain->GetPlayerPosition().z);
  147. terrain->RegeneratedChunks = 0;
  148. GetDevice()->GetMainWindow()->SetTitle(title.c_str());
  149. beforeTime = timer->GetMilliseconds();
  150. }
  151. }
  152. Device* mDevice;
  153. Crown::Timer* timer;
  154. Crown::ulong beforeTime;
  155. };
  156. int main(int argc, char** argv) {
  157. int wndW, wndH;
  158. wndW = 1024;
  159. wndH = 768;
  160. bool full = false;
  161. if (argc == 3) {
  162. wndW = atoi(argv[1]);
  163. wndH = atoi(argv[2]);
  164. }
  165. Device* device = GetDevice();
  166. if (!device->Init(wndW, wndH, 32, false)) {
  167. return 0;
  168. }
  169. Renderer* renderer = device->GetRenderer();
  170. SceneManager* smgr = device->GetSceneManager();
  171. ResourceManager* resman = device->GetResourceManager();
  172. device->GetMainWindow()->SetTitle("Crown Engine v0.1");
  173. device->GetMainWindow()->GetCursorControl()->SetVisible(true);
  174. device->GetMainWindow()->SetFullscreen(full);
  175. Scene* scene = new MainScene();
  176. smgr->SelectNextScene(scene);
  177. device->Run();
  178. device->Shutdown();
  179. // Scene* scene = new Scene();
  180. //
  181. // cam = scene->AddMovableCamera(
  182. // 0,
  183. // Vec3((CHUNKS_COUNT+2)/2.0f*CHUNK_SIZE*CubeSize, CHUNKS_COUNT_H*0.7*CHUNK_SIZE*CubeSize, (CHUNKS_COUNT+2)/2.0f*CHUNK_SIZE*CubeSize),
  184. // //Vec3(0, 1.5*CHUNK_SIZE*CubeSize, 0),
  185. // Angles(0, 0, 0),
  186. // Vec3(1, 1, 1),
  187. // true,
  188. // 90.0f,
  189. // 1.59f,
  190. // true,
  191. // 1.5,
  192. // 1);
  193. // cam->SetActive(true);
  194. // cam->SetSpeed(0.1);
  195. // cam->SetFarClipDistance(150.0f);
  196. //
  197. // std::cout << "Entity count: " << scene->GetEntityCount() << std::endl;
  198. // std::cout << "Light count: " << scene->GetLightCount() << std::endl;
  199. // std::cout << "Sizeof RenderWindow: " << sizeof(RenderWindow) << std::endl;
  200. ///*
  201. // Material material;
  202. // material.mAmbient = Color(0.1, 0.1, 0.1, 1);
  203. // material.mDiffuse = Color(1, 1, 1, 1);
  204. // material.mSpecular = Color(0, 0, 0, 1);
  205. // material.mShininess = 128;
  206. // material.mSeparateSpecularColor = true;
  207. // material.mTexturing = false;
  208. // material.mLighting = true;
  209. //
  210. // renderer->SetMaterial(material);
  211. //*/
  212. // Mat4 identity;
  213. // identity.LoadIdentity();
  214. //
  215. // renderer->SetClearColor(Color(0.457f, 0.754f, 1.0f, 1.0f));
  216. //
  217. // Mat4 ortho;
  218. // ortho.BuildProjectionOrthoRH(wndW, wndH, 1, -1);
  219. //
  220. // Mat4 perspective;
  221. // perspective.BuildProjectionPerspectiveRH(90.0f, 1.59f, 0.1f, 100.0f);
  222. //
  223. // Mat4 text;
  224. // text.LoadIdentity();
  225. // text.SetTranslation(Vec3(400, 350, 0));
  226. //
  227. //
  228. // //Crown::Font font;
  229. // //Image* testImg = font.LoadFont("tests/font/arialbd.ttf");
  230. //
  231. // glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  232. //
  233. // srand(time(NULL));
  234. // seed = rand();
  235. // terrain = new WorldTerrain();
  236. // terrain->RandomMap(groundLevel, seed, octaves, freq);
  237. // //terrain->RandomMap1();
  238. //
  239. // while (device->IsRunning()) {
  240. //
  241. // WindowEventHandler::GetInstance()->ManageEvents();
  242. // GetDevice()->GetMainWindow()->GetRenderContext()->MakeCurrent();
  243. //
  244. // // ----------- Begin Scene -----------
  245. // renderer->_BeginFrame();
  246. //
  247. // renderer->SetMatrix(MT_PROJECTION, ortho);
  248. // //renderer->SetMatrix(MT_MODEL, text);
  249. //
  250. // glDisable(GL_LIGHTING);
  251. // glColor3f(1, 1, 1);
  252. //
  253. // cam->Render();
  254. //
  255. // renderer->SetMatrix(MT_MODEL, identity);
  256. //
  257. // DrawAxis();
  258. //
  259. // if (attached)
  260. // terrain->SetPlayerPosition(cam->GetPosition());
  261. //
  262. // terrain->Render();
  263. //
  264. // renderer->_EndFrame();
  265. // // ----------- End Scene -------------
  266. //
  267. // GetDevice()->GetMainWindow()->Update();
  268. // GetDevice()->GetMainWindow()->SetTitle(Str(renderer->GetFPS()).c_str());
  269. // }
  270. //
  271. // delete terrain;
  272. //
  273. // device->Shutdown();
  274. return 0;
  275. }
  276. void DrawAxis() {
  277. glBegin(GL_LINES);
  278. glColor3f(1, 0, 0);
  279. glVertex3f(0, 0, 0);
  280. glVertex3f(1000, 0, 0);
  281. glColor3f(0, 1, 0);
  282. glVertex3f(0, 0, 0);
  283. glVertex3f(0, 1000, 0);
  284. glColor3f(0, 0, 1);
  285. glVertex3f(0, 0, 0);
  286. glVertex3f(0, 0, 1000);
  287. glEnd();
  288. }
  289. void DrawGrid(float xStart, float zStart, float xStep, float zStep) {
  290. glColor3f(.5f, .5f, .5f);
  291. const unsigned int steps = 128;
  292. for (unsigned int i = 0; i <= steps; i++) {
  293. glBegin(GL_LINES);
  294. glVertex3f(xStart + xStep * (float)i, -1.0f, zStart);
  295. glVertex3f(xStart + xStep * (float)i, -1.0f, zStart-steps*zStep);
  296. glEnd();
  297. glBegin(GL_LINES);
  298. glVertex3f(xStart, -1.0f, zStart - zStep * (float)i);
  299. glVertex3f(xStart+steps*xStep, -1.0f, zStart - zStep * (float)i);
  300. glEnd();
  301. }
  302. }
  303. void DrawCube(float x, float y, float z) {
  304. glBegin(GL_QUADS);
  305. //top
  306. glColor3f(.3f, .0f, .0f);
  307. glVertex3f(x , y + CubeSize, z);
  308. glColor3f(.25f, .0f, .0f);
  309. glVertex3f(x + CubeSize, y + CubeSize, z);
  310. glColor3f(.3f, .0f, .0f);
  311. glVertex3f(x + CubeSize, y + CubeSize, z - CubeSize);
  312. glColor3f(.25f, .0f, .0f);
  313. glVertex3f(x , y + CubeSize, z - CubeSize);
  314. //bottom
  315. glColor3f(.0f, .0f, .3f);
  316. glVertex3f(x , y, z);
  317. glColor3f(.0f, .0f, .25f);
  318. glVertex3f(x , y, z - CubeSize);
  319. glColor3f(.0f, .0f, .3f);
  320. glVertex3f(x + CubeSize, y, z - CubeSize);
  321. glColor3f(.0f, .0f, .25f);
  322. glVertex3f(x + CubeSize, y, z);
  323. //left
  324. glColor3f(.0f, .0f, .3f);
  325. glVertex3f(x, y , z);
  326. glColor3f(.0f, .0f, .25f);
  327. glVertex3f(x, y + CubeSize, z);
  328. glColor3f(.0f, .0f, .3f);
  329. glVertex3f(x, y + CubeSize, z - CubeSize);
  330. glColor3f(.0f, .0f, .25f);
  331. glVertex3f(x, y , z - CubeSize);
  332. //right
  333. glColor3f(.0f, .0f, .3f);
  334. glVertex3f(x + CubeSize, y , z);
  335. glColor3f(.0f, .0f, .25f);
  336. glVertex3f(x + CubeSize, y , z - CubeSize);
  337. glColor3f(.0f, .0f, .3f);
  338. glVertex3f(x + CubeSize, y + CubeSize, z - CubeSize);
  339. glColor3f(.0f, .0f, .25f);
  340. glVertex3f(x + CubeSize, y + CubeSize, z);
  341. //front
  342. glColor3f(.0f, .0f, .3f);
  343. glVertex3f(x , y , z);
  344. glColor3f(.0f, .0f, .25f);
  345. glVertex3f(x + CubeSize, y , z);
  346. glColor3f(.0f, .0f, .3f);
  347. glVertex3f(x + CubeSize, y + CubeSize, z);
  348. glColor3f(.0f, .0f, .25f);
  349. glVertex3f(x , y + CubeSize, z);
  350. //back
  351. glColor3f(.0f, .0f, .3f);
  352. glVertex3f(x , y , z - CubeSize);
  353. glColor3f(.0f, .0f, .25f);
  354. glVertex3f(x , y + CubeSize, z - CubeSize);
  355. glColor3f(.0f, .0f, .3f);
  356. glVertex3f(x + CubeSize, y + CubeSize, z - CubeSize);
  357. glColor3f(.0f, .0f, .25f);
  358. glVertex3f(x + CubeSize, y , z - CubeSize);
  359. glEnd();
  360. }
  361. void DrawVoxelGrid(float xStart, float yStart, float zStart, float xStep, float yStep, float zStep) {
  362. glColor3f(.5f, .5f, .5f);
  363. const unsigned int steps = 16;
  364. glBegin(GL_LINES);
  365. for (unsigned int i = 0; i <= steps; i++) {
  366. for(unsigned int j = 0; j <= steps; j++) {
  367. glVertex3f(xStart + xStep * (float)i, j * yStep, zStart);
  368. glVertex3f(xStart + xStep * (float)i, j * yStep, zStart-steps*zStep);
  369. glVertex3f(xStart + xStep * (float)j, yStart, zStart - zStep * (float)i);
  370. glVertex3f(xStart + xStep * (float)j, yStart+steps*yStep, zStart - zStep * (float)i);
  371. glVertex3f(xStart, j * yStep, zStart - zStep * (float)i);
  372. glVertex3f(xStart+steps*xStep, j * yStep, zStart - zStep * (float)i);
  373. }
  374. }
  375. glEnd();
  376. }