graphics.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 int windowFlags;
  21. static SDL_Window* window;
  22. static SDL_Renderer* renderer;
  23. static TTF_Font *font;
  24. public:
  25. static int width() { return graphics::windowWidth; }
  26. static int height() { return graphics::windowHeight; }
  27. static bool openWindow(bool fullScreen = true);
  28. static void closeWindow();
  29. static void clearScreen(uint32_t color);
  30. static void renderFrame();
  31. static SDL_Texture *loadTexture(const char *filename);
  32. static uint32_t makeColour(uint8_t r, uint8_t g, uint8_t b)
  33. {
  34. return 0xFF000000 | (r << 16) | (g << 8) | (b << 0);
  35. }
  36. static uint32_t makeColour(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
  37. {
  38. return (a << 24) | (r << 16) | (g << 8) | (b << 0);
  39. }
  40. struct draw
  41. {
  42. static void line(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint32_t color);
  43. static void circle(int16_t x, int16_t y, int16_t radius, double angle, uint32_t color);
  44. static void arrow(int16_t x, int16_t y, int16_t w, int16_t h, uint32_t color);
  45. static void fillCircle(int16_t x, int16_t y, int16_t radius, uint32_t color);
  46. static void rect(int16_t x, int16_t y, int16_t width, int16_t height, uint32_t color);
  47. static void fillRect(int16_t x, int16_t y, int16_t width, int16_t height, uint32_t color);
  48. static void polygon(int16_t x, int16_t y, const std::vector<vec2>& vertices, uint32_t color);
  49. static void fillPolygon(int16_t x, int16_t y, const std::vector<vec2> &vertices, uint32_t color);
  50. static void texture(int16_t x, int16_t y, int16_t width, int16_t height, double rotation, SDL_Texture *texture);
  51. static void text(int16_t x, int16_t y, uint32_t colour, const char *format, ...);
  52. };
  53. };
  54. #endif /* PHYSICENGINE_GRAPHICS_H */