engineGL2DSupport.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "engineGL2DSupport.h"
  2. //todo use global allocator and test.
  3. ::gl2d::Texture pika::gl2d::loadTextureWithPixelPadding(const char *path, RequestedContainerInfo &info, int blockSize,
  4. bool pixelated, bool useMipMaps)
  5. {
  6. ::gl2d::Texture t = {};
  7. size_t s = 0;
  8. if (info.getFileSizeBinary(path, s))
  9. {
  10. void *data = new unsigned char[s];
  11. if (info.readEntireFileBinary(path, data, s))
  12. {
  13. t.createFromFileDataWithPixelPadding((unsigned char *)data, s, blockSize, pixelated, useMipMaps);
  14. }
  15. else
  16. {
  17. info.consoleWrite(std::string("error loading texture: ") + path);
  18. }
  19. delete[] data;
  20. }
  21. else
  22. {
  23. info.consoleWrite(std::string("error loading texture: ") + path);
  24. return {};
  25. }
  26. return t;
  27. }
  28. ::gl2d::Texture pika::gl2d::loadTexture(const char *path, RequestedContainerInfo &info, bool pixelated, bool useMipMaps)
  29. {
  30. ::gl2d::Texture t = {};
  31. size_t s = 0;
  32. if (info.getFileSizeBinary(path, s))
  33. {
  34. void *data = new unsigned char[s];
  35. if (info.readEntireFileBinary(path, data, s))
  36. {
  37. t.createFromFileData((unsigned char *)data, s, pixelated, useMipMaps);
  38. }
  39. else
  40. {
  41. info.consoleWrite(std::string("error loading texture: ") + path);
  42. }
  43. delete[] data;
  44. }
  45. else
  46. {
  47. info.consoleWrite(std::string("error loading texture: ") + path);
  48. return {};
  49. }
  50. return t;
  51. }
  52. //todo make this use the default allocator or a temporary allocator
  53. ::gl2d::Font pika::gl2d::loadFont(const char *path, RequestedContainerInfo &info)
  54. {
  55. ::gl2d::Font f = {};
  56. size_t s = 0;
  57. if (info.getFileSizeBinary(path, s))
  58. {
  59. void *data = new unsigned char[s];
  60. if (info.readEntireFileBinary(path, data, s))
  61. {
  62. f.createFromTTF((unsigned char *)data, s);
  63. }
  64. else
  65. {
  66. info.consoleWrite(std::string("error loading font, parsing file ") + path);
  67. //todo
  68. //info.log((std::string("error loading font, parsing file: ") + path).c_str(), pika::logError);
  69. }
  70. delete[] data;
  71. }
  72. else
  73. {
  74. info.consoleWrite(std::string("error loading font: ") + path);
  75. //todo
  76. //info.log((std::string("error loading font, openning file: ") + path).c_str(), pika::logError);
  77. return {};
  78. }
  79. return f;
  80. }
  81. void pika::gl2d::cameraController(::gl2d::Camera &c, Input &input, float speed, float zoomSpeed)
  82. {
  83. if (input.buttons[pika::Button::W].held())
  84. {
  85. c.position.y -= speed * input.deltaTime;
  86. }
  87. if (input.buttons[pika::Button::S].held())
  88. {
  89. c.position.y += speed * input.deltaTime;
  90. }
  91. if (input.buttons[pika::Button::A].held())
  92. {
  93. c.position.x -= speed * input.deltaTime;
  94. }
  95. if (input.buttons[pika::Button::D].held())
  96. {
  97. c.position.x += speed * input.deltaTime;
  98. }
  99. if (input.buttons[pika::Button::Q].held())
  100. {
  101. c.zoom -= zoomSpeed * input.deltaTime;
  102. }
  103. if (input.buttons[pika::Button::E].held())
  104. {
  105. c.zoom += zoomSpeed * input.deltaTime;
  106. }
  107. c.zoom = glm::clamp(c.zoom, 0.001f, 1000.f);
  108. }