main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/time.h>
  4. #include <ctime>
  5. #include <GL/glew.h>
  6. #include <GL/glut.h>
  7. #include "card.h"
  8. #include "cardMenu.h"
  9. #include "string3d.h"
  10. #include "shaders.h"
  11. //Functions
  12. char init();
  13. void handleResize(int w, int h);
  14. void update(int value); void handleKeypress(unsigned char key, int x, int y);
  15. void handleKeyrelease(unsigned char key, int x, int y);
  16. void drawScene();
  17. int every;
  18. /* The game is divined into "cards". Each card defines a "3d world".
  19. * The class Card is abstruct, the Card::card pointer should only point to
  20. * subclasses of it.
  21. * Each subclass defines the details of its 3d world and how to draw it.
  22. * Card has an `update()` function, that is called once every frame,
  23. * and may change the card's type (which instance the pointer is pointing to).
  24. * It also has a `draw()` function that draws itself.
  25. */
  26. int main(int argc, char** argv)
  27. {
  28. //Initialize GLUT
  29. glutInit(&argc, argv);
  30. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  31. glutInitWindowSize(800, 400);
  32. //Create window
  33. glutCreateWindow("dargite3d");
  34. if (init())
  35. {
  36. fprintf(stderr, "Error initializing game!\n");
  37. return -1;
  38. }
  39. //Initialize card
  40. Card::card = new CardMenu;
  41. //Handlers
  42. glutReshapeFunc(handleResize);
  43. glutKeyboardFunc(handleKeypress);
  44. glutKeyboardUpFunc(handleKeyrelease);
  45. glutDisplayFunc(drawScene);
  46. //Start update
  47. glutTimerFunc(25, update, 0);
  48. //Main loop
  49. glutMainLoop();
  50. return 0;
  51. }
  52. char init()
  53. {
  54. //Init glew
  55. glewInit();
  56. //Use for drawing everything depending on z
  57. glEnable(GL_DEPTH_TEST);
  58. //glEnable(GL_CULL_FACE);
  59. //Clear color
  60. glClearColor(0.7f, 0.9f, 1.0f, 1.0f);
  61. //Init string
  62. string3d::init();
  63. //Ignore this (DEBUG)
  64. every = 30;
  65. //Shaders
  66. GLuint program = load_program("shaders/vertex.glsl", "shaders/fragment.glsl");
  67. if (!program) return -1;
  68. glUseProgram(program);
  69. //Everything is ok
  70. return 0;
  71. }
  72. void handleResize(int w, int h)
  73. {
  74. //Tell openGL how to convert from coordinates to pixel values
  75. glViewport(0, 0, w, h);
  76. glMatrixMode(GL_PROJECTION); //switch to setting camera perspective
  77. //Set camera perspective
  78. glLoadIdentity(); //Reset camera
  79. gluPerspective(45.0, //Camera angle
  80. (double)w / (double)h, //width-to-height ratio
  81. 1.0, //near z clipping coordinate
  82. 200.0); //far z clipping coordinate
  83. }
  84. void handleKeypress(unsigned char key, int x, int y)
  85. {
  86. //Pass input to card
  87. Card::card->input(key, 1);
  88. }
  89. void handleKeyrelease(unsigned char key, int x, int y)
  90. {
  91. //Pass input to card
  92. Card::card->input(key, 0);
  93. }
  94. void update(int value)
  95. {
  96. //Update
  97. Card::card->update();
  98. //Re-draw
  99. glutPostRedisplay();
  100. //Run again in 25 milliseconds
  101. glutTimerFunc(25, update, 0);
  102. }
  103. void drawScene()
  104. {
  105. //Get frame start
  106. //clock_t start = clock();
  107. //Clear
  108. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109. //Modelview
  110. glMatrixMode(GL_MODELVIEW);
  111. glLoadIdentity();
  112. //Card
  113. Card::card->draw();
  114. //Swap buffers
  115. glutSwapBuffers();
  116. //glFinish();
  117. //Calculate frame end
  118. /*clock_t elapsed = clock() -start;
  119. if (++every >= 10)
  120. {
  121. printf("%d\n", elapsed);
  122. every = 0;
  123. }*/
  124. }