graphics.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * 2D Physic Engine
  3. * graphics.h: header for the utilities to display stuff.
  4. * Based on pikuma.com Learn Game Physics Engine Programming course.
  5. * Copyright (c) 2022 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 07/06/2022.
  8. */
  9. #ifndef PHYSICENGINE_GRAPHICS_H
  10. #define PHYSICENGINE_GRAPHICS_H
  11. #include <cstdint>
  12. #include <vector>
  13. #include <SDL.h>
  14. #include <SDL_ttf.h>
  15. #include <physics/vec2.h>
  16. class graphics {
  17. private:
  18. static int windowWidth;
  19. static int windowHeight;
  20. static SDL_Window* window;
  21. static SDL_Renderer* renderer;
  22. static TTF_Font *font;
  23. public:
  24. static int width() { return graphics::windowWidth; }
  25. static int height() { return graphics::windowHeight; }
  26. static bool openWindow();
  27. static void closeWindow();
  28. static void clearScreen(uint32_t color);
  29. static void renderFrame();
  30. static SDL_Texture *loadTexture(const char *filename);
  31. static uint32_t makeColour(uint8_t r, uint8_t g, uint8_t b)
  32. {
  33. return 0xFF000000 | (r << 16) | (g << 8) | (b << 0);
  34. }
  35. static uint32_t makeColour(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
  36. {
  37. return (a << 24) | (r << 16) | (g << 8) | (b << 0);
  38. }
  39. struct draw
  40. {
  41. static void line(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint32_t color);
  42. static void circle(int16_t x, int16_t y, int16_t radius, double angle, uint32_t color);
  43. static void fillCircle(int16_t x, int16_t y, int16_t radius, uint32_t color);
  44. static void rect(int16_t x, int16_t y, int16_t width, int16_t height, uint32_t color);
  45. static void fillRect(int16_t x, int16_t y, int16_t width, int16_t height, uint32_t color);
  46. static void polygon(int16_t x, int16_t y, const std::vector<vec2>& vertices, uint32_t color);
  47. static void fillPolygon(int16_t x, int16_t y, const std::vector<vec2> &vertices, uint32_t color);
  48. static void texture(int16_t x, int16_t y, int16_t width, int16_t height, double rotation, SDL_Texture *texture);
  49. static void text(int16_t x, int16_t y, uint32_t colour, const char *format, ...);
  50. };
  51. };
  52. #endif /* PHYSICENGINE_GRAPHICS_H */