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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdexcept>
  4. #include "util.h"
  5. namespace Cygnet {
  6. struct GlCompileError: public std::exception {
  7. GlCompileError(std::string message): message(std::move(message)) {}
  8. const char *what() const noexcept override { return message.c_str(); }
  9. std::string message;
  10. };
  11. class GlShader {
  12. public:
  13. enum class Type {
  14. VERTEX,
  15. FRAGMENT,
  16. };
  17. GlShader(Type type, const char *source);
  18. ~GlShader();
  19. GLuint id() const { return id_; }
  20. private:
  21. GLuint id_;
  22. };
  23. class GlVxShader: public GlShader {
  24. public:
  25. explicit GlVxShader(const char *source): GlShader(Type::VERTEX, source) {}
  26. };
  27. class GlFrShader: public GlShader {
  28. public:
  29. explicit GlFrShader(const char *source): GlShader(Type::FRAGMENT, source) {}
  30. };
  31. class GlProgram {
  32. public:
  33. template <typename... T>
  34. GlProgram(const T &... shaders): GlProgram() { (addShader(shaders), ...); link(); }
  35. GlProgram();
  36. ~GlProgram();
  37. void use();
  38. GLuint id() const { return id_; }
  39. protected:
  40. GLint attribLoc(const char *name);
  41. GLint uniformLoc(const char *name);
  42. private:
  43. void addShader(const GlShader &shader);
  44. void link();
  45. GLuint id_;
  46. };
  47. class GlTexture {
  48. public:
  49. GlTexture();
  50. void bind();
  51. void upload(GLsizei width, GLsizei height, void *data,
  52. GLenum format, GLenum type);
  53. GLuint id() { return id_; }
  54. int width() { return w_; }
  55. int height() { return h_; }
  56. private:
  57. GLuint id_;
  58. int w_;
  59. int h_;
  60. };
  61. }