A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.cc 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <cygnet/Window.h>
  2. #include <cygnet/GlWrappers.h>
  3. #include <cygnet/builtins.h>
  4. #include <cygnet/glutil.h>
  5. #include <cygnet/Image.h>
  6. #include <cygnet/RenderQueue.h>
  7. #include <stdio.h>
  8. #include <memory>
  9. #include <vector>
  10. #include <iostream>
  11. const char *vertexShader = R"(
  12. uniform mat3 transform;
  13. attribute vec2 position;
  14. attribute vec2 texCoord;
  15. varying vec2 v_texCoord;
  16. void main() {
  17. vec3 pos = transform * vec3(position, 0);
  18. gl_Position = vec4(pos.x, pos.y, 0, 1);
  19. v_texCoord = texCoord;
  20. }
  21. )";
  22. const char *fragmentShader = R"(
  23. precision mediump float;
  24. varying vec2 v_texCoord;
  25. uniform sampler2D tex;
  26. void main() {
  27. gl_FragColor = texture2D(tex, v_texCoord);
  28. }
  29. )";
  30. enum class Key {
  31. UP, DOWN, LEFT, RIGHT, NONE,
  32. };
  33. struct State {
  34. bool keys[(int)Key::NONE]{};
  35. Cygnet::Window &win;
  36. Cygnet::GlProgram &program;
  37. Cygnet::RenderQueue &q;
  38. };
  39. class Entity {
  40. public:
  41. virtual ~Entity() = default;
  42. virtual void update(State &state, float dt) = 0;
  43. virtual void draw(State &state) = 0;
  44. };
  45. class PlayerEntity: public Entity {
  46. public:
  47. PlayerEntity(float x, float y): x_(x), y_(y) {}
  48. void update(State &state, float dt) override {
  49. float fx{}, fy{};
  50. if (state.keys[(int)Key::LEFT])
  51. fx -= 1;
  52. if (state.keys[(int)Key::RIGHT])
  53. fx += 1;
  54. if (state.keys[(int)Key::UP])
  55. fy -= 1;
  56. if (state.keys[(int)Key::DOWN])
  57. fy += 1;
  58. fy += vy_ * -0.9;
  59. fx += vx_ * -0.9;
  60. vx_ += fx * dt;
  61. vy_ += fy * dt;
  62. x_ += vx_ * dt;
  63. y_ += vy_ * dt;
  64. }
  65. void draw(State &state) override {
  66. state.q.show(image_.texture(), x_, y_);
  67. }
  68. private:
  69. float x_, y_;
  70. float vx_{}, vy_{};
  71. Cygnet::Image image_{"libcygnet/samples/game/assets/player.png"};
  72. };
  73. Key keyFromSym(SDL_Keysym sym) {
  74. switch (sym.scancode) {
  75. case SDL_SCANCODE_W:
  76. return Key::UP;
  77. case SDL_SCANCODE_A:
  78. return Key::LEFT;
  79. case SDL_SCANCODE_S:
  80. return Key::DOWN;
  81. case SDL_SCANCODE_D:
  82. return Key::RIGHT;
  83. default:
  84. return Key::NONE;
  85. }
  86. }
  87. int main() {
  88. SDL_Init(SDL_INIT_VIDEO);
  89. Cygnet::Deferred<SDL_Quit> sdl;
  90. Cygnet::Window win("Game", 640, 480);
  91. Cygnet::GlProgram program(
  92. Cygnet::GlShader(Cygnet::GlShader::Type::VERTEX, vertexShader),
  93. Cygnet::GlShader(Cygnet::GlShader::Type::FRAGMENT, fragmentShader));
  94. program.use();
  95. Cygnet::RenderQueue q(program, 1/32.0);
  96. State state{
  97. .keys{},
  98. .win = win,
  99. .program = program,
  100. .q = q,
  101. };
  102. std::vector<std::unique_ptr<Entity>> entities;
  103. entities.emplace_back(std::make_unique<PlayerEntity>(0, 0));
  104. SDL_Event evt;
  105. while (true) {
  106. while (SDL_PollEvent(&evt)) {
  107. switch (evt.type) {
  108. case SDL_QUIT:
  109. goto exit;
  110. break;
  111. case SDL_WINDOWEVENT:
  112. if (evt.window.event == SDL_WINDOWEVENT_RESIZED) {
  113. win.onResize(evt.window.data1, evt.window.data2);
  114. }
  115. break;
  116. case SDL_KEYDOWN:
  117. {
  118. Key key = keyFromSym(evt.key.keysym);
  119. if (key != Key::NONE) {
  120. state.keys[(int)key] = true;
  121. }
  122. }
  123. break;
  124. case SDL_KEYUP:
  125. {
  126. Key key = keyFromSym(evt.key.keysym);
  127. if (key != Key::NONE) {
  128. state.keys[(int)key] = false;
  129. }
  130. }
  131. break;
  132. }
  133. }
  134. win.clear();
  135. for (auto &ent: entities) {
  136. ent->update(state, 1/60.0);
  137. }
  138. for (auto &ent: entities) {
  139. ent->draw(state);
  140. }
  141. q.setScale(win.xScale() * 0.5, win.yScale() * 0.5);
  142. q.draw();
  143. win.flip();
  144. }
  145. exit: ;
  146. }